Core: add jwt auth

This commit is contained in:
Omma 2024-08-21 16:38:37 +03:30
parent 1e0ffd9b2d
commit 495312b060
12 changed files with 234 additions and 17 deletions

0
jwtauth/__init__.py Normal file
View file

3
jwtauth/admin.py Normal file
View file

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
jwtauth/apps.py Normal file
View file

@ -0,0 +1,6 @@
from django.apps import AppConfig
class JwtauthConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'jwtauth'

View file

@ -0,0 +1,22 @@
# Generated by Django 5.0.7 on 2024-08-19 12:06
from django.db import migrations, models
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='OkService',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=30)),
('province', models.CharField(max_length=30)),
],
),
]

View file

7
jwtauth/models.py Normal file
View file

@ -0,0 +1,7 @@
from django.db import models
# Create your models here.
class OkService(models.Model):
name = models.CharField(max_length=30)
province = models.CharField(max_length=30)

3
jwtauth/tests.py Normal file
View file

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

20
jwtauth/urls.py Normal file
View file

@ -0,0 +1,20 @@
from django.urls import path
from rest_framework_simplejwt.views import (
TokenObtainPairView,
TokenRefreshView,
TokenVerifyView,
)
from .views import *
urlpatterns = [
#to do: costumize this!
path('register/', register_service, name='register_service'),
#post username and password, get access and refresh token
path('', TokenObtainPairView.as_view(), name='token_obtain_pair'),
#post refresh token to this url to get a new access token
path('refresh/', TokenRefreshView.as_view(), name='token_refresh'),
#validates the access token
path('verify/', TokenVerifyView.as_view(), name='token_verify'),
]

43
jwtauth/views.py Normal file
View file

@ -0,0 +1,43 @@
from django.shortcuts import render
from rest_framework.decorators import api_view
from rest_framework_simplejwt.tokens import RefreshToken
from rest_framework_simplejwt.exceptions import InvalidToken
from rest_framework_simplejwt.tokens import UntypedToken
import json
from .models import OkService
@api_view(['POST'])
def register_service(request):
try:
data = dict(json.loads(request.body.decode('utf-8')))
print(data, type(data))
if "shared_key" in dict(data).keys:
if first_step_check():
...
services = OkService.objects.all()
except Exception as ex:
print(ex)
def first_step_check(shared_key):
service_id = 1
return service_id
def generate_tokens(service_id):
refresh = RefreshToken.for_user(service_id)
return {
'refresh': str(refresh),
'access': str(refresh.access_token),
}
def validate_token(token):
try:
UntypedToken(token)
return True
except InvalidToken:
return False