@@ -14,6 +14,7 @@ class Auth(object):
14
14
_TOKEN_CACHE = "_token_cache"
15
15
_AUTH_FLOW = "_auth_flow"
16
16
_USER = "_logged_in_user"
17
+ _EXPLICITLY_REQUESTED_SCOPES = f"{ __name__ } .explicitly_requested_scopes"
17
18
__STATE_NO_OP = f"{ __name__ } .no_op" # A special state to indicate an auth response shall be ignored
18
19
__NEXT_LINK = f"{ __name__ } .next_link" # The next page after a successful auth
19
20
def __init__ (
@@ -124,6 +125,7 @@ def log_in(
124
125
flow = app .initiate_device_flow (_scopes )
125
126
if "error" in flow :
126
127
return flow
128
+ flow [self ._EXPLICITLY_REQUESTED_SCOPES ] = _scopes # Can be different than the flow["scope"] which is possibly injected by OIDC library
127
129
flow [self .__NEXT_LINK ] = next_link
128
130
self ._session [self ._AUTH_FLOW ] = flow
129
131
if redirect_uri :
@@ -185,6 +187,18 @@ def complete_log_in(self, auth_response=None):
185
187
)
186
188
if "error" in result :
187
189
return result
190
+ if "scope" in result :
191
+ # Only partial scopes were granted, others were likely unsupported.
192
+ # according to https://datatracker.ietf.org/doc/html/rfc6749#section-5.1
193
+ ungranted_scopes = set (
194
+ auth_flow [self ._EXPLICITLY_REQUESTED_SCOPES ]
195
+ ) - set (result ["scope" ].split ())
196
+ if ungranted_scopes :
197
+ return {
198
+ "error" : "invalid_scope" , # https://datatracker.ietf.org/doc/html/rfc6749#section-5.2
199
+ "error_description" : "Ungranted scope(s): {}" .format (
200
+ ' ' .join (ungranted_scopes )),
201
+ }
188
202
# TODO: Reject a re-log-in with a different account?
189
203
self ._save_user_into_session (result ["id_token_claims" ])
190
204
self ._save_cache (cache )
@@ -211,7 +225,7 @@ def get_user(self):
211
225
return self ._load_user_from_session ()
212
226
213
227
def get_token_for_user (self , scopes ):
214
- """Get access token for the current user, with specified scopes.
228
+ """Get access token silently for the current user, with specified scopes.
215
229
216
230
:param list scopes:
217
231
A list of scopes that your app will need to use.
0 commit comments