mirror of
https://github.com/TheSmallHanCat/sora2api.git
synced 2026-04-27 10:07:31 +08:00
sora2api
This commit is contained in:
38
src/core/auth.py
Normal file
38
src/core/auth.py
Normal file
@@ -0,0 +1,38 @@
|
||||
"""Authentication module"""
|
||||
import bcrypt
|
||||
from typing import Optional
|
||||
from fastapi import HTTPException, Security
|
||||
from fastapi.security import HTTPBearer, HTTPAuthorizationCredentials
|
||||
from .config import config
|
||||
|
||||
security = HTTPBearer()
|
||||
|
||||
class AuthManager:
|
||||
"""Authentication manager"""
|
||||
|
||||
@staticmethod
|
||||
def verify_api_key(api_key: str) -> bool:
|
||||
"""Verify API key"""
|
||||
return api_key == config.api_key
|
||||
|
||||
@staticmethod
|
||||
def verify_admin(username: str, password: str) -> bool:
|
||||
"""Verify admin credentials"""
|
||||
return username == config.admin_username and password == config.admin_password
|
||||
|
||||
@staticmethod
|
||||
def hash_password(password: str) -> str:
|
||||
"""Hash password"""
|
||||
return bcrypt.hashpw(password.encode(), bcrypt.gensalt()).decode()
|
||||
|
||||
@staticmethod
|
||||
def verify_password(password: str, hashed: str) -> bool:
|
||||
"""Verify password"""
|
||||
return bcrypt.checkpw(password.encode(), hashed.encode())
|
||||
|
||||
async def verify_api_key_header(credentials: HTTPAuthorizationCredentials = Security(security)) -> str:
|
||||
"""Verify API key from Authorization header"""
|
||||
api_key = credentials.credentials
|
||||
if not AuthManager.verify_api_key(api_key):
|
||||
raise HTTPException(status_code=401, detail="Invalid API key")
|
||||
return api_key
|
||||
Reference in New Issue
Block a user