|
| 1 | +use std::fmt; |
| 2 | + |
| 3 | +use a2a_auth_callout::CallerId; |
| 4 | +use a2a_nats::{A2aAgentId, AgentIdError}; |
| 5 | + |
| 6 | +use crate::error::BridgeError; |
| 7 | + |
| 8 | +#[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| 9 | +pub struct MintedCallerId(String); |
| 10 | + |
| 11 | +impl MintedCallerId { |
| 12 | + #[must_use] |
| 13 | + pub fn from_caller_id(id: CallerId) -> Self { |
| 14 | + Self(id.as_str().to_owned()) |
| 15 | + } |
| 16 | + |
| 17 | + pub fn as_str(&self) -> &str { |
| 18 | + &self.0 |
| 19 | + } |
| 20 | +} |
| 21 | + |
| 22 | +#[derive(Clone, PartialEq, Eq, Hash)] |
| 23 | +pub struct CallerHttpsAuth(String); |
| 24 | + |
| 25 | +impl fmt::Debug for CallerHttpsAuth { |
| 26 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 27 | + // Debug must not leak the raw Authorization value either — |
| 28 | + // tracing/log macros use the `?` field syntax which routes |
| 29 | + // through Debug, not Display. |
| 30 | + f.debug_tuple("CallerHttpsAuth").field(&"<redacted>").finish() |
| 31 | + } |
| 32 | +} |
| 33 | + |
| 34 | +impl CallerHttpsAuth { |
| 35 | + pub fn new(raw: impl Into<String>) -> Self { |
| 36 | + Self(raw.into()) |
| 37 | + } |
| 38 | + |
| 39 | + pub fn as_str(&self) -> &str { |
| 40 | + &self.0 |
| 41 | + } |
| 42 | + |
| 43 | + pub fn into_inner(self) -> String { |
| 44 | + self.0 |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +impl fmt::Display for CallerHttpsAuth { |
| 49 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 50 | + // The wrapped value carries the raw `Authorization` header — Bearer |
| 51 | + // tokens, Basic credentials, etc. Redact in Display so accidental |
| 52 | + // tracing/log interpolation can't leak the secret. Callers that |
| 53 | + // genuinely need the value go through `as_str` / `into_inner`. |
| 54 | + f.pad("<redacted>") |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +#[derive(Clone, PartialEq, Eq, Hash)] |
| 59 | +pub struct BridgeUserJwt(String); |
| 60 | + |
| 61 | +impl fmt::Debug for BridgeUserJwt { |
| 62 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 63 | + f.debug_tuple("BridgeUserJwt").field(&"<redacted>").finish() |
| 64 | + } |
| 65 | +} |
| 66 | + |
| 67 | +impl BridgeUserJwt { |
| 68 | + /// Wrap a minted user JWT after validating compact-JWT shape. Mirrors |
| 69 | + /// the gate in `a2a_auth_callout::MintedUserJwt::new` so a malformed |
| 70 | + /// value can't be smuggled past this boundary and only fail later when |
| 71 | + /// the bridge tries to decode it. |
| 72 | + pub fn new(token: impl Into<String>) -> Result<Self, BridgeError> { |
| 73 | + let token = token.into().trim().to_owned(); |
| 74 | + if token.is_empty() { |
| 75 | + return Err(BridgeError::Mint("minted user JWT must be non-empty".into())); |
| 76 | + } |
| 77 | + let parts: Vec<&str> = token.split('.').collect(); |
| 78 | + if parts.len() != 3 || parts.iter().any(|p| p.is_empty()) { |
| 79 | + return Err(BridgeError::Mint( |
| 80 | + "minted user JWT must be a compact 3-segment JWT".into(), |
| 81 | + )); |
| 82 | + } |
| 83 | + Ok(Self(token)) |
| 84 | + } |
| 85 | + |
| 86 | + pub fn as_str(&self) -> &str { |
| 87 | + &self.0 |
| 88 | + } |
| 89 | + |
| 90 | + pub fn into_inner(self) -> String { |
| 91 | + self.0 |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +impl fmt::Display for BridgeUserJwt { |
| 96 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 97 | + f.pad("<redacted>") |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +#[cfg(test)] |
| 102 | +mod tests { |
| 103 | + use super::*; |
| 104 | + |
| 105 | + #[test] |
| 106 | + fn caller_https_auth_display_redacts() { |
| 107 | + let auth = CallerHttpsAuth::new("Bearer secret-token"); |
| 108 | + assert_eq!(format!("{auth}"), "<redacted>"); |
| 109 | + assert_eq!(auth.as_str(), "Bearer secret-token"); |
| 110 | + } |
| 111 | + |
| 112 | + #[test] |
| 113 | + fn bridge_user_jwt_rejects_empty_and_non_three_segment() { |
| 114 | + assert!(BridgeUserJwt::new("").is_err()); |
| 115 | + assert!(BridgeUserJwt::new("a.b").is_err()); |
| 116 | + assert!(BridgeUserJwt::new("a..c").is_err()); |
| 117 | + assert!(BridgeUserJwt::new("a.b.c").is_ok()); |
| 118 | + } |
| 119 | + |
| 120 | + #[test] |
| 121 | + fn bridge_user_jwt_display_redacts() { |
| 122 | + let jwt = BridgeUserJwt::new("h.p.s").expect("valid shape"); |
| 123 | + assert_eq!(format!("{jwt}"), "<redacted>"); |
| 124 | + } |
| 125 | + |
| 126 | + #[test] |
| 127 | + fn debug_does_not_leak_secrets() { |
| 128 | + let auth = CallerHttpsAuth::new("Bearer secret-token"); |
| 129 | + let auth_dbg = format!("{auth:?}"); |
| 130 | + assert!(!auth_dbg.contains("secret-token"), "{auth_dbg}"); |
| 131 | + assert!(auth_dbg.contains("<redacted>"), "{auth_dbg}"); |
| 132 | + |
| 133 | + let jwt = BridgeUserJwt::new("hhh.ppp.sss").expect("valid shape"); |
| 134 | + let jwt_dbg = format!("{jwt:?}"); |
| 135 | + assert!(!jwt_dbg.contains("hhh"), "{jwt_dbg}"); |
| 136 | + assert!(jwt_dbg.contains("<redacted>"), "{jwt_dbg}"); |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +#[derive(Debug, Clone, PartialEq, Eq, Hash)] |
| 141 | +pub struct BridgeAgentId(A2aAgentId); |
| 142 | + |
| 143 | +impl BridgeAgentId { |
| 144 | + pub fn parse(raw: &str) -> Result<Self, BridgeError> { |
| 145 | + A2aAgentId::new(raw) |
| 146 | + .map(Self) |
| 147 | + .map_err(|e: AgentIdError| BridgeError::InvalidAgent(e.to_string())) |
| 148 | + } |
| 149 | + |
| 150 | + #[must_use] |
| 151 | + pub fn as_str(&self) -> &str { |
| 152 | + self.0.as_str() |
| 153 | + } |
| 154 | + |
| 155 | + #[must_use] |
| 156 | + pub fn as_agent_id(&self) -> &A2aAgentId { |
| 157 | + &self.0 |
| 158 | + } |
| 159 | + |
| 160 | + #[must_use] |
| 161 | + pub fn into_agent_id(self) -> A2aAgentId { |
| 162 | + self.0 |
| 163 | + } |
| 164 | +} |
| 165 | + |
| 166 | +impl fmt::Display for BridgeAgentId { |
| 167 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 168 | + self.0.fmt(f) |
| 169 | + } |
| 170 | +} |
0 commit comments