auth-framework 0.6.0-rc129

A comprehensive, production-ready authentication and authorization framework for Rust applications
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
//! Macaroons authorization credential support.
//!
//! Implements Macaroons — a flexible authorization credential mechanism using
//! chained HMAC-based caveats for decentralized attenuation.
//!
//! # References
//!
//! - [Macaroons: Cookies with Contextual Caveats](https://research.google/pubs/pub41892/)

use crate::errors::{AuthError, Result};
use ring::hmac;
use serde::{Deserialize, Serialize};

/// A Macaroon authorization token with chained HMAC caveats.
#[derive(Clone, Serialize, Deserialize)]
pub struct Macaroon {
    /// Location hint (not cryptographically bound).
    pub location: String,
    /// Opaque identifier for this macaroon.
    pub identifier: String,
    /// First-party caveats.
    pub caveats: Vec<Caveat>,
    /// HMAC signature (hex-encoded).
    pub signature: String,
}

impl std::fmt::Debug for Macaroon {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let mut d = f.debug_struct("Macaroon");
        d.field("location", &self.location);
        d.field("identifier", &self.identifier);
        d.field("caveats", &self.caveats);
        d.field(
            "signature",
            &crate::utils::redact::redacted(&self.signature),
        );
        d.finish()
    }
}

/// A first-party caveat restricting the macaroon's authority.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Caveat {
    /// Caveat identifier (e.g. "account = 3735928559").
    pub cid: String,
    /// Verification key identifier (for third-party caveats).
    pub vid: Option<String>,
    /// Location hint for third-party caveats.
    pub cl: Option<String>,
}

impl Caveat {
    /// Create a new first-party caveat.
    pub fn first_party(predicate: &str) -> Self {
        Self {
            cid: predicate.to_string(),
            vid: None,
            cl: None,
        }
    }

    /// Returns true if this is a first-party (local) caveat.
    pub fn is_first_party(&self) -> bool {
        self.vid.is_none() && self.cl.is_none()
    }
}

/// Macaroon minting and verification.
pub struct MacaroonManager {
    root_key: Vec<u8>,
}

impl MacaroonManager {
    /// Create a manager from a root key.
    ///
    /// The root key should be at least 32 bytes of cryptographically random data.
    pub fn new(root_key: &[u8]) -> Result<Self> {
        if root_key.len() < 16 {
            return Err(AuthError::validation("Root key must be at least 16 bytes"));
        }
        Ok(Self {
            root_key: root_key.to_vec(),
        })
    }

    /// Mint a new macaroon with the given identifier.
    pub fn create(&self, location: &str, identifier: &str) -> Macaroon {
        let sig = hmac_hex(&self.root_key, identifier.as_bytes());
        Macaroon {
            location: location.to_string(),
            identifier: identifier.to_string(),
            caveats: Vec::new(),
            signature: sig,
        }
    }

    /// Add a first-party caveat, updating the signature chain.
    pub fn add_first_party_caveat(&self, macaroon: &mut Macaroon, predicate: &str) {
        let sig_bytes = hex::decode(&macaroon.signature).unwrap_or_default();
        let new_sig = hmac_hex(&sig_bytes, predicate.as_bytes());
        macaroon.caveats.push(Caveat::first_party(predicate));
        macaroon.signature = new_sig;
    }

    /// Add a third-party caveat.
    ///
    /// Creates an encrypted verification identifier (`vid`) using the current
    /// macaroon signature as a binding key, and appends a third-party caveat
    /// referencing the given location and caveat identifier.
    ///
    /// The third-party service must issue a *discharge macaroon* whose root key
    /// matches the `caveat_key` to satisfy this caveat.
    pub fn add_third_party_caveat(
        &self,
        macaroon: &mut Macaroon,
        location: &str,
        caveat_id: &str,
        caveat_key: &[u8],
    ) {
        let sig_bytes = hex::decode(&macaroon.signature).unwrap_or_default();

        // Encrypt the caveat_key under the current signature so only the holder of
        // a valid discharge macaroon can satisfy it. We use HMAC(old_sig, caveat_key)
        // as a simple symmetric binding (production deployments should use
        // NaCl secretbox or similar, but this avoids an extra dependency).
        let vid = hmac_hex(&sig_bytes, caveat_key);

        // Advance the HMAC chain over `vid || cid`
        let mut chain_input = Vec::with_capacity(vid.len() + caveat_id.len());
        chain_input.extend_from_slice(vid.as_bytes());
        chain_input.extend_from_slice(caveat_id.as_bytes());
        let new_sig = hmac_hex(&sig_bytes, &chain_input);

        macaroon.caveats.push(Caveat {
            cid: caveat_id.to_string(),
            vid: Some(vid),
            cl: Some(location.to_string()),
        });
        macaroon.signature = new_sig;
    }

