use std::fmt;
use crate::{IdentityError, Result};
const REDACTED: &str = "<redacted>";
pub const TOKEN_TYPE_ACCESS_TOKEN: &str = "urn:ietf:params:oauth:token-type:access_token";
pub const TOKEN_TYPE_REFRESH_TOKEN: &str = "urn:ietf:params:oauth:token-type:refresh_token";
pub const TOKEN_TYPE_ID_TOKEN: &str = "urn:ietf:params:oauth:token-type:id_token";
pub const TOKEN_TYPE_SAML1: &str = "urn:ietf:params:oauth:token-type:saml1";
pub const TOKEN_TYPE_SAML2: &str = "urn:ietf:params:oauth:token-type:saml2";
pub const TOKEN_TYPE_JWT: &str = "urn:ietf:params:oauth:token-type:jwt";
pub const TOKEN_TYPE_URIS: [&str; 6] = [
TOKEN_TYPE_ACCESS_TOKEN,
TOKEN_TYPE_REFRESH_TOKEN,
TOKEN_TYPE_ID_TOKEN,
TOKEN_TYPE_SAML1,
TOKEN_TYPE_SAML2,
TOKEN_TYPE_JWT,
];
#[derive(Clone)]
pub struct TokenExchangeRequest {
pub(crate) subject_token: String,
pub(crate) subject_token_type: String,
pub(crate) actor_token: Option<String>,
pub(crate) actor_token_type: Option<String>,
pub(crate) requested_token_type: Option<String>,
pub(crate) scope: Option<String>,
pub(crate) resources: Vec<String>,
pub(crate) audiences: Vec<String>,
}
impl fmt::Debug for TokenExchangeRequest {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("TokenExchangeRequest")
.field("subject_token", &REDACTED)
.field("subject_token_type", &self.subject_token_type)
.field("actor_token", &self.actor_token.as_ref().map(|_| REDACTED))
.field("actor_token_type", &self.actor_token_type)
.field("requested_token_type", &self.requested_token_type)
.field("scope", &self.scope)
.field("resources", &self.resources)
.field("audiences", &self.audiences)
.finish()
}
}
impl TokenExchangeRequest {
pub fn new(subject_token: impl Into<String>, subject_token_type: impl Into<String>) -> Self {
Self {
subject_token: subject_token.into(),
subject_token_type: subject_token_type.into(),
actor_token: None,
actor_token_type: None,
requested_token_type: None,
scope: None,
resources: Vec::new(),
audiences: Vec::new(),
}
}
pub fn actor_token(mut self, token: impl Into<String>, token_type: impl Into<String>) -> Self {
self.actor_token = Some(token.into());
self.actor_token_type = Some(token_type.into());
self
}
pub fn requested_token_type(mut self, uri: impl Into<String>) -> Self {
self.requested_token_type = Some(uri.into());
self
}
pub fn scope(mut self, scope: impl Into<String>) -> Self {
self.scope = Some(scope.into());
self
}
pub fn resource(mut self, resource: impl Into<String>) -> Self {
self.resources.push(resource.into());
self
}
pub fn audience(mut self, audience: impl Into<String>) -> Self {
self.audiences.push(audience.into());
self
}
pub(crate) fn validate(&self) -> Result<()> {
if self.subject_token.is_empty() {
return Err(IdentityError::Validation(
"token exchange: subject_token is required".to_string(),
));
}
if self.subject_token_type.is_empty() {
return Err(IdentityError::Validation(
"token exchange: subject_token_type is required".to_string(),
));
}
if self.actor_token.as_deref().is_some_and(|t| !t.is_empty())
&& !self
.actor_token_type
.as_deref()
.is_some_and(|t| !t.is_empty())
{
return Err(IdentityError::Validation(
"token exchange: actor_token_type is required when actor_token is set".to_string(),
));
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn token_type_uris_are_the_six_rfc_8693_uris() {
assert_eq!(
TOKEN_TYPE_URIS,
[
"urn:ietf:params:oauth:token-type:access_token",
"urn:ietf:params:oauth:token-type:refresh_token",
"urn:ietf:params:oauth:token-type:id_token",
"urn:ietf:params:oauth:token-type:saml1",
"urn:ietf:params:oauth:token-type:saml2",
"urn:ietf:params:oauth:token-type:jwt",
]
);
}
#[test]
fn new_builds_a_bare_impersonation_request() {
let request = TokenExchangeRequest::new("subject.tok", TOKEN_TYPE_ACCESS_TOKEN);
assert_eq!(request.subject_token, "subject.tok");
assert_eq!(request.subject_token_type, TOKEN_TYPE_ACCESS_TOKEN);
assert!(request.actor_token.is_none());
assert!(request.actor_token_type.is_none());
assert!(request.requested_token_type.is_none());
assert!(request.scope.is_none());
assert!(request.resources.is_empty());
assert!(request.audiences.is_empty());
request.validate().expect("a bare impersonation is valid");
}
#[test]
fn resource_and_audience_accumulate() {
let request = TokenExchangeRequest::new("s", TOKEN_TYPE_ACCESS_TOKEN)
.resource("https://rs1.example.com")
.resource("https://rs2.example.com")
.audience("aud-a")
.audience("aud-b");
assert_eq!(
request.resources,
["https://rs1.example.com", "https://rs2.example.com"]
);
assert_eq!(request.audiences, ["aud-a", "aud-b"]);
}
#[test]
fn validate_rejects_empty_subject_token() {
let err = TokenExchangeRequest::new("", TOKEN_TYPE_ACCESS_TOKEN)
.validate()
.expect_err("empty subject_token must be rejected");
assert!(matches!(err, IdentityError::Validation(_)), "{err:?}");
}
#[test]
fn validate_rejects_empty_subject_token_type() {
let err = TokenExchangeRequest::new("subject.tok", "")
.validate()
.expect_err("empty subject_token_type must be rejected");
assert!(matches!(err, IdentityError::Validation(_)), "{err:?}");
}
#[test]
fn validate_rejects_actor_token_without_type() {
let err = TokenExchangeRequest::new("subject.tok", TOKEN_TYPE_ACCESS_TOKEN)
.actor_token("actor.tok", "")
.validate()
.expect_err("actor_token without a type must be rejected");
assert!(matches!(err, IdentityError::Validation(_)), "{err:?}");
}
#[test]
fn validate_allows_empty_actor_token_without_type() {
TokenExchangeRequest::new("subject.tok", TOKEN_TYPE_ACCESS_TOKEN)
.actor_token("", "")
.validate()
.expect("an empty actor token is not a delegation");
}
#[test]
fn debug_redacts_subject_and_actor_tokens() {
let request = TokenExchangeRequest::new("SUBJECT-SECRET", TOKEN_TYPE_ACCESS_TOKEN)
.actor_token("ACTOR-SECRET", TOKEN_TYPE_JWT);
let dbg = format!("{request:?}");
assert!(!dbg.contains("SUBJECT-SECRET"), "subject leaked: {dbg}");
assert!(!dbg.contains("ACTOR-SECRET"), "actor leaked: {dbg}");
assert!(dbg.contains(REDACTED), "no redaction marker: {dbg}");
assert!(dbg.contains("actor_token: Some"), "{dbg}");
assert!(dbg.contains(TOKEN_TYPE_ACCESS_TOKEN), "{dbg}");
}
#[test]
fn debug_shows_absent_actor_as_none() {
let request = TokenExchangeRequest::new("s", TOKEN_TYPE_ACCESS_TOKEN);
assert!(format!("{request:?}").contains("actor_token: None"));
}
}