|
8 | 8 | from fastapi.responses import RedirectResponse
|
9 | 9 | from fastapi.security import HTTPBearer
|
10 | 10 | from fastapi.security import OAuth2
|
11 |
| -from fastapi.security.base import SecurityBase |
12 | 11 | from fastapi.security.utils import get_authorization_scheme_param
|
13 | 12 | from jose import jwt
|
14 | 13 | from jwt import PyJWTError
|
15 |
| -from pydantic import BaseModel |
16 | 14 | from starlette.requests import Request
|
17 | 15 | from starlette.status import HTTP_403_FORBIDDEN
|
18 | 16 |
|
@@ -46,26 +44,6 @@ def create_access_token(data: dict, expires_delta: Optional[timedelta] = None):
|
46 | 44 | return encoded_jwt
|
47 | 45 |
|
48 | 46 |
|
49 |
| -class Token(BaseModel): |
50 |
| - access_token: str |
51 |
| - token_type: str |
52 |
| - |
53 |
| - |
54 |
| -class TokenData(BaseModel): |
55 |
| - username: str = None |
56 |
| - |
57 |
| - |
58 |
| -class User(BaseModel): |
59 |
| - username: str |
60 |
| - email: str = None |
61 |
| - full_name: str = None |
62 |
| - disabled: bool = None |
63 |
| - |
64 |
| - |
65 |
| -class UserInDB(User): |
66 |
| - hashed_password: str |
67 |
| - |
68 |
| - |
69 | 47 | class OAuth2PasswordBearerCookie(OAuth2):
|
70 | 48 | def __init__(
|
71 | 49 | self,
|
@@ -113,37 +91,26 @@ async def __call__(self, request: Request) -> Optional[str]:
|
113 | 91 | return param
|
114 | 92 |
|
115 | 93 |
|
116 |
| -class BasicAuth(SecurityBase): |
117 |
| - def __init__(self, scheme_name: str = None, auto_error: bool = True): |
118 |
| - self.scheme_name = scheme_name or self.__class__.__name__ |
119 |
| - self.auto_error = auto_error |
120 |
| - |
121 |
| - async def __call__(self, request: Request) -> Optional[str]: |
122 |
| - authorization: str = request.headers.get("Authorization") |
123 |
| - scheme, param = get_authorization_scheme_param(authorization) |
124 |
| - if not authorization or scheme.lower() != "basic": |
125 |
| - if self.auto_error: |
126 |
| - raise HTTPException( |
127 |
| - status_code=HTTP_403_FORBIDDEN, detail="Not authenticated" |
128 |
| - ) |
129 |
| - else: |
130 |
| - return None |
131 |
| - return param |
132 |
| - |
133 |
| - |
134 |
| -basic_auth = BasicAuth(auto_error=False) |
135 |
| - |
136 | 94 | oauth2_scheme = OAuth2PasswordBearerCookie(tokenUrl="/token")
|
137 | 95 |
|
138 | 96 |
|
139 | 97 | async def get_current_user(token: str = Depends(oauth2_scheme)):
|
140 |
| - credentials_exception = HTTPException( |
141 |
| - status_code=HTTP_403_FORBIDDEN, detail="Could not validate credentials" |
142 |
| - ) |
143 | 98 | try:
|
144 | 99 | return jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
145 | 100 | except PyJWTError:
|
146 |
| - raise credentials_exception |
| 101 | + raise HTTPException( |
| 102 | + status_code=HTTP_403_FORBIDDEN, detail="Could not validate credentials" |
| 103 | + ) |
| 104 | + |
| 105 | + |
| 106 | +@router.get("/user") |
| 107 | +def user(current_user=Depends(get_current_user)): |
| 108 | + return current_user |
| 109 | + |
| 110 | + |
| 111 | +@router.post("/token") |
| 112 | +def token(request: Request): |
| 113 | + return request.cookies.get("Authorization") |
147 | 114 |
|
148 | 115 |
|
149 | 116 | @router.get("/auth/login")
|
@@ -176,11 +143,3 @@ async def auth_logout():
|
176 | 143 | response = RedirectResponse(redirect_url_main_page)
|
177 | 144 | response.delete_cookie("Authorization")
|
178 | 145 | return response
|
179 |
| - |
180 |
| - |
181 |
| -@router.get("/auth/status") |
182 |
| -async def auth_status(user=Depends(get_current_user)): |
183 |
| - return { |
184 |
| - "ok": True, |
185 |
| - "user": user, |
186 |
| - } |
0 commit comments