Spaces:
Sleeping
Sleeping
Update main.py
Browse files
main.py
CHANGED
|
@@ -205,13 +205,13 @@ def verify_token(req):
|
|
| 205 |
|
| 206 |
# Verify the token
|
| 207 |
decoded = firebase_auth.verify_id_token(token)
|
| 208 |
-
|
| 209 |
# Get user UID and email from decoded token
|
| 210 |
uid = decoded.get('uid')
|
| 211 |
-
email = decoded.get('email')
|
| 212 |
-
|
| 213 |
if not uid:
|
| 214 |
-
|
| 215 |
return None
|
| 216 |
|
| 217 |
# Check if user is admin by querying Firebase Realtime Database using UID
|
|
@@ -220,43 +220,42 @@ def verify_token(req):
|
|
| 220 |
|
| 221 |
if admin_data and admin_data.get("is_admin", False):
|
| 222 |
# User is already an admin in the database
|
|
|
|
| 223 |
return decoded
|
|
|
|
| 224 |
elif email and email in ADMIN_EMAILS:
|
| 225 |
# User's email is in the approved list, but UID not found in DB.
|
| 226 |
# This is likely their first time accessing the API.
|
| 227 |
# Automatically create their admin entry using their UID.
|
| 228 |
-
|
| 229 |
admins_ref.child(uid).set({
|
| 230 |
"email": email,
|
| 231 |
"is_admin": True,
|
| 232 |
-
"first_seen": datetime.datetime.utcnow().isoformat() + 'Z'
|
| 233 |
})
|
| 234 |
-
|
| 235 |
# Return the decoded token as they are now an admin
|
| 236 |
return decoded
|
|
|
|
| 237 |
else:
|
| 238 |
# User is not in the approved admin list
|
| 239 |
-
|
| 240 |
return None
|
| 241 |
-
|
| 242 |
except firebase_auth.InvalidIdTokenError as e:
|
| 243 |
-
|
| 244 |
return None
|
| 245 |
except firebase_auth.ExpiredIdTokenError as e:
|
| 246 |
-
|
| 247 |
return None
|
| 248 |
except firebase_auth.RevokedIdTokenError as e:
|
| 249 |
-
|
| 250 |
return None
|
| 251 |
except Exception as e:
|
| 252 |
-
|
| 253 |
-
# Consider logging the full traceback in production
|
| 254 |
-
# import traceback
|
| 255 |
-
# print(traceback.format_exc())
|
| 256 |
return None
|
| 257 |
|
| 258 |
|
| 259 |
-
|
| 260 |
# === Admin Setup (Legacy - kept for compatibility) ===
|
| 261 |
def setup_admins():
|
| 262 |
ref = db.reference("admins")
|
|
|
|
| 205 |
|
| 206 |
# Verify the token
|
| 207 |
decoded = firebase_auth.verify_id_token(token)
|
| 208 |
+
|
| 209 |
# Get user UID and email from decoded token
|
| 210 |
uid = decoded.get('uid')
|
| 211 |
+
email = decoded.get('email') # Firebase ID tokens usually contain the email
|
| 212 |
+
|
| 213 |
if not uid:
|
| 214 |
+
logger.error("Verified token does not contain a UID.")
|
| 215 |
return None
|
| 216 |
|
| 217 |
# Check if user is admin by querying Firebase Realtime Database using UID
|
|
|
|
| 220 |
|
| 221 |
if admin_data and admin_data.get("is_admin", False):
|
| 222 |
# User is already an admin in the database
|
| 223 |
+
logger.info(f"User {uid} ({email}) is authorized as admin.")
|
| 224 |
return decoded
|
| 225 |
+
|
| 226 |
elif email and email in ADMIN_EMAILS:
|
| 227 |
# User's email is in the approved list, but UID not found in DB.
|
| 228 |
# This is likely their first time accessing the API.
|
| 229 |
# Automatically create their admin entry using their UID.
|
| 230 |
+
logger.info(f"First time admin access for {email} (UID: {uid}). Creating database entry.")
|
| 231 |
admins_ref.child(uid).set({
|
| 232 |
"email": email,
|
| 233 |
"is_admin": True,
|
| 234 |
+
"first_seen": datetime.datetime.utcnow().isoformat() + 'Z' # UTC Timestamp
|
| 235 |
})
|
| 236 |
+
logger.info(f"Admin entry created for UID {uid}.")
|
| 237 |
# Return the decoded token as they are now an admin
|
| 238 |
return decoded
|
| 239 |
+
|
| 240 |
else:
|
| 241 |
# User is not in the approved admin list
|
| 242 |
+
logger.warning(f"User {uid} ({email}) is not in the approved admin list or not found in DB.")
|
| 243 |
return None
|
| 244 |
+
|
| 245 |
except firebase_auth.InvalidIdTokenError as e:
|
| 246 |
+
logger.error(f"Invalid Firebase ID token provided: {e}")
|
| 247 |
return None
|
| 248 |
except firebase_auth.ExpiredIdTokenError as e:
|
| 249 |
+
logger.error(f"Firebase ID token has expired: {e}")
|
| 250 |
return None
|
| 251 |
except firebase_auth.RevokedIdTokenError as e:
|
| 252 |
+
logger.error(f"Firebase ID token has been revoked: {e}")
|
| 253 |
return None
|
| 254 |
except Exception as e:
|
| 255 |
+
logger.error(f"Unexpected error during token verification: {e}")
|
|
|
|
|
|
|
|
|
|
| 256 |
return None
|
| 257 |
|
| 258 |
|
|
|
|
| 259 |
# === Admin Setup (Legacy - kept for compatibility) ===
|
| 260 |
def setup_admins():
|
| 261 |
ref = db.reference("admins")
|