Spaces:
Sleeping
Sleeping
File size: 6,027 Bytes
1f97573 4b6e3ab 1f97573 4b6e3ab 1f97573 4b7043c 3b39dcd 4b7043c 3b39dcd 4b7043c 3b39dcd 4b7043c ce9c78e 3b39dcd 4b7043c 4b6e3ab c7d81f9 4b6e3ab c7d81f9 4b6e3ab c7d81f9 4b6e3ab 7a8349e 4b6e3ab 5d646a8 4b7043c c7d81f9 1f97573 4b7043c 1f97573 e66ceeb 81fa8a1 1249068 c7d81f9 77fa5a9 2a425c9 4b7043c 77fa5a9 2a425c9 5d646a8 2a425c9 77fa5a9 f8b6cb2 4b7043c c7d81f9 2a425c9 4b7043c 2a425c9 4b6e3ab 4b7043c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 | """Gradio login page component for multi-project authorization."""
from typing import Tuple
import gradio as gr
from src.auth.auth_service import AuthService
def oauth_action_button_html(*, signed_in: bool = False) -> str:
"""Render the visible HF OAuth action while keeping login intent explicit."""
if signed_in:
href = "/logout"
label = "Sign out"
state_class = "bn-hf-oauth-link-signed-in"
onclick = (
"sessionStorage.removeItem('birdnet_hf_login_intent');"
)
else:
href = "/login/huggingface?birdnet_login_intent=1"
label = "Sign in with Hugging Face"
state_class = "bn-hf-oauth-link-signed-out"
onclick = (
"const q=new URLSearchParams(window.location.search);"
"q.set('birdnet_login_intent','1');"
"sessionStorage.setItem('birdnet_hf_login_intent','1');"
"window.parent?.postMessage({type:'SET_SCROLLING',enabled:true},'*');"
"event.preventDefault();"
"window.location.assign('/login/huggingface?'+q.toString());"
)
return (
"<div class='bn-oauth-login-html' "
"style='box-sizing:border-box;width:100%;max-width:var(--bn-login-width,680px);margin:0 auto;'>"
f"<a class='bn-hf-oauth-link {state_class}' href='{href}' target='_self' onclick=\"{onclick}\" "
f"aria-label='{label}'>"
"<span class='bn-hf-oauth-mark' aria-hidden='true'>HF</span>"
f"<span class='bn-hf-oauth-label'>{label}</span>"
"</a>"
"</div>"
)
def perform_login(
auth_service: AuthService,
username: str,
*,
allow_username_login: bool = True,
) -> Tuple[str, str]:
"""Attempt a development-only username login and return its session status."""
if not allow_username_login:
return (
"",
"Username-only login is disabled for this deployment. Sign in with Hugging Face OAuth.",
)
if not username or not username.strip():
return "", "Please enter a username"
username = username.strip()
session = auth_service.login(username)
if session is None:
return "", f"User '{username}' not found or inactive"
admin_projects = 0
validator_projects = 0
for project_slug in session.authorized_projects:
project_role = auth_service.get_user_role_for_project(username, project_slug)
if project_role is None:
continue
if project_role.value == "admin":
admin_projects += 1
else:
validator_projects += 1
return (
session.session_id,
(
f"Welcome, {username}. "
f"Admin in {admin_projects} project(s), validator in {validator_projects} project(s)."
),
)
def perform_oauth_login(
auth_service: AuthService,
profile: gr.OAuthProfile | None,
oauth_token: gr.OAuthToken | None,
) -> Tuple[str, str]:
"""Create an app session using the verified identity supplied by HF Spaces OAuth."""
if profile is None or oauth_token is None:
return "", "Sign in with Hugging Face first, then continue to the workspace."
username = str(profile.get("preferred_username") or profile.get("name") or "").strip()
email = str(profile.get("email") or "").strip() or None
session, message = auth_service.login_with_verified_hf_identity(
username=username,
token=oauth_token.token,
email=email,
authentication_method="oauth",
)
if session is None:
return "", message
return session.session_id, message
def create_login_page(
auth_service: AuthService,
*,
allow_username_login: bool = True,
enable_oauth_login: bool = False,
auth_mode_label: str = "",
admin_storage_mode: bool = False,
) -> Tuple[gr.Textbox, gr.Markdown, gr.HTML | None]:
"""Create an OAuth production login with an optional local username fallback.
Args:
auth_service: AuthService instance for login validation
Returns:
Tuple of (session_output, error_message, login_action)
"""
with gr.Row(elem_classes=["bn-login-row"]):
with gr.Column(scale=0, min_width=680, elem_classes=["bn-login-panel"]):
gr.Markdown("# BirdNET Validation Platform")
session_output = gr.Textbox(
label="Session ID",
interactive=False,
visible=False,
)
username_input = None
login_button = None
login_action = None
if enable_oauth_login:
gr.Markdown(
(
"Secure login with your Hugging Face account. The app verifies your identity and only grants access to projects assigned to you."
if admin_storage_mode
else "Secure login with your Hugging Face account to access your validation workspace."
)
)
gr.LoginButton(
visible=False,
elem_id="bn-hidden-hf-oauth-route",
)
login_action = gr.HTML(oauth_action_button_html(signed_in=False))
elif allow_username_login:
gr.Markdown("Local development login")
username_input = gr.Textbox(
label="Username",
placeholder="Enter your username",
lines=1,
)
login_button = gr.Button("Login", variant="primary", scale=1)
error_message = gr.Markdown()
if login_button is not None and username_input is not None:
login_button.click(
fn=lambda username: perform_login(
auth_service,
username,
allow_username_login=True,
),
inputs=[username_input],
outputs=[session_output, error_message],
)
return session_output, error_message, login_action
|