mirror of
https://github.com/rommapp/romm.git
synced 2026-11-29 10:01:00 +01:00
136 lines
3.9 KiB
Python
136 lines
3.9 KiB
Python
import base64
|
|
import binascii
|
|
import secrets
|
|
from typing import Annotated
|
|
from datetime import datetime, timedelta
|
|
from fastapi import Depends, HTTPException, status
|
|
from fastapi.security import OAuth2PasswordBearer
|
|
from jose import JWTError, jwt
|
|
from passlib.context import CryptContext
|
|
from starlette.authentication import (
|
|
AuthCredentials,
|
|
AuthenticationBackend,
|
|
AuthenticationError,
|
|
)
|
|
from starlette.requests import HTTPConnection
|
|
|
|
from handler import dbh
|
|
from models.user import User
|
|
from config import SECRET_KEY
|
|
from utils.cache import cache
|
|
|
|
ALGORITHM = "HS256"
|
|
ACCESS_TOKEN_EXPIRE_MINUTES = 30
|
|
|
|
|
|
def verify_password(plain_password, hashed_password):
|
|
return pwd_context.verify(plain_password, hashed_password)
|
|
|
|
|
|
def get_password_hash(password):
|
|
return pwd_context.hash(password)
|
|
|
|
|
|
def authenticate_user(username: str, password: str):
|
|
user = dbh.get_user(username)
|
|
if not user:
|
|
return False
|
|
|
|
if not verify_password(password, user.hashed_password):
|
|
return False
|
|
|
|
return user
|
|
|
|
|
|
# JWT based authentication
|
|
|
|
pwd_context = CryptContext(schemes=["bcrypt"], deprecated="auto")
|
|
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="token")
|
|
|
|
|
|
def create_access_token(data: dict, expires_delta: timedelta | None = 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})
|
|
|
|
return jwt.encode(to_encode, SECRET_KEY, algorithm=ALGORITHM)
|
|
|
|
|
|
async def get_current_user(token: Annotated[str, Depends(oauth2_scheme)]):
|
|
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])
|
|
username: str = payload.get("sub")
|
|
if username is None:
|
|
raise credentials_exception
|
|
except JWTError:
|
|
raise credentials_exception
|
|
|
|
user = dbh.get_user(username)
|
|
if user is None:
|
|
raise credentials_exception
|
|
|
|
return user
|
|
|
|
|
|
async def get_current_active_user(
|
|
current_user: Annotated[User, Depends(get_current_user)]
|
|
):
|
|
if current_user.disabled:
|
|
raise HTTPException(status_code=400, detail="Inactive user")
|
|
|
|
return current_user
|
|
|
|
|
|
# Session based authentication
|
|
|
|
|
|
class BasicAuthBackend(AuthenticationBackend):
|
|
async def authenticate(self, conn: HTTPConnection):
|
|
# Check if session key already stored in redis
|
|
session_id = conn.session.get('session_id')
|
|
if session_id:
|
|
username = cache.get(f'romm:{session_id}')
|
|
if username:
|
|
user = dbh.get_user(username)
|
|
if user:
|
|
# Key exists therefore user is authenticated
|
|
return AuthCredentials(["authenticated", "admin"]), user
|
|
|
|
if "Authorization" not in conn.headers:
|
|
return
|
|
|
|
auth = conn.headers["Authorization"]
|
|
try:
|
|
scheme, credentials = auth.split()
|
|
if scheme.lower() != "basic":
|
|
return
|
|
decoded = base64.b64decode(credentials).decode("ascii")
|
|
except (ValueError, UnicodeDecodeError, binascii.Error):
|
|
raise AuthenticationError("Invalid basic auth credentials")
|
|
|
|
username, _, password = decoded.partition(":")
|
|
user = authenticate_user(username, password)
|
|
if not user:
|
|
raise HTTPException(
|
|
status_code=status.HTTP_401_UNAUTHORIZED,
|
|
detail="Incorrect username or password",
|
|
headers={"WWW-Authenticate": "Authorization"},
|
|
)
|
|
|
|
# Generate unique session key and store in redis
|
|
conn.session['session_id'] = secrets.token_hex(16)
|
|
cache.set(f'romm:{conn.session["session_id"]}', user.username)
|
|
|
|
return AuthCredentials(["authenticated", "admin"]), user
|