-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.py
42 lines (33 loc) · 1.4 KB
/
auth.py
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
from datetime import datetime, timedelta
from typing import Optional
from jose import JWTError, jwt
from fastapi import Header, HTTPException
SECRET = "bank1100500934930"
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE = 500000
def createAccessToken(data: dict, expires_delta: Optional[timedelta] = None):
if expires_delta:
expire = datetime.utcnow() + expires_delta
else:
expire = datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_EXPIRE)
data.update({"exp": expire})
encoded_jwt = jwt.encode(data, SECRET, algorithm=ALGORITHM)
payload = {"type":"bearer", "token": encoded_jwt}
return payload
def validateAccessToken(authorization: Optional[str] = Header(None)):
if authorization is None:
raise HTTPException(status_code=401, detail="Unauthorized")
try:
token_type, token_value = authorization.split()
except:
raise HTTPException(status_code=401, detail="Token not in the right format. Use 'Authorization: Bearer <token>'")
if token_type != "Bearer":
raise HTTPException(status_code=401, detail="Invalid Token Type")
try:
payload = jwt.decode(token_value, SECRET, algorithms=[ALGORITHM])
return payload
except JWTError as jwt_error:
if jwt_error:
raise HTTPException(status_code=500, detail=f"{jwt_error}")
else:
raise HTTPException(status_code=400, detail="Unauthorized")