use std::{collections::HashMap, iter, str::from_utf8};
use base64::{
Engine, alphabet,
engine::{GeneralPurpose, GeneralPurposeConfig, general_purpose::STANDARD as B64},
};
use crate::AuthError;
pub(super) const JAVA_B64: GeneralPurpose = GeneralPurpose::new(
&alphabet::STANDARD,
GeneralPurposeConfig::new().with_decode_allow_trailing_bits(true),
);
pub const TOKEN_AUTH_EXTENSION: &str = "tokenauth";
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ScramClientFirst {
pub gs2_header: String,
pub authzid: Option<String>,
pub username: String,
pub nonce: String,
pub extensions: Vec<(String, String)>,
pub bare: String,
}
impl ScramClientFirst {
pub fn parse(bytes: &[u8]) -> Result<Self, AuthError> {
let message = from_utf8(bytes).map_err(|_| AuthError::MalformedMessage)?;
let after_flag = message
.strip_prefix("n,")
.ok_or(AuthError::MalformedMessage)?;
let (authzid_part, bare) = after_flag
.split_once(',')
.ok_or(AuthError::MalformedMessage)?;
let authzid = if authzid_part.is_empty() {
None
} else {
let raw = authzid_part
.strip_prefix("a=")
.ok_or(AuthError::MalformedMessage)?;
Some(decode_saslname(raw)?)
};
let gs2_header = format!("n,{authzid_part},");
let mut attributes = bare.split(',').peekable();
if let Some(reserved) = attributes
.peek()
.copied()
.and_then(|a| a.strip_prefix("m="))
{
attributes.next();
if !is_value(reserved) {
return Err(AuthError::MalformedMessage);
}
}
let saslname = attributes
.next()
.and_then(|a| a.strip_prefix("n="))
.ok_or(AuthError::MalformedMessage)?;
let username = decode_saslname(saslname)?;
let nonce = attributes
.next()
.and_then(|a| a.strip_prefix("r="))
.filter(|n| is_printable(n))
.ok_or(AuthError::MalformedMessage)?
.to_owned();
let extensions = attributes
.map(|attribute| {
attribute
.split_once('=')
.filter(|(key, value)| is_extension(key, value))
.map(|(key, value)| (key.to_owned(), value.to_owned()))
.ok_or(AuthError::MalformedMessage)
})
.collect::<Result<Vec<_>, _>>()?;
let bare = iter::once(format!("n={saslname},r={nonce}"))
.chain(
distinct_extensions(&extensions)
.into_iter()
.map(|(key, value)| format!("{key}={value}")),
)
.collect::<Vec<_>>()
.join(",");
Ok(Self {
gs2_header,
authzid,
username,
nonce,
extensions,
bare,
})
}
#[must_use]
pub fn extension(&self, key: &str) -> Option<&str> {
self.extensions
.iter()
.rev()
.find(|(k, _)| k == key)
.map(|(_, v)| v.as_str())
}
#[must_use]
pub fn token_authenticated(&self) -> bool {
self.extension(TOKEN_AUTH_EXTENSION)
.is_some_and(|v| v.eq_ignore_ascii_case("true"))
}
}
fn distinct_extensions(extensions: &[(String, String)]) -> Vec<(&str, &str)> {
let mut last: HashMap<&str, &str> = HashMap::with_capacity(extensions.len());
for (key, value) in extensions {
last.insert(key, value);
}
extensions
.iter()
.filter_map(|(key, _)| last.remove(key.as_str()).map(|value| (key.as_str(), value)))
.collect()
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct ScramClientFinal<'a> {
pub channel_binding: &'a str,
pub nonce: &'a str,
pub extensions: Vec<(&'a str, &'a str)>,
pub proof: &'a str,
pub without_proof: String,
}
impl<'a> ScramClientFinal<'a> {
pub(super) fn parse(bytes: &'a [u8]) -> Result<Self, AuthError> {
let message = from_utf8(bytes).map_err(|_| AuthError::MalformedMessage)?;
let (before_proof, proof) = message
.rsplit_once(",p=")
.filter(|(_, proof)| is_base64(proof))
.ok_or(AuthError::MalformedMessage)?;
let mut attributes = before_proof.split(',');
let channel_binding = attributes
.next()
.and_then(|a| a.strip_prefix("c="))
.filter(|c| is_base64(c))
.ok_or(AuthError::MalformedMessage)?;
let nonce = attributes
.next()
.and_then(|a| a.strip_prefix("r="))
.filter(|n| is_printable(n))
.ok_or(AuthError::MalformedMessage)?;
let extensions = attributes
.map(|attribute| {
attribute
.split_once('=')
.filter(|(key, value)| is_extension(key, value))
.ok_or(AuthError::MalformedMessage)
})
.collect::<Result<Vec<_>, _>>()?;
let channel_binding_bytes = JAVA_B64
.decode(channel_binding)
.map_err(|_| AuthError::MalformedMessage)?;
let without_proof = format!("c={},r={nonce}", B64.encode(channel_binding_bytes));
Ok(Self {
channel_binding,
nonce,
extensions,
proof,
without_proof,
})
}
}
fn is_base64(value: &str) -> bool {
let (body, remainder) = value.strip_suffix("==").map_or_else(
|| value.strip_suffix('=').map_or((value, 0), |body| (body, 3)),
|body| (body, 2),
);
body.len() % 4 == remainder
&& body
.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'+' || b == b'/')
}
#[must_use]
pub fn encode_saslname(username: &str) -> String {
username.replace('=', "=3D").replace(',', "=2C")
}
pub fn decode_saslname(saslname: &str) -> Result<String, AuthError> {
if saslname.is_empty() || saslname.contains(',') {
return Err(AuthError::MalformedMessage);
}
let mut decoded = String::with_capacity(saslname.len());
let mut rest = saslname;
while let Some((before, after)) = rest.split_once('=') {
decoded.push_str(before);
let (escape, tail) = after
.split_at_checked(2)
.ok_or(AuthError::MalformedMessage)?;
decoded.push(match escape {
"2C" => ',',
"3D" => '=',
_ => return Err(AuthError::MalformedMessage),
});
rest = tail;
}
decoded.push_str(rest);
Ok(decoded)
}
pub(super) fn is_extension(key: &str, value: &str) -> bool {
!key.is_empty() && key.bytes().all(|b| b.is_ascii_alphabetic()) && is_value(value)
}
fn is_value(value: &str) -> bool {
!value.is_empty()
&& value
.bytes()
.all(|b| (0x01..=0x7F).contains(&b) && b != b',')
}
fn is_printable(value: &str) -> bool {
!value.is_empty()
&& value
.bytes()
.all(|b| (0x21..=0x7E).contains(&b) && b != b',')
}
#[cfg(test)]
mod tests {
use assert2::check;
use super::*;
#[test]
fn client_first_parses_kafkas_grammar() {
for (case, message, want) in [
(
"plain header",
"n,,n=alice,r=abc",
ScramClientFirst {
gs2_header: "n,,".into(),
authzid: None,
username: "alice".into(),
nonce: "abc".into(),
extensions: vec![],
bare: "n=alice,r=abc".into(),
},
),
(
"authzid header",
"n,a=alice,n=alice,r=abc",
ScramClientFirst {
gs2_header: "n,a=alice,".into(),
authzid: Some("alice".into()),
username: "alice".into(),
nonce: "abc".into(),
extensions: vec![],
bare: "n=alice,r=abc".into(),
},
),
(
"token extension",
"n,,n=tok-id,r=abc,tokenauth=true",
ScramClientFirst {
gs2_header: "n,,".into(),
authzid: None,
username: "tok-id".into(),
nonce: "abc".into(),
extensions: vec![("tokenauth".into(), "true".into())],
bare: "n=tok-id,r=abc,tokenauth=true".into(),
},
),
(
"escaped names and reserved attribute",
"n,a=a=2Cb=3Dc,m=ignored,n=a=2Cb=3Dc,r=x,foo=1,bar=2",
ScramClientFirst {
gs2_header: "n,a=a=2Cb=3Dc,".into(),
authzid: Some("a,b=c".into()),
username: "a,b=c".into(),
nonce: "x".into(),
extensions: vec![("foo".into(), "1".into()), ("bar".into(), "2".into())],
bare: "n=a=2Cb=3Dc,r=x,foo=1,bar=2".into(),
},
),
(
"repeated extension keeps its first place and last value",
"n,,n=alice,r=x,a=1,b=2,a=3",
ScramClientFirst {
gs2_header: "n,,".into(),
authzid: None,
username: "alice".into(),
nonce: "x".into(),
extensions: vec![
("a".into(), "1".into()),
("b".into(), "2".into()),
("a".into(), "3".into()),
],
bare: "n=alice,r=x,a=3,b=2".into(),
},
),
] {
check!(
ScramClientFirst::parse(message.as_bytes()) == Ok(want),
"case {case}"
);
}
}
#[test]
fn client_first_rejects_what_kafka_rejects() {
for (case, message) in [
(
"channel binding requested",
&b"p=tls-unique,,n=alice,r=abc"[..],
),
("client supports binding", b"y,,n=alice,r=abc"),
("no GS2 header", b"n=alice,r=abc"),
("authzid without a=", b"n,alice,n=alice,r=abc"),
("empty authzid", b"n,a=,n=alice,r=abc"),
("attributes out of order", b"n,,r=abc,n=alice"),
("missing nonce", b"n,,n=alice"),
("empty nonce", b"n,,n=alice,r="),
("bad escape", b"n,,n=a=2Xb,r=abc"),
("truncated escape", b"n,,n=ab=2,r=abc"),
("extension without value", b"n,,n=alice,r=abc,tokenauth"),
(
"non-alphabetic extension key",
b"n,,n=alice,r=abc,t0ken=true",
),
("not UTF-8", b"n,,n=\xff,r=abc"),
] {
check!(
ScramClientFirst::parse(message) == Err(AuthError::MalformedMessage),
"case {case}"
);
}
}
#[test]
fn client_first_rebuilds_a_large_extension_list_in_order() {
let keys: Vec<String> = (0..60_000_u32)
.map(|mut n| {
let mut key = String::new();
loop {
key.push(char::from(b'a' + u8::try_from(n % 26).unwrap()));
n /= 26;
if n == 0 {
break key;
}
}
})
.collect();
let mut message = String::from("n,,n=alice,r=cn");
let mut expected = String::from("n=alice,r=cn");
for key in &keys {
let value = if key == "a" { "2" } else { "1" };
for (out, value) in [(&mut message, "1"), (&mut expected, value)] {
out.extend([",", key.as_str(), "=", value]);
}
}
message.push_str(",a=2");
let parsed = ScramClientFirst::parse(message.as_bytes()).unwrap();
check!(parsed.bare == expected);
}
#[test]
fn token_authenticated_reads_the_tokenauth_extension() {
for (message, want) in [
("n,,n=t,r=a,tokenauth=true", true),
("n,,n=t,r=a,tokenauth=TRUE", true),
("n,,n=t,r=a,tokenauth=false", false),
("n,,n=t,r=a", false),
("n,,n=t,r=a,tokenauth=true,tokenauth=false", false),
("n,,n=t,r=a,tokenauth=false,tokenauth=true", true),
] {
let parsed = ScramClientFirst::parse(message.as_bytes()).unwrap();
check!(parsed.token_authenticated() == want, "message {message}");
}
}
#[test]
fn client_final_parses_kafkas_grammar() {
for (case, message, want) in [
(
"plain",
"c=biws,r=NS,p=AAAA",
ScramClientFinal {
channel_binding: "biws",
nonce: "NS",
extensions: vec![],
proof: "AAAA",
without_proof: "c=biws,r=NS".into(),
},
),
(
"padded base64",
"c=bixhPWFsaWNlLA==,r=N!~,p=AAA=",
ScramClientFinal {
channel_binding: "bixhPWFsaWNlLA==",
nonce: "N!~",
extensions: vec![],
proof: "AAA=",
without_proof: "c=bixhPWFsaWNlLA==,r=N!~".into(),
},
),
(
"extensions before the proof",
"c=biws,r=NS,foo=1,Bar=a=b,p=x,p=AA==",
ScramClientFinal {
channel_binding: "biws",
nonce: "NS",
extensions: vec![("foo", "1"), ("Bar", "a=b"), ("p", "x")],
proof: "AA==",
without_proof: "c=biws,r=NS".into(),
},
),
(
"repeated nonce and proof",
"c=biws,r=NS,r=NS,p=AAAA,p=AAAA",
ScramClientFinal {
channel_binding: "biws",
nonce: "NS",
extensions: vec![("r", "NS"), ("p", "AAAA")],
proof: "AAAA",
without_proof: "c=biws,r=NS".into(),
},
),
(
"unused bits in the channel binding",
"c=bixhPWFsaWNlLB==,r=NS,p=AAAA",
ScramClientFinal {
channel_binding: "bixhPWFsaWNlLB==",
nonce: "NS",
extensions: vec![],
proof: "AAAA",
without_proof: "c=bixhPWFsaWNlLA==,r=NS".into(),
},
),
(
"empty base64",
"c=,r=NS,p=",
ScramClientFinal {
channel_binding: "",
nonce: "NS",
extensions: vec![],
proof: "",
without_proof: "c=,r=NS".into(),
},
),
] {
check!(
ScramClientFinal::parse(message.as_bytes()) == Ok(want),
"case {case}"
);
}
}
#[test]
fn client_final_rejects_what_kafka_rejects() {
for (case, message) in [
("nonce first", &b"r=NS,c=biws,p=AAAA"[..]),
("proof before nonce", b"c=biws,p=AAAA,r=NS"),
("missing channel binding", b"r=NS,p=AAAA"),
("missing nonce", b"c=biws,p=AAAA"),
("missing proof", b"c=biws,r=NS"),
(
"unknown attribute after the proof",
b"c=biws,r=NS,p=AAAA,x=1",
),
("duplicate channel binding", b"c=biws,c=biws,r=NS,p=AAAA"),
("extension before the nonce", b"c=biws,x=1,r=NS,p=AAAA"),
("empty extension value", b"c=biws,r=NS,x=,p=AAAA"),
("extension without value", b"c=biws,r=NS,x,p=AAAA"),
("non-alphabetic extension key", b"c=biws,r=NS,x1=a,p=AAAA"),
(
"non-ASCII extension value",
"c=biws,r=NS,x=\u{e9},p=AAAA".as_bytes(),
),
("channel binding not base64", b"c=bi*s,r=NS,p=AAAA"),
("channel binding unpadded", b"c=biw,r=NS,p=AAAA"),
("proof not base64", b"c=biws,r=NS,p=AA-A"),
("proof unpadded", b"c=biws,r=NS,p=AAAAA"),
("proof over-padded", b"c=biws,r=NS,p=A==="),
("proof padding mid-string", b"c=biws,r=NS,p=AA==AAAA"),
("empty nonce", b"c=biws,r=,p=AAAA"),
("nonce with a space", b"c=biws,r=N S,p=AAAA"),
("nonce with a control byte", b"c=biws,r=N\x7f,p=AAAA"),
("trailing comma", b"c=biws,r=NS,p=AAAA,"),
("trailing newline", b"c=biws,r=NS,p=AAAA\n"),
("leading space", b" c=biws,r=NS,p=AAAA"),
("not UTF-8", b"c=biws,r=\xff,p=AAAA"),
] {
check!(
ScramClientFinal::parse(message) == Err(AuthError::MalformedMessage),
"case {case}"
);
}
}
#[test]
fn saslname_encoding_round_trips() {
for (name, encoded) in [
("alice", "alice"),
("a,b", "a=2Cb"),
("a=b", "a=3Db"),
("=,", "=3D=2C"),
] {
check!(
(encode_saslname(name), decode_saslname(encoded))
== (encoded.to_owned(), Ok(name.to_owned()))
);
}
}
}