Spaces:
Sleeping
Sleeping
Fix user detection by decoding JWT token from referer
Browse files- src/mosaic/analysis.py +31 -24
src/mosaic/analysis.py
CHANGED
|
@@ -469,31 +469,38 @@ def analyze_slide(
|
|
| 469 |
username = "anonymous"
|
| 470 |
if request is not None:
|
| 471 |
try:
|
| 472 |
-
#
|
| 473 |
-
|
| 474 |
-
logger.info(f"Request attributes: {dir(request)}")
|
| 475 |
-
if hasattr(request, '__dict__'):
|
| 476 |
-
logger.info(f"Request dict: {request.__dict__}")
|
| 477 |
-
|
| 478 |
-
# Check if user is logged in
|
| 479 |
-
# In HF Spaces, request.username is None for anonymous users
|
| 480 |
-
if hasattr(request, 'username') and request.username:
|
| 481 |
-
username = request.username
|
| 482 |
-
is_logged_in = True
|
| 483 |
-
logger.info(f"Found username: {username}")
|
| 484 |
-
else:
|
| 485 |
-
logger.info(f"No username found. request.username = {getattr(request, 'username', 'ATTR_NOT_FOUND')}")
|
| 486 |
-
|
| 487 |
-
# Also check request headers for auth
|
| 488 |
if hasattr(request, 'headers'):
|
| 489 |
-
|
| 490 |
-
|
| 491 |
-
|
| 492 |
-
|
| 493 |
-
|
| 494 |
-
|
| 495 |
-
|
| 496 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 497 |
logger.info(f"User: {username} | Logged in: {is_logged_in}")
|
| 498 |
except Exception as e:
|
| 499 |
logger.warning(f"Failed to detect user: {e}")
|
|
|
|
| 469 |
username = "anonymous"
|
| 470 |
if request is not None:
|
| 471 |
try:
|
| 472 |
+
# Check if user is logged in via JWT token in referer
|
| 473 |
+
# HF Spaces doesn't populate request.username but includes JWT in URL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 474 |
if hasattr(request, 'headers'):
|
| 475 |
+
referer = request.headers.get('referer', '')
|
| 476 |
+
if '__sign=' in referer:
|
| 477 |
+
# Extract and decode JWT token
|
| 478 |
+
import re
|
| 479 |
+
import json
|
| 480 |
+
import base64
|
| 481 |
+
|
| 482 |
+
match = re.search(r'__sign=([^&]+)', referer)
|
| 483 |
+
if match:
|
| 484 |
+
token = match.group(1)
|
| 485 |
+
try:
|
| 486 |
+
# JWT format: header.payload.signature
|
| 487 |
+
# We only need the payload (middle part)
|
| 488 |
+
parts = token.split('.')
|
| 489 |
+
if len(parts) == 3:
|
| 490 |
+
# Decode base64 payload (add padding if needed)
|
| 491 |
+
payload = parts[1]
|
| 492 |
+
payload += '=' * (4 - len(payload) % 4)
|
| 493 |
+
decoded = base64.urlsafe_b64decode(payload)
|
| 494 |
+
token_data = json.loads(decoded)
|
| 495 |
+
|
| 496 |
+
# Check if user is in token
|
| 497 |
+
if 'onBehalfOf' in token_data and 'user' in token_data['onBehalfOf']:
|
| 498 |
+
username = token_data['onBehalfOf']['user']
|
| 499 |
+
is_logged_in = True
|
| 500 |
+
logger.info(f"Found user in JWT token: {username}")
|
| 501 |
+
except Exception as e:
|
| 502 |
+
logger.warning(f"Failed to decode JWT: {e}")
|
| 503 |
+
|
| 504 |
logger.info(f"User: {username} | Logged in: {is_logged_in}")
|
| 505 |
except Exception as e:
|
| 506 |
logger.warning(f"Failed to detect user: {e}")
|