Delete auth.py
Browse files
auth.py
DELETED
@@ -1,326 +0,0 @@
|
|
1 |
-
"""
|
2 |
-
Authentication module for Dynamic Highscores system.
|
3 |
-
|
4 |
-
This module handles user authentication with HuggingFace,
|
5 |
-
user session management, and access control.
|
6 |
-
"""
|
7 |
-
|
8 |
-
import os
|
9 |
-
import json
|
10 |
-
import time
|
11 |
-
import requests
|
12 |
-
import gradio as gr
|
13 |
-
from huggingface_hub import HfApi, login
|
14 |
-
from functools import wraps
|
15 |
-
|
16 |
-
class HuggingFaceAuth:
|
17 |
-
"""Authentication manager for HuggingFace integration."""
|
18 |
-
|
19 |
-
def __init__(self, db_manager):
|
20 |
-
"""Initialize the authentication manager.
|
21 |
-
|
22 |
-
Args:
|
23 |
-
db_manager: Database manager instance for user storage
|
24 |
-
"""
|
25 |
-
self.db_manager = db_manager
|
26 |
-
self.hf_api = HfApi()
|
27 |
-
self.admin_username = os.environ.get("ADMIN_USERNAME", "Quazim0t0")
|
28 |
-
self.running_in_space = 'SPACE_ID' in os.environ
|
29 |
-
|
30 |
-
def login_user(self, token):
|
31 |
-
"""Log in a user with their HuggingFace token.
|
32 |
-
|
33 |
-
Args:
|
34 |
-
token: HuggingFace API token
|
35 |
-
|
36 |
-
Returns:
|
37 |
-
dict: User information if login successful, None otherwise
|
38 |
-
"""
|
39 |
-
try:
|
40 |
-
# Validate token with HuggingFace
|
41 |
-
login(token=token, add_to_git_credential=False)
|
42 |
-
|
43 |
-
# Get user info from HuggingFace
|
44 |
-
user_info = self.hf_api.whoami(token=token)
|
45 |
-
|
46 |
-
if not user_info:
|
47 |
-
return None
|
48 |
-
|
49 |
-
# Check if user exists in our database, create if not
|
50 |
-
username = user_info.get("name", user_info.get("fullname", ""))
|
51 |
-
hf_user_id = user_info.get("id", "")
|
52 |
-
|
53 |
-
if not hf_user_id:
|
54 |
-
return None
|
55 |
-
|
56 |
-
# Check if this is the admin account
|
57 |
-
is_admin = (username == self.admin_username)
|
58 |
-
|
59 |
-
# Add or get user from database
|
60 |
-
user_id = self.db_manager.add_user(username, hf_user_id, is_admin)
|
61 |
-
|
62 |
-
# Get complete user info from database
|
63 |
-
user = self.db_manager.get_user(hf_user_id)
|
64 |
-
|
65 |
-
if user:
|
66 |
-
# Add token to user info for session only (not stored in database)
|
67 |
-
user['token'] = token
|
68 |
-
return user
|
69 |
-
|
70 |
-
return None
|
71 |
-
except Exception as e:
|
72 |
-
print(f"Login error: {e}")
|
73 |
-
return None
|
74 |
-
|
75 |
-
def check_login(self, request: gr.Request):
|
76 |
-
"""Check if a user is logged in from a Gradio request.
|
77 |
-
|
78 |
-
Args:
|
79 |
-
request: Gradio request object
|
80 |
-
|
81 |
-
Returns:
|
82 |
-
dict: User information if logged in, None otherwise
|
83 |
-
"""
|
84 |
-
if not request:
|
85 |
-
return None
|
86 |
-
|
87 |
-
# First, check if we're in a HuggingFace Space with OAuth
|
88 |
-
if self.running_in_space:
|
89 |
-
# Check for HF-User header from Space OAuth
|
90 |
-
username = request.headers.get("HF-User")
|
91 |
-
if username:
|
92 |
-
# Check if user exists in our database, create if not
|
93 |
-
user = self.db_manager.get_user_by_username(username)
|
94 |
-
if not user:
|
95 |
-
# Create a new user
|
96 |
-
is_admin = (username == self.admin_username)
|
97 |
-
user_id = self.db_manager.add_user(username, username, is_admin)
|
98 |
-
user = self.db_manager.get_user_by_username(username)
|
99 |
-
return user
|
100 |
-
|
101 |
-
# Fallback to token-based auth for local development
|
102 |
-
token = request.cookies.get("hf_token")
|
103 |
-
|
104 |
-
if not token:
|
105 |
-
return None
|
106 |
-
|
107 |
-
try:
|
108 |
-
# Validate token with HuggingFace
|
109 |
-
user_info = self.hf_api.whoami(token=token)
|
110 |
-
|
111 |
-
if not user_info:
|
112 |
-
return None
|
113 |
-
|
114 |
-
# Get user from database
|
115 |
-
hf_user_id = user_info.get("id", "")
|
116 |
-
user = self.db_manager.get_user(hf_user_id)
|
117 |
-
|
118 |
-
if user:
|
119 |
-
# Add token to user info for session only (not stored in database)
|
120 |
-
user['token'] = token
|
121 |
-
return user
|
122 |
-
|
123 |
-
return None
|
124 |
-
except Exception as e:
|
125 |
-
print(f"Check login error: {e}")
|
126 |
-
return None
|
127 |
-
|
128 |
-
def require_login(self, func):
|
129 |
-
"""Decorator to require login for a function.
|
130 |
-
|
131 |
-
Args:
|
132 |
-
func: Function to decorate
|
133 |
-
|
134 |
-
Returns:
|
135 |
-
Function: Decorated function that requires login
|
136 |
-
"""
|
137 |
-
@wraps(func)
|
138 |
-
def wrapper(*args, **kwargs):
|
139 |
-
# Find the request argument
|
140 |
-
request = None
|
141 |
-
for arg in args:
|
142 |
-
if isinstance(arg, gr.Request):
|
143 |
-
request = arg
|
144 |
-
break
|
145 |
-
|
146 |
-
if not request and 'request' in kwargs:
|
147 |
-
request = kwargs['request']
|
148 |
-
|
149 |
-
if not request:
|
150 |
-
return "Please log in to access this feature."
|
151 |
-
|
152 |
-
# Check if user is logged in
|
153 |
-
user = self.check_login(request)
|
154 |
-
|
155 |
-
if not user:
|
156 |
-
return "Please log in to access this feature."
|
157 |
-
|
158 |
-
# Add user to kwargs
|
159 |
-
kwargs['user'] = user
|
160 |
-
|
161 |
-
# Call the original function
|
162 |
-
return func(*args, **kwargs)
|
163 |
-
|
164 |
-
return wrapper
|
165 |
-
|
166 |
-
def require_admin(self, func):
|
167 |
-
"""Decorator to require admin privileges for a function.
|
168 |
-
|
169 |
-
Args:
|
170 |
-
func: Function to decorate
|
171 |
-
|
172 |
-
Returns:
|
173 |
-
Function: Decorated function that requires admin privileges
|
174 |
-
"""
|
175 |
-
@wraps(func)
|
176 |
-
def wrapper(*args, **kwargs):
|
177 |
-
# Find the request argument
|
178 |
-
request = None
|
179 |
-
for arg in args:
|
180 |
-
if isinstance(arg, gr.Request):
|
181 |
-
request = arg
|
182 |
-
break
|
183 |
-
|
184 |
-
if not request and 'request' in kwargs:
|
185 |
-
request = kwargs['request']
|
186 |
-
|
187 |
-
if not request:
|
188 |
-
return "Admin access required."
|
189 |
-
|
190 |
-
# Check if user is logged in
|
191 |
-
user = self.check_login(request)
|
192 |
-
|
193 |
-
if not user:
|
194 |
-
return "Admin access required."
|
195 |
-
|
196 |
-
# Check if user is admin
|
197 |
-
if not user.get('is_admin', False):
|
198 |
-
return "Admin access required."
|
199 |
-
|
200 |
-
# Add user to kwargs
|
201 |
-
kwargs['user'] = user
|
202 |
-
|
203 |
-
# Call the original function
|
204 |
-
return func(*args, **kwargs)
|
205 |
-
|
206 |
-
return wrapper
|
207 |
-
|
208 |
-
def can_submit_benchmark(self, user_id):
|
209 |
-
"""Check if a user can submit a benchmark today.
|
210 |
-
|
211 |
-
Args:
|
212 |
-
user_id: User ID to check
|
213 |
-
|
214 |
-
Returns:
|
215 |
-
bool: True if user can submit, False otherwise
|
216 |
-
"""
|
217 |
-
return self.db_manager.can_submit_today(user_id)
|
218 |
-
|
219 |
-
def update_submission_date(self, user_id):
|
220 |
-
"""Update the last submission date for a user.
|
221 |
-
|
222 |
-
Args:
|
223 |
-
user_id: User ID to update
|
224 |
-
"""
|
225 |
-
self.db_manager.update_submission_date(user_id)
|
226 |
-
|
227 |
-
# Authentication UI components
|
228 |
-
def create_login_ui():
|
229 |
-
"""Create the login UI components.
|
230 |
-
|
231 |
-
Returns:
|
232 |
-
tuple: (login_button, logout_button, user_info)
|
233 |
-
"""
|
234 |
-
with gr.Row():
|
235 |
-
with gr.Column(scale=3):
|
236 |
-
# If running in a HuggingFace Space, use their OAuth
|
237 |
-
if 'SPACE_ID' in os.environ:
|
238 |
-
login_button = gr.Button("Login with HuggingFace", visible=False)
|
239 |
-
logout_button = gr.Button("Logout", visible=False)
|
240 |
-
else:
|
241 |
-
# For local development, use token-based login
|
242 |
-
login_button = gr.Button("Login with HuggingFace Token")
|
243 |
-
logout_button = gr.Button("Logout", visible=False)
|
244 |
-
|
245 |
-
with gr.Column(scale=2):
|
246 |
-
user_info = gr.Markdown("Checking login status...")
|
247 |
-
|
248 |
-
return login_button, logout_button, user_info
|
249 |
-
|
250 |
-
def login_handler(auth_manager):
|
251 |
-
"""Handle login button click.
|
252 |
-
|
253 |
-
Args:
|
254 |
-
auth_manager: Authentication manager instance
|
255 |
-
|
256 |
-
Returns:
|
257 |
-
tuple: JS to redirect to login and updated UI visibility
|
258 |
-
"""
|
259 |
-
# This is only used for local development
|
260 |
-
# For HuggingFace Spaces, the built-in OAuth is used
|
261 |
-
return (
|
262 |
-
gr.update(visible=False), # Hide login button
|
263 |
-
gr.update(visible=True), # Show logout button
|
264 |
-
"Redirecting to login...",
|
265 |
-
"""
|
266 |
-
<script>
|
267 |
-
// Open a popup window for token entry
|
268 |
-
function promptForToken() {
|
269 |
-
const token = prompt("Enter your HuggingFace token:");
|
270 |
-
if (token) {
|
271 |
-
// Set the token as a cookie
|
272 |
-
document.cookie = "hf_token=" + token + "; path=/; SameSite=Strict";
|
273 |
-
// Reload the page to apply the token
|
274 |
-
window.location.reload();
|
275 |
-
}
|
276 |
-
}
|
277 |
-
|
278 |
-
// Call the function
|
279 |
-
promptForToken();
|
280 |
-
</script>
|
281 |
-
"""
|
282 |
-
)
|
283 |
-
|
284 |
-
def logout_handler():
|
285 |
-
"""Handle logout button click.
|
286 |
-
|
287 |
-
Returns:
|
288 |
-
tuple: Updated UI components visibility and user info
|
289 |
-
"""
|
290 |
-
# Clear token cookie in JavaScript
|
291 |
-
return (
|
292 |
-
gr.update(visible=True), # Show login button
|
293 |
-
gr.update(visible=False), # Hide logout button
|
294 |
-
"Logged out",
|
295 |
-
"""
|
296 |
-
<script>
|
297 |
-
// Clear the token cookie
|
298 |
-
document.cookie = "hf_token=; path=/; max-age=0; SameSite=Strict";
|
299 |
-
// Reload the page
|
300 |
-
window.location.reload();
|
301 |
-
</script>
|
302 |
-
"""
|
303 |
-
)
|
304 |
-
|
305 |
-
def setup_auth_handlers(login_button, logout_button, user_info, auth_manager):
|
306 |
-
"""Set up event handlers for authentication UI components.
|
307 |
-
|
308 |
-
Args:
|
309 |
-
login_button: Login button component
|
310 |
-
logout_button: Logout button component
|
311 |
-
user_info: User info component
|
312 |
-
auth_manager: Authentication manager instance
|
313 |
-
"""
|
314 |
-
# Only add event handlers if not running in a HuggingFace Space
|
315 |
-
if 'SPACE_ID' not in os.environ:
|
316 |
-
login_button.click(
|
317 |
-
fn=lambda: login_handler(auth_manager),
|
318 |
-
inputs=[],
|
319 |
-
outputs=[login_button, logout_button, user_info, gr.HTML()]
|
320 |
-
)
|
321 |
-
|
322 |
-
logout_button.click(
|
323 |
-
fn=logout_handler,
|
324 |
-
inputs=[],
|
325 |
-
outputs=[login_button, logout_button, user_info, gr.HTML()]
|
326 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|