Skip to content

Commit 2cf0c8b

Browse files
Jwt simple
1 parent 0d5d8de commit 2cf0c8b

28 files changed

+361
-0
lines changed

jwtsimple/.env

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
DEBUG = True
2+
SECRET_KEY = 'm1o$5hh&n&#s0q6579xww)ez+dy&s!b@u*usye64w21bpai56-'
3+
ALLOWED_HOSTS = '172.28.49.148'
4+
#DATABASE_URL = 'postgresql://postgres:postgres@localhost:5432/api_wallet'
5+
DATABASE_URL = 'postgresql://postgres:postgres@localhost:5432/wallet'
6+

jwtsimple/README.MD

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
https://egcoder.com/posts/django-rest-framework-custom-jwt-authentication/

jwtsimple/brian/__init__.py

Whitespace-only changes.
132 Bytes
Binary file not shown.
173 Bytes
Binary file not shown.
407 Bytes
Binary file not shown.
170 Bytes
Binary file not shown.

jwtsimple/brian/admin.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.contrib import admin
2+
3+
# Register your models here.

jwtsimple/brian/apps.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.apps import AppConfig
2+
3+
4+
class BrianConfig(AppConfig):
5+
default_auto_field = 'django.db.models.BigAutoField'
6+
name = 'brian'

jwtsimple/brian/migrations/__init__.py

Whitespace-only changes.
Binary file not shown.

jwtsimple/brian/models.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.db import models
2+
3+
# Create your models here.

jwtsimple/brian/serializers.py

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
from rest_framework_simplejwt.tokens import RefreshToken
2+
3+
def get_tokens_for_user(user):
4+
refresh = RefreshToken.for_user(user)
5+
6+
return {
7+
'refresh': str(refresh),
8+
'access': str(refresh.access_token),
9+
}

jwtsimple/brian/tests.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.test import TestCase
2+
3+
# Create your tests here.

jwtsimple/brian/url.py

Whitespace-only changes.

jwtsimple/brian/views.py

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from django.shortcuts import render
2+
3+
# Create your views here.

jwtsimple/db.sqlite3

128 KB
Binary file not shown.

jwtsimple/jwtsimple/__init__.py

Whitespace-only changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
543 Bytes
Binary file not shown.

jwtsimple/jwtsimple/serializers.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
from rest_framework_simplejwt.serializers import TokenObtainPairSerializer
2+
from rest_framework_simplejwt.views import TokenObtainPairView
3+
from rest_framework_simplejwt.tokens import RefreshToken
4+
5+
6+
class MyTokenObtainPairSerializer(TokenObtainPairSerializer):
7+
@classmethod
8+
def get_token(cls, user):
9+
print("-------->")
10+
token = super().get_token(user)
11+
12+
# Add custom claims
13+
token['name'] = user.name
14+
# ...
15+
16+
return token
17+
18+
class MyTokenObtainPairView(TokenObtainPairView):
19+
serializer_class = MyTokenObtainPairSerializer
20+
21+
22+
def get_tokens_for_user(user):
23+
refresh = RefreshToken.for_user(user)
24+
25+
return {
26+
'refresh1': str(refresh),
27+
'access1': str(refresh.access_token),
28+
}

jwtsimple/jwtsimple/settings.py

