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/swagger auth #1141

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix code style error
  • Loading branch information
Dawn0472 committed Jun 11, 2023
commit bd15fa30eee1d5814e2338ca7a233bff0eef5894
14 changes: 7 additions & 7 deletions src/core/authentication.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from rest_framework.authentication import TokenAuthentication
from rest_framework import exceptions
from rest_framework import HTTP_HEADER_ENCODING, exceptions
from django.utils.translation import gettext_lazy as _
from .models import Token
Expand All @@ -8,13 +7,14 @@
class BearerAuthentication(TokenAuthentication):
keyword = 'Bearer'
model = Token

def get_model(self):
if self.model is not None:
return self.model
from rest_framework.authtoken.models import Token
return Token
def get_authorization_header(self,request):

def get_authorization_header(self, request):
"""
Return request's 'Authorization:' header, as a bytestring.
Hide some test client ickyness where the header can be unicode.
Expand All @@ -24,13 +24,13 @@ def get_authorization_header(self,request):
# Work around django test client oddness
auth = auth.encode(HTTP_HEADER_ENCODING)
return auth

def authenticate(self, request):
auth = self.get_authorization_header(request).split()

if not auth :
if not auth:
return None

token = auth[0].decode()

return self.authenticate_credentials(token)
Expand All @@ -45,4 +45,4 @@ def authenticate_credentials(self, key):
if not token.user.is_active:
raise exceptions.AuthenticationFailed(_('User inactive or deleted.'))

return (token.user, token)
return (token.user, token)
2 changes: 1 addition & 1 deletion src/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ class Token(models.Model):
"""
key = models.CharField(_("Key"), max_length=40, primary_key=True)
user = BigForeignKey(
to=settings.AUTH_USER_MODEL,
to=settings.AUTH_USER_MODEL,
related_name='%(app_label)s_auth_token',
verbose_name=_('user'),
on_delete=models.CASCADE,
Expand Down
2 changes: 1 addition & 1 deletion src/pycontw2016/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
'rest_framework.authentication.TokenAuthentication',
'core.authentication.BearerAuthentication',
],
'DEFAULT_PERMISSION_CLASSES':{
'DEFAULT_PERMISSION_CLASSES': {
'rest_framework.permissions.IsAuthenticated'

}
Expand Down
23 changes: 11 additions & 12 deletions src/users/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from core.models import Token
from drf_yasg.utils import swagger_auto_schema
from drf_yasg import openapi
from rest_framework import exceptions
from rest_framework import exceptions
from django.contrib.auth import get_user_model
from users.models import User
from datetime import datetime, timedelta
Expand All @@ -14,40 +14,39 @@ class CustomAuthToken(ObtainAuthToken):
@swagger_auto_schema(
request_body=openapi.Schema(
type=openapi.TYPE_OBJECT,
required=['username','password'],
required=['username', 'password'],
order=['username', 'password'],
properties={
'username':openapi.Schema(type=openapi.TYPE_STRING),
'password':openapi.Schema(type=openapi.TYPE_STRING)
'username': openapi.Schema(type=openapi.TYPE_STRING),
'password': openapi.Schema(type=openapi.TYPE_STRING)
},
),
operation_description='Get account token'
operation_description='Get account token'
)

def post(self, request):
username = request.data['username']
try:
user = get_user_model().objects.get(email=username)
except User.DoesNotExist:
raise exceptions.AuthenticationFailed(('User matching query does not exist'))

tokens = Token.objects.filter(user=user)
if len(tokens)== 0:
if len(tokens) == 0:
Token.objects.create(user=user)

token = Token.objects.get(user=user)
token = str(token)

token_create_time = Token.objects.get(key=token).created
pre_week_day = datetime.now(token_create_time.tzinfo) + timedelta(days=-7)
if token_create_time < pre_week_day:
Token.objects.get(key=token).delete()
Token.objects.create(user=user)
Token.objects.create(user=user)

serializer = self.serializer_class(data=request.data, context={'request': request})
serializer.is_valid(raise_exception=True)
user = serializer.validated_data['user']

return Response({
'username': user.email,
'token': token
Expand Down
Loading