feat: implement session_jwt and enhance auth

This commit is contained in:
Siddhant Rai
2025-03-17 11:51:30 +05:30
parent 7fd377bdbe
commit 4406426515
9 changed files with 148 additions and 59 deletions

View File

@@ -1,15 +1,13 @@
import uuid
from jose import jwt
from application.core.settings import settings
def handle_auth(request, data={}):
if settings.AUTH_TYPE == "simple_jwt":
if settings.AUTH_TYPE in ["simple_jwt", "session_jwt"]:
jwt_token = request.headers.get("Authorization")
if not jwt_token:
return {"message": "Missing Authorization header"}
return None
jwt_token = jwt_token.replace("Bearer ", "")
@@ -22,18 +20,9 @@ def handle_auth(request, data={}):
)
return decoded_token
except Exception as e:
return {"message": f"Authentication error: {str(e)}"}
return {
"message": f"Authentication error: {str(e)}",
"error": "invalid_token",
}
else:
return {"sub": "local"}
def get_or_create_user_id():
try:
with open(settings.USER_ID_FILE, "r") as f:
user_id = f.read().strip()
return user_id
except FileNotFoundError:
user_id = str(uuid.uuid4())
with open(settings.USER_ID_FILE, "w") as f:
f.write(user_id)
return user_id