Spaces:
Sleeping
Sleeping
Sync app from GitHub Actions (feature/project-state-security-privacy-review)
Browse files- src/ui/app_factory.py +19 -7
- src/ui/login_page.py +40 -31
- src/ui/theme.py +67 -0
- tests/unit/test_login_page.py +12 -1
src/ui/app_factory.py
CHANGED
|
@@ -52,10 +52,10 @@ from src.services.hf_project_resource_deletion import HfProjectResourceDeleter,
|
|
| 52 |
from src.services.validation_service import ValidationService
|
| 53 |
from src.services.invite_email_notifier import EmailJSInviteEmailNotifier, InviteEmailNotifier
|
| 54 |
from src.auth.auth_service import AuthService
|
| 55 |
-
from src.ui.login_page import create_login_page, perform_oauth_login
|
| 56 |
from src.ui.admin_panel import AdminPanelManager
|
| 57 |
from src.ui.components import admin_overview_html, compact_metric_grid, coverage_bars_html, inline_hint_html, invite_panel_html, paged_activity_html, project_context_html, section_header_html, selected_segment_html, settings_health_html
|
| 58 |
-
from src.ui.theme import APP_CSS, app_header_html
|
| 59 |
|
| 60 |
|
| 61 |
class _AudioFetchResultProtocol(Protocol):
|
|
@@ -3162,7 +3162,13 @@ def create_app() -> gr.Blocks:
|
|
| 3162 |
report_cache: dict[tuple[str, int, int, str], tuple[float, tuple[object, ...]]] = {}
|
| 3163 |
report_cache_ttl_seconds = 30.0
|
| 3164 |
|
| 3165 |
-
with gr.Blocks(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3166 |
gr.HTML(app_header_html(state_backend_message))
|
| 3167 |
if state_backend_message.startswith("⚠"):
|
| 3168 |
gr.Markdown(state_backend_message)
|
|
@@ -3464,9 +3470,7 @@ def create_app() -> gr.Blocks:
|
|
| 3464 |
)
|
| 3465 |
|
| 3466 |
def render_oauth_button_label(session):
|
| 3467 |
-
|
| 3468 |
-
return gr.update(value="Sign in with Hugging Face")
|
| 3469 |
-
return gr.update(value="Sign out")
|
| 3470 |
|
| 3471 |
if oauth_login_button is not None and _is_running_in_hf_space():
|
| 3472 |
session_state.change(
|
|
@@ -3494,7 +3498,15 @@ def create_app() -> gr.Blocks:
|
|
| 3494 |
outputs=[oauth_login_intent_state],
|
| 3495 |
js="""
|
| 3496 |
() => {
|
| 3497 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 3498 |
}
|
| 3499 |
""",
|
| 3500 |
)
|
|
|
|
| 52 |
from src.services.validation_service import ValidationService
|
| 53 |
from src.services.invite_email_notifier import EmailJSInviteEmailNotifier, InviteEmailNotifier
|
| 54 |
from src.auth.auth_service import AuthService
|
| 55 |
+
from src.ui.login_page import create_login_page, oauth_action_button_html, perform_oauth_login
|
| 56 |
from src.ui.admin_panel import AdminPanelManager
|
| 57 |
from src.ui.components import admin_overview_html, compact_metric_grid, coverage_bars_html, inline_hint_html, invite_panel_html, paged_activity_html, project_context_html, section_header_html, selected_segment_html, settings_health_html
|
| 58 |
+
from src.ui.theme import APP_CSS, CRITICAL_HEAD_HTML, app_header_html
|
| 59 |
|
| 60 |
|
| 61 |
class _AudioFetchResultProtocol(Protocol):
|
|
|
|
| 3162 |
report_cache: dict[tuple[str, int, int, str], tuple[float, tuple[object, ...]]] = {}
|
| 3163 |
report_cache_ttl_seconds = 30.0
|
| 3164 |
|
| 3165 |
+
with gr.Blocks(
|
| 3166 |
+
title="BirdNET-Validator-App",
|
| 3167 |
+
css=APP_CSS,
|
| 3168 |
+
head=CRITICAL_HEAD_HTML,
|
| 3169 |
+
fill_width=False,
|
| 3170 |
+
elem_classes=["bn-shell"],
|
| 3171 |
+
) as wrapper:
|
| 3172 |
gr.HTML(app_header_html(state_backend_message))
|
| 3173 |
if state_backend_message.startswith("⚠"):
|
| 3174 |
gr.Markdown(state_backend_message)
|
|
|
|
| 3470 |
)
|
| 3471 |
|
| 3472 |
def render_oauth_button_label(session):
|
| 3473 |
+
return gr.update(value=oauth_action_button_html(signed_in=session is not None))
|
|
|
|
|
|
|
| 3474 |
|
| 3475 |
if oauth_login_button is not None and _is_running_in_hf_space():
|
| 3476 |
session_state.change(
|
|
|
|
| 3498 |
outputs=[oauth_login_intent_state],
|
| 3499 |
js="""
|
| 3500 |
() => {
|
| 3501 |
+
const params = new URLSearchParams(window.location.search);
|
| 3502 |
+
const intentFromUrl = params.get("birdnet_login_intent") === "1";
|
| 3503 |
+
if (intentFromUrl) {
|
| 3504 |
+
params.delete("birdnet_login_intent");
|
| 3505 |
+
const nextQuery = params.toString();
|
| 3506 |
+
const nextUrl = window.location.pathname + (nextQuery ? `?${nextQuery}` : "") + window.location.hash;
|
| 3507 |
+
window.history.replaceState({}, "", nextUrl);
|
| 3508 |
+
}
|
| 3509 |
+
return sessionStorage.getItem("birdnet_hf_login_intent") || (intentFromUrl ? "1" : "");
|
| 3510 |
}
|
| 3511 |
""",
|
| 3512 |
)
|
src/ui/login_page.py
CHANGED
|
@@ -7,6 +7,40 @@ import gradio as gr
|
|
| 7 |
from src.auth.auth_service import AuthService
|
| 8 |
|
| 9 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 10 |
def perform_login(
|
| 11 |
auth_service: AuthService,
|
| 12 |
username: str,
|
|
@@ -78,14 +112,14 @@ def create_login_page(
|
|
| 78 |
enable_oauth_login: bool = False,
|
| 79 |
auth_mode_label: str = "",
|
| 80 |
admin_storage_mode: bool = False,
|
| 81 |
-
) -> Tuple[gr.Textbox, gr.Markdown, gr.
|
| 82 |
"""Create an OAuth production login with an optional local username fallback.
|
| 83 |
|
| 84 |
Args:
|
| 85 |
auth_service: AuthService instance for login validation
|
| 86 |
|
| 87 |
Returns:
|
| 88 |
-
Tuple of (session_output, error_message,
|
| 89 |
"""
|
| 90 |
with gr.Row(elem_classes=["bn-login-row"]):
|
| 91 |
with gr.Column(scale=0, min_width=680, elem_classes=["bn-login-panel"]):
|
|
@@ -99,6 +133,7 @@ def create_login_page(
|
|
| 99 |
|
| 100 |
username_input = None
|
| 101 |
login_button = None
|
|
|
|
| 102 |
if enable_oauth_login:
|
| 103 |
gr.Markdown(
|
| 104 |
(
|
|
@@ -111,11 +146,7 @@ def create_login_page(
|
|
| 111 |
visible=False,
|
| 112 |
elem_id="bn-hidden-hf-oauth-route",
|
| 113 |
)
|
| 114 |
-
|
| 115 |
-
"Sign in with Hugging Face",
|
| 116 |
-
variant="primary",
|
| 117 |
-
elem_classes=["bn-oauth-login-button"],
|
| 118 |
-
)
|
| 119 |
elif allow_username_login:
|
| 120 |
gr.Markdown("Local development login")
|
| 121 |
username_input = gr.Textbox(
|
|
@@ -125,29 +156,7 @@ def create_login_page(
|
|
| 125 |
)
|
| 126 |
login_button = gr.Button("Login", variant="primary", scale=1)
|
| 127 |
error_message = gr.Markdown()
|
| 128 |
-
if
|
| 129 |
-
login_button.click(
|
| 130 |
-
fn=lambda _button_value: None,
|
| 131 |
-
inputs=[login_button],
|
| 132 |
-
outputs=None,
|
| 133 |
-
js="""
|
| 134 |
-
(buttonValue) => {
|
| 135 |
-
const label = String(buttonValue || "").toLowerCase();
|
| 136 |
-
sessionStorage.setItem("birdnet_hf_login_intent", "1");
|
| 137 |
-
window.parent?.postMessage({ type: "SET_SCROLLING", enabled: true }, "*");
|
| 138 |
-
setTimeout(() => {
|
| 139 |
-
if (label.includes("sign out")) {
|
| 140 |
-
sessionStorage.removeItem("birdnet_hf_login_intent");
|
| 141 |
-
window.location.assign("/logout" + window.location.search);
|
| 142 |
-
} else {
|
| 143 |
-
window.location.assign("/login/huggingface" + window.location.search);
|
| 144 |
-
}
|
| 145 |
-
}, 100);
|
| 146 |
-
return [];
|
| 147 |
-
}
|
| 148 |
-
""",
|
| 149 |
-
)
|
| 150 |
-
elif login_button is not None and username_input is not None:
|
| 151 |
login_button.click(
|
| 152 |
fn=lambda username: perform_login(
|
| 153 |
auth_service,
|
|
@@ -158,4 +167,4 @@ def create_login_page(
|
|
| 158 |
outputs=[session_output, error_message],
|
| 159 |
)
|
| 160 |
|
| 161 |
-
return session_output, error_message,
|
|
|
|
| 7 |
from src.auth.auth_service import AuthService
|
| 8 |
|
| 9 |
|
| 10 |
+
def oauth_action_button_html(*, signed_in: bool = False) -> str:
|
| 11 |
+
"""Render the visible HF OAuth action without depending on Gradio button layout."""
|
| 12 |
+
if signed_in:
|
| 13 |
+
href = "/logout"
|
| 14 |
+
label = "Sign out"
|
| 15 |
+
onclick = (
|
| 16 |
+
"sessionStorage.removeItem('birdnet_hf_login_intent');"
|
| 17 |
+
)
|
| 18 |
+
else:
|
| 19 |
+
href = "/login/huggingface?birdnet_login_intent=1"
|
| 20 |
+
label = "Sign in with Hugging Face"
|
| 21 |
+
onclick = (
|
| 22 |
+
"const q=new URLSearchParams(window.location.search);"
|
| 23 |
+
"q.set('birdnet_login_intent','1');"
|
| 24 |
+
"sessionStorage.setItem('birdnet_hf_login_intent','1');"
|
| 25 |
+
"window.parent?.postMessage({type:'SET_SCROLLING',enabled:true},'*');"
|
| 26 |
+
"event.preventDefault();"
|
| 27 |
+
"window.location.assign('/login/huggingface?'+q.toString());"
|
| 28 |
+
)
|
| 29 |
+
return (
|
| 30 |
+
"<div class='bn-oauth-login-html' "
|
| 31 |
+
"style='box-sizing:border-box;width:100%;max-width:680px;margin:0 auto;'>"
|
| 32 |
+
f"<a href='{href}' target='_self' onclick=\"{onclick}\" "
|
| 33 |
+
"style='box-sizing:border-box;display:flex;align-items:center;justify-content:center;"
|
| 34 |
+
"width:100%;max-width:680px;min-height:48px;padding:12px 18px;"
|
| 35 |
+
"border:1px solid #f97316;border-radius:8px;background:#f97316;color:#fff;"
|
| 36 |
+
"font-weight:780;text-decoration:none;white-space:nowrap;overflow:hidden;"
|
| 37 |
+
"text-overflow:ellipsis;'>"
|
| 38 |
+
f"{label}"
|
| 39 |
+
"</a>"
|
| 40 |
+
"</div>"
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
|
| 44 |
def perform_login(
|
| 45 |
auth_service: AuthService,
|
| 46 |
username: str,
|
|
|
|
| 112 |
enable_oauth_login: bool = False,
|
| 113 |
auth_mode_label: str = "",
|
| 114 |
admin_storage_mode: bool = False,
|
| 115 |
+
) -> Tuple[gr.Textbox, gr.Markdown, gr.HTML | None]:
|
| 116 |
"""Create an OAuth production login with an optional local username fallback.
|
| 117 |
|
| 118 |
Args:
|
| 119 |
auth_service: AuthService instance for login validation
|
| 120 |
|
| 121 |
Returns:
|
| 122 |
+
Tuple of (session_output, error_message, login_action)
|
| 123 |
"""
|
| 124 |
with gr.Row(elem_classes=["bn-login-row"]):
|
| 125 |
with gr.Column(scale=0, min_width=680, elem_classes=["bn-login-panel"]):
|
|
|
|
| 133 |
|
| 134 |
username_input = None
|
| 135 |
login_button = None
|
| 136 |
+
login_action = None
|
| 137 |
if enable_oauth_login:
|
| 138 |
gr.Markdown(
|
| 139 |
(
|
|
|
|
| 146 |
visible=False,
|
| 147 |
elem_id="bn-hidden-hf-oauth-route",
|
| 148 |
)
|
| 149 |
+
login_action = gr.HTML(oauth_action_button_html(signed_in=False))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
elif allow_username_login:
|
| 151 |
gr.Markdown("Local development login")
|
| 152 |
username_input = gr.Textbox(
|
|
|
|
| 156 |
)
|
| 157 |
login_button = gr.Button("Login", variant="primary", scale=1)
|
| 158 |
error_message = gr.Markdown()
|
| 159 |
+
if login_button is not None and username_input is not None:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
login_button.click(
|
| 161 |
fn=lambda username: perform_login(
|
| 162 |
auth_service,
|
|
|
|
| 167 |
outputs=[session_output, error_message],
|
| 168 |
)
|
| 169 |
|
| 170 |
+
return session_output, error_message, login_action
|
src/ui/theme.py
CHANGED
|
@@ -1,3 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
APP_CSS = """
|
| 2 |
:root {
|
| 3 |
--bn-bg: #f5f7fb;
|
|
|
|
| 1 |
+
CRITICAL_HEAD_HTML = """
|
| 2 |
+
<style>
|
| 3 |
+
:root {
|
| 4 |
+
--bn-bg: #f5f7fb;
|
| 5 |
+
}
|
| 6 |
+
|
| 7 |
+
html,
|
| 8 |
+
body,
|
| 9 |
+
gradio-app,
|
| 10 |
+
main.app,
|
| 11 |
+
.wrap,
|
| 12 |
+
.contain,
|
| 13 |
+
.gradio-container {
|
| 14 |
+
background: var(--bn-bg) !important;
|
| 15 |
+
}
|
| 16 |
+
|
| 17 |
+
.gradio-container {
|
| 18 |
+
box-sizing: border-box !important;
|
| 19 |
+
width: calc(100% - 32px) !important;
|
| 20 |
+
max-width: 1540px !important;
|
| 21 |
+
margin-left: auto !important;
|
| 22 |
+
margin-right: auto !important;
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
.bn-shell {
|
| 26 |
+
box-sizing: border-box !important;
|
| 27 |
+
width: 100% !important;
|
| 28 |
+
max-width: 1540px !important;
|
| 29 |
+
margin-left: auto !important;
|
| 30 |
+
margin-right: auto !important;
|
| 31 |
+
}
|
| 32 |
+
|
| 33 |
+
.bn-app-header,
|
| 34 |
+
.bn-tabs,
|
| 35 |
+
.bn-panel,
|
| 36 |
+
.bn-login-row {
|
| 37 |
+
box-sizing: border-box !important;
|
| 38 |
+
width: 100% !important;
|
| 39 |
+
max-width: 1490px !important;
|
| 40 |
+
margin-left: auto !important;
|
| 41 |
+
margin-right: auto !important;
|
| 42 |
+
}
|
| 43 |
+
|
| 44 |
+
.bn-login-row {
|
| 45 |
+
justify-content: center !important;
|
| 46 |
+
}
|
| 47 |
+
|
| 48 |
+
.bn-login-panel {
|
| 49 |
+
box-sizing: border-box !important;
|
| 50 |
+
flex: 0 1 680px !important;
|
| 51 |
+
max-width: 680px !important;
|
| 52 |
+
min-width: 0 !important;
|
| 53 |
+
margin-left: auto !important;
|
| 54 |
+
margin-right: auto !important;
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
.bn-oauth-login-html {
|
| 58 |
+
box-sizing: border-box !important;
|
| 59 |
+
width: 100% !important;
|
| 60 |
+
max-width: 680px !important;
|
| 61 |
+
margin-left: auto !important;
|
| 62 |
+
margin-right: auto !important;
|
| 63 |
+
}
|
| 64 |
+
</style>
|
| 65 |
+
"""
|
| 66 |
+
|
| 67 |
+
|
| 68 |
APP_CSS = """
|
| 69 |
:root {
|
| 70 |
--bn-bg: #f5f7fb;
|
tests/unit/test_login_page.py
CHANGED
|
@@ -2,7 +2,7 @@ import gradio as gr
|
|
| 2 |
|
| 3 |
from src.auth.auth_service import AuthService
|
| 4 |
from src.domain.models import Role
|
| 5 |
-
from src.ui.login_page import create_login_page, perform_login, perform_oauth_login
|
| 6 |
|
| 7 |
|
| 8 |
def test_perform_login_blocks_username_when_disabled() -> None:
|
|
@@ -68,3 +68,14 @@ def test_oauth_login_page_registers_gradio_oauth_routes_without_exposing_native_
|
|
| 68 |
create_login_page(auth_service, enable_oauth_login=True, admin_storage_mode=True)
|
| 69 |
|
| 70 |
assert demo.expects_oauth is True
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
from src.auth.auth_service import AuthService
|
| 4 |
from src.domain.models import Role
|
| 5 |
+
from src.ui.login_page import create_login_page, oauth_action_button_html, perform_login, perform_oauth_login
|
| 6 |
|
| 7 |
|
| 8 |
def test_perform_login_blocks_username_when_disabled() -> None:
|
|
|
|
| 68 |
create_login_page(auth_service, enable_oauth_login=True, admin_storage_mode=True)
|
| 69 |
|
| 70 |
assert demo.expects_oauth is True
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def test_oauth_action_button_html_has_fixed_width_and_intent_route() -> None:
|
| 74 |
+
signed_out = oauth_action_button_html(signed_in=False)
|
| 75 |
+
signed_in = oauth_action_button_html(signed_in=True)
|
| 76 |
+
|
| 77 |
+
assert "max-width:680px" in signed_out
|
| 78 |
+
assert "/login/huggingface?birdnet_login_intent=1" in signed_out
|
| 79 |
+
assert "birdnet_hf_login_intent" in signed_out
|
| 80 |
+
assert "Sign out" in signed_in
|
| 81 |
+
assert "/logout" in signed_in
|