-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
94 lines (71 loc) · 2.78 KB
/
utils.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
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
91
92
93
94
from fastapi import Depends, HTTPException, status, Path
from fastapi.security import OAuth2PasswordBearer
from jose import JWTError, jwt
from datetime import datetime, timedelta, date
from typing import Optional
from passlib.context import CryptContext
from sqlalchemy.orm import Session
import crud
from database import SessionLocal
from config import SECRET_KEY, ALGORITHM
from schemas import TokenData, User
admin_text_desc = 'Only for admin users'
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
def verify_password(plain_password: str, hashed_password: str):
return pwd_context.verify(plain_password, hashed_password)
def get_password_hash(password: str):
return pwd_context.hash(password)
def authenticate_user(email: str, password: str, db: Session):
user = crud.get_user_by_email(db, email=email)
if not user:
return False
if not verify_password(password, user.password):
return False
return user
def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
to_encode = data.copy()
if expires_delta:
expire = datetime.utcnow() + expires_delta
else:
expire = datetime.utcnow() + timedelta(minutes=15)
to_encode.update({"exp": expire})
encoded_jwt = jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
return encoded_jwt
async def get_current_user(token: str = Depends(oauth2_scheme), db: Session = Depends(get_db)):
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])
email: str = payload.get("sub")
if email is None:
raise credentials_exception
token_data = TokenData(email=email)
except JWTError:
raise credentials_exception
user = crud.get_user_by_email(db, email=token_data.email)
if user is None:
raise credentials_exception
return user
def get_admin(user: User = Depends(get_current_user)):
if user.is_admin:
return True
else:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail='Insufficient permissions')
def get_user(user_id: int = Path(..., ge=1), db: Session = Depends(get_db)) -> User:
db_user = crud.get_user(db, user_id=user_id)
if not db_user:
raise HTTPException(status_code=status.HTTP_404_NOT_FOUND, detail='User not found')
return db_user
def get_date(str_date: str) -> date:
year, month, day = (int(x) for x in str_date.split('-'))
return date(year, month, day)