Delete auth.py
Browse files
auth.py
DELETED
@@ -1,309 +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 |
-
|
29 |
-
def login_user(self, token):
|
30 |
-
"""Log in a user with their HuggingFace token.
|
31 |
-
|
32 |
-
Args:
|
33 |
-
token: HuggingFace API token
|
34 |
-
|
35 |
-
Returns:
|
36 |
-
dict: User information if login successful, None otherwise
|
37 |
-
"""
|
38 |
-
try:
|
39 |
-
# Validate token with HuggingFace
|
40 |
-
login(token=token, add_to_git_credential=False)
|
41 |
-
|
42 |
-
# Get user info from HuggingFace
|
43 |
-
user_info = self.hf_api.whoami(token=token)
|
44 |
-
|
45 |
-
if not user_info:
|
46 |
-
return None
|
47 |
-
|
48 |
-
# Check if user exists in our database, create if not
|
49 |
-
username = user_info.get("name", user_info.get("fullname", ""))
|
50 |
-
hf_user_id = user_info.get("id", "")
|
51 |
-
|
52 |
-
if not hf_user_id:
|
53 |
-
return None
|
54 |
-
|
55 |
-
# Check if this is the admin account
|
56 |
-
is_admin = (username == self.admin_username)
|
57 |
-
|
58 |
-
# Add or get user from database
|
59 |
-
user_id = self.db_manager.add_user(username, hf_user_id, is_admin)
|
60 |
-
|
61 |
-
# Get complete user info from database
|
62 |
-
user = self.db_manager.get_user(hf_user_id)
|
63 |
-
|
64 |
-
if user:
|
65 |
-
# Add token to user info for session only (not stored in database)
|
66 |
-
user['token'] = token
|
67 |
-
return user
|
68 |
-
|
69 |
-
return None
|
70 |
-
except Exception as e:
|
71 |
-
print(f"Login error: {e}")
|
72 |
-
return None
|
73 |
-
|
74 |
-
def check_login(self, request: gr.Request):
|
75 |
-
"""Check if a user is logged in from a Gradio request.
|
76 |
-
|
77 |
-
Args:
|
78 |
-
request: Gradio request object
|
79 |
-
|
80 |
-
Returns:
|
81 |
-
dict: User information if logged in, None otherwise
|
82 |
-
"""
|
83 |
-
if not request:
|
84 |
-
return None
|
85 |
-
|
86 |
-
# Get token from cookies
|
87 |
-
token = request.cookies.get("hf_token")
|
88 |
-
|
89 |
-
if not token:
|
90 |
-
return None
|
91 |
-
|
92 |
-
try:
|
93 |
-
# Validate token with HuggingFace
|
94 |
-
user_info = self.hf_api.whoami(token=token)
|
95 |
-
|
96 |
-
if not user_info:
|
97 |
-
return None
|
98 |
-
|
99 |
-
# Get user from database
|
100 |
-
hf_user_id = user_info.get("id", "")
|
101 |
-
user = self.db_manager.get_user(hf_user_id)
|
102 |
-
|
103 |
-
if user:
|
104 |
-
# Add token to user info for session only (not stored in database)
|
105 |
-
user['token'] = token
|
106 |
-
return user
|
107 |
-
|
108 |
-
return None
|
109 |
-
except Exception as e:
|
110 |
-
print(f"Check login error: {e}")
|
111 |
-
return None
|
112 |
-
|
113 |
-
def require_login(self, func):
|
114 |
-
"""Decorator to require login for a function.
|
115 |
-
|
116 |
-
Args:
|
117 |
-
func: Function to decorate
|
118 |
-
|
119 |
-
Returns:
|
120 |
-
Function: Decorated function that requires login
|
121 |
-
"""
|
122 |
-
@wraps(func)
|
123 |
-
def wrapper(*args, **kwargs):
|
124 |
-
# Find the request argument
|
125 |
-
request = None
|
126 |
-
for arg in args:
|
127 |
-
if isinstance(arg, gr.Request):
|
128 |
-
request = arg
|
129 |
-
break
|
130 |
-
|
131 |
-
if not request and 'request' in kwargs:
|
132 |
-
request = kwargs['request']
|
133 |
-
|
134 |
-
if not request:
|
135 |
-
return "Please log in to access this feature."
|
136 |
-
|
137 |
-
# Check if user is logged in
|
138 |
-
user = self.check_login(request)
|
139 |
-
|
140 |
-
if not user:
|
141 |
-
return "Please log in to access this feature."
|
142 |
-
|
143 |
-
# Add user to kwargs
|
144 |
-
kwargs['user'] = user
|
145 |
-
|
146 |
-
# Call the original function
|
147 |
-
return func(*args, **kwargs)
|
148 |
-
|
149 |
-
return wrapper
|
150 |
-
|
151 |
-
def require_admin(self, func):
|
152 |
-
"""Decorator to require admin privileges for a function.
|
153 |
-
|
154 |
-
Args:
|
155 |
-
func: Function to decorate
|
156 |
-
|
157 |
-
Returns:
|
158 |
-
Function: Decorated function that requires admin privileges
|
159 |
-
"""
|
160 |
-
@wraps(func)
|
161 |
-
def wrapper(*args, **kwargs):
|
162 |
-
# Find the request argument
|
163 |
-
request = None
|
164 |
-
for arg in args:
|
165 |
-
if isinstance(arg, gr.Request):
|
166 |
-
request = arg
|
167 |
-
break
|
168 |
-
|
169 |
-
if not request and 'request' in kwargs:
|
170 |
-
request = kwargs['request']
|
171 |
-
|
172 |
-
if not request:
|
173 |
-
return "Admin access required."
|
174 |
-
|
175 |
-
# Check if user is logged in
|
176 |
-
user = self.check_login(request)
|
177 |
-
|
178 |
-
if not user:
|
179 |
-
return "Admin access required."
|
180 |
-
|
181 |
-
# Check if user is admin
|
182 |
-
if not user.get('is_admin', False):
|
183 |
-
return "Admin access required."
|
184 |
-
|
185 |
-
# Add user to kwargs
|
186 |
-
kwargs['user'] = user
|
187 |
-
|
188 |
-
# Call the original function
|
189 |
-
return func(*args, **kwargs)
|
190 |
-
|
191 |
-
return wrapper
|
192 |
-
|
193 |
-
def can_submit_benchmark(self, user_id):
|
194 |
-
"""Check if a user can submit a benchmark today.
|
195 |
-
|
196 |
-
Args:
|
197 |
-
user_id: User ID to check
|
198 |
-
|
199 |
-
Returns:
|
200 |
-
bool: True if user can submit, False otherwise
|
201 |
-
"""
|
202 |
-
return self.db_manager.can_submit_today(user_id)
|
203 |
-
|
204 |
-
def update_submission_date(self, user_id):
|
205 |
-
"""Update the last submission date for a user.
|
206 |
-
|
207 |
-
Args:
|
208 |
-
user_id: User ID to update
|
209 |
-
"""
|
210 |
-
self.db_manager.update_submission_date(user_id)
|
211 |
-
|
212 |
-
# Authentication UI components
|
213 |
-
def create_login_ui():
|
214 |
-
"""Create the login UI components.
|
215 |
-
|
216 |
-
Returns:
|
217 |
-
tuple: (login_button, logout_button, token_input, user_info)
|
218 |
-
"""
|
219 |
-
with gr.Row():
|
220 |
-
with gr.Column(scale=3):
|
221 |
-
token_input = gr.Textbox(
|
222 |
-
placeholder="Enter your HuggingFace token",
|
223 |
-
label="HuggingFace Token",
|
224 |
-
type="password",
|
225 |
-
visible=True,
|
226 |
-
info="Your token is only stored temporarily in browser session cookies and is never saved permanently"
|
227 |
-
)
|
228 |
-
login_button = gr.Button("Login")
|
229 |
-
logout_button = gr.Button("Logout", visible=False)
|
230 |
-
|
231 |
-
with gr.Column(scale=2):
|
232 |
-
user_info = gr.Markdown("Not logged in")
|
233 |
-
|
234 |
-
return login_button, logout_button, token_input, user_info
|
235 |
-
|
236 |
-
def login_handler(token, auth_manager):
|
237 |
-
"""Handle login button click.
|
238 |
-
|
239 |
-
Args:
|
240 |
-
token: HuggingFace token
|
241 |
-
auth_manager: Authentication manager instance
|
242 |
-
|
243 |
-
Returns:
|
244 |
-
tuple: Updated UI components visibility and user info
|
245 |
-
"""
|
246 |
-
if not token:
|
247 |
-
return gr.update(visible=True), gr.update(visible=False), "Please enter your HuggingFace token"
|
248 |
-
|
249 |
-
user = auth_manager.login_user(token)
|
250 |
-
|
251 |
-
if user:
|
252 |
-
# Set cookie in JavaScript with session-only flag (no persistent storage)
|
253 |
-
# Cookie will expire when browser is closed
|
254 |
-
js = f"""
|
255 |
-
document.cookie = "hf_token={token}; path=/; SameSite=Strict";
|
256 |
-
"""
|
257 |
-
|
258 |
-
# Return updated UI components
|
259 |
-
return (
|
260 |
-
gr.update(visible=False), # Hide token input
|
261 |
-
gr.update(visible=True), # Show logout button
|
262 |
-
f"Logged in as {user['username']}" # Update user info
|
263 |
-
)
|
264 |
-
else:
|
265 |
-
return (
|
266 |
-
gr.update(visible=True), # Keep token input visible
|
267 |
-
gr.update(visible=False), # Hide logout button
|
268 |
-
"Login failed. Please check your token and try again." # Update user info
|
269 |
-
)
|
270 |
-
|
271 |
-
def logout_handler():
|
272 |
-
"""Handle logout button click.
|
273 |
-
|
274 |
-
Returns:
|
275 |
-
tuple: Updated UI components visibility and user info
|
276 |
-
"""
|
277 |
-
# Clear cookie in JavaScript
|
278 |
-
js = """
|
279 |
-
document.cookie = "hf_token=; path=/; max-age=0; SameSite=Strict";
|
280 |
-
"""
|
281 |
-
|
282 |
-
# Return updated UI components
|
283 |
-
return (
|
284 |
-
gr.update(visible=True), # Show token input
|
285 |
-
gr.update(visible=False), # Hide logout button
|
286 |
-
"Logged out" # Update user info
|
287 |
-
)
|
288 |
-
|
289 |
-
def setup_auth_handlers(login_button, logout_button, token_input, user_info, auth_manager):
|
290 |
-
"""Set up event handlers for authentication UI components.
|
291 |
-
|
292 |
-
Args:
|
293 |
-
login_button: Login button component
|
294 |
-
logout_button: Logout button component
|
295 |
-
token_input: Token input component
|
296 |
-
user_info: User info component
|
297 |
-
auth_manager: Authentication manager instance
|
298 |
-
"""
|
299 |
-
login_button.click(
|
300 |
-
fn=lambda token: login_handler(token, auth_manager),
|
301 |
-
inputs=[token_input],
|
302 |
-
outputs=[token_input, logout_button, user_info]
|
303 |
-
)
|
304 |
-
|
305 |
-
logout_button.click(
|
306 |
-
fn=logout_handler,
|
307 |
-
inputs=[],
|
308 |
-
outputs=[token_input, logout_button, user_info]
|
309 |
-
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|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