Class: Sorcery::Providers::Linkedin
- Includes:
- Sorcery::Protocols::Oauth2
- Defined in:
- lib/sorcery/providers/linkedin.rb
Overview
This class adds support for OAuth with LinkedIn.
config.linkedin.key = <key>
config.linkedin.secret = <secret>
...
Instance Attribute Summary collapse
-
#auth_url ⇒ Object
Returns the value of attribute auth_url.
-
#email_info_url ⇒ Object
Returns the value of attribute email_info_url.
-
#scope ⇒ Object
Returns the value of attribute scope.
-
#token_url ⇒ Object
Returns the value of attribute token_url.
-
#user_info_url ⇒ Object
Returns the value of attribute user_info_url.
Attributes inherited from Base
#access_token, #callback_url, #key, #original_callback_url, #secret, #site, #state, #user_info_mapping
Instance Method Summary collapse
- #email_in_scope? ⇒ Boolean
- #fetch_email(access_token) ⇒ Object
- #get_user_hash(access_token) ⇒ Object
- #get_user_info(access_token) ⇒ Object
-
#initialize ⇒ Linkedin
constructor
A new instance of Linkedin.
-
#login_url(_params, _session) ⇒ Object
calculates and returns the url to which the user should be redirected, to get authenticated at the external provider’s site.
-
#process_callback(params, _session) ⇒ Object
tries to login the user from access token.
Methods included from Sorcery::Protocols::Oauth2
#authorize_url, #build_client, #get_access_token, #oauth_version
Methods inherited from Base
#auth_hash, descendants, #has_callback?, name
Constructor Details
#initialize ⇒ Linkedin
Returns a new instance of Linkedin.
14 15 16 17 18 19 20 21 22 23 24 |
# 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 |
Instance Attribute Details
#auth_url ⇒ Object
Returns the value of attribute auth_url.
12 13 14 |
# File 'lib/sorcery/providers/linkedin.rb', line 12 def auth_url @auth_url end |
#email_info_url ⇒ Object
Returns the value of attribute email_info_url.
12 13 14 |
# File 'lib/sorcery/providers/linkedin.rb', line 12 def email_info_url @email_info_url end |
#scope ⇒ Object
Returns the value of attribute scope.
12 13 14 |
# File 'lib/sorcery/providers/linkedin.rb', line 12 def scope @scope end |
#token_url ⇒ Object
Returns the value of attribute token_url.
12 13 14 |
# File 'lib/sorcery/providers/linkedin.rb', line 12 def token_url @token_url end |
#user_info_url ⇒ Object
Returns the value of attribute user_info_url.
12 13 14 |
# File 'lib/sorcery/providers/linkedin.rb', line 12 def user_info_url @user_info_url end |
Instance Method Details
#email_in_scope? ⇒ Boolean
63 64 65 |
# File 'lib/sorcery/providers/linkedin.rb', line 63 def email_in_scope? scope.include?('r_emailaddress') end |
#fetch_email(access_token) ⇒ Object
67 68 69 70 71 72 |
# 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) ⇒ Object
26 27 28 29 30 31 32 33 |
# 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) ⇒ Object
50 51 52 53 54 55 56 57 58 59 60 61 |
# 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) ⇒ Object
calculates and returns the url to which the user should be redirected, to get authenticated at the external provider’s site.
37 38 39 |
# File 'lib/sorcery/providers/linkedin.rb', line 37 def login_url(_params, _session) (authorize_url: auth_url) end |
#process_callback(params, _session) ⇒ Object
tries to login the user from access token
42 43 44 45 46 47 48 |
# 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 |