Skip to content

Commit 11ea8ca

Browse files
Update
1 parent 2cf0c8b commit 11ea8ca

33 files changed

+426
-0
lines changed

jwtpersonalizada/.env

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
DEBUG = True
2+
SECRET_KEY = 'lwt5nsx3dt3br^gpm&ww_^nfi$h*v&y-t91x+$0@b9pfr#rslr'
3+
ALLOWED_HOSTS ='172.23.206.236'
4+
#DATABASE_URL = 'postgresql://postgres:postgres@localhost:5432/api_wallet'
5+
DATABASE_URL = 'postgresql://postgres:postgres@localhost:5432/demo'

jwtpersonalizada/.gitignore

Whitespace-only changes.

jwtpersonalizada/README.md

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## Link de la Pagina
2+
3+
https://egcoder.com/posts/django-rest-framework-custom-jwt-authentication/

jwtpersonalizada/accounts/__init__.py

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

jwtpersonalizada/accounts/admin.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.contrib import admin
2+
from django.contrib.auth.admin import UserAdmin
3+
from accounts.models import User
4+
5+
# Register your models here.
6+
admin.site.register(User, UserAdmin)

jwtpersonalizada/accounts/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 AccountsConfig(AppConfig):
5+
default_auto_field = 'django.db.models.BigAutoField'
6+
name = 'accounts'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Generated by Django 4.0.4 on 2022-05-31 16:42
2+
3+
import django.contrib.auth.models
4+
import django.contrib.auth.validators
5+
from django.db import migrations, models
6+
import django.utils.timezone
7+
8+
9+
class Migration(migrations.Migration):
10+
11+
initial = True
12+
13+
dependencies = [
14+
('auth', '0012_alter_user_first_name_max_length'),
15+
]
16+
17+
operations = [
18+
migrations.CreateModel(
19+
name='User',
20+
fields=[
21+
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
22+
('password', models.CharField(max_length=128, verbose_name='password')),
23+
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
24+
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
25+
('username', models.CharField(error_messages={'unique': 'A user with that username already exists.'}, help_text='Required. 150 characters or fewer. Letters, digits and @/./+/-/_ only.', max_length=150, unique=True, validators=[django.contrib.auth.validators.UnicodeUsernameValidator()], verbose_name='username')),
26+
('first_name', models.CharField(blank=True, max_length=150, verbose_name='first name')),
27+
('last_name', models.CharField(blank=True, max_length=150, verbose_name='last name')),
28+
('email', models.EmailField(blank=True, max_length=254, verbose_name='email address')),
29+
('is_staff', models.BooleanField(default=False, help_text='Designates whether the user can log into this admin site.', verbose_name='staff status')),
30+
('is_active', models.BooleanField(default=True, help_text='Designates whether this user should be treated as active. Unselect this instead of deleting accounts.', verbose_name='active')),
31+
('date_joined', models.DateTimeField(default=django.utils.timezone.now, verbose_name='date joined')),
32+
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.group', verbose_name='groups')),
33+
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.permission', verbose_name='user permissions')),
34+
],
35+
options={
36+
'verbose_name': 'user',
37+
'verbose_name_plural': 'users',
38+
'abstract': False,
39+
},
40+
managers=[
41+
('objects', django.contrib.auth.models.UserManager()),
42+
],
43+
),
44+
]

jwtpersonalizada/accounts/migrations/__init__.py

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

jwtpersonalizada/accounts/models.py

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from django.db import models
2+
from django.contrib.auth.models import AbstractUser
3+
4+
# Create your models here.
5+
class User(AbstractUser):
6+
pass
+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# accounts.serializers
2+
from rest_framework import serializers
3+
from accounts.models import User
4+
5+
6+
class UserSerializer(serializers.ModelSerializer):
7+
class Meta:
8+
model = User
9+
fields = ['id', 'username', 'email',
10+
'first_name', 'last_name', 'is_active']

jwtpersonalizada/accounts/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.

jwtpersonalizada/accounts/urls.py

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# project.urls
2+
# accounts.urls
3+
4+
5+
from django.urls import path
6+
from .views import login_view, profile
7+
8+
9+
urlpatterns = [
10+
path('profile', profile, name='profile'),
11+
path('login/', login_view, name='login'),
12+
]