+172
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
"""
2+
Django settings for jwtsimple project.
3+
4+
Generated by 'django-admin startproject' using Django 2.2.12.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/2.2/topics/settings/
8+
9+
For the full list of settings and their values, see
10+
https://docs.djangoproject.com/en/2.2/ref/settings/
11+
"""
12+
13+
import os
14+
import environs
15+
16+
from pathlib import Path
17+
from datetime import timedelta
18+
19+
# Import .env file as env
20+
env = environs.Env()
21+
env.read_env()
22+
23+
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
24+
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
25+
26+
27+
# Quick-start development settings - unsuitable for production
28+
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
29+
30+
# SECURITY WARNING: keep the secret key used in production secret!
31+
SECRET_KEY = 'm1o$5hh&n&#s0q6579xww)ez+dy&s!b@u*usye64w21bpai56-'
32+
33+
# SECURITY WARNING: don't run with debug turned on in production!
34+
DEBUG = True
35+
36+
ALLOWED_HOSTS = []
37+
38+
39+
# Application definition
40+
41+
INSTALLED_APPS = [
42+
'django.contrib.admin',
43+
'django.contrib.auth',
44+
'django.contrib.contenttypes',
45+
'django.contrib.sessions',
46+
'django.contrib.messages',
47+
'django.contrib.staticfiles',
48+
'rest_framework_simplejwt',
49+
'coreapi', # Coreapi for coreapi documentation
50+
'drf_yasg',
51+
'brian',
52+
]
53+
54+
MIDDLEWARE = [
55+
'django.middleware.security.SecurityMiddleware',
56+
'django.contrib.sessions.middleware.SessionMiddleware',
57+
'django.middleware.common.CommonMiddleware',
58+
'django.middleware.csrf.CsrfViewMiddleware',
59+
'django.contrib.auth.middleware.AuthenticationMiddleware',
60+
'django.contrib.messages.middleware.MessageMiddleware',
61+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
62+
]
63+
64+
ROOT_URLCONF = 'jwtsimple.urls'
65+
66+
TEMPLATES = [
67+
{
68+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
69+
'DIRS': [],
70+
'APP_DIRS': True,
71+
'OPTIONS': {
72+
'context_processors': [
73+
'django.template.context_processors.debug',
74+
'django.template.context_processors.request',
75+
'django.contrib.auth.context_processors.auth',
76+
'django.contrib.messages.context_processors.messages',
77+
],
78+
},
79+
},
80+
]
81+
82+
WSGI_APPLICATION = 'jwtsimple.wsgi.application'
83+
84+
REST_FRAMEWORK = {
85+
'DEFAULT_AUTHENTICATION_CLASSES': (
86+
'rest_framework_simplejwt.authentication.JWTAuthentication',
87+
)
88+
89+
}
90+
91+
92+
93+
# Database
94+
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
95+
96+
DATABASES = {
97+
'default': env.dj_db_url(
98+
'DATABASE_URL',
99+
default='sqlite:///' + str(str(BASE_DIR) + '/db.sqlite3')
100+
)
101+
}
102+
103+
104+
GRAPH_MODELS = {
105+
'all_applications': True,
106+
'group_models': True,
107+
'app_labels': ['brian'],
108+
}
109+
110+
# Password validation
111+
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
112+
113+
AUTH_PASSWORD_VALIDATORS = [
114+
{
115+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
116+
},
117+
{
118+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
119+
},
120+
{
121+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
122+
},
123+
{
124+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
125+
},
126+
]
127+
128+
129+
# Rest Framework validation
130+
REST_FRAMEWORK = {
131+
'EXCEPTION_HANDLER':
132+
'jwtsimple.custom_exception_handler.custom_exception_handler',
133+
'DEFAULT_PERMISSION_CLASSES': [
134+
'rest_framework.permissions.IsAuthenticated',
135+
],
136+
'DEFAULT_AUTHENTICATION_CLASSES': [
137+
# 'rest_framework.authentication.TokenAuthentication',
138+
# 'rest_framework.authentication.BasicAuthentication',
139+
# 'rest_framework.authentication.SessionAuthentication',
140+
'rest_framework_simplejwt.authentication.JWTAuthentication',
141+
],
142+
'TEST_REQUEST_DEFAULT_FORMAT': 'json'
143+
}
144+
145+
146+
147+
# Internationalization
148+
# https://docs.djangoproject.com/en/2.2/topics/i18n/
149+
150+
LANGUAGE_CODE = 'es-pe'
151+
152+
TIME_ZONE = 'America/Lima'
153+
154+
USE_I18N = True
155+
156+
USE_L10N = True
157+
158+
USE_TZ = True
159+
160+
161+
# Static files (CSS, JavaScript, Images)
162+
# https://docs.djangoproject.com/en/2.2/howto/static-files/
163+
164+
STATIC_URL = env.str('STATIC_URL', default='/static/')
165+
166+
MEDIA_URL = env.str('MEDIA_URL', default='/media/')
167+
MEDIA_ROOT = os.path.join(BASE_DIR, 'mediafiles')
168+
169+
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles/')
170+
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static/")]
171+
172+
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')

