@@ -27,18 +27,33 @@ def authenticate!
27
27
authenticate!
28
28
else
29
29
access_token = session [ :access_token ]
30
- scopes = session [ :scopes ]
31
- has_user_email_scope = scopes . include? 'user:email'
30
+ scopes = [ ]
32
31
33
- auth_result = JSON . parse ( RestClient . get ( 'https://api.github.com/user' ,
34
- { :params => { :access_token => access_token } ,
35
- :accept => :json } ) )
32
+ begin
33
+ auth_result = RestClient . get ( 'https://api.github.com/user' ,
34
+ { :params => { :access_token => access_token } ,
35
+ :accept => :json } )
36
+ rescue => e
37
+ # request didn't succeed because the token was revoked so we
38
+ # invalidate the token stored in the session and render the
39
+ # index page so that the user can start the OAuth flow again
36
40
37
- if has_user_email_scope
41
+ session [ :access_token ] = nil
42
+ return authenticate!
43
+ end
44
+
45
+ # the request succeeded, so we check the list of current scopes
46
+ if auth_result . headers . include? :x_oauth_scopes
47
+ scopes = auth_result . headers [ :x_oauth_scopes ] . split ( ', ' )
48
+ end
49
+
50
+ auth_result = JSON . parse ( auth_result )
51
+
52
+ if scopes . include? 'user:email'
38
53
auth_result [ 'private_emails' ] =
39
54
JSON . parse ( RestClient . get ( 'https://api.github.com/user/emails' ,
40
- { :params => { :access_token => access_token } ,
41
- :accept => :json } ) )
55
+ { :params => { :access_token => access_token } ,
56
+ :accept => :json } ) )
42
57
end
43
58
44
59
erb :advanced , :locals => auth_result
@@ -55,7 +70,6 @@ def authenticate!
55
70
:accept => :json )
56
71
57
72
session [ :access_token ] = JSON . parse ( result ) [ 'access_token' ]
58
- session [ :scopes ] = JSON . parse ( result ) [ 'scope' ] . split ( ',' )
59
73
60
74
redirect '/'
61
75
end
0 commit comments