Skip to content

Commit 0211253

Browse files
committed
fix: OP prompts for logout when no OP session
The OAuth provider is prompting users who no longer have an user session with the OAuth Provider to logout of the OP. This happens in scenarios given the user has logged out of the OP directly or via another client. In cases where the user does not have a session on the OP we should not prompt them to log out of the OP as there is no session, but we should still clear out their tokens to terminate the session for the Application.
1 parent 28b512a commit 0211253

File tree

2 files changed

+49
-9
lines changed

2 files changed

+49
-9
lines changed

oauth2_provider/views/oidc.py

+45-9
Original file line numberDiff line numberDiff line change
@@ -367,17 +367,53 @@ def validate_logout_request(self, id_token_hint, client_id, post_logout_redirect
367367
return application, id_token.user if id_token else None
368368

369369
def must_prompt(self, token_user):
370-
"""Indicate whether the logout has to be confirmed by the user. This happens if the
371-
specifications force a confirmation, or it is enabled by `OIDC_RP_INITIATED_LOGOUT_ALWAYS_PROMPT`.
370+
"""
371+
per: https://openid.net/specs/openid-connect-rpinitiated-1_0.html
372+
373+
> At the Logout Endpoint, the OP SHOULD ask the End-User whether to log
374+
> out of the OP as well. Furthermore, the OP MUST ask the End-User this
375+
> question if an id_token_hint was not provided or if the supplied ID
376+
> Token does not belong to the current OP session with the RP and/or
377+
> currently logged in End-User.
372378
373-
A logout without user interaction (i.e. no prompt) is only allowed
374-
if an ID Token is provided that matches the current user.
375379
"""
376-
return (
377-
oauth2_settings.OIDC_RP_INITIATED_LOGOUT_ALWAYS_PROMPT
378-
or token_user is None
379-
or token_user != self.request.user
380-
)
380+
381+
if oauth2_settings.OIDC_RP_INITIATED_LOGOUT_ALWAYS_PROMPT:
382+
"""
383+
> At the Logout Endpoint, the OP SHOULD ask the End-User whether to
384+
> log out of the OP as well
385+
386+
The admin has configured the OP to always prompt the userfor logout
387+
per the SHOULD recommendation.
388+
"""
389+
return True
390+
391+
if token_user is None:
392+
"""
393+
> the OP MUST ask ask the End-User whether to log out of the OP as
394+
> well if the supplied ID Token does not belong to the current OP
395+
> session with the RP.
396+
397+
token_user will only be populated if an ID token was found for the
398+
RP (Application) that is requesting the logout. If token_user is not
399+
then we must prompt the user.
400+
"""
401+
return True
402+
403+
if self.request.user.is_authenticated and token_user != self.request.user:
404+
"""
405+
> the OP MUST ask ask the End-User whether to log out of the OP as
406+
> well if the supplied ID Token does not belong to the logged in
407+
> End-User.
408+
409+
is_authenticated indicates that there is a logged in user.
410+
token_user != self.request.user indicates that the token does not
411+
belong to the logged in user. Therefore we need to prompt the user.
412+
"""
413+
return True
414+
415+
""" We didn't find a reason to prompt the user """
416+
return False
381417

382418
def do_logout(self, application=None, post_logout_redirect_uri=None, state=None, token_user=None):
383419
user = token_user or self.request.user

tests/test_oidc_views.py

+4
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,10 @@ def test_must_prompt(oidc_tokens, other_user, rp_settings, ALWAYS_PROMPT):
311311
== ALWAYS_PROMPT
312312
)
313313
assert RPInitiatedLogoutView(request=mock_request_for(other_user)).must_prompt(oidc_tokens.user) is True
314+
assert (
315+
RPInitiatedLogoutView(request=mock_request_for(AnonymousUser())).must_prompt(oidc_tokens.user)
316+
is False
317+
)
314318

315319

316320
def test__load_id_token():

0 commit comments

Comments
 (0)