Added ref login
This commit is contained in:
+19
-15
@@ -6,7 +6,7 @@ from fastapi.security import OAuth2PasswordBearer
|
||||
import jwt
|
||||
from jwt.exceptions import PyJWTError
|
||||
|
||||
from .config import SECRET_KEY, ALGORITHM, ADMIN_USER
|
||||
from .config import SECRET_KEY, ALGORITHM
|
||||
|
||||
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="auth/token")
|
||||
oauth2_scheme_optional = OAuth2PasswordBearer(tokenUrl="auth/token", auto_error=False)
|
||||
@@ -20,43 +20,47 @@ def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
|
||||
expire = datetime.now(timezone.utc) + 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)):
|
||||
async def get_authenticated_user(token: str = Depends(oauth2_scheme)):
|
||||
"""Allows both Admins and Refs"""
|
||||
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 = payload.get("sub")
|
||||
|
||||
if username is None or username != ADMIN_USER:
|
||||
role = payload.get("role")
|
||||
if role not in ["admin", "ref"]:
|
||||
raise credentials_exception
|
||||
|
||||
return payload
|
||||
except PyJWTError:
|
||||
raise credentials_exception
|
||||
|
||||
return username
|
||||
|
||||
async def get_admin_user(token: str = Depends(oauth2_scheme)):
|
||||
"""Strictly allows ONLY Admins"""
|
||||
user = await get_authenticated_user(token)
|
||||
if user.get("role") != "admin":
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_403_FORBIDDEN, detail="Admin privileges required"
|
||||
)
|
||||
return user
|
||||
|
||||
|
||||
async def get_optional_user(
|
||||
token: Optional[str] = Depends(oauth2_scheme_optional),
|
||||
) -> Optional[str]:
|
||||
) -> Optional[dict]:
|
||||
"""Returns user payload if valid token exists, else None"""
|
||||
if not token:
|
||||
return None
|
||||
|
||||
try:
|
||||
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
||||
username = payload.get("sub")
|
||||
if username == ADMIN_USER:
|
||||
return username
|
||||
if payload.get("role") in ["admin", "ref"]:
|
||||
return payload
|
||||
except Exception:
|
||||
pass
|
||||
|
||||
return None
|
||||
|
||||
@@ -13,8 +13,13 @@ ACCESS_TOKEN_EXPIRE_MINUTES = 60 * 24 # 24 hours
|
||||
ADMIN_USER = os.getenv("ADMIN_USER", "admin")
|
||||
ADMIN_PASSWORD = os.getenv("ADMIN_PASSWORD", "admin")
|
||||
|
||||
# Ref Credentials
|
||||
REF_USER = os.getenv("REF_USER", "ref")
|
||||
REF_PASSWORD = os.getenv("REF_PASSWORD", "ref")
|
||||
|
||||
password_hash = PasswordHash.recommended()
|
||||
ADMIN_HASH = password_hash.hash(ADMIN_PASSWORD)
|
||||
REF_HASH = password_hash.hash(REF_PASSWORD)
|
||||
|
||||
|
||||
def verify_password(plain_password, hashed_password):
|
||||
|
||||
@@ -1,63 +0,0 @@
|
||||
# backend/app/core/utils.py
|
||||
from .brackets import Match
|
||||
|
||||
|
||||
class MermaidLive:
|
||||
|
||||
@staticmethod
|
||||
def _get_visual_target(match: Match, is_win=True) -> Match | None:
|
||||
current = match.next_win if is_win else match.next_loss
|
||||
while current and current.is_bye:
|
||||
current = current.next_win
|
||||
return current
|
||||
|
||||
@classmethod
|
||||
def export(cls, matches: list[Match]) -> None:
|
||||
print("\n--- COPY TO MERMAID.LIVE ---")
|
||||
print("graph LR")
|
||||
print(" classDef wb stroke:#01579b,stroke-width:2px;")
|
||||
print(" classDef lb stroke:#b71c1c,stroke-width:2px,stroke-dasharray: 5 5;")
|
||||
print(" classDef final stroke:#e65100,stroke-width:4px;")
|
||||
|
||||
wb_nodes = [
|
||||
m
|
||||
for m in matches
|
||||
if "WB" in m.name or "Semifinal" in m.name or "Winners Final" in m.name
|
||||
]
|
||||
|
||||
lb_nodes = [m for m in matches if "LB" in m.name or "Losers Final" in m.name]
|
||||
final_nodes = [
|
||||
m for m in matches if "Grand Final" in m.name or "3rd Place" in m.name
|
||||
]
|
||||
|
||||
print(" subgraph Winners Bracket")
|
||||
for m in wb_nodes:
|
||||
cls._print_node(m, "wb")
|
||||
print(" end")
|
||||
|
||||
if lb_nodes:
|
||||
print(" subgraph Losers Bracket")
|
||||
for m in lb_nodes:
|
||||
cls._print_node(m, "lb")
|
||||
print(" end")
|
||||
|
||||
print(" subgraph Championship / 3rd Place")
|
||||
for m in final_nodes:
|
||||
cls._print_node(m, "final")
|
||||
print(" end")
|
||||
|
||||
for m in matches:
|
||||
target_win = cls._get_visual_target(m, is_win=True)
|
||||
if target_win:
|
||||
print(f" M{m.id} --> M{target_win.id}")
|
||||
|
||||
target_loss = cls._get_visual_target(m, is_win=False)
|
||||
if target_loss:
|
||||
print(f" M{m.id} -.-> M{target_loss.id}")
|
||||
|
||||
@staticmethod
|
||||
def _print_node(m: Match, style: str) -> None:
|
||||
label = m.name
|
||||
if m.teams[0] and m.teams[1]:
|
||||
label += f" ({m.teams[0]} vs {m.teams[1]})"
|
||||
print(f' M{m.id}["{label}"]:::{style}')
|
||||
Reference in New Issue
Block a user