Spaces:
Running
Running
Update api/routers/auth.py
Browse files- api/routers/auth.py +25 -11
api/routers/auth.py
CHANGED
|
@@ -100,6 +100,9 @@ async def login(
|
|
| 100 |
"""
|
| 101 |
Login endpoint - validates credentials and creates session
|
| 102 |
"""
|
|
|
|
|
|
|
|
|
|
| 103 |
# Verify credentials
|
| 104 |
if not verify_credentials(username, password):
|
| 105 |
logger.warning(f"Failed login attempt for username: {username}")
|
|
@@ -107,18 +110,22 @@ async def login(
|
|
| 107 |
|
| 108 |
# Create session
|
| 109 |
token = create_session(username)
|
|
|
|
| 110 |
|
| 111 |
# Set secure cookie
|
| 112 |
-
#
|
| 113 |
-
|
| 114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 115 |
|
| 116 |
-
|
| 117 |
-
parsed_origin = urlparse(origin) if origin else None
|
| 118 |
-
is_cross_site = bool(parsed_origin and parsed_origin.hostname and parsed_origin.hostname != request.url.hostname)
|
| 119 |
-
is_https = request.url.scheme == "https"
|
| 120 |
-
samesite = "none" if (is_https and (is_production or is_cross_site)) else "lax"
|
| 121 |
-
secure = True if samesite == "none" else is_production
|
| 122 |
|
| 123 |
response.set_cookie(
|
| 124 |
key="session_token",
|
|
@@ -126,7 +133,8 @@ async def login(
|
|
| 126 |
httponly=True,
|
| 127 |
max_age=SESSION_MAX_AGE,
|
| 128 |
samesite=samesite,
|
| 129 |
-
secure=secure
|
|
|
|
| 130 |
)
|
| 131 |
|
| 132 |
logger.info(f"Successful login for user: {username}")
|
|
@@ -175,12 +183,18 @@ async def verify(session_token: Optional[str] = Cookie(None)):
|
|
| 175 |
|
| 176 |
|
| 177 |
@router.get("/status")
|
| 178 |
-
async def status(session_token: Optional[str] = Cookie(None)):
|
| 179 |
"""
|
| 180 |
Check authentication status without raising exception
|
| 181 |
"""
|
|
|
|
| 182 |
session_data = verify_session(session_token)
|
| 183 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
return {
|
| 185 |
"authenticated": session_data is not None,
|
| 186 |
"username": session_data.get("username") if session_data else None
|
|
|
|
| 100 |
"""
|
| 101 |
Login endpoint - validates credentials and creates session
|
| 102 |
"""
|
| 103 |
+
# Log login attempt
|
| 104 |
+
logger.info(f"Login attempt for username: {username}, Origin: {request.headers.get('origin')}")
|
| 105 |
+
|
| 106 |
# Verify credentials
|
| 107 |
if not verify_credentials(username, password):
|
| 108 |
logger.warning(f"Failed login attempt for username: {username}")
|
|
|
|
| 110 |
|
| 111 |
# Create session
|
| 112 |
token = create_session(username)
|
| 113 |
+
logger.info(f"Session created for user: {username}")
|
| 114 |
|
| 115 |
# Set secure cookie
|
| 116 |
+
# Detect if we're running on HTTPS (Hugging Face Spaces use HTTPS)
|
| 117 |
+
is_https = request.url.scheme == "https" or request.headers.get("x-forwarded-proto") == "https"
|
| 118 |
+
|
| 119 |
+
# For HTTPS (production/HF Spaces), use SameSite=None with Secure=True for cross-origin
|
| 120 |
+
# For HTTP (local dev), use SameSite=Lax with Secure=False
|
| 121 |
+
if is_https:
|
| 122 |
+
samesite = "none"
|
| 123 |
+
secure = True
|
| 124 |
+
else:
|
| 125 |
+
samesite = "lax"
|
| 126 |
+
secure = False
|
| 127 |
|
| 128 |
+
logger.info(f"Setting cookie with samesite={samesite}, secure={secure}, is_https={is_https}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 129 |
|
| 130 |
response.set_cookie(
|
| 131 |
key="session_token",
|
|
|
|
| 133 |
httponly=True,
|
| 134 |
max_age=SESSION_MAX_AGE,
|
| 135 |
samesite=samesite,
|
| 136 |
+
secure=secure,
|
| 137 |
+
path="/"
|
| 138 |
)
|
| 139 |
|
| 140 |
logger.info(f"Successful login for user: {username}")
|
|
|
|
| 183 |
|
| 184 |
|
| 185 |
@router.get("/status")
|
| 186 |
+
async def status(request: Request, session_token: Optional[str] = Cookie(None)):
|
| 187 |
"""
|
| 188 |
Check authentication status without raising exception
|
| 189 |
"""
|
| 190 |
+
logger.info(f"Status check - Cookie present: {session_token is not None}, Origin: {request.headers.get('origin')}")
|
| 191 |
session_data = verify_session(session_token)
|
| 192 |
|
| 193 |
+
if session_data:
|
| 194 |
+
logger.info(f"Status check - Authenticated as: {session_data.get('username')}")
|
| 195 |
+
else:
|
| 196 |
+
logger.info("Status check - Not authenticated")
|
| 197 |
+
|
| 198 |
return {
|
| 199 |
"authenticated": session_data is not None,
|
| 200 |
"username": session_data.get("username") if session_data else None
|