PhotosAPI/modules/security.py

145 lines
4.0 KiB
Python
Raw Normal View History

2023-01-25 17:02:28 +02:00
from datetime import datetime, timedelta, timezone
2022-12-20 12:36:54 +02:00
from typing import List, Union
from fastapi import Depends, HTTPException, Security, status
2023-06-22 14:17:53 +03:00
from fastapi.security import OAuth2PasswordBearer, SecurityScopes
2022-12-20 12:36:54 +02:00
from jose import JWTError, jwt
from passlib.context import CryptContext
from pydantic import BaseModel, ValidationError
2023-06-22 14:17:53 +03:00
from modules.database import col_users
2022-12-20 12:36:54 +02:00
with open("secret_key", "r", encoding="utf-8") as f:
SECRET_KEY = f.read()
ALGORITHM = "HS256"
ACCESS_TOKEN_EXPIRE_DAYS = 180
class Token(BaseModel):
access_token: str
token_type: str
class TokenData(BaseModel):
user: Union[str, None] = None
scopes: List[str] = []
class User(BaseModel):
user: str
email: Union[str, None] = None
disabled: Union[bool, None] = None
class UserInDB(User):
hash: str
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
oauth2_scheme = OAuth2PasswordBearer(
tokenUrl="token",
scopes={
"me": "Get current user's data.",
2022-12-20 14:28:50 +02:00
"albums.list": "List albums.",
"albums.read": "Read albums data.",
"albums.write": "Modify albums.",
"photos.list": "List photos.",
"photos.read": "View photos.",
2022-12-21 00:59:35 +02:00
"photos.write": "Modify photos.",
"videos.list": "List videos.",
"videos.read": "View videos.",
2023-03-12 15:59:13 +02:00
"videos.write": "Modify videos.",
2022-12-20 14:28:50 +02:00
},
2022-12-20 12:36:54 +02:00
)
2023-08-14 14:44:07 +03:00
def verify_password(plain_password, hashed_password) -> bool:
2022-12-20 12:36:54 +02:00
return pwd_context.verify(plain_password, hashed_password)
2023-08-14 14:44:07 +03:00
def get_password_hash(password) -> str:
2022-12-20 12:36:54 +02:00
return pwd_context.hash(password)
2023-08-14 14:44:07 +03:00
async def get_user(user: str) -> UserInDB:
found_user = await col_users.find_one({"user": user})
if found_user is None:
raise RuntimeError(f"User {user} does not exist")
2023-03-12 15:59:13 +02:00
return UserInDB(
user=found_user["user"],
email=found_user["email"],
disabled=found_user["disabled"],
hash=found_user["hash"],
)
2022-12-20 12:36:54 +02:00
2023-08-14 14:44:07 +03:00
async def authenticate_user(user_name: str, password: str) -> Union[UserInDB, bool]:
if user := await get_user(user_name):
2023-06-23 13:17:01 +03:00
return user if verify_password(password, user.hash) else False
else:
2022-12-20 12:36:54 +02:00
return False
2023-08-14 14:44:07 +03:00
def create_access_token(
data: dict, expires_delta: Union[timedelta, None] = None
) -> str:
2022-12-20 12:36:54 +02:00
to_encode = data.copy()
if expires_delta:
2023-01-25 17:02:28 +02:00
expire = datetime.now(tz=timezone.utc) + expires_delta
2022-12-20 12:36:54 +02:00
else:
2023-03-12 15:59:13 +02:00
expire = datetime.now(tz=timezone.utc) + timedelta(
days=ACCESS_TOKEN_EXPIRE_DAYS
)
2023-06-23 13:17:01 +03:00
to_encode["exp"] = expire
return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
2022-12-20 12:36:54 +02:00
2023-03-12 15:59:13 +02:00
async def get_current_user(
security_scopes: SecurityScopes, token: str = Depends(oauth2_scheme)
2023-08-14 14:44:07 +03:00
) -> UserInDB:
2022-12-20 12:36:54 +02:00
if security_scopes.scopes:
authenticate_value = f'Bearer scope="{security_scopes.scope_str}"'
else:
authenticate_value = "Bearer"
2022-12-20 14:28:50 +02:00
2022-12-20 12:36:54 +02:00
credentials_exception = HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Could not validate credentials",
headers={"WWW-Authenticate": authenticate_value},
)
2022-12-20 14:28:50 +02:00
2022-12-20 12:36:54 +02:00
try:
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
user: str = payload.get("sub")
if user is None:
raise credentials_exception
token_scopes = payload.get("scopes", [])
token_data = TokenData(scopes=token_scopes, user=user)
2023-08-14 14:44:07 +03:00
except (JWTError, ValidationError) as exc:
raise credentials_exception from exc
2023-03-12 15:59:13 +02:00
2023-08-14 14:44:07 +03:00
user_record = await get_user(user=token_data.user)
2022-12-20 14:28:50 +02:00
2023-08-14 14:44:07 +03:00
if user_record is None:
2022-12-20 12:36:54 +02:00
raise credentials_exception
2022-12-20 14:28:50 +02:00
2022-12-20 12:36:54 +02:00
for scope in security_scopes.scopes:
if scope not in token_data.scopes:
raise HTTPException(
status_code=status.HTTP_401_UNAUTHORIZED,
detail="Not enough permissions",
headers={"WWW-Authenticate": authenticate_value},
)
2023-08-14 14:44:07 +03:00
return user_record
2022-12-20 12:36:54 +02:00
2023-03-12 15:59:13 +02:00
async def get_current_active_user(
current_user: User = Security(get_current_user, scopes=["me"])
):
2022-12-20 12:36:54 +02:00
if current_user.disabled:
raise HTTPException(status_code=400, detail="Inactive user")
2023-03-12 15:59:13 +02:00
return current_user