-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoauth.py
More file actions
42 lines (33 loc) · 1.45 KB
/
Copy pathoauth.py
File metadata and controls
42 lines (33 loc) · 1.45 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
from jose import JWTError,jwt
from datetime import datetime,timedelta
from model.user import TokenData,Token,User,UserType
from config.database import get_db
from sqlalchemy.orm import Session
from fastapi import Depends,status,HTTPException
from fastapi.security import OAuth2PasswordBearer
oauth2_scheme = OAuth2PasswordBearer(tokenUrl='login')
# secret key
SECRET_KEY = 'user_activity'
ALGORITHM = 'HS256'
ACCESS_TOKEN_MINUTES = 30
def create_access_token(data:dict):
to_encode = data.copy()
expire = datetime.utcnow() + timedelta(minutes=ACCESS_TOKEN_MINUTES)
to_encode.update({'exp': expire})
encoded_jwt = jwt.encode(to_encode,SECRET_KEY,algorithm=ALGORITHM)
return encoded_jwt
def verify_access_token (token:str,credentials_exceiptions):
try:
payload = jwt.decode(token,SECRET_KEY,algorithm=[ALGORITHM])
id:str = payload.get('user_id')
if id is None:
raise credentials_exceiptions
token_data = TokenData(id=id)
except JWTError:
raise credentials_exceiptions
return token_data
def get_current_user(token:str = Depends(oauth2_scheme),db:Session = Depends(get_db)):
credential_exception = HTTPException(status_code=status.HTTP_401_UNAUTHORIZED,detail='not validate credential',headers={'WWW-Authenticate': 'Bearer'})
tokens = verify_access_token(token,credential_exception)
user = db.query(User).filter(User.id == tokens.id).first()
return user