confluent_kafka/common/security/token/delegation/
delegation_token.rs1use std::fmt;
21
22use crate::common::security::token::delegation::TokenInformation;
23
24const BASE64_ALPHABET: &[u8; 64] = b"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
26
27fn base64_encode(data: &[u8]) -> String {
30 let mut out = String::with_capacity(data.len().div_ceil(3) * 4);
31 for chunk in data.chunks(3) {
32 let b0 = chunk[0] as u32;
33 let b1 = if chunk.len() > 1 { chunk[1] as u32 } else { 0 };
34 let b2 = if chunk.len() > 2 { chunk[2] as u32 } else { 0 };
35 let triple = (b0 << 16) | (b1 << 8) | b2;
36 out.push(BASE64_ALPHABET[((triple >> 18) & 0x3F) as usize] as char);
37 out.push(BASE64_ALPHABET[((triple >> 12) & 0x3F) as usize] as char);
38 if chunk.len() > 1 {
39 out.push(BASE64_ALPHABET[((triple >> 6) & 0x3F) as usize] as char);
40 } else {
41 out.push('=');
42 }
43 if chunk.len() > 2 {
44 out.push(BASE64_ALPHABET[(triple & 0x3F) as usize] as char);
45 } else {
46 out.push('=');
47 }
48 }
49 out
50}
51
52#[derive(Clone, PartialEq, Eq, Hash)]
54#[doc(alias = "org.apache.kafka.common.security.token.delegation.DelegationToken")]
55pub struct DelegationToken {
56 token_information: TokenInformation,
57 hmac: Vec<u8>,
58}
59
60impl DelegationToken {
61 #[doc(alias = "org.apache.kafka.common.security.token.delegation.DelegationToken#DelegationToken")]
68 pub fn new(token_information: TokenInformation, hmac: Vec<u8>) -> Self {
69 Self { token_information, hmac }
70 }
71
72 #[doc(alias = "org.apache.kafka.common.security.token.delegation.DelegationToken#tokenInfo")]
74 pub fn token_info(&self) -> &TokenInformation {
75 &self.token_information
76 }
77
78 pub(crate) fn token_info_mut(&mut self) -> &mut TokenInformation {
83 &mut self.token_information
84 }
85
86 #[doc(alias = "org.apache.kafka.common.security.token.delegation.DelegationToken#hmac")]
88 pub fn hmac(&self) -> &[u8] {
89 &self.hmac
90 }
91
92 #[doc(alias = "org.apache.kafka.common.security.token.delegation.DelegationToken#hmacAsBase64String")]
94 pub fn hmac_as_base64_string(&self) -> String {
95 base64_encode(&self.hmac)
96 }
97}
98
99impl fmt::Display for DelegationToken {
100 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101 write!(
103 f,
104 "DelegationToken{{tokenInformation={}, hmac=[*******]}}",
105 self.token_information
106 )
107 }
108}
109
110impl fmt::Debug for DelegationToken {
111 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
114 fmt::Display::fmt(self, f)
115 }
116}
117
118#[cfg(test)]
119mod tests {
120 use super::*;
121 use crate::common::security::auth::KafkaPrincipal;
122
123 fn token_info() -> TokenInformation {
124 TokenInformation::new(
125 "token-id",
126 KafkaPrincipal::new(KafkaPrincipal::USER_TYPE, "alice"),
127 vec![KafkaPrincipal::new(KafkaPrincipal::USER_TYPE, "bob")],
128 1,
129 100,
130 50,
131 )
132 }
133
134 #[test]
135 fn accessors_return_components() {
136 let token = DelegationToken::new(token_info(), b"hmac-bytes".to_vec());
137 assert_eq!(token.token_info().token_id(), "token-id");
138 assert_eq!(token.hmac(), b"hmac-bytes");
139 }
140
141 #[test]
142 fn base64_matches_java_encoder() {
143 assert_eq!(base64_encode(b""), "");
145 assert_eq!(base64_encode(b"f"), "Zg==");
146 assert_eq!(base64_encode(b"fo"), "Zm8=");
147 assert_eq!(base64_encode(b"foo"), "Zm9v");
148 assert_eq!(base64_encode(b"foob"), "Zm9vYg==");
149 assert_eq!(base64_encode(b"fooba"), "Zm9vYmE=");
150 assert_eq!(base64_encode(b"foobar"), "Zm9vYmFy");
151 assert_eq!(base64_encode(&[0xFB, 0xFF, 0xFE]), "+//+");
153 }
154
155 #[test]
156 fn hmac_as_base64_string_uses_standard_encoder() {
157 let token = DelegationToken::new(token_info(), b"foobar".to_vec());
158 assert_eq!(token.hmac_as_base64_string(), "Zm9vYmFy");
159 }
160
161 #[test]
162 fn display_redacts_hmac() {
163 let token = DelegationToken::new(token_info(), b"secret".to_vec());
164 let rendered = token.to_string();
165 assert!(rendered.contains("hmac=[*******]"), "{rendered}");
166 assert!(!rendered.contains("secret"), "{rendered}");
167 }
168
169 #[test]
173 fn debug_redacts_hmac() {
174 let token = DelegationToken::new(token_info(), b"secret".to_vec());
175 for rendered in [format!("{token:?}"), format!("{token:#?}")] {
176 assert_eq!(rendered, token.to_string());
177 assert!(rendered.contains("hmac=[*******]"), "{rendered}");
178 assert!(!rendered.contains("secret"), "{rendered}");
179 assert!(!rendered.contains(&format!("{:?}", token.hmac())), "{rendered}");
180 assert!(!rendered.contains("115, 101, 99"), "{rendered}");
181 }
182 }
183
184 #[test]
185 fn equals_considers_info_and_hmac() {
186 let a = DelegationToken::new(token_info(), b"x".to_vec());
187 let b = DelegationToken::new(token_info(), b"x".to_vec());
188 let c = DelegationToken::new(token_info(), b"y".to_vec());
189 assert_eq!(a, b);
190 assert_ne!(a, c);
191 }
192}