Skip to content

Commit 27f083b

Browse files
committed
auth: Skip OIDC flow if Bearer access_token is present (bug 1979246)
1 parent 86660c2 commit 27f083b

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

mozilla_django_oidc/auth.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,29 @@ def get_userinfo(self, access_token, id_token, payload):
276276
return user_response.json()
277277

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

281281
self.request = request
282282
if not self.request:
283283
return None
284284

285+
# If a bearer token is present in the request, use it to authenticate the user.
286+
if authorization := request.META.get("HTTP_AUTHORIZATION"):
287+
scheme, token = authorization.split(maxsplit=1)
288+
if scheme.lower() == "bearer":
289+
# get_or_create_user and get_userinfo uses neither id_token nor payload.
290+
# XXX: maybe we only want to _get_ the user, and not create the if they
291+
# aren't alrealdy registered.
292+
try:
293+
return self.get_or_create_user(token, None, None)
294+
except HTTPError as exc:
295+
if exc.response.status_code in [401, 403]:
296+
LOGGER.warning(
297+
"failed to authenticate user from bearer token: %s", exc
298+
)
299+
return None
300+
raise exc
301+
285302
state = self.request.GET.get("state")
286303
code = self.request.GET.get("code")
287304
nonce = kwargs.pop("nonce", None)

0 commit comments

Comments
 (0)