jwtpersonalizada/accounts/utils.py

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# accounts.utils
2+
import datetime
3+
import jwt
4+
from django.conf import settings
5+
6+
REFRESH_TOKEN_SECRET = "hola"
7+
8+
def generate_access_token(user):
9+
10+
access_token_payload = {
11+
'user_id': user.id,
12+
'exp': datetime.datetime.utcnow() + datetime.timedelta(days=1, minutes=40),
13+
'iat': datetime.datetime.utcnow(),
14+
}
15+
access_token = jwt.encode(access_token_payload,
16+
settings.SECRET_KEY, algorithm='HS256')
17+
return access_token
18+
19+
20+
def generate_refresh_token(user):
21+
refresh_token_payload = {
22+
'user_id': user.id,
23+
'exp': datetime.datetime.utcnow() + datetime.timedelta(days=7),
24+
'iat': datetime.datetime.utcnow()
25+
}
26+
refresh_token = jwt.encode(
27+
refresh_token_payload, REFRESH_TOKEN_SECRET, algorithm='HS256')#.decode('utf-8')
28+
29+
return refresh_token

jwtpersonalizada/accounts/views.py

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
from django.contrib.auth import get_user_model
2+
from rest_framework.response import Response
3+
from rest_framework import exceptions
4+
from rest_framework.permissions import AllowAny
5+
from rest_framework.decorators import api_view, permission_classes
6+
from django.views.decorators.csrf import ensure_csrf_cookie
7+
from accounts.serializers import UserSerializer
8+
from accounts.utils import generate_access_token, generate_refresh_token
9+
10+
# Create your views here.
11+
12+
@api_view(['GET'])
13+
def profile(request):
14+
user = request.user
15+
serialized_user = UserSerializer(user).data
16+
return Response({'user': serialized_user })
17+
18+
@api_view(['POST'])
19+
@permission_classes([AllowAny])
20+
@ensure_csrf_cookie
21+
def login_view(request):
22+
User = get_user_model()
23+
username = request.data.get('username')
24+
password = request.data.get('password')
25+
response = Response()
26+
if (username is None) or (password is None):
27+
raise exceptions.AuthenticationFailed(
28+
'username and password required')
29+
30+
user = User.objects.filter(username=username).first()
31+
if(user is None):
32+
raise exceptions.AuthenticationFailed('user not found')
33+
if (not user.check_password(password)):
34+
raise exceptions.AuthenticationFailed('wrong password')
35+
36+
serialized_user = UserSerializer(user).data
37+
print(",,,,,")
38+
39+
access_token = generate_access_token(user)
40+
print(">>>", access_token)
41+
refresh_token = generate_refresh_token(user)
42+
print("<<<", refresh_token)
43+
44+
response.set_cookie(key='refreshtoken', value=refresh_token, httponly=True)
45+
response.data = {
46+
'access_token': access_token,
47+
'user': serialized_user,
48+
}
49+
50+
return response

jwtpersonalizada/jwtpersonalizada/__init__.py

