Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use default scope and token_url for non-members #3

Open
wants to merge 16 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ spec/reports
test/tmp
test/version_tmp
tmp
Gemfile.lock

# YARD artifacts
.yardoc
Expand Down
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
## v.1.0 (July 25, 2015)

[onmiauth-orcid 1.0](https://github.com/datacite/omniauth-orcid/releases/tag/v.1.0) was released on July 25, 2015 with the following changes:

* changed default scope to `authenticate`, and use the public API `https://pub.orcid.org` by default. These settings work for non-members.
* added `name` and `email` to the `info` hash returned by omniauth (`email` will be empty in almost all cases)
* cleaned up documentation in `README.md`
4 changes: 2 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
source :rubygems
source 'https://rubygems.org'

# Specify your gem's dependencies in omniauth-github.gemspec
gemspec

gem "omniauth-oauth2"

# These are development dependencies
# These are development dependencies
gem "rake"
#gem "rspec", "2.0.0.beta.8"
2 changes: 1 addition & 1 deletion LICENSE.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License

Copyright (C) 2012, Gudmundur A. Thorisson
Copyright (C) 2012-2015, Gudmundur A. Thorisson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
129 changes: 49 additions & 80 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
# OmniAuth ORCID

ORCID OAuth 2.0 Strategy for the wonderful [OmniAuth Ruby authentication framework](http://www.omniauth.org).
[![DOI](https://zenodo.org/badge/15088/datacite/omniauth-orcid.svg)](https://zenodo.org/badge/latestdoi/15088/datacite/omniauth-orcid)

ORCID OAuth 2.0 Strategy for the [OmniAuth Ruby authentication framework](http://www.omniauth.org).

Provides basic support for connecting a client application to the [Open Researcher & Contributor ID registry service](http://orcid.org).

Originally created for the [ORCID example client application in Rails](https://github.com/gthorisson/ORCID-example-client-app-rails), then turned into a gem.

This gem is used in the [DataCite-ORCID claiming tool](https://github.com/datacite/DataCite-ORCID) and the [Lagotto](https://github.com/lagotto/lagotto) open source application for tracking events around articles and other scholarly outputs.

[GrowKudos](https://www.growkudos.com) is a web app where the gem is in active use. There's a free registration during which (and after which) an ORCID can be connected via oAuth.


## Installation
Expand All @@ -16,66 +21,63 @@ The usual way with Bundler: add the following to your `Gemfile` to install the c
gem 'omniauth-orcid'
```

Or, if you're impatient, you can install straight from GitHub

```ruby
gem 'omniauth-orcid' , :git => 'git://github.com/gthorisson/omniauth-orcid.git'
```

Then run `bundle install` to install into your environment.

You can also install the gem system-wide in the usual way:

```bash
[mummi@nfmac07]gem install omniauth-orcid
gem install omniauth-orcid
```


## Getting started

Like other OmniAuth strategies, `OmniAuth::Strategies::ORCID` is a piece of Rack middleware. Please read the OmniAuth documentation for detailed instructions: https://github.com/intridea/omniauth.


By default the module connects to the live ORCID service. In the very simplest usage, all you have to provide are your client app credentials ([see more here](http://support.orcid.org/knowledgebase/articles/116739)):

```ruby
use OmniAuth::Builder do
provider :orcid, ENV['ORCID_KEY'], ENV['ORCID_SECRET']
provider :orcid, ENV['ORCID_CLIENT_ID'], ENV['ORCID_CLIENT_SECRET']
end
```

There are three ways to register a client application and obtain client app credentials (`client_id` and `client_secret`) as well as a `site URL`:

* for non-members (the default): Register your client application in the `Developer Tools` section of your ORCID profile. Use `https://pub.orcid.org` as `site URL`
* for members (production): Register your client application [here](http://orcid.org/content/register-client-application). Use https://api.orcid.org as `site URL`
* for development (sandbox): Register your client application [here](https://orcid.org/content/register-client-application-sandbox). Use https://sandbox.orcid.org as `site URL`

OmniAuth takes care of the OAuth external-authentication handshake or "dance". All that the gem does is grab the identifier and tokens at the end of the dance and stick it into the OmniAuth hash which is subsequently accessible to your app via `request.env['omniauth.auth']` (see [AuthHashSchema](https://github.com/intridea/omniauth/wiki/Auth-Hash-Schema)). The hash looks something like this:

```json
{
provider: "orcid",
uid: "0000-0003-2012-0010",
info: {
name: null
},
credentials: {
token: "e82938fa-a287-42cf-a2ce-f48ef68c9a35",
refresh_token: "f94c58dd-b452-44f4-8863-0bf8486a0071",
expires_at: 1979903874,
expires: true
},
extra: { }
"provider": "orcid",
"uid": "0000-0003-2012-0010",
"info": {
"name": "John Smith",
"email": null
},
"credentials": {
"token": "e82938fa-a287-42cf-a2ce-f48ef68c9a35",
"refresh_token": "f94c58dd-b452-44f4-8863-0bf8486a0071",
"expires_at": 1979903874,
"expires": true
},
"extra": {
}
}
```

You have to implement a callback handler to grab at least the `uid` from the hash and (typically) save it in a session. This effectively provides basic "Log in with your ORCiD" functionality.

Most likely, with the token in hand, you'll want to do something more sophisticated with the API, like retrieving profile data and do something cool with it. See the API guide for more details:

http://support.orcid.org/knowledgebase/articles/116874-orcid-api-guide

You have to implement a callback handler to grab at least the `uid` from the hash and (typically) save it in a session. This effectively provides basic **Log in with your ORCID** functionality.

Most likely, with the token in hand, you'll want to do something more sophisticated with the API, like retrieving profile data and do something cool with it. See the [API documentation](http://members.orcid.org/api/api-calls) for more details:

Here's how to get going with a couple of popular Rack-based frameworks:


### Sinatra


Configure the strategy and implement a callback routine in your app:

```ruby
Expand All @@ -85,7 +87,7 @@ require 'omniauth-orcid'
enable :sessions

use OmniAuth::Builder do
provider :orcid, ENV['ORCID_KEY'], ENV['ORCID_SECRET']
provider :orcid, ENV['ORCID_CLIENT_ID'], ENV['ORCID_CLIENT_SECRET']
end
...
get '/auth/orcid/callback' do
Expand All @@ -94,7 +96,7 @@ get '/auth/orcid/callback' do
end

get '/' do

if session[:omniauth]
@orcid = session[:omniauth][:uid]
end
Expand All @@ -114,7 +116,7 @@ ruby demo.rb
```


### Rails
### Rails


Add this to `config/initializers/omniauth.rb` to configure the strategy:
Expand All @@ -123,7 +125,7 @@ Add this to `config/initializers/omniauth.rb` to configure the strategy:
require 'omniauth-orcid'

Rails.application.config.middleware.use OmniAuth::Builder do
provider :orcid, ENV['ORCID_KEY'], ENV['ORCID_SECRET']
provider :orcid, ENV['ORCID_CLIENT_ID'], ENV['ORCID_CLIENT_SECRET']
end
```

Expand Down Expand Up @@ -151,68 +153,35 @@ class AuthenticationsController < ApplicationController

## Configuration

You can also grab parameters from a config file (recommended) and pass
to the strategy, along with other options specific to your app. The OAuth scope or
You can also grab parameters from a environment variables (e.g. using the [dotenv](https://github.com/bkeepers/dotenv) gem) and pass to the strategy, along with other options specific to your app. The OAuth scope or
scopes in particular frequently need to be customized. Here's an example from the bundled Sinatra app in `demo.rb`:

*UPDATE The [omniauth-oauth2 gem](https://github.com/intridea/omniauth-oauth2) was recently been updated to process options slightly differently. The `:scope` string must now be passed in via `:authorize_params`, see below*

```ruby
config_file 'config.yml'
use OmniAuth::Builder do
provider :orcid, settings.client_id, settings.client_secret,
:authorize_params => {
:scope => '/orcid-profile/read-limited'
provider :orcid, ENV['ORCID_CLIENT_ID'], ENV['ORCID_CLIENT_SECRET'],
authorize_params: {
scope: '/orcid-profile/read-limited'
},
:client_options => {
:site => settings.site,
:authorize_url => settings.authorize_url,
:token_url => settings.token_url
client_options: {
site: ENV['ORCID_URL'],
authorize_url: "#{ENV['ORCID_URL']}/oauth/authorize",
token_url: "#{ENV['ORCID_URL']}/oauth/token",
}
end

```
Where `ENV['ORCID_CLIENT_ID']` and `ENV['ORCID_CLIENT_SECRET']` are provided by ORCID when registering the application, and `ENV['ORCID_URL']` depends on the API you are using (see above).

Different sets of params from `config.yml` are used for production environment (points to live ORCID service) vs. development environment (points to ORCID sandbox service).

You can do something similar with in Rails with the same config file, or something . See a working example here: https://github.com/gthorisson/ORCID-example-client-app-rails



## More information

* ORCID Open Source Project - https://github.com/ORCID/ORCID-Source
* Developer Wiki - https://github.com/ORCID/ORCID-Source/wiki
* Technical community - http://orcid.org/about/community/orcid-technical-community
## More information

* [ORCID Open Source Project](https://github.com/ORCID/ORCID-Source)
* [Developer Wiki](https://github.com/ORCID/ORCID-Source/wiki)
* [Technical community](http://orcid.org/about/community/orcid-technical-community)


## License

The MIT License (OSI approved, see more at http://www.opensource.org/licenses/mit-license.php)

=============================================================================

Copyright (C) 2012 by Gudmundur A. Thorisson

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

=============================================================================
The [MIT License](license.txt) (OSI approved, see more at http://www.opensource.org/licenses/mit-license.php)

![Open Source Initiative Approved License](http://www.opensource.org/trademarks/opensource/web/opensource-110x95.jpg)
16 changes: 0 additions & 16 deletions config.yml

This file was deleted.

21 changes: 11 additions & 10 deletions demo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,32 @@

require 'rubygems'
require 'sinatra'
require 'sinatra/config_file'
require 'haml'
require 'omniauth-orcid'
require_relative 'lib/omniauth-orcid'
require 'oauth2'
require 'json'

enable :sessions
use Rack::Session::Cookie

config_file 'config.yml'
if development?
puts "Sinatra running in development mode"
elsif production?
puts "Sinatra running in production mode"
puts "Sinatra running in production mode"
end

puts "Connecting to ORCID API at " + settings.site + " as client app #{settings.client_id}"

# Configure the ORCID strategy
use OmniAuth::Builder do
provider :orcid, settings.client_id, settings.client_secret,
:client_options => {
:site => settings.site,
:authorize_url => settings.authorize_url,
:token_url => settings.token_url
provider :orcid, ENV['ORCID_CLIENT_ID'], ENV['ORCID_CLIENT_SECRET'],
authorize_params: {
scope: '/orcid-profile/read-limited'
},
client_options: {
site: ENV['ORCID_URL'],
authorize_url: "#{ENV['ORCID_URL']}/oauth/authorize",
token_url: "#{ENV['ORCID_URL']}/oauth/token",
}
end

Expand All @@ -38,7 +39,7 @@
get '/' do

@orcid = ''

if session[:omniauth]
@orcid = session[:omniauth][:uid]
end
Expand Down
3 changes: 1 addition & 2 deletions lib/omniauth-orcid.rb
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
require "omniauth-orcid/version"
require 'omniauth/strategies/orcid'
require 'omniauth/orcid'
5 changes: 0 additions & 5 deletions lib/omniauth-orcid/version.rb

This file was deleted.

5 changes: 0 additions & 5 deletions lib/omniauth-orcid/version.rb~

This file was deleted.

2 changes: 2 additions & 0 deletions lib/omniauth/orcid.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
require "omniauth/orcid/version"
require 'omniauth/strategies/orcid'
5 changes: 5 additions & 0 deletions lib/omniauth/orcid/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module OmniAuth
module Orcid
VERSION = "1.0.1"
end
end
18 changes: 10 additions & 8 deletions lib/omniauth/strategies/orcid.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,29 @@ module OmniAuth
module Strategies
class ORCID < OmniAuth::Strategies::OAuth2

DEFAULT_SCOPE = '/orcid-bio/read-limited'
DEFAULT_SCOPE = '/authenticate'

option :client_options, {
:site => 'http://api.orcid.org',
:authorize_url => 'http://orcid.org/oauth/authorize',
:token_url => 'https://api.orcid.org/oauth/token',
:scope => '/orcid-profile/read-limited',
:token_url => 'https://pub.orcid.org/oauth/token',
:scope => '/authenticate',
:response_type => 'code',
:mode => :header
}


# Pull out unique ID for the user in the external system
uid { access_token.params["orcid"] }
uid { access_token.params["orcid"] }

info do{} end
info do
{ name: access_token.params["name"],
email: access_token.params["email"] }
end

# Customize the parameters passed to the OAuth provider in the authorization phase
def authorize_params
# Trick shamelessly borrowed from the omniauth-facebook gem!

# Trick shamelessly borrowed from the omniauth-facebook gem!
super.tap do |params|
%w[scope].each { |v| params[v.to_sym] = request.params[v] if request.params[v] }
params[:scope] ||= DEFAULT_SCOPE # ensure that we're always request *some* default scope
Expand Down
Loading