-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeps.py
50 lines (42 loc) · 1.6 KB
/
deps.py
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
from typing import Union, Any
from datetime import datetime
from fastapi import Depends, HTTPException, status
from fastapi.security import OAuth2PasswordBearer
from schemas import TokenPayload
from utils import (ALGORITHM, JWT_SECRET_KEY)
from database import Database
from jose import jwt
from pydantic import ValidationError
reuseable_oauth = OAuth2PasswordBearer(
tokenUrl="/login",
scheme_name="JWT"
)
async def get_current_user(token: str = Depends(reuseable_oauth)):
try:
payload = jwt.decode(
token, JWT_SECRET_KEY, algorithms=[ALGORITHM]
)
token_data = TokenPayload(**payload)
if datetime.fromtimestamp(token_data.exp) < datetime.now():
raise HTTPException(
status_code = status.HTTP_401_UNAUTHORIZED,
detail="Token expired",
headers={"WWW-Authenticate": "Bearer"},
)
except(jwt.JWTError, ValidationError):
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Could not validate credentials",
headers={"WWW-Authenticate": "Bearer"},
)
with Database() as db:
athlete = db.query_fetchone("SELECT id,username FROM public.athlete WHERE username = %s", (token_data.sub,))
db.close
if athlete is None:
raise HTTPException(
status_code=status.HTTP_404_NOT_FOUND,
detail="Could not find user",
)
# athlete to dictionary
athlete = { "id": athlete[0], "username": athlete[1] }
return athlete