Whitespace-only changes.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,190 @@
1+
"""
2+
Django settings for jwtpersonalizada 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+
18+
# Import .env file as env
19+
env = environs.Env()
20+
env.read_env()
21+
22+
# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
23+
#BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
24+
BASE_DIR = Path(__file__).resolve().parent.parent
25+
26+
# SECURITY WARNING: don't run with debug turned on in production!
27+
DEBUG = env.str('DEBUG', default=False)
28+
29+
ALLOWED_HOSTS = env.list('ALLOWED_HOSTS', default=['*'])
30+
31+
AUTH_USER_MODEL = 'accounts.User'
32+
33+
# Quick-start development settings - unsuitable for production
34+
# See https://docs.djangoproject.com/en/2.2/howto/deployment/checklist/
35+
36+
# SECURITY WARNING: keep the secret key used in production secret!
37+
SECRET_KEY = env.str('SECRET_KEY')
38+
REFRESH_TOKEN_SECRET = "hola"
39+
40+
# SECURITY WARNING: don't run with debug turned on in production!
41+
DEBUG = True
42+
43+
44+
45+
# Application definition
46+
47+
INSTALLED_APPS = [
48+
'django.contrib.admin',
49+
'django.contrib.auth',
50+
'django.contrib.contenttypes',
51+
'django.contrib.sessions',
52+
'django.contrib.messages',
53+
'django.contrib.staticfiles',
54+
# 3rd party apps
55+
'corsheaders',
56+
'rest_framework',
57+
58+
# project apps
59+
'accounts',
60+
]
61+
62+
CORS_ALLOW_CREDENTIALS = True # to accept cookies via ajax request
63+
CORS_ORIGIN_WHITELIST = [
64+
'http://localhost:3000' # the domain for front-end app(you can add more than 1)
65+
]
66+
67+
68+
MIDDLEWARE = [
69+
'django.middleware.security.SecurityMiddleware',
70+
'django.contrib.sessions.middleware.SessionMiddleware',
71+
'django.middleware.common.CommonMiddleware',
72+
'django.middleware.csrf.CsrfViewMiddleware',
73+
'django.contrib.auth.middleware.AuthenticationMiddleware',
74+
'django.contrib.messages.middleware.MessageMiddleware',
75+
'django.middleware.clickjacking.XFrameOptionsMiddleware',
76+
]
77+
78+
ROOT_URLCONF = 'jwtpersonalizada.urls'
79+
80+
TEMPLATES = [
81+
{
82+
'BACKEND': 'django.template.backends.django.DjangoTemplates',
83+
'DIRS': [],
84+
'APP_DIRS': True,
85+
'OPTIONS': {
86+
'context_processors': [
87+
'django.template.context_processors.debug',
88+
'django.template.context_processors.request',
89+
'django.contrib.auth.context_processors.auth',
90+
'django.contrib.messages.context_processors.messages',
91+
],
92+
},
93+
},
94+
]
95+
96+
WSGI_APPLICATION = 'jwtpersonalizada.wsgi.application'
97+
98+
99+
# Database
100+
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
101+
102+
DATABASES = {
103+
'default': env.dj_db_url(
104+
'DATABASE_URL',
105+
default='sqlite:///' + str(str(BASE_DIR) + '/db.sqlite3')
106+
)
107+
}
108+
109+
GRAPH_MODELS = {
110+
'all_applications': True,
111+
'group_models': True,
112+
'app_labels': ['applications.clients', 'applications.company',
113+
'applications.cryptocurrencies',
114+
'applications.transactions', 'applications.reports'],
115+
}
116+
117+
118+
119+
# Password validation
120+
# https://docs.djangoproject.com/en/2.2/ref/settings/#auth-password-validators
121+
122+
AUTH_PASSWORD_VALIDATORS = [
123+
{
124+
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
125+
},
126+
{
127+
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
128+
},
129+
{
130+
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
131+
},
132+
{
133+
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
134+
},
135+
]
136+
137+
# Rest Framework validation
138+
# REST_FRAMEWORK = {
139+
# 'EXCEPTION_HANDLER':
140+
# 'jwtpersonalizada.custom_exception_handler.custom_exception_handler',
141+
# 'DEFAULT_PERMISSION_CLASSES': [
142+
# 'rest_framework.permissions.IsAuthenticated',
143+
# ],
144+
# 'DEFAULT_AUTHENTICATION_CLASSES': [
145+
# # 'rest_framework.authentication.TokenAuthentication',
146+
# # 'rest_framework.authentication.BasicAuthentication',
147+
# # 'rest_framework.authentication.SessionAuthentication',
148+
# 'rest_framework_simplejwt.authentication.JWTAuthentication',
149+
# ],
150+
# 'TEST_REQUEST_DEFAULT_FORMAT': 'json'
151+
# }
152+
153+
154+
REST_FRAMEWORK = {
155+
'DEFAULT_PERMISSION_CLASSES': (
156+
'rest_framework.permissions.IsAuthenticated', # make all endpoints private
157+
)
158+
}
159+
160+
161+
162+
# Internationalization
163+
# https://docs.djangoproject.com/en/2.2/topics/i18n/
164+
165+
LANGUAGE_CODE = 'es-pe'
166+
167+
TIME_ZONE = 'America/Lima'
168+
169+
USE_I18N = True
170+
171+
USE_L10N = True
172+
173+
USE_TZ = True
174+
175+
176+
# Static files (CSS, JavaScript, Images)
177+
# https://docs.djangoproject.com/en/2.2/howto/static-files/
178+
179+
# STATIC_URL = '/static/'
180+
STATIC_URL = env.str('STATIC_URL', default='/static/')
181+
MEDIA_URL = env.str('MEDIA_URL', default='/media/')
182+
MEDIA_ROOT = os.path.join(BASE_DIR, 'mediafiles')
183+
184+
STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles/')
185+
STATICFILES_DIRS = [os.path.join(BASE_DIR, "static/")]
186+
187+
MEDIA_ROOT = os.path.join(BASE_DIR, 'media/')
188+
189+
# STATIC_URL = '/static/'
190+
# STATIC_ROOT = os.path.join(BASE_DIR, 'staticfiles')

0 commit comments

Comments
 (0)