From d36c3e92ab4f10c99829c9350cf4671b0950ca7f Mon Sep 17 00:00:00 2001 From: Sharang Parnerkar <30073382+mighty840@users.noreply.github.com> Date: Thu, 24 Sep 2026 13:49:12 +0200 Subject: [PATCH] feat(dashboard): a link back to the platform portal The sidebar links to the customer's workspace in the portal (//dashboard). PORTAL_URL is read at runtime, since staging and production share an image; the slug comes from the access token's tenant_slug claim at login and is kept in the session. Without PORTAL_URL the link is hidden. Co-Authored-By: Claude Opus 5.5 (1M context) Claude-Session: https://claude.ai/code/session_01EYKjxacrr6K88DruUmP48p --- .env.example | 5 ++ compliance-core/src/models/auth.rs | 4 + compliance-dashboard/assets/main.css | 14 +++ .../src/components/sidebar.rs | 14 +++ compliance-dashboard/src/i18n/texts.rs | 4 + .../src/infrastructure/auth.rs | 4 +- .../src/infrastructure/auth_check.rs | 4 +- .../src/infrastructure/server.rs | 6 ++ .../src/infrastructure/server_state.rs | 4 + .../src/infrastructure/user_state.rs | 85 +++++++++++++++++++ 10 files changed, 142 insertions(+), 2 deletions(-) diff --git a/.env.example b/.env.example index c184674..6005bd3 100644 --- a/.env.example +++ b/.env.example @@ -104,6 +104,11 @@ AGENT_API_URL=http://localhost:3001 # with `openssl rand -hex 64`. Required in staging/production; unset in dev # means sessions end when the dashboard restarts. DASHBOARD_SESSION_KEY= +# The platform portal, for the sidebar's "Zur Plattform" link to the user's +# workspace (//dashboard). Staging: +# https://app-stage.breakpilot.com, production: https://app.breakpilot.com. +# Unset hides the link. +# PORTAL_URL= # MCP Server MCP_ENDPOINT_URL=http://localhost:8090 diff --git a/compliance-core/src/models/auth.rs b/compliance-core/src/models/auth.rs index b0c935b..a783bc1 100644 --- a/compliance-core/src/models/auth.rs +++ b/compliance-core/src/models/auth.rs @@ -11,4 +11,8 @@ pub struct AuthInfo { pub email: String, pub name: String, pub avatar_url: String, + /// The link back to the platform portal (the workspace's dashboard + /// there); empty when no portal is configured. + #[serde(default)] + pub portal_url: String, } diff --git a/compliance-dashboard/assets/main.css b/compliance-dashboard/assets/main.css index 400ab72..ef4e557 100644 --- a/compliance-dashboard/assets/main.css +++ b/compliance-dashboard/assets/main.css @@ -278,6 +278,20 @@ code { gap: 9px; } +/* Back to the platform portal: above the product's own navigation. */ +.nav-item.portal-link { + margin: 8px 8px 0; + font-family: var(--font-mono); + font-size: 11px; + letter-spacing: 0.04em; + text-transform: uppercase; + color: var(--text-faint); +} + +.nav-item.portal-link:hover { + color: var(--accent); +} + /* Breakpilot mark + wordmark */ .brand-mark { width: 26px; diff --git a/compliance-dashboard/src/components/sidebar.rs b/compliance-dashboard/src/components/sidebar.rs index f2a1b34..4b66b79 100644 --- a/compliance-dashboard/src/components/sidebar.rs +++ b/compliance-dashboard/src/components/sidebar.rs @@ -92,6 +92,9 @@ pub fn Sidebar() -> Element { // With the trailing slash: the docs route matches /docs/*, and a plain // /docs lands on the dashboard's "route not found" page. let docs_url = option_env!("DOCS_URL").unwrap_or("/docs/"); + // Back to the customer's workspace in the platform portal; hidden when + // the dashboard is not configured with one (local development). + let portal_url = use_context::>()().portal_url; let sidebar_class = if collapsed() { "sidebar collapsed" @@ -112,6 +115,17 @@ pub fn Sidebar() -> Element { } } } + if !portal_url.is_empty() { + a { + href: "{portal_url}", + class: "nav-item portal-link", + title: nav::PORTAL.get(lang), + Icon { icon: BsArrowLeft, width: 18, height: 18 } + if !collapsed() { + span { {nav::PORTAL.get(lang)} } + } + } + } div { class: "sidebar-nav", for item in nav_items { { diff --git a/compliance-dashboard/src/i18n/texts.rs b/compliance-dashboard/src/i18n/texts.rs index ff3a0e8..1a59358 100644 --- a/compliance-dashboard/src/i18n/texts.rs +++ b/compliance-dashboard/src/i18n/texts.rs @@ -71,6 +71,10 @@ pub mod nav { de: "Pentest", en: "Pentest", }; + pub const PORTAL: Text = Text { + de: "Zur Plattform", + en: "Back to the platform", + }; pub const DOCS: Text = Text { de: "Dokumentation", en: "Docs", diff --git a/compliance-dashboard/src/infrastructure/auth.rs b/compliance-dashboard/src/infrastructure/auth.rs index 131ae1c..a0c2f2c 100644 --- a/compliance-dashboard/src/infrastructure/auth.rs +++ b/compliance-dashboard/src/infrastructure/auth.rs @@ -16,7 +16,7 @@ use url::Url; use super::{ error::DashboardError, server_state::ServerState, - user_state::{User, UserStateInner}, + user_state::{tenant_slug_from_token, User, UserStateInner}, }; pub const LOGGED_IN_USER_SESS_KEY: &str = "logged-in-user"; @@ -240,8 +240,10 @@ pub async fn auth_callback( .or(userinfo.preferred_username) .unwrap_or_default(); + let tenant_slug = tenant_slug_from_token(&tokens.access_token); let user_state = UserStateInner { sub: userinfo.sub, + tenant_slug, access_token: tokens.access_token, refresh_token: tokens.refresh_token.unwrap_or_default(), user: User { diff --git a/compliance-dashboard/src/infrastructure/auth_check.rs b/compliance-dashboard/src/infrastructure/auth_check.rs index 067f68c..8767c28 100644 --- a/compliance-dashboard/src/infrastructure/auth_check.rs +++ b/compliance-dashboard/src/infrastructure/auth_check.rs @@ -10,7 +10,7 @@ use dioxus::prelude::*; pub async fn check_auth() -> Result { use super::auth::LOGGED_IN_USER_SESS_KEY; use super::server_state::ServerState; - use super::user_state::UserStateInner; + use super::user_state::{portal_link, UserStateInner}; use dioxus_fullstack::FullstackContext; let state: ServerState = FullstackContext::extract().await?; @@ -20,6 +20,7 @@ pub async fn check_auth() -> Result { return Ok(AuthInfo { authenticated: true, name: "Local User".into(), + portal_url: portal_link(state.portal_url.as_deref(), ""), ..Default::default() }); } @@ -34,6 +35,7 @@ pub async fn check_auth() -> Result { match user_state { Some(u) => Ok(AuthInfo { authenticated: true, + portal_url: portal_link(state.portal_url.as_deref(), &u.tenant_slug), sub: u.sub, email: u.user.email, name: u.user.name, diff --git a/compliance-dashboard/src/infrastructure/server.rs b/compliance-dashboard/src/infrastructure/server.rs index c3fb46f..e0a6f10 100644 --- a/compliance-dashboard/src/infrastructure/server.rs +++ b/compliance-dashboard/src/infrastructure/server.rs @@ -58,6 +58,11 @@ pub fn server_start(app: fn() -> Element) -> Result<(), DashboardError> { .map(|v| v.trim().to_string()) .filter(|v| !v.is_empty()); + let portal_url = std::env::var("PORTAL_URL") + .ok() + .map(|v| v.trim().to_string()) + .filter(|v| !v.is_empty()); + // Sessions survive a redeploy: signed with a key from the // environment, stored sealed in Mongo (#260). let key = load_session_key(app_env)?; @@ -76,6 +81,7 @@ pub fn server_start(app: fn() -> Element) -> Result<(), DashboardError> { config, keycloak, mcp_health_url, + portal_url, } .into(); diff --git a/compliance-dashboard/src/infrastructure/server_state.rs b/compliance-dashboard/src/infrastructure/server_state.rs index 1124c4c..be96b9e 100644 --- a/compliance-dashboard/src/infrastructure/server_state.rs +++ b/compliance-dashboard/src/infrastructure/server_state.rs @@ -25,6 +25,10 @@ pub struct ServerStateInner { /// probe derives the URL from each server's endpoint (see /// `infrastructure::mcp::mcp_health_url_candidates`). pub mcp_health_url: Option, + /// The platform portal (`PORTAL_URL`), for the link back to it. It + /// differs between staging and production, which share one image, so it + /// is read at runtime. `None` hides the link. + pub portal_url: Option, } impl From for ServerState { diff --git a/compliance-dashboard/src/infrastructure/user_state.rs b/compliance-dashboard/src/infrastructure/user_state.rs index 320f8b1..ee63c94 100644 --- a/compliance-dashboard/src/infrastructure/user_state.rs +++ b/compliance-dashboard/src/infrastructure/user_state.rs @@ -7,6 +7,10 @@ pub struct UserStateInner { pub access_token: String, pub refresh_token: String, pub user: User, + /// The workspace's slug (`tenant_slug` claim), for the link back to the + /// portal. Empty in sessions from before it was stored. + #[serde(default)] + pub tenant_slug: String, } /// Basic user profile stored alongside the session. @@ -16,3 +20,84 @@ pub struct User { pub name: String, pub avatar_url: String, } + +/// The `tenant_slug` claim of a Keycloak access token, or `""` without one. +/// +/// The signature is not checked: the token comes straight from Keycloak's +/// token endpoint, and the slug only builds a link. +pub fn tenant_slug_from_token(access_token: &str) -> String { + use base64::Engine; + + access_token + .split('.') + .nth(1) + .and_then(|payload| { + base64::engine::general_purpose::URL_SAFE_NO_PAD + .decode(payload.trim_end_matches('=')) + .ok() + }) + .and_then(|json| serde_json::from_slice::(&json).ok()) + .and_then(|claims| claims.get("tenant_slug")?.as_str().map(str::to_owned)) + .unwrap_or_default() +} + +/// The link back to the platform: the workspace's portal dashboard, or the +/// portal's start page when the slug is unknown. Empty without a portal URL. +/// +/// # Arguments +/// +/// * `portal_url` - the portal's base URL (`PORTAL_URL`), if configured +/// * `tenant_slug` - the workspace's slug; used only if it is a plain slug +pub fn portal_link(portal_url: Option<&str>, tenant_slug: &str) -> String { + let Some(base) = portal_url.map(|u| u.trim_end_matches('/')) else { + return String::new(); + }; + // Slugs are lowercase letters, digits and dashes; anything else would + // change the path, so the start page is the safe fallback. + let plain = !tenant_slug.is_empty() + && tenant_slug + .bytes() + .all(|b| b.is_ascii_lowercase() || b.is_ascii_digit() || b == b'-'); + if plain { + format!("{base}/{tenant_slug}/dashboard") + } else { + format!("{base}/") + } +} + +#[cfg(test)] +mod tests { + use super::*; + use base64::Engine; + + fn token(claims: &serde_json::Value) -> String { + let payload = base64::engine::general_purpose::URL_SAFE_NO_PAD.encode(claims.to_string()); + format!("eyJhbGciOiJSUzI1NiJ9.{payload}.signature") + } + + #[test] + fn the_slug_is_read_from_the_access_token() { + let with = token(&serde_json::json!({ "sub": "u1", "tenant_slug": "faller" })); + let without = token(&serde_json::json!({ "sub": "u1" })); + + assert_eq!(tenant_slug_from_token(&with), "faller"); + assert_eq!(tenant_slug_from_token(&without), ""); + assert_eq!(tenant_slug_from_token("not-a-jwt"), ""); + } + + #[test] + fn the_link_goes_to_the_workspace_when_the_slug_is_plain() { + let portal = Some("https://app-stage.breakpilot.com/"); + + assert_eq!( + portal_link(portal, "faller-gmbh"), + "https://app-stage.breakpilot.com/faller-gmbh/dashboard" + ); + assert_eq!(portal_link(portal, ""), "https://app-stage.breakpilot.com/"); + assert_eq!( + portal_link(portal, "../admin"), + "https://app-stage.breakpilot.com/" + ); + assert_eq!(portal_link(None, "faller"), ""); + } +}