Skip to content

Commit

Permalink
Adds ability to specify permission scopes
Browse files Browse the repository at this point in the history
Upades the strategy to allow scope specification. Updates
readme and example app. Ups the version, make sure
we use the latest omniauth gem.
  • Loading branch information
skorks committed Aug 15, 2012
1 parent 772a72a commit ecc3929
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 17 deletions.
2 changes: 0 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@ source "http://rubygems.org"
# Specify your gem's dependencies in omniauth-linkedin.gemspec
gemspec

gem 'omniauth-oauth', :git => 'https://github.com/intridea/omniauth-oauth.git'

group :development, :test do
gem 'guard'
gem 'guard-rspec'
Expand Down
41 changes: 33 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

This gem contains the LinkedIn strategy for OmniAuth 1.0 .

LinkedIn uses the OAuth 1.0a flow, you can read about it here: https://developer.linkedin.com/documents/authentication
LinkedIn uses the OAuth 1.0a flow, you can read about it here: https://developer.linkedin.com/documents/authentication

## How To Use It

Expand All @@ -11,21 +11,46 @@ Usage is as per any other OmniAuth 1.0 strategy. So let's say you're using Rails
gem 'omniauth'
gem 'omniauth-linkedin'

Of course if one or both of these are unreleased, you may have to pull them in directly from github e.g.:

gem 'omniauth', :git => 'https://github.com/intridea/omniauth.git'
gem 'omniauth-linkedin', :git => 'https://github.com/skorks/omniauth-linkedin.git'

Once these are in, you need to add the following to your `config/initializers/omniauth.rb`:

Rails.application.config.middleware.use OmniAuth::Builder do
provider :linkedin, "consumer_key", "consumer_secret"
provider :linkedin, "consumer_key", "consumer_secret"
end

You will obviously have to put in your key and secret, which you get when you register your app with LinkedIn (they call them API Key and Secret Key).
You will obviously have to put in your key and secret, which you get when you register your app with LinkedIn (they call them API Key and Secret Key).

Now just follow the README at: https://github.com/intridea/omniauth

## Additional permissions

LinkedIn recently (August 2012) provided the ability to request different permissions by specifying a scope. You can find more information on the different permissions at https://developer.linkedin.com/documents/authentication

By default, omniauth-linkedin requests the following permissions:

r_basicprofile+r_emailaddress

This allows us to obtain enough information from LinkedIn to satisfy the requirements for the basic auth hash schema.

To change the scope, simply change your initializer to something like this:

Rails.application.config.middleware.use OmniAuth::Builder do
provider :linkedin, "consumer_key", "consumer_secret", :scope => 'r_fullprofile+r_emailaddress+r_network'
end

One thing to note is the fact that when you ask for extra permissions, you will probably want to specify the array of fields that you want returned in the omniauth hash. If you don't then only the default fields (see below) will be returned which would defeat the purpose of asking for the extra permissions. So do the following:

Rails.application.config.middleware.use OmniAuth::Builder do
provider :linkedin, "consumer_key", "consumer_secret", :scope => 'r_fullprofile+r_emailaddress+r_network', :fields => ["id", "email-address", "first-name", "last-name", "headline", "industry", "picture-url", "public-profile-url", "location", "connections"]
end

We have to repeat the list of default fields in order to get the extra 'connections' field.

The list of default fields is as follows:

["id", "email-address", "first-name", "last-name", "headline", "industry", "picture-url", "public-profile-url", "location"]

To see a complete list of available fields, consult the LinkedIn documentation at https://developer.linkedin.com/documents/profile-fields

## Using It With The LinkedIn Gem

You may find that you want to use OmniAuth for authentication, but you want to use an API wrapper such as this one https://github.com/pengwynn/linkedin to actually make your api calls. But the LinkedIn gem provides it's own way to authenticate with LinkedIn via OAuth. In this case you can do the following.
Expand Down
1 change: 0 additions & 1 deletion example/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ source :rubygems
gem 'sinatra'
gem 'multi_json'
gem 'omniauth-linkedin', :path => '../'
#gem 'omniauth-linkedin'
7 changes: 5 additions & 2 deletions example/config.ru
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class App < Sinatra::Base
content_type 'application/json'
MultiJson.encode(request.env)
end

get '/auth/failure' do
content_type 'application/json'
MultiJson.encode(request.env)
Expand All @@ -21,7 +21,10 @@ end
use Rack::Session::Cookie

use OmniAuth::Builder do
provider :linkedin, ENV['LINKEDIN_CONSUMER_KEY'], ENV['LINKEDIN_CONSUMER_SECRET']
#note that the scope is different from the default
#we also have to repeat the default fields in order to get
#the extra 'connections' field in there
provider :linkedin, ENV['LINKEDIN_CONSUMER_KEY'], ENV['LINKEDIN_CONSUMER_SECRET'], :scope => 'r_fullprofile+r_emailaddress+r_network', :fields => ["id", "email-address", "first-name", "last-name", "headline", "industry", "picture-url", "public-profile-url", "location", "connections"]
end

run App.new
2 changes: 1 addition & 1 deletion lib/omniauth-linkedin/version.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
module Omniauth
module Linkedin
VERSION = "0.0.6"
VERSION = "0.0.7"
end
end
11 changes: 10 additions & 1 deletion lib/omniauth/strategies/linkedin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ class LinkedIn < OmniAuth::Strategies::OAuth
:authorize_url => 'https://www.linkedin.com/uas/oauth/authenticate'
}

option :fields, ["id", "first-name", "last-name", "headline", "industry", "picture-url", "public-profile-url"]
option :fields, ["id", "email-address", "first-name", "last-name", "headline", "industry", "picture-url", "public-profile-url", "location"]
option :scope, 'r_basicprofile+r_emailaddress'

uid{ raw_info['id'] }

info do
{
:email => raw_info['emailAddress'],
:first_name => raw_info['firstName'],
:last_name => raw_info['lastName'],
:name => "#{raw_info['firstName']} #{raw_info['lastName']}",
:headline => raw_info['headline'],
:description => raw_info['headline'],
:image => raw_info['pictureUrl'],
:industry => raw_info['industry'],
:urls => {
Expand All @@ -37,6 +40,12 @@ class LinkedIn < OmniAuth::Strategies::OAuth
def raw_info
@raw_info ||= MultiJson.decode(access_token.get("/v1/people/~:(#{options.fields.join(',')})?format=json").body)
end

def request_phase
options.client_options.request_token_path = "#{options.client_options.request_token_path}?scope=#{options.scope}"

super
end
end
end
end
Expand Down
4 changes: 2 additions & 2 deletions omniauth-linkedin.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ Gem::Specification.new do |s|
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
s.require_paths = ["lib"]

s.add_runtime_dependency 'omniauth-oauth', '~> 1.0.0'
s.add_runtime_dependency 'omniauth-oauth', '~> 1.0'

s.add_development_dependency 'rspec', '~> 2.7.0'
s.add_development_dependency 'rspec', '~> 2.7'
s.add_development_dependency 'rake'
s.add_development_dependency 'webmock'
s.add_development_dependency 'rack-test'
Expand Down

0 comments on commit ecc3929

Please sign in to comment.