Spaces:
Sleeping
Sleeping
Added token expiration endpoint
Browse files
app/__pycache__/auth.cpython-311.pyc
CHANGED
|
Binary files a/app/__pycache__/auth.cpython-311.pyc and b/app/__pycache__/auth.cpython-311.pyc differ
|
|
|
app/__pycache__/get_current_users.cpython-311.pyc
CHANGED
|
Binary files a/app/__pycache__/get_current_users.cpython-311.pyc and b/app/__pycache__/get_current_users.cpython-311.pyc differ
|
|
|
app/auth.py
CHANGED
|
@@ -1,12 +1,12 @@
|
|
| 1 |
from datetime import datetime, timedelta, timezone
|
| 2 |
-
from jose import JWTError, jwt
|
| 3 |
from passlib.context import CryptContext
|
| 4 |
from app.config import *
|
| 5 |
|
| 6 |
settings = Settings()
|
| 7 |
SECRET_KEY = settings.jwt_secret
|
| 8 |
ALGORITHM="HS256"
|
| 9 |
-
ACCESS_TOKEN_EXPIRE_MINUTES = 10080 # 7 days
|
| 10 |
|
| 11 |
pwd_context = CryptContext(schemes=['argon2'], deprecated="auto")
|
| 12 |
|
|
@@ -26,5 +26,9 @@ def decode_access_token(token: str):
|
|
| 26 |
try:
|
| 27 |
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
| 28 |
return payload
|
|
|
|
|
|
|
|
|
|
| 29 |
except JWTError:
|
|
|
|
| 30 |
return None
|
|
|
|
| 1 |
from datetime import datetime, timedelta, timezone
|
| 2 |
+
from jose import JWTError, jwt, ExpiredSignatureError
|
| 3 |
from passlib.context import CryptContext
|
| 4 |
from app.config import *
|
| 5 |
|
| 6 |
settings = Settings()
|
| 7 |
SECRET_KEY = settings.jwt_secret
|
| 8 |
ALGORITHM="HS256"
|
| 9 |
+
ACCESS_TOKEN_EXPIRE_MINUTES = 1 #10080 # 7 days
|
| 10 |
|
| 11 |
pwd_context = CryptContext(schemes=['argon2'], deprecated="auto")
|
| 12 |
|
|
|
|
| 26 |
try:
|
| 27 |
payload = jwt.decode(token, SECRET_KEY, algorithms=[ALGORITHM])
|
| 28 |
return payload
|
| 29 |
+
except ExpiredSignatureError:
|
| 30 |
+
# Raise explicitly so backend knows token expired
|
| 31 |
+
raise ExpiredSignatureError("Token Expired")
|
| 32 |
except JWTError:
|
| 33 |
+
# Any other JWT error (invalid signature, tampered, etc.)
|
| 34 |
return None
|
app/get_current_users.py
CHANGED
|
@@ -3,6 +3,8 @@ from fastapi.security import OAuth2PasswordBearer
|
|
| 3 |
from app.auth import decode_access_token
|
| 4 |
from app.config import settings
|
| 5 |
from motor.motor_asyncio import AsyncIOMotorClient
|
|
|
|
|
|
|
| 6 |
|
| 7 |
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/auth/login")
|
| 8 |
client = AsyncIOMotorClient(settings.mongo_uri)
|
|
@@ -10,12 +12,14 @@ db = client[settings.mongo_db_name]
|
|
| 10 |
users = db["Users"]
|
| 11 |
|
| 12 |
async def get_current_user(token: str = Depends(oauth2_scheme)):
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
|
|
|
|
|
|
|
|
|
| 17 |
user = await users.find_one({"_id": payload["user_id"]})
|
| 18 |
if not user:
|
| 19 |
raise HTTPException(status_code=404, detail="User Not Found")
|
| 20 |
-
|
| 21 |
return user
|
|
|
|
| 3 |
from app.auth import decode_access_token
|
| 4 |
from app.config import settings
|
| 5 |
from motor.motor_asyncio import AsyncIOMotorClient
|
| 6 |
+
from jose import ExpiredSignatureError
|
| 7 |
+
|
| 8 |
|
| 9 |
oauth2_scheme = OAuth2PasswordBearer(tokenUrl="/auth/login")
|
| 10 |
client = AsyncIOMotorClient(settings.mongo_uri)
|
|
|
|
| 12 |
users = db["Users"]
|
| 13 |
|
| 14 |
async def get_current_user(token: str = Depends(oauth2_scheme)):
|
| 15 |
+
try:
|
| 16 |
+
payload = decode_access_token(token)
|
| 17 |
+
if not payload:
|
| 18 |
+
raise HTTPException(status_code=401, detail="Invalid Token")
|
| 19 |
+
except ExpiredSignatureError:
|
| 20 |
+
raise HTTPException(status_code=401, detail="Token Expired")
|
| 21 |
+
|
| 22 |
user = await users.find_one({"_id": payload["user_id"]})
|
| 23 |
if not user:
|
| 24 |
raise HTTPException(status_code=404, detail="User Not Found")
|
|
|
|
| 25 |
return user
|