Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

from django.apps import AppConfig
class TestappConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'testapp'
def ready(self):
import djwto.tokens as tokens
import requests
sess = requests.Session()
sess.verify = False # For testing locally
sess.post('https://localhost:8002/login/',
data={'username': 'alice', 'password': 'pass'})
sess.headers.update({'X-CSRFToken': sess.cookies['csrftoken']})
r = sess.get('https://localhost:8002/protect/')
# ./testapp/urls.py
from django.urls import path
from .views import ProtectedView, PermsProtectedView
urlpatterns = [
path('protect/', ProtectedView.as_view(), name='protect'),
path('perms_protect/', PermsProtectedView.as_view(), name='perms_protect')
]
# ./testapp/views.py
import djwto.authentication as auth # type: ignore
from django.views import View
from django.utils.decorators import method_decorator
from django.http.response import HttpResponse
class ProtectedView(View):
def dispatch(self, request, *args, **kwargs):
return super().dispatch(request, *args, **kwargs)
import requests
sess = requests.Session()
sess.verify = False # For testing locally
sess.post('https://localhost:8002/login/',
data={'username': 'alice', 'password': 'pass'})
sess.headers.update({'X-CSRFToken': sess.cookies['csrftoken']})
r = sess.post('https://localhost:8002/api/token/refresh/validate_refresh/',
import requests
sess = requests.Session()
sess.verify = False # For testing locally
sess.post('https://localhost:8002/login/',
data={'username': 'alice', 'password': 'pass'})
sess.headers.update({'X-CSRFToken': sess.cookies['csrftoken']})
r = sess.post('https://localhost:8002/validate_access/',
import requests
sess = requests.Session()
sess.verify = False # For testing locally
r = sess.post('https://localhost:8002/login/',
data={'username': 'alice', 'password': 'pass'})
sess.headers.update({'AUTHORIZATION': f'Bearer {r.json()["access"]}'})
import requests
sess = requests.Session()
sess.verify = False # For testing locally
r = sess.post('https://localhost:8002/login/',
data={'username': 'alice', 'password': 'pass'})
from django.contrib.auth import get_user_model
User = get_user_model()
user = User.objects.create_user('alice', 'alice@djwto.com', 'pass')
user.save()
#./djwto_project/settings.py
(...)
DJWTO_SIGNING_KEY = os.environ['DJWTO_SIGNING_KEY']
DJWTO_MODE = 'JSON'
DJWTO_REFRESH_COOKIE_PATH = 'api/token/refresh'
DJWTO_SAME_SITE = 'Lax'