Skip to content

Commit 21a5430

Browse files
committed
DRF JWT configured
1 parent af436ae commit 21a5430

File tree

4 files changed

+42
-2
lines changed

4 files changed

+42
-2
lines changed

curl-commands.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
curl --header "Content-Type: application/json" -X POST http://127.0.0.1:8000/api/token/obtain/ --data '{"username":"djsr","password":"djsr"}'
2+
curl --header "Content-Type: application/json" -X POST http://127.0.0.1:8000/api/user/create/user/ --data '{"email":"blue2@blue.com","username":"blue2","password":"blueblue"}'
3+
curl --header "Content-Type: application/json" --header "Authorization: JWT eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTU2MTYyMTg0OSwianRpIjoiYmE3OWUxZTEwOWJkNGU3NmI1YWZhNWQ5OTg5MTE0NjgiLCJ1c2VyX2lkIjoxfQ.S7tDJaaymUUNs74Gnt6dX2prIU_E8uqCPzMtd8Le0VI","access":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoiYWNjZXNzIiwiZXhwIjoxNTYwMjIxODU1LCJqdGkiOiI0ZjFiZDE3OTE0Zjk0MjRhOTNlZDA1YTBhMTM0N2U3YSIsInVzZXJfaWQiOjg3LCJ1c2VybmFtZSI6ImJsdWU4In0.69YNgpXhkzOklIS_nXolVuYwvN4vh7jlSeQ-oQxqxjg" -X POST http://127.0.0.1:8000/api/token/refresh/ --data '{"refresh":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTU2MTYyMTg0OSwianRpIjoiYmE3OWUxZTEwOWJkNGU3NmI1YWZhNWQ5OTg5MTE0NjgiLCJ1c2VyX2lkIjoxfQ.S7tDJaaymUUNs74Gnt6dX2prIU_E8uqCPzMtd8Le0VI"}'
4+
curl --header "Content-Type: application/json" -X POST http://127.0.0.1:8000/api/token/refresh/ --data '{"refresh":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ0b2tlbl90eXBlIjoicmVmcmVzaCIsImV4cCI6MTU2MTYyMTg0OSwianRpIjoiYmE3OWUxZTEwOWJkNGU3NmI1YWZhNWQ5OTg5MTE0NjgiLCJ1c2VyX2lkIjoxfQ.S7tDJaaymUUNs74Gnt6dX2prIU_E8uqCPzMtd8Le0VI"}'

djsr/authentication/urls.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
from django.urls import path
2+
from rest_framework_simplejwt import views as jwt_views
3+
4+
urlpatterns = [
5+
path('token/obtain/', jwt_views.TokenObtainPairView.as_view(), name='token_create'), # override sjwt stock token
6+
path('token/refresh/', jwt_views.TokenRefreshView.as_view(), name='token_refresh'),
7+
]

djsr/djsr/settings.py

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
"""
1212

1313
import os
14+
from datetime import timedelta
15+
1416

1517
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
1618
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
@@ -37,7 +39,8 @@
3739
'django.contrib.sessions',
3840
'django.contrib.messages',
3941
'django.contrib.staticfiles',
40-
'authentication'
42+
'authentication',
43+
'rest_framework'
4144
]
4245

4346
MIDDLEWARE = [
@@ -122,3 +125,28 @@
122125

123126
# Custom user model
124127
AUTH_USER_MODEL = "authentication.CustomUser"
128+
129+
# Rest Framework
130+
REST_FRAMEWORK = {
131+
'DEFAULT_PERMISSION_CLASSES': (
132+
'rest_framework.permissions.IsAuthenticated',
133+
),
134+
'DEFAULT_AUTHENTICATION_CLASSES': (
135+
'rest_framework_simplejwt.authentication.JWTAuthentication',
136+
),
137+
}
138+
139+
SIMPLE_JWT = {
140+
'ACCESS_TOKEN_LIFETIME': timedelta(minutes=5),
141+
'REFRESH_TOKEN_LIFETIME': timedelta(days=14),
142+
'ROTATE_REFRESH_TOKENS': True,
143+
'BLACKLIST_AFTER_ROTATION': False,
144+
'ALGORITHM': 'HS256',
145+
'SIGNING_KEY': SECRET_KEY,
146+
'VERIFYING_KEY': None,
147+
'AUTH_HEADER_TYPES': ('JWT',),
148+
'USER_ID_FIELD': 'id',
149+
'USER_ID_CLAIM': 'user_id',
150+
'AUTH_TOKEN_CLASSES': ('rest_framework_simplejwt.tokens.AccessToken',),
151+
'TOKEN_TYPE_CLAIM': 'token_type',
152+
}

djsr/djsr/urls.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
1515
"""
1616
from django.contrib import admin
17-
from django.urls import path
17+
from django.urls import path, include
1818

1919
urlpatterns = [
2020
path('admin/', admin.site.urls),
21+
path('api/', include('authentication.urls'))
2122
]

0 commit comments

Comments
 (0)