Spaces:
Sleeping
Sleeping
Upload app/core/security.py
Browse files- app/core/security.py +17 -0
app/core/security.py
CHANGED
|
@@ -64,3 +64,20 @@ async def get_current_user(
|
|
| 64 |
detail="Invalid token payload",
|
| 65 |
)
|
| 66 |
return int(user_id)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
detail="Invalid token payload",
|
| 65 |
)
|
| 66 |
return int(user_id)
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
async def get_optional_current_user(
|
| 70 |
+
credentials: Optional[HTTPAuthorizationCredentials] = Depends(HTTPBearer(auto_error=False)),
|
| 71 |
+
) -> Optional[int]:
|
| 72 |
+
if credentials is None:
|
| 73 |
+
return None
|
| 74 |
+
try:
|
| 75 |
+
payload = decode_token(credentials.credentials)
|
| 76 |
+
if payload.get("type") != "access":
|
| 77 |
+
return None
|
| 78 |
+
user_id = payload.get("sub")
|
| 79 |
+
if user_id is None:
|
| 80 |
+
return None
|
| 81 |
+
return int(user_id)
|
| 82 |
+
except Exception:
|
| 83 |
+
return None
|