Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ The format follows [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), and

### Added

- Added Hub web sign-in for browser apps on `PANDAR_CORS_ALLOWED_ORIGINS` origins: the `/web-sign-in` page, `POST /api/v1/tenants/{tenant_id}/web/login-tickets`, and the CORS-enabled `POST /api/v1/web/login-tickets/exchange`, which trades a one-use PKCE ticket for a 30-day `web:session` tenant token. Callbacks must be `http(s)` URLs without userinfo, query, or fragment on an allowlisted origin; with no allowlist, web sign-in is refused.
- Added an opt-in Hub CORS allowlist, `PANDAR_CORS_ALLOWED_ORIGINS`, so browser apps on listed origins (for example the Ares slicer web app) can call the Bearer-token client API under `/api/v1/me` and `/api/v1/tenants/...`. Only exact `http(s)` origins are accepted, wildcards and paths fail startup, credentials are never allowed, and unset keeps the previous no-CORS behavior.

## [0.2.2] - 2026-09-07
Expand Down
129 changes: 129 additions & 0 deletions contracts/hub-client.openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,47 @@
}
}
},
"/api/v1/tenants/{tenant_id}/web/login-tickets": {
"parameters": [
{
"$ref": "#/components/parameters/TenantId"
}
],
"post": {
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/WebLoginTicketRequest"
}
}
}
},
"responses": {
"201": {
"description": "Created web ticket",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/TicketResponse"
}
}
}
},
"default": {
"description": "Hub API error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ErrorResponse"
}
}
}
}
}
}
},
"/api/v1/tenants/{tenant_id}/artifact-metadata-preview": {
"parameters": [
{
Expand Down Expand Up @@ -914,6 +955,42 @@
}
}
}
},
"/api/v1/web/login-tickets/exchange": {
"post": {
"requestBody": {
"required": true,
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/WebTicketExchangeRequest"
}
}
}
},
"responses": {
"200": {
"description": "Exchanged web login ticket",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/WebTicketExchangeResponse"
}
}
}
},
"default": {
"description": "Hub API error",
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ErrorResponse"
}
}
}
}
}
}
}
},
"components": {
Expand Down Expand Up @@ -3614,6 +3691,58 @@
"x-kotlin-name": "MobileTicketExchangeResponse",
"x-typescript-group": "core"
},
"WebLoginTicketRequest": {
"type": "object",
"required": [
"redirect_url",
"code_challenge"
],
"properties": {
"redirect_url": {
"type": "string"
},
"code_challenge": {
"type": "string"
}
},
"x-typescript-group": "core"
},
"WebTicketExchangeRequest": {
"type": "object",
"required": [
"ticket",
"code_verifier"
],
"properties": {
"ticket": {
"type": "string"
},
"code_verifier": {
"type": "string"
}
},
"x-typescript-group": "core"
},
"WebTicketExchangeResponse": {
"type": "object",
"required": [
"token",
"expires_at",
"profile"
],
"properties": {
"token": {
"type": "string"
},
"expires_at": {
"type": "string"
},
"profile": {
"$ref": "#/components/schemas/MobileAuthProfile"
}
},
"x-typescript-group": "core"
},
"PrinterAxis": {
"type": "string",
"enum": [
Expand Down
27 changes: 24 additions & 3 deletions crates/pandar-hub/src/repositories/auth/plugin_tickets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ use time::{Duration, OffsetDateTime, format_description::well_known::Rfc3339};

mod mobile;
mod types;
mod web;

pub use types::{
LoginTicketKind, PluginLoginTicket, PluginLoginTicketExchange, PluginLoginTicketWithPlaintext,
Expand All @@ -29,8 +30,10 @@ use crate::{

const PLUGIN_LOGIN_TICKET_PREFIX: &str = "pandar_plugin_ticket_";
const MOBILE_LOGIN_TICKET_PREFIX: &str = "pandar_mobile_ticket_";
const WEB_LOGIN_TICKET_PREFIX: &str = "pandar_web_ticket_";
const PLUGIN_TOKEN_SCOPE: &str = "plugin:studio";
const MOBILE_TOKEN_SCOPE: &str = "mobile:session";
const WEB_TOKEN_SCOPE: &str = "web:session";
const PLUGIN_TOKEN_TTL_DAYS: i64 = 30;

#[derive(Serialize)]
Expand Down Expand Up @@ -133,8 +136,8 @@ impl AuthRepository {
if plugin_login_ticket_expired(&ticket)? {
return Ok(None);
}
if expected_kind == LoginTicketKind::Mobile
&& !mobile_pkce_matches(&ticket, code_verifier.unwrap_or_default())
if expected_kind != LoginTicketKind::Plugin
&& !pkce_matches(&ticket, code_verifier.unwrap_or_default())
{
return Ok(None);
}
Expand Down Expand Up @@ -181,6 +184,16 @@ impl AuthRepository {
)
.await?
}
LoginTicketKind::Web => {
AuthRepository::create_web_token_from_ticket_tx(
&tx,
used_ticket.tenant_id,
"Ares web",
used_ticket.user_id.clone(),
token_expires_at,
)
.await?
}
};
let (event_name, actor) = match expected_kind {
LoginTicketKind::Plugin => (
Expand All @@ -199,6 +212,14 @@ impl AuthRepository {
vec![MOBILE_TOKEN_SCOPE],
),
),
LoginTicketKind::Web => (
"web_login_ticket.exchange",
AuditActor::tenant_token(
used_ticket.user_id.clone(),
tenant_token.token.id.clone(),
vec![WEB_TOKEN_SCOPE],
),
),
};
insert_audit_event_tx(
&tx,
Expand Down Expand Up @@ -325,7 +346,7 @@ fn plugin_login_ticket_model(
}
}

fn mobile_pkce_matches(ticket: &PluginLoginTicket, code_verifier: &str) -> bool {
fn pkce_matches(ticket: &PluginLoginTicket, code_verifier: &str) -> bool {
if !(43..=128).contains(&code_verifier.len())
|| !code_verifier
.bytes()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,23 @@ pub struct PluginLoginTicket {
pub enum LoginTicketKind {
Plugin,
Mobile,
Web,
}

impl LoginTicketKind {
pub(super) fn as_str(self) -> &'static str {
match self {
Self::Plugin => "plugin",
Self::Mobile => "mobile",
Self::Web => "web",
}
}

pub(super) fn parse(value: &str) -> RepositoryResult<Self> {
match value {
"plugin" => Ok(Self::Plugin),
"mobile" => Ok(Self::Mobile),
"web" => Ok(Self::Web),
other => Err(anyhow::anyhow!("invalid persisted login ticket kind {other}").into()),
}
}
Expand Down
99 changes: 99 additions & 0 deletions crates/pandar-hub/src/repositories/auth/plugin_tickets/web.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
use anyhow::Context;
use axum::http::HeaderValue;
use pandar_core::{TenantId, created_at_now};
use sea_orm::TransactionTrait;

use super::{
LoginTicketKind, PluginLoginTicket, PluginLoginTicketExchange, PluginLoginTicketWithPlaintext,
WEB_LOGIN_TICKET_PREFIX, insert_plugin_login_ticket, plugin_login_ticket_audit_event,
};
use crate::repositories::{
AuditActor, AuthRepository, RepositoryError, RepositoryResult,
audit::insert_audit_event_tx,
auth::{hash_token, secrets::generate_secret},
};

impl AuthRepository {
#[allow(clippy::too_many_arguments)]
pub async fn create_web_login_ticket_with_audit(
&self,
tenant_id: TenantId,
user_id: Option<String>,
redirect_url: impl AsRef<str>,
allowed_origins: &[HeaderValue],
code_challenge: String,
expires_at: String,
actor: AuditActor,
) -> RepositoryResult<PluginLoginTicketWithPlaintext> {
let redirect_url =
self.validate_web_redirect_url(redirect_url.as_ref(), allowed_origins)?;
let plaintext_ticket = generate_secret(WEB_LOGIN_TICKET_PREFIX);
let ticket = PluginLoginTicket {
id: uuid::Uuid::new_v4().to_string(),
tenant_id,
user_id,
redirect_url,
kind: LoginTicketKind::Web,
code_challenge: Some(code_challenge),
created_at: created_at_now(),
expires_at,
used_at: None,
revoked_at: None,
};
let ticket_hash = hash_token(&plaintext_ticket);

let connection = self.database.sea_orm_connection();
let tx = connection
.begin()
.await
.context("failed to begin web login ticket create transaction")?;
insert_plugin_login_ticket(&tx, &ticket, &ticket_hash).await?;
insert_audit_event_tx(
&tx,
&plugin_login_ticket_audit_event(&ticket, "web_login_ticket.create", actor, None),
)
.await?;
tx.commit()
.await
.context("failed to commit web login ticket create transaction")?;

Ok(PluginLoginTicketWithPlaintext {
ticket,
plaintext_ticket,
})
}

pub async fn exchange_web_login_ticket(
&self,
plaintext_ticket: &str,
code_verifier: &str,
) -> RepositoryResult<Option<PluginLoginTicketExchange>> {
self.exchange_login_ticket(plaintext_ticket, LoginTicketKind::Web, Some(code_verifier))
.await
}

/// Accepts only http(s) callbacks whose origin is listed in `PANDAR_CORS_ALLOWED_ORIGINS`.
pub fn validate_web_redirect_url(
&self,
redirect_url: impl AsRef<str>,
allowed_origins: &[HeaderValue],
) -> RepositoryResult<String> {
let redirect_url = redirect_url.as_ref();
let url = reqwest::Url::parse(redirect_url)
.map_err(|_| RepositoryError::InvalidPluginRedirectUrl)?;
let origin = url.origin().ascii_serialization();
if !matches!(url.scheme(), "http" | "https")
|| !url.username().is_empty()
|| url.password().is_some()
|| url.query().is_some()
|| url.fragment().is_some()
|| !allowed_origins
.iter()
.any(|allowed| allowed.as_bytes() == origin.as_bytes())
{
return Err(RepositoryError::InvalidPluginRedirectUrl);
}

Ok(redirect_url.to_owned())
}
}
4 changes: 3 additions & 1 deletion crates/pandar-hub/src/repositories/auth/tenant_tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ mod mobile;
mod no_auth_session;
mod revocation;
mod types;
mod web;

pub use types::{
AuthenticatedTenantToken, TenantToken, TenantTokenScope, TenantTokenWithPlaintext,
Expand All @@ -32,6 +33,7 @@ use helpers::{is_expired, tenant_token_audit_event, tenant_token_model};
const TENANT_TOKEN_PREFIX: &str = "pandar_tenant_";
const PLUGIN_TOKEN_PREFIX: &str = "pandar_plugin_";
const MOBILE_TOKEN_PREFIX: &str = "pandar_mobile_";
const WEB_TOKEN_PREFIX: &str = "pandar_web_";

impl AuthRepository {
pub async fn create_tenant_token_with_audit(
Expand Down Expand Up @@ -120,7 +122,7 @@ impl AuthRepository {
.await
.context("failed to update tenant token last_used_at")?;

let session_user = if token.scopes.contains(&TenantTokenScope::MobileSession)
let session_user = if token.scopes.iter().any(|scope| scope.is_user_session())
|| token.created_by_user_id.is_some()
&& token.scopes.contains(&TenantTokenScope::PluginStudio)
{
Expand Down
Loading