From ec5a68a98290c1a73ac81027057a2aa25999666d Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar Date: Tue, 29 Sep 2026 08:23:35 +0000 Subject: [PATCH] fix(dashboard): staff sign in directly, and old history entries read as people (#426) --- compliance-dashboard/src/i18n/texts.rs | 5 ++ .../src/infrastructure/auth.rs | 27 +++++++ .../src/infrastructure/keycloak_config.rs | 8 +++ .../src/infrastructure/mod.rs | 2 +- .../src/infrastructure/server.rs | 3 +- .../src/pages/finding_detail.rs | 70 ++++++++++++++++++- 6 files changed, 112 insertions(+), 3 deletions(-) diff --git a/compliance-dashboard/src/i18n/texts.rs b/compliance-dashboard/src/i18n/texts.rs index 3835e1d..d671f5a 100644 --- a/compliance-dashboard/src/i18n/texts.rs +++ b/compliance-dashboard/src/i18n/texts.rs @@ -1653,6 +1653,11 @@ pub mod findings { de: "Verlauf", en: "History", }; + /// A user of the tenant whose name is not known. + pub const SOMEONE: Text = Text { + de: "Benutzer", + en: "User", + }; pub const REASON: Text = Text { de: "Begründung", en: "Reason", diff --git a/compliance-dashboard/src/infrastructure/auth.rs b/compliance-dashboard/src/infrastructure/auth.rs index c7a671d..50d2b64 100644 --- a/compliance-dashboard/src/infrastructure/auth.rs +++ b/compliance-dashboard/src/infrastructure/auth.rs @@ -126,6 +126,30 @@ pub async fn auth_login( Extension(pending): Extension, Query(params): Query>, ) -> Result { + start_login(state, &pending, ¶ms, false) +} + +/// `GET /auth/staff`: the sign-in for BreakPilot staff, straight to the +/// company login (`kc_idp_hint`, as the portal's staff sign-in does). The +/// realm's own login form does not offer it. Without `STAFF_IDP_HINT` this +/// is the ordinary sign-in. +#[axum::debug_handler] +pub async fn auth_staff_login( + Extension(state): Extension, + Extension(pending): Extension, + Query(params): Query>, +) -> Result { + start_login(state, &pending, ¶ms, true) +} + +/// Send the browser to Keycloak's login; with `staff`, to the staff +/// identity provider when one is configured. +fn start_login( + state: ServerState, + pending: &PendingOAuthStore, + params: &HashMap, + staff: bool, +) -> Result { let kc = state .keycloak .ok_or(DashboardError::Other("Keycloak not configured".into()))?; @@ -157,6 +181,9 @@ pub async fn auth_login( .append_pair("state", &csrf_state) .append_pair("code_challenge", &code_challenge) .append_pair("code_challenge_method", "S256"); + if let Some(hint) = kc.staff_idp_hint.as_deref().filter(|_| staff) { + url.query_pairs_mut().append_pair("kc_idp_hint", hint); + } Ok(Redirect::temporary(url.as_str())) } diff --git a/compliance-dashboard/src/infrastructure/keycloak_config.rs b/compliance-dashboard/src/infrastructure/keycloak_config.rs index 476b264..7a9371d 100644 --- a/compliance-dashboard/src/infrastructure/keycloak_config.rs +++ b/compliance-dashboard/src/infrastructure/keycloak_config.rs @@ -6,6 +6,9 @@ pub struct KeycloakConfig { pub client_id: String, pub redirect_uri: String, pub app_url: String, + /// The identity provider BreakPilot staff sign in with (`STAFF_IDP_HINT`, + /// as for the portal); `/auth/staff` goes straight to it. + pub staff_idp_hint: Option, } impl KeycloakConfig { @@ -15,12 +18,17 @@ impl KeycloakConfig { let client_id = std::env::var("KEYCLOAK_CLIENT_ID").ok()?; let redirect_uri = std::env::var("REDIRECT_URI").ok()?; let app_url = std::env::var("APP_URL").ok()?; + let staff_idp_hint = std::env::var("STAFF_IDP_HINT") + .ok() + .map(|h| h.trim().to_string()) + .filter(|h| !h.is_empty()); Some(Self { url, realm, client_id, redirect_uri, app_url, + staff_idp_hint, }) } diff --git a/compliance-dashboard/src/infrastructure/mod.rs b/compliance-dashboard/src/infrastructure/mod.rs index 8147b90..d873311 100644 --- a/compliance-dashboard/src/infrastructure/mod.rs +++ b/compliance-dashboard/src/infrastructure/mod.rs @@ -46,7 +46,7 @@ mod session_store; mod user_state; #[cfg(feature = "server")] -pub use auth::{auth_callback, auth_login, logout, PendingOAuthStore}; +pub use auth::{auth_callback, auth_login, auth_staff_login, logout, PendingOAuthStore}; #[cfg(feature = "server")] pub use auth_middleware::require_auth; #[cfg(feature = "server")] diff --git a/compliance-dashboard/src/infrastructure/server.rs b/compliance-dashboard/src/infrastructure/server.rs index 5c4975f..db5e14c 100644 --- a/compliance-dashboard/src/infrastructure/server.rs +++ b/compliance-dashboard/src/infrastructure/server.rs @@ -15,7 +15,7 @@ use super::error::DashboardError; use super::keycloak_config::KeycloakConfig; use super::server_state::{ServerState, ServerStateInner}; use super::session_store::{load_session_key, MongoSessionStore}; -use super::{auth_callback, auth_login, logout, require_auth, PendingOAuthStore}; +use super::{auth_callback, auth_login, auth_staff_login, logout, require_auth, PendingOAuthStore}; /// The docs site when `DOCS_URL` is unset: served on the dashboard host. /// Keep the trailing slash: the docs route matches `/docs/*`, and a plain @@ -118,6 +118,7 @@ pub fn server_start(app: fn() -> Element) -> Result<(), DashboardError> { get(|| async { "User-agent: *\nDisallow: /\n" }), ) .route("/auth", get(auth_login)) + .route("/auth/staff", get(auth_staff_login)) .route("/auth/callback", get(auth_callback)) .route("/logout", get(logout)) .serve_dioxus_application(ServeConfig::new(), app) diff --git a/compliance-dashboard/src/pages/finding_detail.rs b/compliance-dashboard/src/pages/finding_detail.rs index b5f1a79..d74b1c2 100644 --- a/compliance-dashboard/src/pages/finding_detail.rs +++ b/compliance-dashboard/src/pages/finding_detail.rs @@ -24,6 +24,7 @@ enum SaveState { #[component] pub fn FindingDetailPage(id: String) -> Element { let lang = use_lang()(); + let me = use_context::>()(); let finding_id = id.clone(); let mut status_save = use_signal(|| SaveState::Idle); let mut feedback_save = use_signal(|| SaveState::Idle); @@ -334,7 +335,7 @@ pub fn FindingDetailPage(id: String) -> Element { strong { {tx::status(&change.status).get(lang)} } " · " span { class: if change.support { "history-by history-by-support" } else { "history-by" }, - {change.by_name.as_deref().unwrap_or(&change.by)} + {changed_by(change, &me, lang)} } " · {format_time(&change.at, lang)}" if let Some(reason) = &change.reason { @@ -437,3 +438,70 @@ fn CatalogControls(ids: Vec) -> Element { } } } + +/// Who made a status change, in words. Changes from before names were +/// recorded carry only a user id: the reader's own reads as their name, +/// anyone else's as "Benutzer" and the start of the id. +fn changed_by( + change: &compliance_core::models::finding::StatusChange, + me: &compliance_core::models::AuthInfo, + lang: crate::i18n::Lang, +) -> String { + if let Some(name) = change.by_name.as_deref().filter(|n| !n.trim().is_empty()) { + return name.to_string(); + } + if !me.sub.is_empty() && change.by == me.sub && !me.name.trim().is_empty() { + return me.name.clone(); + } + let short: String = change.by.chars().take(8).collect(); + format!("{} {short}", tx::SOMEONE.get(lang)) +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::i18n::Lang; + use compliance_core::models::{AuthInfo, StatusChange}; + + fn change(by: &str, by_name: Option<&str>) -> StatusChange { + StatusChange { + status: FindingStatus::FalsePositive, + by: by.to_string(), + by_name: by_name.map(String::from), + support: false, + at: chrono::Utc::now(), + reason: None, + } + } + + fn me() -> AuthInfo { + AuthInfo { + authenticated: true, + sub: "14e9a93b-3abb-4358-b5d8-c34a0f800a16".to_string(), + name: "Sharang Parnerkar".to_string(), + ..Default::default() + } + } + + #[test] + fn a_recorded_name_is_shown() { + let c = change("x", Some("BreakPilot-Support (Sharang)")); + assert_eq!( + changed_by(&c, &me(), Lang::De), + "BreakPilot-Support (Sharang)" + ); + } + + #[test] + fn an_old_change_of_ones_own_shows_ones_name() { + let c = change("14e9a93b-3abb-4358-b5d8-c34a0f800a16", None); + assert_eq!(changed_by(&c, &me(), Lang::De), "Sharang Parnerkar"); + } + + #[test] + fn an_old_change_of_someone_else_shows_a_short_id() { + let c = change("0b7d51aa-9f1c-4e33-8a1f-2d8c1e0f9a77", None); + assert_eq!(changed_by(&c, &me(), Lang::De), "Benutzer 0b7d51aa"); + assert_eq!(changed_by(&c, &me(), Lang::En), "User 0b7d51aa"); + } +}