-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjwt.py
51 lines (40 loc) · 1.57 KB
/
jwt.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
40
41
42
43
44
45
46
47
48
49
50
51
import os
from datetime import datetime, timedelta
from typing import Optional
from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
from jose import JWTError, jwt
from schemas import TokenData
# JWT settings
SECRET_KEY = os.environ.get('SECRET_KEY', 'bf7234b66257ea32f286db2d64de00761b89667a126f25e509a180237aaa54cf')
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_MINUTES = 30
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="login")
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None) -> str:
to_encode = data.copy()
if expires_delta:
expire = datetime.utcnow() + expires_delta
else:
expire = datetime.utcnow() + timedelta(minutes=10080)
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt
def verify_token(token: str) -> TokenData:
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
id: int = payload.get("id")
name: str = payload.get("name")
if name is None:
raise credentials_exception
token_data = TokenData(name=name, id=id)
except JWTError:
raise credentials_exception
return token_data
async def get_current_user(token: str = Depends(oauth2_scheme)) -> TokenData:
token_data = verify_token(token=token)
return token_data