-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add token authentication for django views (#762)
* add custom backend with token auth * fix return * add as a separate backend instead of subclassing the default * api endpoint logs user in * add to client test * lint * add backend field to HAWCUser model * change filename and subclass * add anon check to test * fix backend issue without a migration * make backend change as attr of user model * only query db if a token value is provided * no need to reauthenticate * lint Co-authored-by: Andy Shapiro <[email protected]>
- Loading branch information
1 parent
f0c4317
commit ff70df3
Showing
5 changed files
with
44 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
from django.contrib.auth.backends import ModelBackend | ||
from rest_framework.authentication import get_authorization_header | ||
from rest_framework.authtoken.models import Token | ||
|
||
|
||
class TokenBackend(ModelBackend): | ||
keyword = "Token" | ||
|
||
def get_token_from_header(self, request) -> str: | ||
auth = get_authorization_header(request).split() | ||
token = "" | ||
if auth and auth[0].lower() == self.keyword.lower().encode() and len(auth) == 2: | ||
try: | ||
token = auth[1].decode() | ||
except UnicodeError: | ||
pass | ||
return token | ||
|
||
def authenticate(self, request, token=None, **kwargs): | ||
if request is not None and token is None: | ||
token = self.get_token_from_header(request) | ||
if not token: | ||
return None | ||
try: | ||
token_obj = Token.objects.select_related("user").get(key=token) | ||
except Token.DoesNotExist: | ||
return | ||
return token_obj.user if self.user_can_authenticate(token_obj.user) else None |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -47,11 +47,19 @@ def test_set_authentication_token(self): | |
client.set_authentication_token("123") | ||
assert err.value.status_code == 403 | ||
|
||
with pytest.raises(HawcClientException) as err: | ||
client.session.get(f"{self.live_server_url}/assessment/1/") | ||
assert err.value.status_code == 403 | ||
|
||
user = HAWCUser.objects.get(email="[email protected]") | ||
token = user.get_api_token().key | ||
resp = client.set_authentication_token(token) | ||
assert resp == {"valid": True} | ||
|
||
# can also access regular Django views that are private | ||
resp = client.session.get(f"{self.live_server_url}/assessment/1/") | ||
assert resp.status_code == 200 | ||
|
||
#################### | ||
# BaseClient tests # | ||
#################### | ||
|