use std::collections::BTreeMap; use base64::{Engine as _, engine::general_purpose::STANDARD}; use crank_core::{AuthConfig, AuthProfile, SecretId}; use serde_json::Value; use crate::{PreparedRequest, RuntimeError}; #[derive(Clone, PartialEq, Eq)] pub enum ResolvedAuth { Bearer { header_name: String, token: String }, Basic { username: String, password: String }, ApiKeyHeader { header_name: String, value: String }, ApiKeyQuery { param_name: String, value: String }, } impl std::fmt::Debug for ResolvedAuth { fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let kind = match self { Self::Bearer { .. } => "bearer", Self::Basic { .. } => "basic", Self::ApiKeyHeader { .. } => "api_key_header", Self::ApiKeyQuery { .. } => "api_key_query", }; formatter .debug_struct("ResolvedAuth") .field("kind", &kind) .field("material", &"[REDACTED]") .finish() } } impl ResolvedAuth { pub fn from_profile( profile: &AuthProfile, secrets: &BTreeMap, ) -> Result { if profile.kind != profile.config.kind() { return Err(RuntimeError::InvalidAuthProfileConfig { auth_profile_id: profile.id.as_str().to_owned(), reason: "auth kind and config kind do not match".to_owned(), }); } match &profile.config { AuthConfig::Bearer(config) => Ok(Self::Bearer { header_name: config.header_name.clone(), token: extract_secret_string(secrets, &config.secret_id, &["token", "value"])?, }), AuthConfig::Basic(config) => Ok(Self::Basic { username: extract_secret_string( secrets, &config.username_secret_id, &["username", "value"], )?, password: extract_secret_string( secrets, &config.password_secret_id, &["password", "value"], )?, }), AuthConfig::ApiKeyHeader(config) => Ok(Self::ApiKeyHeader { header_name: config.header_name.clone(), value: extract_secret_string(secrets, &config.secret_id, &["token", "value"])?, }), AuthConfig::ApiKeyQuery(config) => Ok(Self::ApiKeyQuery { param_name: config.param_name.clone(), value: extract_secret_string(secrets, &config.secret_id, &["token", "value"])?, }), } } pub fn apply(&self, prepared_request: &mut PreparedRequest) { match self { Self::Bearer { header_name, token } => { remove_header_case_insensitive(&mut prepared_request.headers, header_name); prepared_request .headers .insert(header_name.clone(), format!("Bearer {token}")); prepared_request .trusted_header_names .insert(header_name.to_ascii_lowercase()); } Self::Basic { username, password } => { let credentials = STANDARD.encode(format!("{username}:{password}")); remove_header_case_insensitive(&mut prepared_request.headers, "Authorization"); prepared_request .headers .insert("Authorization".to_owned(), format!("Basic {credentials}")); prepared_request .trusted_header_names .insert("authorization".to_owned()); } Self::ApiKeyHeader { header_name, value } => { remove_header_case_insensitive(&mut prepared_request.headers, header_name); prepared_request .headers .insert(header_name.clone(), value.clone()); prepared_request .trusted_header_names .insert(header_name.to_ascii_lowercase()); } Self::ApiKeyQuery { param_name, value } => { prepared_request .query_params .insert(param_name.clone(), value.clone()); } } } } fn remove_header_case_insensitive(headers: &mut BTreeMap, header_name: &str) { let existing = headers .keys() .filter(|candidate| candidate.eq_ignore_ascii_case(header_name)) .cloned() .collect::>(); for existing in existing { headers.remove(&existing); } } pub(crate) fn apply_resolved_auth( mut prepared_request: PreparedRequest, resolved_auth: Option<&ResolvedAuth>, ) -> PreparedRequest { if let Some(resolved_auth) = resolved_auth { resolved_auth.apply(&mut prepared_request); } prepared_request } fn extract_secret_string( secrets: &BTreeMap, secret_id: &SecretId, preferred_fields: &[&str], ) -> Result { let Some(value) = secrets.get(secret_id) else { return Err(RuntimeError::MissingSecret { secret_id: secret_id.as_str().to_owned(), }); }; if let Some(text) = value.as_str() { return Ok(text.to_owned()); } let Some(object) = value.as_object() else { return Err(RuntimeError::InvalidAuthSecretValue { secret_id: secret_id.as_str().to_owned(), reason: "secret payload must be a string or object".to_owned(), }); }; for field in preferred_fields { if let Some(text) = object.get(*field).and_then(Value::as_str) { return Ok(text.to_owned()); } } Err(RuntimeError::InvalidAuthSecretValue { secret_id: secret_id.as_str().to_owned(), reason: format!( "secret payload must contain one of: {}", preferred_fields.join(", ") ), }) } #[cfg(test)] mod tests { use std::collections::BTreeMap; use crank_core::{ ApiKeyHeaderAuthConfig, ApiKeyQueryAuthConfig, AuthConfig, AuthKind, AuthProfile, BasicAuthConfig, BearerAuthConfig, SecretId, WorkspaceId, }; use serde_json::json; use time::{OffsetDateTime, format_description::well_known::Rfc3339}; use crate::{PreparedRequest, auth::ResolvedAuth}; fn timestamp(value: &str) -> OffsetDateTime { OffsetDateTime::parse(value, &Rfc3339).unwrap() } #[test] fn applies_bearer_auth_to_headers() { let auth = ResolvedAuth::from_profile( &AuthProfile { id: "auth_01".into(), workspace_id: WorkspaceId::new("ws_default"), name: "bearer".to_owned(), kind: AuthKind::Bearer, config: AuthConfig::Bearer(BearerAuthConfig { header_name: "Authorization".to_owned(), secret_id: SecretId::new("secret_token"), }), created_at: timestamp("2026-04-07T00:00:00Z"), updated_at: timestamp("2026-04-07T00:00:00Z"), }, &BTreeMap::from([(SecretId::new("secret_token"), json!({"token":"abc"}))]), ) .unwrap(); let mut request = PreparedRequest::default(); auth.apply(&mut request); assert_eq!( request.headers.get("Authorization").map(String::as_str), Some("Bearer abc") ); } #[test] fn bearer_auth_overwrites_untrusted_mapped_header_and_marks_it_trusted() { let auth = ResolvedAuth::Bearer { header_name: "Authorization".to_owned(), token: "system-token".to_owned(), }; let mut request = PreparedRequest { headers: BTreeMap::from([ ("authorization".to_owned(), "Bearer attacker".to_owned()), ( "AUTHORIZATION".to_owned(), "Bearer second-attacker".to_owned(), ), ]), ..PreparedRequest::default() }; auth.apply(&mut request); assert_eq!( request.headers.get("Authorization").map(String::as_str), Some("Bearer system-token") ); assert!(!request.headers.contains_key("authorization")); assert!(!request.headers.contains_key("AUTHORIZATION")); assert!(request.trusted_header_names.contains("authorization")); } #[test] fn api_key_header_auth_overwrites_untrusted_mapped_header_and_marks_it_trusted() { let auth = ResolvedAuth::ApiKeyHeader { header_name: "X-Api-Key".to_owned(), value: "system-key".to_owned(), }; let mut request = PreparedRequest { headers: BTreeMap::from([ ("x-api-key".to_owned(), "attacker".to_owned()), ("X-API-KEY".to_owned(), "second-attacker".to_owned()), ]), ..PreparedRequest::default() }; auth.apply(&mut request); assert_eq!( request.headers.get("X-Api-Key").map(String::as_str), Some("system-key") ); assert!(!request.headers.contains_key("x-api-key")); assert!(!request.headers.contains_key("X-API-KEY")); assert!(request.trusted_header_names.contains("x-api-key")); } #[test] fn rejects_auth_profile_kind_config_mismatch_before_secret_use() { let error = ResolvedAuth::from_profile( &AuthProfile { id: "auth_01".into(), workspace_id: WorkspaceId::new("ws_default"), name: "mismatch".to_owned(), kind: AuthKind::Bearer, config: AuthConfig::ApiKeyHeader(ApiKeyHeaderAuthConfig { header_name: "X-Api-Key".to_owned(), secret_id: SecretId::new("secret_api_key"), }), created_at: timestamp("2026-04-07T00:00:00Z"), updated_at: timestamp("2026-04-07T00:00:00Z"), }, &BTreeMap::from([(SecretId::new("secret_api_key"), json!("key-123"))]), ) .unwrap_err(); assert!(matches!( error, crate::RuntimeError::InvalidAuthProfileConfig { .. } )); } #[test] fn resolved_auth_debug_never_exposes_secret_material() { let auth = ResolvedAuth::Bearer { header_name: "Authorization".to_owned(), token: "story18-secret-canary".to_owned(), }; let rendered = format!("{auth:?}"); assert!(!rendered.contains("story18-secret-canary")); assert!(rendered.contains("[REDACTED]")); } #[test] fn applies_basic_auth_to_headers() { let auth = ResolvedAuth::from_profile( &AuthProfile { id: "auth_01".into(), workspace_id: WorkspaceId::new("ws_default"), name: "basic".to_owned(), kind: AuthKind::Basic, config: AuthConfig::Basic(BasicAuthConfig { username_secret_id: SecretId::new("secret_user"), password_secret_id: SecretId::new("secret_pass"), }), created_at: timestamp("2026-04-07T00:00:00Z"), updated_at: timestamp("2026-04-07T00:00:00Z"), }, &BTreeMap::from([ (SecretId::new("secret_user"), json!({"username":"demo"})), (SecretId::new("secret_pass"), json!({"password":"s3cr3t"})), ]), ) .unwrap(); let mut request = PreparedRequest::default(); auth.apply(&mut request); assert_eq!( request.headers.get("Authorization").map(String::as_str), Some("Basic ZGVtbzpzM2NyM3Q=") ); } #[test] fn applies_api_key_header_auth() { let auth = ResolvedAuth::from_profile( &AuthProfile { id: "auth_01".into(), workspace_id: WorkspaceId::new("ws_default"), name: "api-header".to_owned(), kind: AuthKind::ApiKeyHeader, config: AuthConfig::ApiKeyHeader(ApiKeyHeaderAuthConfig { header_name: "X-Api-Key".to_owned(), secret_id: SecretId::new("secret_api_key"), }), created_at: timestamp("2026-04-07T00:00:00Z"), updated_at: timestamp("2026-04-07T00:00:00Z"), }, &BTreeMap::from([(SecretId::new("secret_api_key"), json!("key-123"))]), ) .unwrap(); let mut request = PreparedRequest::default(); auth.apply(&mut request); assert_eq!( request.headers.get("X-Api-Key").map(String::as_str), Some("key-123") ); } #[test] fn applies_api_key_query_auth() { let auth = ResolvedAuth::from_profile( &AuthProfile { id: "auth_01".into(), workspace_id: WorkspaceId::new("ws_default"), name: "api-query".to_owned(), kind: AuthKind::ApiKeyQuery, config: AuthConfig::ApiKeyQuery(ApiKeyQueryAuthConfig { param_name: "api_key".to_owned(), secret_id: SecretId::new("secret_api_key"), }), created_at: timestamp("2026-04-07T00:00:00Z"), updated_at: timestamp("2026-04-07T00:00:00Z"), }, &BTreeMap::from([(SecretId::new("secret_api_key"), json!({"value":"key-123"}))]), ) .unwrap(); let mut request = PreparedRequest::default(); auth.apply(&mut request); assert_eq!( request.query_params.get("api_key").map(String::as_str), Some("key-123") ); } }