forked from mtusi2026/mtusi2026lab4
Add third lab code
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
from typing import Optional
|
||||
|
||||
from fastapi import Depends, Header, Query
|
||||
|
||||
from .auth import sessions, unauthorized
|
||||
from .errors import APIError
|
||||
from .store import store
|
||||
|
||||
|
||||
def page_limit(limit: str = Query(default="50")) -> int:
|
||||
if not limit.isascii() or not limit.isdecimal() or len(limit) > 3:
|
||||
raise APIError(400, "invalid_request", "Неверный limit")
|
||||
number = int(limit)
|
||||
if not 1 <= number <= 100:
|
||||
raise APIError(400, "invalid_request", "Неверный limit")
|
||||
return number
|
||||
|
||||
|
||||
|
||||
def bearer_token(authorization: Optional[str] = Header(default=None)) -> str:
|
||||
parts = (authorization or '').split()
|
||||
if len(parts) != 2 or parts[0].lower() != 'bearer':
|
||||
raise unauthorized()
|
||||
return parts[1]
|
||||
|
||||
|
||||
def current_user(token: str = Depends(bearer_token)) -> dict:
|
||||
user = store.get_user(sessions.user_id(token))
|
||||
if user is None:
|
||||
raise unauthorized()
|
||||
return user
|
||||
Reference in New Issue
Block a user