-
Notifications
You must be signed in to change notification settings - Fork 429
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: Catch errors when failing to verify JWTs (#5153)
Co-authored-by: Kim Gustyr <[email protected]>
- Loading branch information
Showing
6 changed files
with
102 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
api/tests/unit/custom_auth/jwt_cookie/test_unit_jwt_cookie_authentication.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
from typing import Type | ||
|
||
import pytest | ||
from pytest_mock import MockerFixture | ||
from rest_framework.request import Request | ||
from rest_framework_simplejwt.exceptions import ( | ||
AuthenticationFailed, | ||
InvalidToken, | ||
TokenError, | ||
) | ||
from rest_framework_simplejwt.tokens import Token | ||
|
||
from custom_auth.jwt_cookie.authentication import JWTCookieAuthentication | ||
from custom_auth.jwt_cookie.constants import JWT_SLIDING_COOKIE_KEY | ||
from users.models import FFAdminUser | ||
|
||
|
||
def test_authenticate_without_cookie(mocker: MockerFixture) -> None: | ||
# Given | ||
auth = JWTCookieAuthentication() | ||
request = mocker.MagicMock(spec=Request) | ||
request.COOKIES = {} | ||
|
||
# When | ||
result = auth.authenticate(request) | ||
|
||
# Then | ||
assert result is None | ||
|
||
|
||
def test_authenticate_valid_cookie(mocker: MockerFixture) -> None: | ||
# Given | ||
auth = JWTCookieAuthentication() | ||
request = mocker.MagicMock(spec=Request) | ||
raw_token = "valid_token" | ||
request.COOKIES = {JWT_SLIDING_COOKIE_KEY: raw_token} | ||
|
||
validated_token = mocker.MagicMock(spec=Token) | ||
user = mocker.MagicMock(spec=FFAdminUser) | ||
|
||
# Mock the validation and user retrieval | ||
mock_validate = mocker.patch.object( | ||
auth, "get_validated_token", return_value=validated_token | ||
) | ||
mock_get_user = mocker.patch.object(auth, "get_user", return_value=user) | ||
|
||
# When | ||
result = auth.authenticate(request) | ||
|
||
# Then | ||
assert result == (user, validated_token) | ||
mock_validate.assert_called_once_with(raw_token) | ||
mock_get_user.assert_called_once_with(validated_token) | ||
|
||
|
||
@pytest.mark.parametrize( | ||
"exception_class", [InvalidToken, TokenError, AuthenticationFailed] | ||
) | ||
def test_authenticate_invalid_cookie( | ||
mocker: MockerFixture, | ||
exception_class: Type[Exception], | ||
) -> None: | ||
# Given | ||
auth = JWTCookieAuthentication() | ||
request = mocker.MagicMock(spec=Request) | ||
raw_token = "invalid_token" | ||
request.COOKIES = {JWT_SLIDING_COOKIE_KEY: raw_token} | ||
|
||
# Test that no further exceptions are raised if the token is invalid in any way | ||
mocker.patch.object( | ||
auth, "get_validated_token", side_effect=exception_class("Error") | ||
).side_effect = exception_class("Error") | ||
|
||
# When | ||
result = auth.authenticate(request) | ||
|
||
# Then | ||
assert result is None |