Skip to content

Commit c306e03

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

1 file changed

Lines changed: 20 additions & 1 deletion

File tree

mozilla_django_oidc/auth.py

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -276,12 +276,31 @@ 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.
280+
281+
When the Django user doesn't yet exist, whether it gets created or not is controlled by the
282+
`settings.OIDC_CREATE_USER` variable.
283+
"""
280284

281285
self.request = request
282286
if not self.request:
283287
return None
284288

289+
# If a bearer token is present in the request, use it to authenticate the user.
290+
if authorization := request.META.get("HTTP_AUTHORIZATION"):
291+
scheme, token = authorization.split(maxsplit=1)
292+
if scheme.lower() == "bearer":
293+
try:
294+
# get_or_create_user and get_userinfo uses neither id_token nor payload.
295+
return self.get_or_create_user(token, None, None)
296+
except HTTPError as exc:
297+
if exc.response.status_code in [401, 403]:
298+
LOGGER.warning(
299+
"failed to authenticate user from bearer token: %s", exc
300+
)
301+
return None
302+
raise exc
303+
285304
state = self.request.GET.get("state")
286305
code = self.request.GET.get("code")
287306
nonce = kwargs.pop("nonce", None)

0 commit comments

Comments
 (0)