Update auth.py
Browse files
auth.py
CHANGED
@@ -25,6 +25,7 @@ class HuggingFaceAuth:
|
|
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.
|
@@ -83,7 +84,21 @@ class HuggingFaceAuth:
|
|
83 |
if not request:
|
84 |
return None
|
85 |
|
86 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
87 |
token = request.cookies.get("hf_token")
|
88 |
|
89 |
if not token:
|
@@ -214,59 +229,57 @@ def create_login_ui():
|
|
214 |
"""Create the login UI components.
|
215 |
|
216 |
Returns:
|
217 |
-
tuple: (login_button, logout_button,
|
218 |
"""
|
219 |
with gr.Row():
|
220 |
with gr.Column(scale=3):
|
221 |
-
|
222 |
-
|
223 |
-
|
224 |
-
|
225 |
-
|
226 |
-
|
227 |
-
|
228 |
-
|
229 |
-
logout_button = gr.Button("Logout", visible=False)
|
230 |
|
231 |
with gr.Column(scale=2):
|
232 |
-
user_info = gr.Markdown("
|
233 |
|
234 |
-
return login_button, logout_button,
|
235 |
|
236 |
-
def login_handler(
|
237 |
"""Handle login button click.
|
238 |
|
239 |
Args:
|
240 |
-
token: HuggingFace token
|
241 |
auth_manager: Authentication manager instance
|
242 |
|
243 |
Returns:
|
244 |
-
tuple:
|
245 |
"""
|
246 |
-
|
247 |
-
|
248 |
-
|
249 |
-
|
250 |
-
|
251 |
-
|
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 |
-
|
259 |
-
|
260 |
-
|
261 |
-
|
262 |
-
|
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.
|
@@ -274,36 +287,40 @@ def logout_handler():
|
|
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
|
285 |
gr.update(visible=False), # Hide logout button
|
286 |
-
"Logged out"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
287 |
)
|
288 |
|
289 |
-
def setup_auth_handlers(login_button, logout_button,
|
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 |
-
|
300 |
-
|
301 |
-
|
302 |
-
|
303 |
-
|
304 |
-
|
305 |
-
|
306 |
-
|
307 |
-
|
308 |
-
|
309 |
-
|
|
|
|
|
|
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.
|
|
|
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:
|
|
|
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.
|
|
|
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 |
+
)
|