Skip to main content

haystack_core/
auth.rs

1//! SCRAM SHA-256 authentication primitives for the Haystack auth protocol.
2//!
3//! This module implements the cryptographic operations needed for SCRAM
4//! (Salted Challenge Response Authentication Mechanism) with SHA-256 as
5//! specified by the [Project Haystack auth spec](https://project-haystack.org/doc/docHaystack/Auth).
6//!
7//! It provides functions shared by both server and client implementations
8//! for the three-phase handshake: HELLO, SCRAM challenge/response, and
9//! BEARER token issuance.
10
11use base64::Engine;
12use base64::engine::general_purpose::STANDARD as BASE64;
13use hmac::{Hmac, Mac};
14use pbkdf2::pbkdf2_hmac;
15use rand::Rng;
16use sha2::{Digest, Sha256};
17use subtle::ConstantTimeEq;
18
19type HmacSha256 = Hmac<Sha256>;
20
21/// Default PBKDF2 iteration count for SCRAM SHA-256.
22pub const DEFAULT_ITERATIONS: u32 = 100_000;
23
24// ---------------------------------------------------------------------------
25// Error type
26// ---------------------------------------------------------------------------
27
28/// Errors that can occur during SCRAM authentication.
29#[derive(Debug, thiserror::Error)]
30pub enum AuthError {
31    #[error("invalid credentials")]
32    InvalidCredentials,
33    #[error("invalid auth header: {0}")]
34    InvalidHeader(String),
35    #[error("handshake failed: {0}")]
36    HandshakeFailed(String),
37    #[error("base64 decode error: {0}")]
38    Base64Error(String),
39}
40
41// ---------------------------------------------------------------------------
42// Types
43// ---------------------------------------------------------------------------
44
45/// Pre-computed SCRAM credentials for a user (stored server-side).
46#[derive(Debug, Clone)]
47pub struct ScramCredentials {
48    pub salt: Vec<u8>,
49    pub iterations: u32,
50    pub stored_key: Vec<u8>,
51    pub server_key: Vec<u8>,
52}
53
54/// In-flight SCRAM handshake state held by the server between the
55/// server-first-message and client-final-message exchanges.
56#[derive(Debug, Clone)]
57pub struct ScramHandshake {
58    pub username: String,
59    pub client_nonce: String,
60    pub server_nonce: String,
61    pub salt: Vec<u8>,
62    pub iterations: u32,
63    pub auth_message: String,
64    pub server_signature: Vec<u8>,
65    /// Stored key from credentials, needed to verify the client proof.
66    stored_key: Vec<u8>,
67}
68
69/// Parsed Haystack `Authorization` header.
70#[derive(Debug, Clone, PartialEq, Eq)]
71pub enum AuthHeader {
72    Hello {
73        username: String,
74        /// Base64-encoded client-first-message (contains the client nonce).
75        data: Option<String>,
76    },
77    Scram {
78        handshake_token: String,
79        data: String,
80    },
81    Bearer {
82        auth_token: String,
83    },
84}
85
86// ---------------------------------------------------------------------------
87// Internal helpers
88// ---------------------------------------------------------------------------
89
90/// Compute HMAC-SHA-256(key, msg).
91fn hmac_sha256(key: &[u8], msg: &[u8]) -> Vec<u8> {
92    let mut mac = HmacSha256::new_from_slice(key).expect("HMAC accepts keys of any size");
93    mac.update(msg);
94    mac.finalize().into_bytes().to_vec()
95}
96
97/// Compute SHA-256(data).
98fn sha256(data: &[u8]) -> Vec<u8> {
99    let mut hasher = Sha256::new();
100    hasher.update(data);
101    hasher.finalize().to_vec()
102}
103
104/// XOR two equal-length byte slices.
105fn xor_bytes(a: &[u8], b: &[u8]) -> Vec<u8> {
106    assert_eq!(a.len(), b.len(), "XOR operands must be the same length");
107    a.iter().zip(b.iter()).map(|(x, y)| x ^ y).collect()
108}
109
110/// PBKDF2-HMAC-SHA-256 key derivation, producing a 32-byte salted password.
111fn pbkdf2_sha256(password: &[u8], salt: &[u8], iterations: u32) -> Vec<u8> {
112    let mut salted_password = vec![0u8; 32];
113    pbkdf2_hmac::<Sha256>(password, salt, iterations, &mut salted_password);
114    salted_password
115}
116
117/// Derive (ClientKey, StoredKey, ServerKey) from a salted password.
118fn derive_keys(salted_password: &[u8]) -> (Vec<u8>, Vec<u8>, Vec<u8>) {
119    let client_key = hmac_sha256(salted_password, b"Client Key");
120    let stored_key = sha256(&client_key);
121    let server_key = hmac_sha256(salted_password, b"Server Key");
122    (client_key, stored_key, server_key)
123}
124
125/// Parse a `key=value` parameter from a SCRAM message segment.
126fn parse_scram_param<'a>(segment: &'a str, prefix: &str) -> Result<&'a str, AuthError> {
127    let trimmed = segment.trim();
128    trimmed.strip_prefix(prefix).ok_or_else(|| {
129        AuthError::HandshakeFailed(format!(
130            "expected prefix '{}' but got '{}'",
131            prefix, trimmed
132        ))
133    })
134}
135
136/// Build the client-first-message-bare: `n=<username>,r=<client_nonce>`.
137fn make_client_first_bare(username: &str, client_nonce: &str) -> String {
138    format!("n={},r={}", username, client_nonce)
139}
140
141// ---------------------------------------------------------------------------
142// Public API
143// ---------------------------------------------------------------------------
144
145/// Derive SCRAM credentials from a password (for user creation/storage).
146///
147/// Uses PBKDF2-HMAC-SHA-256 with the given salt and iteration count.
148pub fn derive_credentials(password: &str, salt: &[u8], iterations: u32) -> ScramCredentials {
149    let salted_password = pbkdf2_sha256(password.as_bytes(), salt, iterations);
150    let (_client_key, stored_key, server_key) = derive_keys(&salted_password);
151    ScramCredentials {
152        salt: salt.to_vec(),
153        iterations,
154        stored_key,
155        server_key,
156    }
157}
158
159/// Generate a random nonce string (base64-encoded 18 random bytes).
160pub fn generate_nonce() -> String {
161    let mut bytes = [0u8; 18];
162    rand::rng().fill(&mut bytes);
163    BASE64.encode(bytes)
164}
165
166/// Client-side: Create the client-first-message data (base64-encoded).
167///
168/// Returns `(client_nonce, client_first_data_base64)`.
169///
170/// The client-first-message-bare is `n=<username>,r=<client_nonce>`.
171/// The full message prepends the GS2 header `n,,` (no channel binding).
172pub fn client_first_message(username: &str) -> (String, String) {
173    let client_nonce = generate_nonce();
174    let bare = make_client_first_bare(username, &client_nonce);
175    let full = format!("n,,{}", bare);
176    let encoded = BASE64.encode(full.as_bytes());
177    (client_nonce, encoded)
178}
179
180/// Server-side: Create the server-first-message data and handshake state.
181///
182/// `username` is taken from the HELLO phase. `client_nonce_b64` is the raw
183/// client nonce (as returned by [`client_first_message`]). `credentials` are
184/// the pre-computed SCRAM credentials for this user.
185///
186/// Returns `(handshake_state, server_first_data_base64)`.
187pub fn server_first_message(
188    username: &str,
189    client_nonce_b64: &str,
190    credentials: &ScramCredentials,
191) -> (ScramHandshake, String) {
192    let server_nonce = generate_nonce();
193    let combined_nonce = format!("{}{}", client_nonce_b64, server_nonce);
194    let salt_b64 = BASE64.encode(&credentials.salt);
195
196    // server-first-message: r=<combined>,s=<salt_b64>,i=<iterations>
197    let server_first_msg = format!(
198        "r={},s={},i={}",
199        combined_nonce, salt_b64, credentials.iterations
200    );
201
202    // client-first-message-bare (includes username per SCRAM spec)
203    let cfmb = make_client_first_bare(username, client_nonce_b64);
204
205    // client-final-message-without-proof (anticipated)
206    let client_final_without_proof = format!("c=biws,r={}", combined_nonce);
207
208    // AuthMessage = client-first-bare "," server-first-msg "," client-final-without-proof
209    let auth_message = format!(
210        "{},{},{}",
211        cfmb, server_first_msg, client_final_without_proof
212    );
213
214    // Pre-compute server signature
215    let server_signature = hmac_sha256(&credentials.server_key, auth_message.as_bytes());
216
217    let server_first_b64 = BASE64.encode(server_first_msg.as_bytes());
218
219    let handshake = ScramHandshake {
220        username: username.to_string(),
221        client_nonce: client_nonce_b64.to_string(),
222        server_nonce,
223        salt: credentials.salt.clone(),
224        iterations: credentials.iterations,
225        auth_message,
226        server_signature,
227        stored_key: credentials.stored_key.clone(),
228    };
229
230    (handshake, server_first_b64)
231}
232
233/// Client-side: Process server-first-message, produce client-final-message.
234///
235/// `username` is the same value originally passed to [`client_first_message`].
236/// `password` is the user's plaintext password. `client_nonce` is the nonce
237/// returned by [`client_first_message`]. `server_first_b64` is the base64
238/// server-first-message data received from the server.
239///
240/// Returns `(client_final_data_base64, expected_server_signature)`.
241pub fn client_final_message(
242    password: &str,
243    client_nonce: &str,
244    server_first_b64: &str,
245    username: &str,
246) -> Result<(String, Vec<u8>), AuthError> {
247    // Decode and parse server-first-message
248    let server_first_bytes = BASE64
249        .decode(server_first_b64)
250        .map_err(|e| AuthError::Base64Error(e.to_string()))?;
251    let server_first_msg = String::from_utf8(server_first_bytes)
252        .map_err(|e| AuthError::HandshakeFailed(e.to_string()))?;
253
254    // Expected format: r=<combined_nonce>,s=<salt_b64>,i=<iterations>
255    let parts: Vec<&str> = server_first_msg.splitn(3, ',').collect();
256    if parts.len() != 3 {
257        return Err(AuthError::HandshakeFailed(
258            "invalid server-first-message format".to_string(),
259        ));
260    }
261
262    let combined_nonce = parse_scram_param(parts[0], "r=")?;
263    let salt_b64 = parse_scram_param(parts[1], "s=")?;
264    let iterations_str = parse_scram_param(parts[2], "i=")?;
265
266    // The combined nonce must start with our client nonce
267    if !combined_nonce.starts_with(client_nonce) {
268        return Err(AuthError::HandshakeFailed(
269            "combined nonce does not start with client nonce".to_string(),
270        ));
271    }
272
273    let salt = BASE64
274        .decode(salt_b64)
275        .map_err(|e| AuthError::Base64Error(e.to_string()))?;
276    let iterations: u32 = iterations_str
277        .parse()
278        .map_err(|e: std::num::ParseIntError| AuthError::HandshakeFailed(e.to_string()))?;
279
280    // Key derivation
281    let salted_password = pbkdf2_sha256(password.as_bytes(), &salt, iterations);
282    let (client_key, stored_key, server_key) = derive_keys(&salted_password);
283
284    // Build AuthMessage
285    let cfmb = make_client_first_bare(username, client_nonce);
286    let client_final_without_proof = format!("c=biws,r={}", combined_nonce);
287    let auth_message = format!(
288        "{},{},{}",
289        cfmb, server_first_msg, client_final_without_proof
290    );
291
292    // ClientSignature = HMAC(StoredKey, AuthMessage)
293    let client_signature = hmac_sha256(&stored_key, auth_message.as_bytes());
294    // ClientProof = ClientKey XOR ClientSignature
295    let client_proof = xor_bytes(&client_key, &client_signature);
296    // ServerSignature = HMAC(ServerKey, AuthMessage)
297    let server_signature = hmac_sha256(&server_key, auth_message.as_bytes());
298
299    // client-final-message: c=biws,r=<combined>,p=<proof_b64>
300    let proof_b64 = BASE64.encode(&client_proof);
301    let client_final_msg = format!("{},p={}", client_final_without_proof, proof_b64);
302    let client_final_b64 = BASE64.encode(client_final_msg.as_bytes());
303
304    Ok((client_final_b64, server_signature))
305}
306
307/// Server-side: Verify client-final-message and produce server signature.
308///
309/// Decodes the client-final-message, verifies the client proof against the
310/// stored key in the handshake state, and returns the server signature for
311/// the client to verify (sent as the `v=` field in server-final-message).
312pub fn server_verify_final(
313    handshake: &ScramHandshake,
314    client_final_b64: &str,
315) -> Result<Vec<u8>, AuthError> {
316    // Decode client-final-message
317    let client_final_bytes = BASE64
318        .decode(client_final_b64)
319        .map_err(|e| AuthError::Base64Error(e.to_string()))?;
320    let client_final_msg = String::from_utf8(client_final_bytes)
321        .map_err(|e| AuthError::HandshakeFailed(e.to_string()))?;
322
323    // Expected format: c=biws,r=<combined_nonce>,p=<proof_b64>
324    let parts: Vec<&str> = client_final_msg.splitn(3, ',').collect();
325    if parts.len() != 3 {
326        return Err(AuthError::HandshakeFailed(
327            "invalid client-final-message format".to_string(),
328        ));
329    }
330
331    // Validate channel binding
332    let channel_binding = parse_scram_param(parts[0], "c=")?;
333    if channel_binding != "biws" {
334        return Err(AuthError::HandshakeFailed(
335            "unexpected channel binding".to_string(),
336        ));
337    }
338
339    // Validate combined nonce
340    let combined_nonce = parse_scram_param(parts[1], "r=")?;
341    let expected_combined = format!("{}{}", handshake.client_nonce, handshake.server_nonce);
342    if combined_nonce != expected_combined {
343        return Err(AuthError::HandshakeFailed("nonce mismatch".to_string()));
344    }
345
346    // Extract and decode client proof
347    let proof_b64 = parse_scram_param(parts[2], "p=")?;
348    let client_proof = BASE64
349        .decode(proof_b64)
350        .map_err(|e| AuthError::Base64Error(e.to_string()))?;
351
352    // Verify the proof per RFC 5802:
353    //   ClientSignature = HMAC(StoredKey, AuthMessage)
354    //   RecoveredClientKey = ClientProof XOR ClientSignature
355    //   Check: SHA-256(RecoveredClientKey) == StoredKey
356    let client_signature = hmac_sha256(&handshake.stored_key, handshake.auth_message.as_bytes());
357    let recovered_client_key = xor_bytes(&client_proof, &client_signature);
358    let recovered_stored_key = sha256(&recovered_client_key);
359
360    if recovered_stored_key
361        .ct_eq(&handshake.stored_key)
362        .unwrap_u8()
363        == 0
364    {
365        return Err(AuthError::InvalidCredentials);
366    }
367
368    // Proof verified -- return server signature for the client to verify
369    Ok(handshake.server_signature.clone())
370}
371
372/// Extract the client nonce from a base64-encoded client-first-message.
373///
374/// The client-first-message format is `n,,n=<username>,r=<client_nonce>`.
375/// Returns the raw nonce string.
376pub fn extract_client_nonce(client_first_b64: &str) -> Result<String, AuthError> {
377    let bytes = BASE64
378        .decode(client_first_b64)
379        .map_err(|e| AuthError::Base64Error(e.to_string()))?;
380    let msg = String::from_utf8(bytes).map_err(|e| AuthError::HandshakeFailed(e.to_string()))?;
381    // Strip GS2 header "n,," prefix
382    let bare = msg
383        .strip_prefix("n,,")
384        .ok_or_else(|| AuthError::HandshakeFailed("missing GS2 header in client-first".into()))?;
385    // Parse n=<user>,r=<nonce>
386    for part in bare.split(',') {
387        if let Some(nonce) = part.strip_prefix("r=") {
388            return Ok(nonce.to_string());
389        }
390    }
391    Err(AuthError::HandshakeFailed(
392        "missing r= nonce in client-first-message".into(),
393    ))
394}
395
396/// Parse a Haystack `Authorization` header value.
397///
398/// Supported formats:
399/// - `HELLO username=<base64(username)>`
400/// - `SCRAM handshakeToken=<token>, data=<data>`
401/// - `BEARER authToken=<token>`
402pub fn parse_auth_header(header: &str) -> Result<AuthHeader, AuthError> {
403    let header = header.trim();
404
405    if let Some(rest) = header.strip_prefix("HELLO ") {
406        let mut username_b64_val = None;
407        let mut data_val = None;
408        for part in rest.split(',') {
409            let part = part.trim();
410            if let Some(val) = part.strip_prefix("username=") {
411                username_b64_val = Some(val.trim().to_string());
412            } else if let Some(val) = part.strip_prefix("data=") {
413                data_val = Some(val.trim().to_string());
414            }
415        }
416        let username_b64 = username_b64_val
417            .ok_or_else(|| AuthError::InvalidHeader("missing username= in HELLO".into()))?;
418        let username_bytes = BASE64
419            .decode(&username_b64)
420            .map_err(|e| AuthError::Base64Error(e.to_string()))?;
421        let username = String::from_utf8(username_bytes)
422            .map_err(|e| AuthError::InvalidHeader(e.to_string()))?;
423        Ok(AuthHeader::Hello {
424            username,
425            data: data_val,
426        })
427    } else if let Some(rest) = header.strip_prefix("SCRAM ") {
428        let mut handshake_token = None;
429        let mut data = None;
430        for part in rest.split(',') {
431            let part = part.trim();
432            if let Some(val) = part.strip_prefix("handshakeToken=") {
433                handshake_token = Some(val.trim().to_string());
434            } else if let Some(val) = part.strip_prefix("data=") {
435                data = Some(val.trim().to_string());
436            }
437        }
438        let handshake_token = handshake_token
439            .ok_or_else(|| AuthError::InvalidHeader("missing handshakeToken= in SCRAM".into()))?;
440        let data = data.ok_or_else(|| AuthError::InvalidHeader("missing data= in SCRAM".into()))?;
441        Ok(AuthHeader::Scram {
442            handshake_token,
443            data,
444        })
445    } else if let Some(rest) = header.strip_prefix("BEARER ") {
446        let token = rest
447            .trim()
448            .strip_prefix("authToken=")
449            .ok_or_else(|| AuthError::InvalidHeader("missing authToken= in BEARER".into()))?;
450        Ok(AuthHeader::Bearer {
451            auth_token: token.trim().to_string(),
452        })
453    } else {
454        Err(AuthError::InvalidHeader(format!(
455            "unrecognized auth scheme: {}",
456            header
457        )))
458    }
459}
460
461/// Format a Haystack `WWW-Authenticate` header for a SCRAM challenge.
462///
463/// Produces: `SCRAM handshakeToken=<token>, hash=<hash>, data=<data_b64>`
464pub fn format_www_authenticate(handshake_token: &str, hash: &str, data_b64: &str) -> String {
465    format!(
466        "SCRAM handshakeToken={}, hash={}, data={}",
467        handshake_token, hash, data_b64
468    )
469}
470
471/// Format a Haystack `Authentication-Info` header with the auth token.
472///
473/// Produces: `authToken=<token>, data=<data_b64>`
474pub fn format_auth_info(auth_token: &str, data_b64: &str) -> String {
475    format!("authToken={}, data={}", auth_token, data_b64)
476}
477
478// ---------------------------------------------------------------------------
479// Tests
480// ---------------------------------------------------------------------------
481
482#[cfg(test)]
483mod tests {
484    use super::*;
485
486    #[test]
487    fn test_derive_credentials() {
488        let password = "pencil";
489        let salt = b"random-salt-value";
490        let iterations = 4096;
491
492        let creds = derive_credentials(password, salt, iterations);
493
494        // Fields are populated correctly
495        assert_eq!(creds.salt, salt.to_vec());
496        assert_eq!(creds.iterations, iterations);
497        assert_eq!(creds.stored_key.len(), 32); // SHA-256 output length
498        assert_eq!(creds.server_key.len(), 32);
499
500        // Deterministic: same inputs produce same outputs
501        let creds2 = derive_credentials(password, salt, iterations);
502        assert_eq!(creds.stored_key, creds2.stored_key);
503        assert_eq!(creds.server_key, creds2.server_key);
504
505        // Different password yields different credentials
506        let creds3 = derive_credentials("other", salt, iterations);
507        assert_ne!(creds.stored_key, creds3.stored_key);
508        assert_ne!(creds.server_key, creds3.server_key);
509    }
510
511    #[test]
512    fn test_generate_nonce() {
513        let n1 = generate_nonce();
514        let n2 = generate_nonce();
515
516        // Each call produces a unique nonce
517        assert_ne!(n1, n2);
518
519        // Valid base64 encoding of 18 bytes
520        let decoded1 = BASE64.decode(&n1).expect("nonce must be valid base64");
521        assert_eq!(decoded1.len(), 18);
522
523        let decoded2 = BASE64.decode(&n2).expect("nonce must be valid base64");
524        assert_eq!(decoded2.len(), 18);
525    }
526
527    #[test]
528    fn test_parse_auth_header_hello() {
529        let username = "user";
530        let username_b64 = BASE64.encode(username.as_bytes());
531        let header = format!("HELLO username={}", username_b64);
532
533        let parsed = parse_auth_header(&header).unwrap();
534        assert_eq!(
535            parsed,
536            AuthHeader::Hello {
537                username: "user".to_string(),
538                data: None,
539            }
540        );
541    }
542
543    #[test]
544    fn test_parse_auth_header_scram() {
545        let header = "SCRAM handshakeToken=abc123, data=c29tZWRhdGE=";
546        let parsed = parse_auth_header(header).unwrap();
547        assert_eq!(
548            parsed,
549            AuthHeader::Scram {
550                handshake_token: "abc123".to_string(),
551                data: "c29tZWRhdGE=".to_string(),
552            }
553        );
554    }
555
556    #[test]
557    fn test_parse_auth_header_bearer() {
558        let header = "BEARER authToken=mytoken123";
559        let parsed = parse_auth_header(header).unwrap();
560        assert_eq!(
561            parsed,
562            AuthHeader::Bearer {
563                auth_token: "mytoken123".to_string(),
564            }
565        );
566    }
567
568    #[test]
569    fn test_parse_auth_header_invalid() {
570        // Unknown scheme
571        assert!(parse_auth_header("UNKNOWN foo=bar").is_err());
572        // HELLO missing username=
573        assert!(parse_auth_header("HELLO foo=bar").is_err());
574        // SCRAM missing data=
575        assert!(parse_auth_header("SCRAM handshakeToken=abc").is_err());
576        // BEARER missing authToken=
577        assert!(parse_auth_header("BEARER token=abc").is_err());
578        // Empty
579        assert!(parse_auth_header("").is_err());
580    }
581
582    #[test]
583    fn test_full_handshake() {
584        // Simulate the complete HELLO -> SCRAM -> BEARER flow.
585        let username = "testuser";
586        let password = "s3cret";
587        let salt = b"test-salt-12345";
588        let iterations = 4096;
589
590        // --- Server: pre-compute credentials (user registration) ---
591        let credentials = derive_credentials(password, salt, iterations);
592
593        // --- Client: HELLO phase ---
594        let username_b64 = BASE64.encode(username.as_bytes());
595        let hello_header = format!("HELLO username={}", username_b64);
596        let parsed = parse_auth_header(&hello_header).unwrap();
597        match &parsed {
598            AuthHeader::Hello { username: u, .. } => assert_eq!(u, username),
599            _ => panic!("expected Hello variant"),
600        }
601
602        // --- Client: generate client-first-message ---
603        let (client_nonce, _client_first_b64) = client_first_message(username);
604
605        // --- Server: generate server-first-message ---
606        let (handshake, server_first_b64) =
607            server_first_message(username, &client_nonce, &credentials);
608
609        // --- Server: format WWW-Authenticate header ---
610        let www_auth = format_www_authenticate("handshake-token-xyz", "SHA-256", &server_first_b64);
611        assert!(www_auth.contains("SCRAM"));
612        assert!(www_auth.contains("SHA-256"));
613        assert!(www_auth.contains("handshake-token-xyz"));
614
615        // --- Client: process server-first, produce client-final ---
616        let (client_final_b64, expected_server_sig) =
617            client_final_message(password, &client_nonce, &server_first_b64, username).unwrap();
618
619        // --- Server: verify client-final ---
620        let server_sig = server_verify_final(&handshake, &client_final_b64).unwrap();
621
622        // Server signature should match what the client expects
623        assert_eq!(server_sig, expected_server_sig);
624
625        // --- Server: format Authentication-Info header ---
626        let server_final_msg = format!("v={}", BASE64.encode(&server_sig));
627        let server_final_b64 = BASE64.encode(server_final_msg.as_bytes());
628        let auth_info = format_auth_info("auth-token-abc", &server_final_b64);
629        assert!(auth_info.contains("authToken=auth-token-abc"));
630
631        // --- Client: verify server signature from server-final ---
632        let server_final_decoded = BASE64.decode(&server_final_b64).unwrap();
633        let server_final_str = String::from_utf8(server_final_decoded).unwrap();
634        let sig_b64 = server_final_str.strip_prefix("v=").unwrap();
635        let received_server_sig = BASE64.decode(sig_b64).unwrap();
636        assert_eq!(received_server_sig, expected_server_sig);
637    }
638
639    #[test]
640    fn test_client_server_roundtrip() {
641        // Full roundtrip using the public API functions.
642        let username = "admin";
643        let password = "correcthorsebatterystaple";
644        let salt = b"unique-salt-value";
645        let iterations = DEFAULT_ITERATIONS;
646
647        // 1. Server: create credentials during user registration
648        let credentials = derive_credentials(password, salt, iterations);
649
650        // 2. Client: create client-first-message
651        let (client_nonce, client_first_b64) = client_first_message(username);
652
653        // Verify client-first is valid base64 and well-formed
654        let client_first_decoded = BASE64.decode(&client_first_b64).unwrap();
655        let client_first_str = String::from_utf8(client_first_decoded).unwrap();
656        assert!(client_first_str.starts_with("n,,"));
657        assert!(client_first_str.contains(&format!("r={}", client_nonce)));
658
659        // 3. Server: create server-first-message
660        let (handshake, server_first_b64) =
661            server_first_message(username, &client_nonce, &credentials);
662
663        // Verify server-first contains expected SCRAM fields
664        let server_first_decoded = BASE64.decode(&server_first_b64).unwrap();
665        let server_first_str = String::from_utf8(server_first_decoded).unwrap();
666        assert!(server_first_str.starts_with("r="));
667        assert!(server_first_str.contains(",s="));
668        assert!(server_first_str.contains(",i="));
669        assert!(server_first_str.contains(&client_nonce));
670
671        // 4. Client: create client-final-message
672        let (client_final_b64, expected_server_sig) =
673            client_final_message(password, &client_nonce, &server_first_b64, username).unwrap();
674
675        // Verify client-final structure
676        let client_final_decoded = BASE64.decode(&client_final_b64).unwrap();
677        let client_final_str = String::from_utf8(client_final_decoded).unwrap();
678        assert!(client_final_str.starts_with("c=biws,"));
679        assert!(client_final_str.contains(",p="));
680
681        // 5. Server: verify and get server signature
682        let server_sig = server_verify_final(&handshake, &client_final_b64).unwrap();
683        assert_eq!(server_sig, expected_server_sig);
684
685        // 6. Wrong password: server rejects the proof
686        let (wrong_final_b64, _) =
687            client_final_message("wrongpassword", &client_nonce, &server_first_b64, username)
688                .unwrap();
689        let result = server_verify_final(&handshake, &wrong_final_b64);
690        assert!(result.is_err());
691        match result {
692            Err(AuthError::InvalidCredentials) => {} // expected
693            other => panic!("expected InvalidCredentials, got {:?}", other),
694        }
695    }
696
697    #[test]
698    fn test_format_www_authenticate() {
699        let result = format_www_authenticate("tok123", "SHA-256", "c29tZQ==");
700        assert_eq!(
701            result,
702            "SCRAM handshakeToken=tok123, hash=SHA-256, data=c29tZQ=="
703        );
704    }
705
706    #[test]
707    fn test_format_auth_info() {
708        let result = format_auth_info("auth-tok", "ZGF0YQ==");
709        assert_eq!(result, "authToken=auth-tok, data=ZGF0YQ==");
710    }
711}