Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add cookie authentication #5

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Added
- Mixin for User to provide properties `is_anonymous_login` and `anonymous_login`
- Cookie authentication support

## [1.1.0]

Expand All @@ -16,8 +17,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [1.0.0]

## [1.0.0]

### Added
- Initial setup

Expand Down
6 changes: 5 additions & 1 deletion drf_anonymous_login/authentication.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,17 @@

AUTH_KEYWORD = "Token"
AUTH_HEADER = "HTTP_X_AUTHORIZATION_ANONYMOUS"
AUTH_COOKIE = "anonymous_token"


class AnonymousLoginAuthentication(authentication.BaseAuthentication):
keyword = AUTH_KEYWORD

def authenticate(self, request):
auth = request.META.get(AUTH_HEADER, "").split()
auth = (
request.META.get(AUTH_HEADER, "").split()
or request.COOKIES.get(AUTH_COOKIE, "").split()
)

if not auth or auth[0].lower() != self.keyword.lower():
return None
Expand Down
4 changes: 3 additions & 1 deletion drf_anonymous_login/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,9 @@ def create(self, request, *args, **kwargs):
"headers": self.extract_request_headers(request),
}
)
return Response({"token": user.token}, status=status.HTTP_201_CREATED)
response = Response({"token": user.token}, status=status.HTTP_201_CREATED)
response.set_cookie("anonymous_token", f"Token {user.token}")
return response


class AnonymousLoginAuthenticationModelViewSet(viewsets.ModelViewSet):
Expand Down
21 changes: 20 additions & 1 deletion tests/testapp/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.test import TestCase
from django.urls import reverse
from django.utils import timezone
from rest_framework.status import HTTP_200_OK, HTTP_403_FORBIDDEN
from rest_framework.status import HTTP_200_OK, HTTP_201_CREATED, HTTP_403_FORBIDDEN
from testapp.models import PrivateModel, PublicModel, User

from drf_anonymous_login.authentication import AUTH_HEADER, AUTH_KEYWORD
Expand Down Expand Up @@ -139,3 +139,22 @@ def test_user_get_no_anonymous_login(self):
"""
user = User.objects.create(username="user", password="password")
self.assertIsNone(user.anonymous_login)

def test_anonymous_login_with_cookie_authentication(self):
"""
Assert that cookie authentication works
:return:
"""
# create anonymous login and check if cookie is set
url = reverse("auth_anonymous-list")
response = self.client.post(url)
self.assertEqual(HTTP_201_CREATED, response.status_code)
self.assertEqual(
response.cookies["anonymous_token"].value, f"Token {response.data['token']}"
)

# set cookies and make sure it works
url = reverse("privatemodel-list")
self.client.cookies.load({"anonymous_token": f"Token {response.data['token']}"})
response = self.client.get(url)
self.assertEqual(HTTP_200_OK, response.status_code)
Loading