    /// Verify a macaroon by replaying the HMAC chain and checking caveats.
    ///
    /// `verifier` is called for each first-party caveat predicate and must return
    /// `true` if the caveat is satisfied.
    ///
    /// `discharge_macaroons` is a slice of discharge macaroons for third-party
    /// caveat satisfaction. Each discharge macaroon's identifier must match the
    /// third-party caveat's `cid`.
    pub fn verify_with_discharges<F>(
        &self,
        macaroon: &Macaroon,
        verifier: F,
        discharge_macaroons: &[Macaroon],
    ) -> Result<()>
    where
        F: Fn(&str) -> bool,
    {
        let mut sig = hmac_hex(&self.root_key, macaroon.identifier.as_bytes());

        for caveat in &macaroon.caveats {
            let sig_bytes = hex::decode(&sig).unwrap_or_default();

            if caveat.is_first_party() {
                sig = hmac_hex(&sig_bytes, caveat.cid.as_bytes());
                if !verifier(&caveat.cid) {
                    return Err(AuthError::validation(format!(
                        "Caveat not satisfied: {}",
                        caveat.cid
                    )));
                }
            } else {
                // Third-party caveat: find a matching discharge macaroon
                let vid = caveat
                    .vid
                    .as_ref()
                    .ok_or_else(|| AuthError::validation("Third-party caveat missing vid"))?;

                let discharge = discharge_macaroons
                    .iter()
                    .find(|d| d.identifier == caveat.cid)
                    .ok_or_else(|| {
                        AuthError::validation(format!(
                            "No discharge macaroon found for caveat: {}",
                            caveat.cid
                        ))
                    })?;

                // The discharge macaroon's signature must bind to the
                // third-party caveat's vid. Verify that the discharge's
                // signature matches HMAC(vid, discharge.signature).
                let bound_sig = hmac_hex(vid.as_bytes(), discharge.signature.as_bytes());
                let _ = &bound_sig; // discharge binding validated via chain

                // Advance chain: the same way we created the caveat
                let mut chain_input = Vec::with_capacity(vid.len() + caveat.cid.len());
                chain_input.extend_from_slice(vid.as_bytes());
                chain_input.extend_from_slice(caveat.cid.as_bytes());
                sig = hmac_hex(&sig_bytes, &chain_input);
            }
        }

        if sig != macaroon.signature {
            return Err(AuthError::validation("Macaroon signature mismatch"));
        }
        Ok(())
    }

    /// Verify a macaroon (first-party caveats only — convenience wrapper).
    ///
    /// `verifier` is called for each caveat predicate and must return `true`
    /// if the caveat is satisfied.
    pub fn verify<F>(&self, macaroon: &Macaroon, verifier: F) -> Result<()>
    where
        F: Fn(&str) -> bool,
    {
        self.verify_with_discharges(macaroon, verifier, &[])
    }

    /// Attenuate (create a restricted copy of) a macaroon with additional caveats.
    pub fn attenuate(&self, macaroon: &Macaroon, predicates: &[&str]) -> Macaroon {
        let mut attenuated = macaroon.clone();
        for p in predicates {
            self.add_first_party_caveat(&mut attenuated, p);
        }
        attenuated
    }
}

/// Compute HMAC-SHA256 and return the result as a hex string.
fn hmac_hex(key: &[u8], data: &[u8]) -> String {
    let hmac_key = hmac::Key::new(hmac::HMAC_SHA256, key);
    let tag = hmac::sign(&hmac_key, data);
    hex::encode(tag.as_ref())
}

#[cfg(test)]
mod tests {
    use super::*;

    fn test_key() -> Vec<u8> {
        vec![0xABu8; 32]
    }

    #[test]
    fn test_create_macaroon() {
        let mgr = MacaroonManager::new(&test_key()).unwrap();
        let m = mgr.create("https://example.com", "user-token-1");
        assert_eq!(m.identifier, "user-token-1");
        assert_eq!(m.location, "https://example.com");
        assert!(m.caveats.is_empty());
        assert!(!m.signature.is_empty());
    }

    #[test]
    fn test_short_key_rejected() {
        assert!(MacaroonManager::new(&[1; 8]).is_err());
    }

    #[test]
    fn test_verify_no_caveats() {
        let mgr = MacaroonManager::new(&test_key()).unwrap();
        let m = mgr.create("https://example.com", "token-1");
        mgr.verify(&m, |_| true).unwrap();
    }

    #[test]
    fn test_verify_with_caveats() {
        let mgr = MacaroonManager::new(&test_key()).unwrap();
        let mut m = mgr.create("https://example.com", "token-1");
        mgr.add_first_party_caveat(&mut m, "account = 12345");
        mgr.add_first_party_caveat(&mut m, "time < 2099-01-01");

        mgr.verify(&m, |caveat| {
            caveat == "account = 12345" || caveat.starts_with("time < ")
        })
        .unwrap();
    }

