File size: 1,128 Bytes
2c41c5f
07f63a8
2c41c5f
 
07f63a8
1404692
2c41c5f
 
1404692
07f63a8
2c41c5f
07f63a8
1404692
2c41c5f
1404692
07f63a8
1404692
 
2c41c5f
07f63a8
1404692
07f63a8
1404692
07f63a8
 
2c41c5f
07f63a8
 
 
 
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
from fastapi import HTTPException, Request
from starlette.status import HTTP_401_UNAUTHORIZED

from Backend.helper.passwords import verify_password
from Backend.helper.settings_manager import SettingsManager


#----- Match a username/password pair against the stored admin credentials
def verify_credentials(username: str, password: str) -> bool:
    s = SettingsManager.current()
    return username == s.admin_username and verify_password(password, s.admin_password)


#----- Whether the session carries a valid authentication flag
def is_authenticated(request: Request) -> bool:
    return bool(request.session.get("authenticated"))


#----- Logged-in username from the session, or None
def get_current_user(request: Request) -> str | None:
    if is_authenticated(request):
        return request.session.get("username", "admin")
    return None


#----- FastAPI dependency: raise 401 (redirected to /login) when unauthenticated
async def require_auth(request: Request) -> bool:
    if not is_authenticated(request):
        raise HTTPException(status_code=HTTP_401_UNAUTHORIZED, detail="Not authenticated")
    return True