Skip to content

Commit

Permalink
Fix 500 errors no user is found during logout (#1284)
Browse files Browse the repository at this point in the history
  • Loading branch information
tonial committed Jun 12, 2023
1 parent 9000f45 commit f28ca84
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added
* #1273 Add caching of loading of OIDC private key.

- ### Fixed
* #1284 Allow to logout whith no id_token_hint even if the browser session already expired

## [2.3.0] 2023-05-31

### WARNING
Expand Down
8 changes: 5 additions & 3 deletions oauth2_provider/views/oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from urllib.parse import urlparse

from django.contrib.auth import logout
from django.contrib.auth.models import AnonymousUser
from django.http import HttpResponse, JsonResponse
from django.urls import reverse
from django.utils.decorators import method_decorator
Expand Down Expand Up @@ -362,12 +363,13 @@ def form_valid(self, form):
return self.error_response(error)

def do_logout(self, application=None, post_logout_redirect_uri=None, state=None, token_user=None):
# Delete Access Tokens
if oauth2_settings.OIDC_RP_INITIATED_LOGOUT_DELETE_TOKENS:
user = token_user or self.request.user
# Delete Access Tokens if a user was found
if oauth2_settings.OIDC_RP_INITIATED_LOGOUT_DELETE_TOKENS and not isinstance(user, AnonymousUser):
AccessToken = get_access_token_model()
RefreshToken = get_refresh_token_model()
access_tokens_to_delete = AccessToken.objects.filter(
user=token_user or self.request.user,
user=user,
application__client_type__in=self.token_deletion_client_types,
application__authorization_grant_type__in=self.token_deletion_grant_types,
)
Expand Down
9 changes: 9 additions & 0 deletions tests/test_oidc_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,6 +401,15 @@ def test_rp_initiated_logout_post_allowed(logged_in_client, oidc_tokens, rp_sett
assert not is_logged_in(logged_in_client)


@pytest.mark.django_db
def test_rp_initiated_logout_post_no_session(client, oidc_tokens, rp_settings):
form_data = {"client_id": oidc_tokens.application.client_id, "allow": True}
rsp = client.post(reverse("oauth2_provider:rp-initiated-logout"), form_data)
assert rsp.status_code == 302
assert rsp["Location"] == "http://testserver/"
assert not is_logged_in(client)


@pytest.mark.django_db
@pytest.mark.oauth2_settings(presets.OIDC_SETTINGS_RP_LOGOUT)
def test_rp_initiated_logout_expired_tokens_accept(logged_in_client, application, expired_id_token):
Expand Down

0 comments on commit f28ca84

Please sign in to comment.