|
3 | 3 | from django.contrib.auth.models import AnonymousUser |
4 | 4 | from django.test import RequestFactory, TestCase |
5 | 5 | from django.urls import reverse |
| 6 | +from django.utils import timezone |
6 | 7 |
|
7 | 8 | from oauth2_provider.exceptions import ClientIdMissmatch, InvalidOIDCClientError, InvalidOIDCRedirectURIError |
8 | | -from oauth2_provider.models import get_id_token_model |
| 9 | +from oauth2_provider.models import get_access_token_model, get_id_token_model, get_refresh_token_model |
9 | 10 | from oauth2_provider.oauth2_validators import OAuth2Validator |
10 | 11 | from oauth2_provider.settings import oauth2_settings |
11 | 12 | from oauth2_provider.views.oidc import _load_id_token, _validate_claims, validate_logout_request |
@@ -474,6 +475,58 @@ def test_userinfo_endpoint_bad_token(oidc_tokens, client): |
474 | 475 | assert rsp.status_code == 401 |
475 | 476 |
|
476 | 477 |
|
| 478 | +@pytest.mark.django_db |
| 479 | +def test_token_deletion_on_logout(oidc_tokens, loggend_in_client, rp_settings): |
| 480 | + AccessToken = get_access_token_model() |
| 481 | + IDToken = get_id_token_model() |
| 482 | + RefreshToken = get_refresh_token_model() |
| 483 | + assert AccessToken.objects.count() == 1 |
| 484 | + assert IDToken.objects.count() == 1 |
| 485 | + assert RefreshToken.objects.count() == 1 |
| 486 | + rsp = loggend_in_client.get( |
| 487 | + reverse("oauth2_provider:rp-initiated-logout"), |
| 488 | + data={ |
| 489 | + "id_token_hint": oidc_tokens.id_token, |
| 490 | + "client_id": oidc_tokens.application.client_id, |
| 491 | + }, |
| 492 | + ) |
| 493 | + assert rsp.status_code == 302 |
| 494 | + assert not is_logged_in(loggend_in_client) |
| 495 | + # Check that all tokens have either been deleted or expired. |
| 496 | + assert all([token.is_expired() for token in AccessToken.objects.all()]) |
| 497 | + assert all([token.is_expired() for token in IDToken.objects.all()]) |
| 498 | + assert all([token.revoked <= timezone.now() for token in RefreshToken.objects.all()]) |
| 499 | + |
| 500 | + |
| 501 | +@pytest.mark.django_db |
| 502 | +@pytest.mark.oauth2_settings(presets.OIDC_SETTINGS_RP_LOGOUT_KEEP_TOKENS) |
| 503 | +def test_token_deletion_on_logout_disabled(oidc_tokens, loggend_in_client, rp_settings): |
| 504 | + rp_settings.OIDC_RP_INITIATED_LOGOUT_DELETE_TOKENS = False |
| 505 | + |
| 506 | + AccessToken = get_access_token_model() |
| 507 | + IDToken = get_id_token_model() |
| 508 | + RefreshToken = get_refresh_token_model() |
| 509 | + assert AccessToken.objects.count() == 1 |
| 510 | + assert IDToken.objects.count() == 1 |
| 511 | + assert RefreshToken.objects.count() == 1 |
| 512 | + rsp = loggend_in_client.get( |
| 513 | + reverse("oauth2_provider:rp-initiated-logout"), |
| 514 | + data={ |
| 515 | + "id_token_hint": oidc_tokens.id_token, |
| 516 | + "client_id": oidc_tokens.application.client_id, |
| 517 | + }, |
| 518 | + ) |
| 519 | + assert rsp.status_code == 302 |
| 520 | + assert not is_logged_in(loggend_in_client) |
| 521 | + # Check that the tokens have not been expired or deleted. |
| 522 | + assert AccessToken.objects.count() == 1 |
| 523 | + assert not any([token.is_expired() for token in AccessToken.objects.all()]) |
| 524 | + assert IDToken.objects.count() == 1 |
| 525 | + assert not any([token.is_expired() for token in IDToken.objects.all()]) |
| 526 | + assert RefreshToken.objects.count() == 1 |
| 527 | + assert not any([token.revoked is not None for token in RefreshToken.objects.all()]) |
| 528 | + |
| 529 | + |
477 | 530 | EXAMPLE_EMAIL = "[email protected]" |
478 | 531 |
|
479 | 532 |
|
|
0 commit comments