-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
[feat] jwt accesstoken 생성
- Loading branch information
Showing
20 changed files
with
180 additions
and
133 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -305,4 +305,5 @@ cython_debug/ | |
Myvenv/ | ||
.DS_Store | ||
|
||
myvenv/ | ||
myvenv/ | ||
.env |
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1 @@ | ||
from django.contrib import admin | ||
from .models import User | ||
|
||
admin.site.register(User) |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,3 @@ | ||
from django.db import models | ||
|
||
class User(models.Model): | ||
uuid = models.CharField(max_length=100) | ||
name = models.CharField(max_length=100) | ||
birthyear = models.CharField(max_length=100) | ||
birthday = models.CharField(max_length=100) | ||
email = models.CharField(max_length=100) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,4 @@ | ||
from django.contrib import admin | ||
from .models import User | ||
|
||
admin.site.register(User) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,11 @@ | ||
from django.db import models | ||
|
||
# Create your models here. | ||
class User(models.Model): | ||
email = models.CharField(max_length=100) | ||
provider = models.CharField(max_length=100) | ||
user_name = models.CharField(max_length=100) | ||
birthday = models.DateField() | ||
access_token = models.CharField(max_length=200) | ||
refresh_token = models.CharField(max_length=200) | ||
expire = models.CharField(max_length=100) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
from rest_framework import serializers | ||
from .models import User | ||
|
||
class UserSerializer(serializers.ModelSerializer): # 유저 추가 | ||
class Meta: | ||
model = User | ||
fields = ('id', 'email', 'provider', 'user_name', 'birthday') | ||
|
||
class UserFindSerializer(serializers.ModelSerializer): # 유저 추가 | ||
class Meta: | ||
model = User | ||
fields = ('email', 'provider') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# 토큰 발급, 복호화하기 위한 함수 관리를 위한 페이지 | ||
|
||
import jwt | ||
import datetime | ||
from decouple import config | ||
|
||
def generate_token(payload, type): # payload 값과 토큰의 종류 | ||
if type == "access": | ||
# 2시간 | ||
exp = datetime.datetime.utcnow() + datetime.timedelta(hours=2) | ||
elif type == "refresh": | ||
# 2주 | ||
exp = datetime.datetime.utcnow() + datetime.timedelta(weeks=2) | ||
else: | ||
raise Exception("Invalid tokenType") | ||
|
||
payload['exp'] = exp | ||
payload['iat'] = datetime.datetime.utcnow() # 발급 시간 | ||
encoded = jwt.encode(payload, config("JWT_SECRET_KEY"), algorithm="HS256") | ||
|
||
return encoded |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,12 @@ | ||
from django.urls import path | ||
from . import views | ||
from django.urls import path, include | ||
from .views import * | ||
from rest_framework.routers import DefaultRouter | ||
|
||
urlpatterns=[ | ||
path('login/',views.login, name='login'), | ||
routers = DefaultRouter() | ||
routers.register('auth', AuthViewSet, basename='auth') | ||
|
||
urlpatterns = [ | ||
path('', include(routers.urls)), | ||
path('signin/', UserList.as_view()), | ||
path('find/', UserFind.as_view()), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,124 @@ | ||
from django.shortcuts import render | ||
|
||
from rest_framework.views import APIView | ||
from rest_framework.response import Response | ||
from rest_framework import status | ||
from rest_framework.decorators import action | ||
from rest_framework import viewsets | ||
|
||
from .serializers import UserFindSerializer, UserSerializer | ||
from .models import User | ||
from .tokens import * | ||
|
||
# Create your views here. | ||
class UserList(APIView): | ||
def post(self, request): # 회원 등록하는 경우 | ||
serializer = UserSerializer(data = request.data) | ||
if serializer.is_valid(): | ||
serializer.save() | ||
return Response(serializer.data, status=status.HTTP_201_CREATED) | ||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST) | ||
|
||
def get(self, request): # 회원 조회하는 경우 | ||
users = User.objects.all() | ||
serializer = UserSerializer(users, many=True) # 다수의 쿼리셋 전달 위해서 many = True | ||
return Response(serializer.data) | ||
|
||
class UserFind(APIView): | ||
def post(self, request): | ||
serializer = UserFindSerializer(data = request.data) | ||
if serializer.is_valid(): | ||
email = request.data['email'] | ||
provider = request.data['provider'] | ||
try: | ||
user = User.objects.get( | ||
email=email, | ||
provider=provider | ||
) | ||
print(user.id) | ||
# payload에 넣을 값 커스텀 가능 | ||
payload_value = user.id | ||
payload = { | ||
"subject": payload_value, | ||
} | ||
|
||
access_token = generate_token(payload, "access") | ||
|
||
data = { | ||
"results": { | ||
"access_token": access_token | ||
} | ||
} | ||
|
||
return Response(data=data, status=status.HTTP_200_OK) | ||
|
||
except User.DoesNotExist: | ||
data = { | ||
"results": { | ||
"msg": "유저 정보가 올바르지 않습니다.", | ||
"code": "E4010" | ||
} | ||
} | ||
return Response(data=data, status=status.HTTP_401_UNAUTHORIZED) | ||
|
||
except Exception as e: | ||
print(e) | ||
data = { | ||
"results": { | ||
"msg": "정상적인 접근이 아닙니다.", | ||
"code": "E5000" | ||
} | ||
} | ||
return Response(data=data, status=status.HTTP_500_INTERNAL_SERVER_ERROR) | ||
|
||
|
||
|
||
class AuthViewSet(viewsets.GenericViewSet): | ||
@action(methods=['POST'], detail=False) | ||
def signin(self, request): | ||
email = request.data['email'] | ||
provider = request.data['provider'] | ||
print(email, provider) | ||
try: | ||
user = User.objects.get( | ||
email=email, | ||
provider=provider | ||
) | ||
|
||
# payload에 넣을 값 커스텀 가능 | ||
payload_value = user.id | ||
payload = { | ||
"subject": payload_value, | ||
} | ||
|
||
access_token = generate_token(payload, "access") | ||
|
||
data = { | ||
"results": { | ||
"access_token": access_token | ||
} | ||
} | ||
|
||
return Response(data=data, status=status.HTTP_200_OK) | ||
|
||
except User.DoesNotExist: | ||
data = { | ||
"results": { | ||
"msg": "유저 정보가 올바르지 않습니다.", | ||
"code": "E4010" | ||
} | ||
} | ||
return Response(data=data, status=status.HTTP_401_UNAUTHORIZED) | ||
|
||
except Exception as e: | ||
print(e) | ||
data = { | ||
"results": { | ||
"msg": "정상적인 접근이 아닙니다.", | ||
"code": "E5000" | ||
} | ||
} | ||
return Response(data=data, status=status.HTTP_500_INTERNAL_SERVER_ERROR) | ||
|
||
def login(request): | ||
return render(request,'userapp/login.html') | ||
return render(request,'userapp/login.html') |