-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjwtfunction.py
More file actions
90 lines (73 loc) · 2.97 KB
/
jwtfunction.py
File metadata and controls
90 lines (73 loc) · 2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
import jwt
from datetime import datetime, timedelta
from jwt.exceptions import DecodeError, InvalidSignatureError, InvalidAlgorithmError
import random
import string
#jwt 구현 메소드
access_expires = timedelta(days=7) # 토큰의 만료 시간 = 1시간
refresh_expires = timedelta(days=7) # 토큰의 만료 시간 = 7일
access_type = 'access'
refresh_type = 'refresh'
def encode_token(email_id, expires, token_type) :
# 현재 시간을 UTC 기준으로 얻기
current_utc_time = datetime.utcnow()
expiration_utc_time = current_utc_time + expires
# UTC 시간을 문자열로 변환
formatted_utc_time = current_utc_time.isoformat()
# UTC 시간을 문자열로 변환
formatted_exp_utc_time = expiration_utc_time.isoformat()
payload = {
"id" : email_id, #유저 ID
"ist" : formatted_utc_time, # 토큰 발급시간
"expir" : formatted_exp_utc_time, # 만료기간
"type" : token_type #토큰 type
}
encoded_token = jwt.encode(payload, "capstonekey", algorithm="HS256") # 키 : capstonekey
return encoded_token
def decode_token(encoded_token) :
#print('trying_this_token : ', encoded_token)
try :
decoded_token = jwt.decode(encoded_token, "capstonekey", algorithms=["HS256"])
except DecodeError as e:
# 토큰 디코드 오류 처리
print(f"토큰 디코드 오류: {e}")
return None
except InvalidSignatureError as e:
# 서명이 올바르지 않은 경우 처리
print(f"서명 오류: {e}")
return None
except InvalidAlgorithmError as e:
# 알고리즘이 허용되지 않는 경우 처리
print(f"알고리즘 오류: {e}")
return None
return decoded_token
def create_access_token(email_id) :
new_access_token = encode_token(email_id, access_expires, access_type)
return new_access_token
def create_refresh_token(email_id) :
new_refresh_token = encode_token(email_id, access_expires, refresh_type)
return new_refresh_token
def generate_random_string(length):
alphabet = string.ascii_letters + string.digits
random_string = ''.join(random.choice(alphabet) for _ in range(length))
return random_string
def create_tmp_token():
expires = timedelta(days=1)
# 현재 시간을 UTC 기준으로 얻기
current_utc_time = datetime.utcnow()
expiration_utc_time = current_utc_time + expires
# UTC 시간을 문자열로 변환
formatted_utc_time = current_utc_time.isoformat()
# UTC 시간을 문자열로 변환
formatted_exp_utc_time = expiration_utc_time.isoformat()
# 랜덤 문자열 생성
random_string_40 = generate_random_string(40)
payload = {
"id" : random_string_40,
"ist" : formatted_utc_time, # 토큰 발급시간
"expir" : formatted_exp_utc_time, # 만료기간
"type" : 'guest'
}
encoded_token = jwt.encode(payload, "capstonekey", algorithm="HS256")
#print('tmp_token : ', encoded_token)
return encoded_token