-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(auth): Correct typo and add JWT authentication
The typo error in the EmployeeFilters was fixed where `fisrt_name` was changed to `first_name`. A new authentication using JWT (JSON Web Tokens) was added on various files, and new settings for JWT were also appended on settings.py. A new router is also added on urls.py for the JWT authentication, and an authentication field is activated on the Employee views. A file auth.py was created to handle customized authentication functionalities. This will establish a secure way to transmit information between parties and prevent unauthorized access.
- Loading branch information
1 parent
233326d
commit 923454b
Showing
5 changed files
with
114 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
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 |
---|---|---|
@@ -0,0 +1,56 @@ | ||
# !/usr/bin/python3 | ||
# -*- coding: utf-8 -*- | ||
from typing import Dict, Type | ||
|
||
from django.contrib.auth import get_user_model | ||
from django.contrib.auth.backends import ModelBackend | ||
from ninja import Schema | ||
from ninja_jwt.schema import TokenObtainInputSchemaBase | ||
from ninja_jwt.tokens import RefreshToken | ||
|
||
from core.schemas import StandResponse | ||
|
||
|
||
class UserSchema(Schema): | ||
first_name: str | ||
email: str | ||
|
||
|
||
class MyTokenObtainPairOutSchema(Schema): | ||
refresh: str | ||
access: str | ||
user: UserSchema | ||
|
||
|
||
class MyTokenObtainPairInputSchema(TokenObtainInputSchemaBase): | ||
@classmethod | ||
def get_response_schema(cls) -> Type[Schema]: | ||
# TODO now only work get pair token success | ||
# not work at get token fail and refresh | ||
return StandResponse[MyTokenObtainPairOutSchema] | ||
|
||
@classmethod | ||
def get_token(cls, user) -> Dict: | ||
values = {} | ||
refresh = RefreshToken.for_user(user) | ||
values["refresh"] = str(refresh) | ||
values["access"] = str(refresh.access_token) | ||
values.update( | ||
user=UserSchema.from_orm(user) | ||
) | ||
return {'data': values} | ||
|
||
|
||
class CustomAuthBackend(ModelBackend): | ||
def authenticate(self, request, username=None, password=None, **kwargs): | ||
User = get_user_model() | ||
|
||
try: | ||
user = User.objects.get(username=username) | ||
except User.DoesNotExist: | ||
return None | ||
|
||
if user.check_password(password): | ||
return user | ||
else: | ||
return 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