@@ -9,25 +9,26 @@ module Providers
99    class  Linkedin  < Base 
1010      include  Protocols ::Oauth2 
1111
12-       attr_accessor  :auth_url ,  :scope ,  :token_url ,  :user_info_url 
12+       attr_accessor  :auth_url ,  :scope ,  :token_url ,  :user_info_url ,   :email_info_url 
1313
1414      def  initialize 
1515        super 
1616
17-         @site           =  'https://api.linkedin.com' 
18-         @auth_url       =  '/oauth/v2/authorization' 
19-         @token_url      =  '/oauth/v2/accessToken' 
20-         @user_info_url  =  'https://api.linkedin.com/v2/me' 
21-         @scope          =  'r_liteprofile' 
22-         @state          =  SecureRandom . hex ( 16 ) 
17+         @site            =  'https://api.linkedin.com' 
18+         @auth_url        =  '/oauth/v2/authorization' 
19+         @token_url       =  '/oauth/v2/accessToken' 
20+         @user_info_url   =  'https://api.linkedin.com/v2/me' 
21+         @email_info_url  =  'https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))' 
22+         @scope           =  'r_liteprofile r_emailaddress' 
23+         @state           =  SecureRandom . hex ( 16 ) 
2324      end 
2425
2526      def  get_user_hash ( access_token ) 
26-         response  =  access_token . get ( user_info_url ) 
27+         user_info  =  get_user_info ( access_token ) 
2728
2829        auth_hash ( access_token ) . tap  do  |h |
29-           h [ :user_info ]  =  JSON . parse ( response . body ) 
30-           h [ :uid ]  =  h [ :user_info ] [ 'id' ] 
30+           h [ :user_info ]  =  user_info 
31+           h [ :uid ]         =  h [ :user_info ] [ 'id' ] 
3132        end 
3233      end 
3334
@@ -45,6 +46,30 @@ def process_callback(params, _session)
4546
4647        get_access_token ( args ,  token_url : token_url ,  token_method : :post ) 
4748      end 
49+ 
50+       def  get_user_info ( access_token ) 
51+         response   =  access_token . get ( user_info_url ) 
52+         user_info  =  JSON . parse ( response . body ) 
53+ 
54+         if  email_in_scope? 
55+           email  =  fetch_email ( access_token ) 
56+ 
57+           return  user_info . merge ( email ) 
58+         end 
59+ 
60+         user_info 
61+       end 
62+ 
63+       def  email_in_scope? 
64+         scope . include? ( 'r_emailaddress' ) 
65+       end 
66+ 
67+       def  fetch_email ( access_token ) 
68+         email_response  =  access_token . get ( email_info_url ) 
69+         email_info      =  JSON . parse ( email_response . body ) [ 'elements' ] . first 
70+ 
71+         email_info [ 'handle~' ] 
72+       end 
4873    end 
4974  end 
5075end 
0 commit comments