Skip to content

Commit a082589

Browse files
committed
auth: Skip OIDC flow if Bearer access_token is present (bug 1979246)
1 parent 2c2334f commit a082589

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

mozilla_django_oidc/auth.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,21 @@ def get_userinfo(self, access_token, id_token, payload):
282282
return user_response.json()
283283

284284
def authenticate(self, request, **kwargs):
285-
"""Authenticates a user based on the OIDC code flow."""
285+
"""Authenticates a user based on a Bearer access_token or the OIDC code flow."""
286286

287287
self.request = request
288288
if not self.request:
289289
return None
290290

291+
# If a bearer token is present in the request, use it to authenticate the user.
292+
if authorization := request.META.get("HTTP_AUTHORIZATION"):
293+
scheme, token = authorization.split(maxsplit=1)
294+
if scheme.lower() == "bearer":
295+
# get_or_create_user and get_userinfo uses neither id_token nor payload.
296+
# XXX: maybe we only want to _get_ the user, and not create the if they
297+
# aren't alrealdy registered.
298+
return self.get_or_create_user(token, None, None)
299+
291300
state = self.request.GET.get("state")
292301
code = self.request.GET.get("code")
293302
nonce = kwargs.pop("nonce", None)
@@ -366,7 +375,7 @@ def get_or_create_user(self, access_token, id_token, payload):
366375
return user
367376
else:
368377
LOGGER.debug(
369-
"Login failed: No user with %s found, and " "OIDC_CREATE_USER is False",
378+
"Login failed: No user with %s found, and OIDC_CREATE_USER is False",
370379
self.describe_user_by_claims(user_info),
371380
)
372381
return None

0 commit comments

Comments
 (0)