|
2 | 2 |
|
3 | 3 | import jwt |
4 | 4 | from fastapi import Depends, HTTPException, status |
5 | | -from fastapi.security import OAuth2PasswordBearer |
| 5 | +# from fastapi.security import OAuth2PasswordBearer |
6 | 6 | from jwt.exceptions import InvalidTokenError |
7 | | -from pydantic import ValidationError |
| 7 | +from pydantic import BaseModel, ValidationError |
8 | 8 | from sqlmodel import Session |
9 | | - |
| 9 | +from common.core.schemas import TokenPayload, XOAuth2PasswordBearer |
10 | 10 | from common.core import security |
11 | 11 | from common.core.config import settings |
12 | 12 | from common.core.db import get_session |
13 | 13 | from apps.system.models.user import sys_user |
14 | | -import threading |
15 | | -""" reusable_oauth2 = OAuth2PasswordBearer( |
| 14 | +reusable_oauth2 = XOAuth2PasswordBearer( |
16 | 15 | tokenUrl=f"{settings.API_V1_STR}/login/access-token" |
17 | 16 | ) |
18 | | - """ |
19 | 17 |
|
20 | | -thread_local = threading.local() |
| 18 | + |
| 19 | + |
21 | 20 |
|
22 | 21 | SessionDep = Annotated[Session, Depends(get_session)] |
23 | | -# TokenDep = Annotated[str, Depends(reusable_oauth2)] |
| 22 | +TokenDep = Annotated[str, Depends(reusable_oauth2)] |
24 | 23 |
|
25 | | -def get_current_user(session: SessionDep, token: str) -> sys_user: |
| 24 | +def get_current_user(session: SessionDep, token: TokenDep) -> sys_user: |
26 | 25 | try: |
27 | 26 | payload = jwt.decode( |
28 | 27 | token, settings.SECRET_KEY, algorithms=[security.ALGORITHM] |
29 | 28 | ) |
30 | | - # token_data = TokenPayload(**payload) |
| 29 | + token_data = TokenPayload(**payload) |
31 | 30 | except (InvalidTokenError, ValidationError): |
32 | 31 | raise HTTPException( |
33 | 32 | status_code=status.HTTP_403_FORBIDDEN, |
34 | 33 | detail="Could not validate credentials", |
35 | 34 | ) |
36 | | - user = session.get(sys_user, payload.sub) |
| 35 | + user = session.get(sys_user, token_data.id) |
37 | 36 | if not user: |
38 | 37 | raise HTTPException(status_code=404, detail="User not found") |
39 | 38 | """ if not user.is_active: |
40 | 39 | raise HTTPException(status_code=400, detail="Inactive user") """ |
41 | | - thread_local.current_user = user |
42 | 40 | return user |
43 | | -def get_current_user_from_thread(): |
44 | | - return getattr(thread_local, "current_user", None) |
45 | | -CurrentUser = Annotated[sys_user, Depends(get_current_user_from_thread)] |
46 | | -""" def get_current_user(session: SessionDep, token: TokenDep) -> User: |
47 | | - try: |
48 | | - payload = jwt.decode( |
49 | | - token, settings.SECRET_KEY, algorithms=[security.ALGORITHM] |
50 | | - ) |
51 | | - token_data = TokenPayload(**payload) |
52 | | - except (InvalidTokenError, ValidationError): |
53 | | - raise HTTPException( |
54 | | - status_code=status.HTTP_403_FORBIDDEN, |
55 | | - detail="Could not validate credentials", |
56 | | - ) |
57 | | - user = session.get(User, token_data.sub) |
58 | | - if not user: |
59 | | - raise HTTPException(status_code=404, detail="User not found") |
60 | | - if not user.is_active: |
61 | | - raise HTTPException(status_code=400, detail="Inactive user") |
62 | | - return user |
63 | | -
|
| 41 | +CurrentUser = Annotated[sys_user, Depends(get_current_user)] |
64 | 42 |
|
65 | | -CurrentUser = Annotated[User, Depends(get_current_user)] |
66 | 43 |
|
67 | 44 |
|
68 | | -def get_current_active_superuser(current_user: CurrentUser) -> User: |
69 | | - if not current_user.is_superuser: |
70 | | - raise HTTPException( |
71 | | - status_code=403, detail="The user doesn't have enough privileges" |
72 | | - ) |
73 | | - return current_user """ |
0 commit comments