jwtsimple/jwtsimple/urls.py

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
"""jwtsimple URL Configuration
2+
3+
The `urlpatterns` list routes URLs to views. For more information please see:
4+
https://docs.djangoproject.com/en/2.2/topics/http/urls/
5+
Examples:
6+
Function views
7+
1. Add an import: from my_app import views
8+
2. Add a URL to urlpatterns: path('', views.home, name='home')
9+
Class-based views
10+
1. Add an import: from other_app.views import Home
11+
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
12+
Including another URLconf
13+
1. Import the include() function: from django.urls import include, path
14+
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
15+
"""
16+
from django.contrib import admin
17+
from django.urls import path, include, re_path
18+
from rest_framework.permissions import IsAuthenticated, AllowAny
19+
20+
from rest_framework_simplejwt.views import (
21+
TokenObtainPairView,
22+
TokenRefreshView,
23+
TokenVerifyView,
24+
)
25+
26+
from drf_yasg import openapi
27+
from drf_yasg.views import get_schema_view
28+
29+
30+
schema_view = get_schema_view(
31+
openapi.Info(
32+
title="JWT SIMPLE",
33+
default_version='v1',
34+
description="It is a demo jwt",
35+
terms_of_service="https://www.google.com/policies/terms/",
36+
contact=openapi.Contact(email="[email protected]"),
37+
license=openapi.License(name="demo"),
38+
),
39+
public=True,
40+
permission_classes=[AllowAny]
41+
)
42+
43+
44+
urlpatterns = [
45+
path('admin/', admin.site.urls),
46+
path('api/token/', TokenObtainPairView.as_view(), name='token_obtain_pair'),
47+
path('api/token/refresh/', TokenRefreshView.as_view(), name='token_refresh'),
48+
path('api/token/verify/', TokenVerifyView.as_view(), name='token_verify'),
49+
path('brian/', include('brian.urls')),
50+
path('redoc/',
51+
schema_view.with_ui('redoc', cache_timeout=0),
52+
name='schema-redoc'),
53+
re_path(r'^swagger(?P<format>\.json|\.yaml)$',
54+
schema_view.without_ui(cache_timeout=0), name='schema-json'),
55+
]

jwtsimple/jwtsimple/wsgi.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
"""
2+
WSGI config for jwtsimple project.
3+
4+
It exposes the WSGI callable as a module-level variable named ``application``.
5+
6+
For more information on this file, see
7+
https://docs.djangoproject.com/en/2.2/howto/deployment/wsgi/
8+
"""
9+
10+
import os
11+
12+
from django.core.wsgi import get_wsgi_application
13+
14+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'jwtsimple.settings')
15+
16+
application = get_wsgi_application()

jwtsimple/manage.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env python3
2+
"""Django's command-line utility for administrative tasks."""
3+
import os
4+
import sys
5+
6+
7+
def main():
8+
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'jwtsimple.settings')
9+
try:
10+
from django.core.management import execute_from_command_line
11+
except ImportError as exc:
12+
raise ImportError(
13+
"Couldn't import Django. Are you sure it's installed and "
14+
"available on your PYTHONPATH environment variable? Did you "
15+
"forget to activate a virtual environment?"
16+
) from exc
17+
execute_from_command_line(sys.argv)
18+
19+
20+
if __name__ == '__main__':
21+
main()

jwtsimple/requirements.txt

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
asgiref==3.5.2
2+
backports.zoneinfo==0.2.1
3+
certifi==2022.5.18.1
4+
charset-normalizer==2.0.12
5+
coreapi==2.3.3
6+
coreschema==0.0.4
7+
Django==4.0.4
8+
django-filter==21.1
9+
djangorestframework==3.13.1
10+
djangorestframework-simplejwt==5.2.0
11+
drf-yasg==1.20.0
12+
environs==9.5.0
13+
future==0.18.2
14+
idna==3.3
15+
importlib-metadata==4.11.4
16+
inflection==0.5.1
17+
itypes==1.2.0
18+
Jinja2==3.1.2
19+
Markdown==3.3.7
20+
MarkupSafe==2.1.1
21+
marshmallow==3.16.0
22+
openapi-codec==1.3.2
23+
packaging==21.3
24+
psycopg2-binary==2.9.3
25+
PyJWT==2.4.0
26+
pyparsing==3.0.9
27+
python-dotenv==0.20.0
28+
pytz==2022.1
29+
requests==2.27.1
30+
ruamel.yaml==0.17.21
31+
ruamel.yaml.clib==0.2.6
32+
sqlparse==0.4.2
33+
uritemplate==4.1.1
34+
urllib3==1.26.9
35+
zipp==3.8.0

0 commit comments

Comments
 (0)