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

Load user email in LinkedIn provider #191

Merged
merged 5 commits into from
Jun 16, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
8 changes: 6 additions & 2 deletions lib/generators/sorcery/templates/initializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,12 @@
# config.linkedin.key = ""
# config.linkedin.secret = ""
# config.linkedin.callback_url = "http://0.0.0.0:3000/oauth/callback?provider=linkedin"
# config.linkedin.user_info_mapping = {first_name: "firstName", last_name: "lastName"}
# config.linkedin.scope = "r_basicprofile"
# config.linkedin.user_info_mapping = {
# first_name: 'localizedFirstName',
joshbuker marked this conversation as resolved.
Show resolved Hide resolved
# last_name: 'localizedLastName',
# email: 'emailAddress'
# }
# config.linkedin.scope = "r_liteprofile r_emailaddress"
#
#
# For information about XING API:
Expand Down
30 changes: 20 additions & 10 deletions lib/sorcery/providers/linkedin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,25 +9,26 @@ module Providers
class Linkedin < Base
include Protocols::Oauth2

attr_accessor :auth_url, :scope, :token_url, :user_info_url
attr_accessor :auth_url, :scope, :token_url, :user_info_url, :email_info_url
joshbuker marked this conversation as resolved.
Show resolved Hide resolved

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'
@scope = 'r_liteprofile'
@state = SecureRandom.hex(16)
@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

def get_user_hash(access_token)
response = access_token.get(user_info_url)
user_info = get_user_info(access_token)

auth_hash(access_token).tap do |h|
h[:user_info] = JSON.parse(response.body)
h[:uid] = h[:user_info]['id']
h[:user_info] = user_info
h[:uid] = h[:user_info]['id']
end
end

Expand All @@ -45,6 +46,15 @@ def process_callback(params, _session)

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

def get_user_info(access_token)
response = access_token.get(user_info_url)
email_response = access_token.get(email_info_url)
joshbuker marked this conversation as resolved.
Show resolved Hide resolved
user_info = JSON.parse(response.body)
email_info = JSON.parse(email_response.body)['elements'].first

user_info.merge(email_info['handle~'])
end
end
end
end