class Sorcery::Providers::Linkedin

This class adds support for OAuth with LinkedIn.

config.linkedin.key = <key>
config.linkedin.secret = <secret>
...

Attributes

auth_url[RW]
email_info_url[RW]
scope[RW]
token_url[RW]
user_info_url[RW]

Public Class Methods

new() click to toggle source
Calls superclass method Sorcery::Providers::Base::new
# File lib/sorcery/providers/linkedin.rb, line 14
def initialize
  super

  @site           = 'https://api.linkedin.com'
  @auth_url       = '/oauth/v2/authorization'
  @token_url      = '/oauth/v2/accessToken'
  @user_info_url  = 'https://api.linkedin.com/v2/me'
  @email_info_url = 'https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))'
  @scope          = 'r_liteprofile r_emailaddress'
  @state          = SecureRandom.hex(16)
end

Public Instance Methods

email_in_scope?() click to toggle source
# File lib/sorcery/providers/linkedin.rb, line 63
def email_in_scope?
  scope.include?('r_emailaddress')
end
fetch_email(access_token) click to toggle source
# File lib/sorcery/providers/linkedin.rb, line 67
def fetch_email(access_token)
  email_response = access_token.get(email_info_url)
  email_info     = JSON.parse(email_response.body)['elements'].first

  email_info['handle~']
end
get_user_hash(access_token) click to toggle source
# File lib/sorcery/providers/linkedin.rb, line 26
def get_user_hash(access_token)
  user_info = get_user_info(access_token)

  auth_hash(access_token).tap do |h|
    h[:user_info] = user_info
    h[:uid]       = h[:user_info]['id']
  end
end
get_user_info(access_token) click to toggle source
# File lib/sorcery/providers/linkedin.rb, line 50
def get_user_info(access_token)
  response  = access_token.get(user_info_url)
  user_info = JSON.parse(response.body)

  if email_in_scope?
    email = fetch_email(access_token)

    return user_info.merge(email)
  end

  user_info
end
login_url(_params, _session) click to toggle source

calculates and returns the url to which the user should be redirected, to get authenticated at the external provider's site.

# File lib/sorcery/providers/linkedin.rb, line 37
def login_url(_params, _session)
  authorize_url(authorize_url: auth_url)
end
process_callback(params, _session) click to toggle source

tries to login the user from access token

# File lib/sorcery/providers/linkedin.rb, line 42
def process_callback(params, _session)
  args = {}.tap do |a|
    a[:code] = params[:code] if params[:code]
  end

  get_access_token(args, token_url: token_url, token_method: :post)
end