├── .ruby-version ├── .rspec ├── .gitignore ├── todo.txt ├── lib ├── omniauth-linkedin │ └── version.rb ├── omniauth-linkedin.rb └── omniauth │ └── strategies │ └── linkedin.rb ├── example ├── Gemfile └── config.ru ├── Rakefile ├── Guardfile ├── Gemfile ├── spec ├── spec_helper.rb └── omniauth │ └── strategies │ └── linkedin_spec.rb ├── omniauth-linkedin.gemspec └── README.md /.ruby-version: -------------------------------------------------------------------------------- 1 | 2.1.2 2 | 3 | -------------------------------------------------------------------------------- /.rspec: -------------------------------------------------------------------------------- 1 | --color 2 | --format=progress 3 | --require spec_helper 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.gem 2 | .bundle 3 | Gemfile.lock 4 | pkg/* 5 | *.swp 6 | -------------------------------------------------------------------------------- /todo.txt: -------------------------------------------------------------------------------- 1 | - need some more tests to make sure location can be parsed correctly 2 | -------------------------------------------------------------------------------- /lib/omniauth-linkedin/version.rb: -------------------------------------------------------------------------------- 1 | module OmniAuth 2 | module Linkedin 3 | VERSION = "0.2.0" 4 | end 5 | end 6 | -------------------------------------------------------------------------------- /example/Gemfile: -------------------------------------------------------------------------------- 1 | source :rubygems 2 | 3 | gem 'sinatra' 4 | gem 'multi_json' 5 | gem 'omniauth-linkedin', :path => '../' 6 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require 'bundler/gem_tasks' 2 | require 'rspec/core/rake_task' 3 | 4 | RSpec::Core::RakeTask.new(:spec) 5 | 6 | task :default => :spec 7 | -------------------------------------------------------------------------------- /lib/omniauth-linkedin.rb: -------------------------------------------------------------------------------- 1 | require "omniauth-linkedin/version" 2 | require 'omniauth/strategies/linkedin' 3 | 4 | 5 | module OmniAuth 6 | module Linkedin 7 | # Your code goes here... 8 | end 9 | end 10 | -------------------------------------------------------------------------------- /Guardfile: -------------------------------------------------------------------------------- 1 | guard 'rspec', :version => 2 do 2 | watch(%r{^spec/.+_spec\.rb$}) 3 | watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" } 4 | watch('spec/spec_helper.rb') { "spec" } 5 | end 6 | 7 | 8 | guard 'bundler' do 9 | watch('Gemfile') 10 | watch(/^.+\.gemspec/) 11 | end 12 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source "http://rubygems.org" 2 | 3 | # Specify your gem's dependencies in omniauth-linkedin.gemspec 4 | gemspec 5 | 6 | group :development, :test do 7 | gem 'guard' 8 | gem 'guard-rspec' 9 | gem 'guard-bundler' 10 | gem 'growl' 11 | gem 'rb-fsevent' 12 | gem 'multi_json' 13 | end 14 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | $:.unshift File.expand_path('..', __FILE__) 2 | $:.unshift File.expand_path('../../lib', __FILE__) 3 | 4 | require 'rspec' 5 | require 'rack/test' 6 | require 'webmock/rspec' 7 | require 'omniauth' 8 | require 'omniauth-linkedin' 9 | 10 | RSpec.configure do |config| 11 | config.include WebMock::API 12 | config.include Rack::Test::Methods 13 | config.extend OmniAuth::Test::StrategyMacros, :type => :strategy 14 | end 15 | -------------------------------------------------------------------------------- /omniauth-linkedin.gemspec: -------------------------------------------------------------------------------- 1 | # -*- encoding: utf-8 -*- 2 | $:.push File.expand_path("../lib", __FILE__) 3 | require "omniauth-linkedin/version" 4 | 5 | Gem::Specification.new do |s| 6 | s.name = "omniauth-linkedin" 7 | s.version = OmniAuth::Linkedin::VERSION 8 | s.authors = ["Alan Skorkin"] 9 | s.email = ["alan@skorks.com"] 10 | s.homepage = "https://github.com/skorks/omniauth-linkedin" 11 | s.summary = %q{LinkedIn strategy for OmniAuth.} 12 | s.description = %q{LinkedIn strategy for OmniAuth.} 13 | 14 | s.files = `git ls-files`.split("\n") 15 | s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") 16 | s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } 17 | s.require_paths = ["lib"] 18 | 19 | s.add_runtime_dependency 'omniauth-oauth', '~> 1.0' 20 | 21 | s.add_development_dependency 'rspec', '~> 3.0' 22 | s.add_development_dependency 'rake' 23 | s.add_development_dependency 'webmock' 24 | s.add_development_dependency 'rack-test' 25 | end 26 | -------------------------------------------------------------------------------- /example/config.ru: -------------------------------------------------------------------------------- 1 | require 'bundler/setup' 2 | require 'sinatra/base' 3 | require 'omniauth-linkedin' 4 | 5 | ENV['LINKEDIN_CONSUMER_KEY'] = "eumx4m8w39qb" 6 | ENV['LINKEDIN_CONSUMER_SECRET'] = "PczJNDDLYic6kQOL" 7 | 8 | class App < Sinatra::Base 9 | get '/' do 10 | redirect '/auth/linkedin' 11 | end 12 | 13 | get '/auth/:provider/callback' do 14 | content_type 'application/json' 15 | MultiJson.encode(request.env) 16 | end 17 | 18 | get '/auth/failure' do 19 | content_type 'application/json' 20 | MultiJson.encode(request.env) 21 | end 22 | end 23 | 24 | use Rack::Session::Cookie, :secret => "change_me" 25 | 26 | use OmniAuth::Builder do 27 | #note that the scope is different from the default 28 | #we also have to repeat the default fields in order to get 29 | #the extra 'connections' field in there 30 | 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"] 31 | end 32 | 33 | run App.new 34 | -------------------------------------------------------------------------------- /spec/omniauth/strategies/linkedin_spec.rb: -------------------------------------------------------------------------------- 1 | RSpec.describe OmniAuth::Strategies::LinkedIn do 2 | subject(:linkedin) do 3 | OmniAuth::Strategies::LinkedIn.new(nil, @options || {}) 4 | end 5 | 6 | it 'adds a camelization for itself' do 7 | expect(OmniAuth::Utils.camelize('linkedin')).to eq 'LinkedIn' 8 | end 9 | 10 | context 'client options' do 11 | it 'has correct LinkedIn site' do 12 | expect(linkedin.options.client_options.site).to eq 'https://api.linkedin.com' 13 | end 14 | 15 | it 'has correct request token path' do 16 | expect(linkedin.options.client_options.request_token_path).to eq '/uas/oauth/requestToken' 17 | end 18 | 19 | it 'has correct access token path' do 20 | expect(linkedin.options.client_options.access_token_path).to eq '/uas/oauth/accessToken' 21 | end 22 | 23 | it 'has correct authorize url' do 24 | expect(linkedin.options.client_options.authorize_url).to eq 'https://www.linkedin.com/uas/oauth/authenticate' 25 | end 26 | end 27 | 28 | context '#uid' do 29 | before do 30 | allow(linkedin).to receive(:raw_info).and_return('id' => '123') 31 | end 32 | 33 | it 'returns the id from raw_info' do 34 | expect(linkedin.uid).to eq '123' 35 | end 36 | end 37 | 38 | context 'returns info hash conformant with omniauth auth hash schema' do 39 | before do 40 | allow(linkedin).to receive(:raw_info).and_return({}) 41 | end 42 | 43 | context 'and therefore has all the necessary fields' do 44 | it {expect(linkedin.info).to have_key :name} 45 | it {expect(linkedin.info).to have_key :name} 46 | it {expect(linkedin.info).to have_key :email} 47 | it {expect(linkedin.info).to have_key :nickname} 48 | it {expect(linkedin.info).to have_key :first_name} 49 | it {expect(linkedin.info).to have_key :last_name} 50 | it {expect(linkedin.info).to have_key :location} 51 | it {expect(linkedin.info).to have_key :description} 52 | it {expect(linkedin.info).to have_key :image} 53 | it {expect(linkedin.info).to have_key :phone} 54 | it {expect(linkedin.info).to have_key :urls} 55 | end 56 | end 57 | end 58 | -------------------------------------------------------------------------------- /lib/omniauth/strategies/linkedin.rb: -------------------------------------------------------------------------------- 1 | require 'omniauth/strategies/oauth' 2 | 3 | module OmniAuth 4 | module Strategies 5 | class LinkedIn < OmniAuth::Strategies::OAuth 6 | option :name, "linkedin" 7 | 8 | option :client_options, { 9 | :site => 'https://api.linkedin.com', 10 | :request_token_path => '/uas/oauth/requestToken', 11 | :access_token_path => '/uas/oauth/accessToken', 12 | :authorize_url => 'https://www.linkedin.com/uas/oauth/authenticate' 13 | } 14 | 15 | option :fields, ["id", "email-address", "first-name", "last-name", "headline", "industry", "picture-url", "public-profile-url", "location"] 16 | 17 | option :scope, 'r_basicprofile r_emailaddress' 18 | 19 | uid{ raw_info['id'] } 20 | 21 | info do 22 | name = [raw_info['firstName'], raw_info['lastName']].compact.join(' ').strip || nil 23 | name = nil_if_empty(name) 24 | { 25 | :name => name, 26 | :email => raw_info['emailAddress'], 27 | :nickname => name, 28 | :first_name => raw_info['firstName'], 29 | :last_name => raw_info['lastName'], 30 | :location => parse_location(raw_info['location']), 31 | :description => raw_info['headline'], 32 | :image => raw_info['pictureUrl'], 33 | :phone => nil, 34 | :headline => raw_info['headline'], 35 | :industry => raw_info['industry'], 36 | :urls => { 37 | 'public_profile' => raw_info['publicProfileUrl'] 38 | } 39 | } 40 | end 41 | 42 | extra do 43 | { 'raw_info' => raw_info } 44 | end 45 | 46 | def raw_info 47 | fields = options.fields 48 | fields.map! { |f| f == "picture-url" ? "picture-url;secure=true" : f } if options[:secure_image_url] 49 | @raw_info ||= MultiJson.decode(access_token.get("/v1/people/~:(#{fields.join(',')})?format=json").body) 50 | end 51 | 52 | def request_phase 53 | options.request_params ||= {} 54 | options.request_params[:scope] = options.scope.gsub("+", " ") 55 | super 56 | end 57 | 58 | private 59 | 60 | def parse_location(location_hash = {}) 61 | location_hash ||= {} 62 | location_name = extract_location_name(location_hash) 63 | country_code = extract_country_code(location_hash) 64 | build_location_value(location_name, country_code) 65 | end 66 | 67 | def extract_location_name(location_hash = {}) 68 | nil_if_empty(location_hash["name"]) 69 | end 70 | 71 | def extract_country_code(location_hash = {}) 72 | country_hash = location_hash["country"] || {} 73 | country_code = nil_if_empty(country_hash["code"]) 74 | country_code = (country_code ? country_code.upcase : nil) 75 | end 76 | 77 | def build_location_value(location_name, country_code) 78 | nil_if_empty([location_name, country_code].compact.join(', ')) 79 | end 80 | 81 | def nil_if_empty(value) 82 | (value.nil? || value.empty?) ? nil : value 83 | end 84 | end 85 | end 86 | end 87 | 88 | OmniAuth.config.add_camelization 'linkedin', 'LinkedIn' 89 | 90 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OmniAuth LinkedIn 2 | 3 | This gem contains the LinkedIn strategy for OmniAuth 1.0 . 4 | 5 | LinkedIn uses the OAuth 1.0a flow, you can read about it here: https://developer.linkedin.com/documents/authentication 6 | 7 | ## How To Use It 8 | 9 | Usage is as per any other OmniAuth 1.0 strategy. So let's say you're using Rails, you need to add the strategy to your `Gemfile` along side omniauth: 10 | 11 | ```ruby 12 | gem 'omniauth' 13 | gem 'omniauth-linkedin' 14 | ``` 15 | 16 | Once these are in, you need to add the following to your `config/initializers/omniauth.rb`: 17 | 18 | ```ruby 19 | Rails.application.config.middleware.use OmniAuth::Builder do 20 | provider :linkedin, "consumer_key", "consumer_secret" 21 | end 22 | ``` 23 | 24 | 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). 25 | 26 | Now just follow the README at: https://github.com/intridea/omniauth 27 | 28 | ## Additional permissions 29 | 30 | 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 31 | 32 | By default, omniauth-linkedin requests the following permissions: 33 | 34 | ```ruby 35 | "r_basicprofile r_emailaddress" 36 | ``` 37 | 38 | This allows us to obtain enough information from LinkedIn to satisfy the requirements for the basic auth hash schema. 39 | 40 | To change the scope, simply change your initializer to something like this: 41 | 42 | ```ruby 43 | Rails.application.config.middleware.use OmniAuth::Builder do 44 | provider :linkedin, "consumer_key", "consumer_secret", :scope => 'r_fullprofile r_emailaddress r_network' 45 | end 46 | ``` 47 | 48 | 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: 49 | 50 | ```ruby 51 | Rails.application.config.middleware.use OmniAuth::Builder do 52 | 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"] 53 | end 54 | ``` 55 | 56 | We have to repeat the list of default fields in order to get the extra 'connections' field. 57 | 58 | The list of default fields is as follows: 59 | 60 | ```ruby 61 | ["id", "email-address", "first-name", "last-name", "headline", "industry", "picture-url", "public-profile-url", "location"] 62 | ``` 63 | 64 | To see a complete list of available fields, consult the LinkedIn documentation at https://developer.linkedin.com/documents/profile-fields 65 | 66 | ## Example Auth Hash 67 | 68 | Here's an example *Auth Hash* available in `request.env['omniauth.auth']`: 69 | 70 | ```ruby 71 | { 72 | "provider"=>"linkedin", 73 | "uid"=>"AbC123", 74 | "info"=> { 75 | "name"=>"John Doe", 76 | "email"=>"john@doe.com", 77 | "nickname"=>"John Doe", 78 | "first_name"=>"John", 79 | "last_name"=>"Doe", 80 | "location"=>"Greater Boston Area, US", 81 | "description"=>"Senior Developer, Hammertech", 82 | "image"=> "http://m.c.lnkd.licdn.com/mpr/mprx/0_aBcD...", 83 | "phone"=>"null", 84 | "headline"=> "Senior Developer, Hammertech", 85 | "industry"=>"Internet", 86 | "urls"=>{ 87 | "public_profile"=>"http://www.linkedin.com/in/johndoe" 88 | } 89 | }, 90 | "credentials"=> { 91 | "token"=>"12312...", 92 | "secret"=>"aBc..." 93 | }, 94 | "extra"=> 95 | { 96 | "access_token"=> { 97 | "token"=>"12312...", 98 | "secret"=>"aBc...", 99 | "consumer"=>nil, # 100 | "params"=> { 101 | :oauth_token=>"12312...", 102 | :oauth_token_secret=>"aBc...", 103 | :oauth_expires_in=>"5183999", 104 | :oauth_authorization_expires_in=>"5183999", 105 | }, 106 | "response"=>nil # 107 | }, 108 | "raw_info"=> { 109 | "firstName"=>"Joe", 110 | "headline"=>"Senior Developer, Hammertech", 111 | "id"=>"AbC123", 112 | "industry"=>"Internet", 113 | "lastName"=>"Doe", 114 | "location"=> {"country"=>{"code"=>"us"}, "name"=>"Greater Boston Area"}, 115 | "pictureUrl"=> "http://m.c.lnkd.licdn.com/mpr/mprx/0_aBcD...", 116 | "publicProfileUrl"=>"http://www.linkedin.com/in/johndoe" 117 | } 118 | } 119 | } 120 | ``` 121 | 122 | ## Using It With The LinkedIn Gem 123 | 124 | 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 its own way to authenticate with LinkedIn via OAuth. In this case you can do the following. 125 | 126 | Configure the LinkedIn gem with your consumer key and secret: 127 | 128 | ```ruby 129 | LinkedIn.configure do |config| 130 | config.token = "consumer_key" 131 | config.secret = "consumer_secret" 132 | end 133 | ``` 134 | 135 | Use OmniAuth as per normal to obtain an access token and an access token secret for your user. Now create the LinkedIn client and authorize it using the access token and secret that you obtained via OmniAuth: 136 | 137 | ```ruby 138 | client = LinkedIn::Client.new 139 | client.authorize_from_access("access_token", "access_token_secret") 140 | ``` 141 | 142 | You can now make API calls as per normal e.g.: 143 | 144 | ```ruby 145 | client.profile 146 | client.add_share({:comment => "blah"}) 147 | ``` 148 | 149 | ## Note on Patches/Pull Requests 150 | 151 | - Fork the project. 152 | - Make your feature addition or bug fix. 153 | - Add tests for it. This is important so I don’t break it in a future version unintentionally. 154 | - Commit, do not mess with rakefile, version, or history. (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull) 155 | - Send me a pull request. Bonus points for topic branches. 156 | 157 | ## License 158 | 159 | Copyright (c) 2011 by Alan Skorkin 160 | 161 | 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: 162 | 163 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 164 | 165 | 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. 166 | --------------------------------------------------------------------------------