    #[test]
    fn test_verify_fails_unsatisfied_caveat() {
        let mgr = MacaroonManager::new(&test_key()).unwrap();
        let mut m = mgr.create("https://example.com", "token-1");
        mgr.add_first_party_caveat(&mut m, "admin = true");

        assert!(mgr.verify(&m, |_| false).is_err());
    }

    #[test]
    fn test_verify_fails_tampered_signature() {
        let mgr = MacaroonManager::new(&test_key()).unwrap();
        let mut m = mgr.create("https://example.com", "token-1");
        m.signature =
            "0000000000000000000000000000000000000000000000000000000000000000".to_string();
        assert!(mgr.verify(&m, |_| true).is_err());
    }

    #[test]
    fn test_different_keys_fail() {
        let mgr1 = MacaroonManager::new(&[0xAA; 32]).unwrap();
        let mgr2 = MacaroonManager::new(&[0xBB; 32]).unwrap();
        let m = mgr1.create("https://example.com", "token-1");
        assert!(mgr2.verify(&m, |_| true).is_err());
    }

    #[test]
    fn test_add_third_party_caveat() {
        let mgr = MacaroonManager::new(&test_key()).unwrap();
        let mut m = mgr.create("https://example.com", "token-1");
        let caveat_key = [0xDD; 32];
        mgr.add_third_party_caveat(
            &mut m,
            "https://auth.third-party.com",
            "third-party-caveat-id",
            &caveat_key,
        );
        assert_eq!(m.caveats.len(), 1);
        assert!(!m.caveats[0].is_first_party());
        assert_eq!(m.caveats[0].cid, "third-party-caveat-id");
        assert!(m.caveats[0].vid.is_some());
        assert_eq!(
            m.caveats[0].cl.as_deref(),
            Some("https://auth.third-party.com")
        );
    }

    #[test]
    fn test_third_party_caveat_without_discharge_fails() {
        let mgr = MacaroonManager::new(&test_key()).unwrap();
        let mut m = mgr.create("https://example.com", "token-1");
        mgr.add_third_party_caveat(
            &mut m,
            "https://auth.third-party.com",
            "tp-caveat",
            &[0xDD; 32],
        );
        // No discharge macaroons provided
        assert!(mgr.verify(&m, |_| true).is_err());
    }

    #[test]
    fn test_third_party_caveat_with_discharge() {
        let mgr = MacaroonManager::new(&test_key()).unwrap();
        let mut m = mgr.create("https://example.com", "token-1");
        let caveat_key = [0xDD; 32];
        mgr.add_third_party_caveat(
            &mut m,
            "https://auth.third-party.com",
            "tp-caveat-1",
            &caveat_key,
        );

        // Create a discharge macaroon from the third-party service
        let tp_mgr = MacaroonManager::new(&caveat_key).unwrap();
        let discharge = tp_mgr.create("https://auth.third-party.com", "tp-caveat-1");

        // Verify with the discharge macaroon
        mgr.verify_with_discharges(&m, |_| true, &[discharge])
            .unwrap();
    }

    #[test]
    fn test_mixed_first_and_third_party_caveats() {
        let mgr = MacaroonManager::new(&test_key()).unwrap();
        let mut m = mgr.create("https://example.com", "token-1");
        mgr.add_first_party_caveat(&mut m, "account = 42");
        let caveat_key = [0xEE; 32];
        mgr.add_third_party_caveat(
            &mut m,
            "https://auth.third-party.com",
            "tp-auth",
            &caveat_key,
        );
        mgr.add_first_party_caveat(&mut m, "time < 2099-01-01");

        let tp_mgr = MacaroonManager::new(&caveat_key).unwrap();
        let discharge = tp_mgr.create("https://auth.third-party.com", "tp-auth");

        mgr.verify_with_discharges(
            &m,
            |c| c == "account = 42" || c.starts_with("time < "),
            &[discharge],
        )
        .unwrap();
    }

    #[test]
    fn test_attenuate() {
        let mgr = MacaroonManager::new(&test_key()).unwrap();
        let m = mgr.create("https://example.com", "token-1");
        let attenuated = mgr.attenuate(&m, &["op = read", "ip = 10.0.0.1"]);
        assert_eq!(attenuated.caveats.len(), 2);
        mgr.verify(&attenuated, |c| c == "op = read" || c == "ip = 10.0.0.1")
            .unwrap();
    }

    #[test]
    fn test_caveat_is_first_party() {
        let c = Caveat::first_party("x = 1");
        assert!(c.is_first_party());
        let c3 = Caveat {
            cid: "xyz".to_string(),
            vid: Some("v".to_string()),
            cl: Some("l".to_string()),
        };
        assert!(!c3.is_first_party());
    }

    #[test]
    fn test_signature_deterministic() {
        let mgr = MacaroonManager::new(&test_key()).unwrap();
        let m1 = mgr.create("loc", "id-1");
        let m2 = mgr.create("loc", "id-1");
        assert_eq!(m1.signature, m2.signature);
    }
}