This repository has been archived by the owner on Jul 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent fallback to session auth when basic auth fails on /auth/ endp…
…oint; upgrade dependencies
- Loading branch information
1 parent
59f6b46
commit 2be1cea
Showing
3 changed files
with
39 additions
and
5 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,33 @@ | ||
from django.contrib.auth import authenticate, get_user_model | ||
from rest_framework import authentication | ||
from rest_framework import exceptions | ||
from rest_framework import status | ||
from django.utils.translation import ugettext_lazy as _ | ||
|
||
|
||
class CustomBasicAuthentication(authentication.BasicAuthentication): | ||
|
||
def authenticate_credentials(self, userid, password, request=None): | ||
""" | ||
Authenticate the userid and password against username and password | ||
with optional request for context. | ||
""" | ||
credentials = { | ||
get_user_model().USERNAME_FIELD: userid, | ||
'password': password | ||
} | ||
user = authenticate(request=request, **credentials) | ||
|
||
if user is None: | ||
raise AuthenticationFailed(_('Invalid username/password.')) | ||
|
||
if not user.is_active: | ||
raise AuthenticationFailed(_('User inactive or deleted.')) | ||
|
||
return (user, None) | ||
|
||
|
||
class AuthenticationFailed(exceptions.APIException): | ||
status_code = status.HTTP_403_FORBIDDEN | ||
default_detail = _('Incorrect authentication credentials.') | ||
default_code = 'authentication_failed' |
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