fix(dashboard): staff sign in directly, and old history entries read as people #426
No files matched your search
@@ -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",
|
||||
|
||||
@@ -126,6 +126,30 @@ pub async fn auth_login(
|
||||
Extension(pending): Extension<PendingOAuthStore>,
|
||||
Query(params): Query<HashMap<String, String>>,
|
||||
) -> Result<impl IntoResponse, DashboardError> {
|
||||
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<ServerState>,
|
||||
Extension(pending): Extension<PendingOAuthStore>,
|
||||
Query(params): Query<HashMap<String, String>>,
|
||||
) -> Result<impl IntoResponse, DashboardError> {
|
||||
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<String, String>,
|
||||
staff: bool,
|
||||
) -> Result<Redirect, DashboardError> {
|
||||
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()))
|
||||
}
|
||||
|
||||
@@ -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<String>,
|
||||
}
|
||||
|
||||
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,
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -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")]
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -24,6 +24,7 @@ enum SaveState {
|
||||
#[component]
|
||||
pub fn FindingDetailPage(id: String) -> Element {
|
||||
let lang = use_lang()();
|
||||
let me = use_context::<Signal<compliance_core::models::AuthInfo>>()();
|
||||
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<String>) -> 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");
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user