Repository navigation
Expand file tree
/
Copy pathauth_app.py
More file actions
76 lines (59 loc) · 2.59 KB
/
Copy pathauth_app.py
File metadata and controls
76 lines (59 loc) · 2.59 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
from datetime import datetime, timedelta, timezone
from typing import Annotated
import jwt
from fastapi import Depends, FastAPI, HTTPException
from fastapi.security import OAuth2PasswordBearer, OAuth2PasswordRequestForm
from jwt.exceptions import InvalidTokenError
from pwdlib import PasswordHash
from pydantic import BaseModel
SECRET_KEY = os.environ["JWT_SECRET_KEY"]
if len(SECRET_KEY) < 32:
raise RuntimeError("Для учебного JWT нужен случайный ключ длиной не меньше 32 символов")
ALGORITHM = "HS256"
ACCESS_TOKEN_MINUTES = 15
app = FastAPI()
password_hash = PasswordHash.recommended()
demo_hash = password_hash.hash("training-password-123")
dummy_hash = password_hash.hash("unused-demo-password")
users = {"alice": {"username": "alice", "password_hash": demo_hash, "disabled": False}}
oauth2 = OAuth2PasswordBearer(tokenUrl="token")
class Token(BaseModel):
access_token: str
token_type: str = "bearer"
class PublicUser(BaseModel):
username: str
def unauthorized() -> HTTPException:
return HTTPException(401, "Неверные учётные данные", headers={"WWW-Authenticate": "Bearer"})
def issue_token(username: str, *, lifetime: timedelta | None = None) -> str:
duration = lifetime if lifetime is not None else timedelta(minutes=ACCESS_TOKEN_MINUTES)
claims = {"sub": username, "exp": datetime.now(timezone.utc) + duration}
return jwt.encode(claims, SECRET_KEY, algorithm=ALGORITHM)
@app.post("/token", response_model=Token)
def login(form: Annotated[OAuth2PasswordRequestForm, Depends()]):
user = users.get(form.username)
stored_hash = user["password_hash"] if user else dummy_hash
valid_password = password_hash.verify(form.password, stored_hash)
if not user or not valid_password or user["disabled"]:
raise unauthorized()
return Token(access_token=issue_token(user["username"]))
def current_user(token: Annotated[str, Depends(oauth2)]) -> PublicUser:
try:
claims = jwt.decode(
token,
SECRET_KEY,
algorithms=[ALGORITHM],
options={"require": ["exp", "sub"]},
)
username = claims["sub"]
if not isinstance(username, str) or not username:
raise unauthorized()
except InvalidTokenError as exc:
raise unauthorized() from exc
user = users.get(username)
if user is None or user["disabled"]:
raise unauthorized()
return PublicUser(username=user["username"])
@app.get("/users/me", response_model=PublicUser)
def me(user: Annotated[PublicUser, Depends(current_user)]):
return user