aauth-protocol 0.1.0

AAuth (draft-hardt-oauth-aauth-protocol): HTTP message signatures, agent, person and auth tokens, and an agent-side Person Server client
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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
//! Agent, person and auth tokens: minting, and verification as AAuth
//! §Request-Context Binding prescribes. A port of `@aauth/resource`'s
//! `verifyToken`, in its order: structure first, then the key binding, then
//! the issuer's signature, and only then anything the claims say.

use serde_json::{json, Map, Value};

use crate::discovery::KeyResolver;
use crate::error::TokenError;
use crate::identifier::is_server_identifier;
use crate::jwk::{Algorithm, Jwk};
use crate::jwt;
use crate::keys::SigningKey;
use crate::metadata::dwk;

/// The four AAuth token types, by their JWT `typ`.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum TokenKind {
    /// `aa-agent+jwt`: an agent provider vouches for an agent and binds its key.
    Agent,
    /// `aa-person+jwt`: a Person Server names the person an agent acts for,
    /// to one resource. Identity, never authorization.
    Person,
    /// `aa-auth+jwt`: what a resource accepts as authorization.
    Auth,
    /// `aa-resource+jwt`: a resource asks the Person Server for an auth token.
    Resource,
}

impl TokenKind {
    /// The JWT `typ` header value.
    pub fn typ(self) -> &'static str {
        match self {
            TokenKind::Agent => "aa-agent+jwt",
            TokenKind::Person => "aa-person+jwt",
            TokenKind::Auth => "aa-auth+jwt",
            TokenKind::Resource => "aa-resource+jwt",
        }
    }

    /// The kind a JWT `typ` names.
    pub fn from_typ(typ: &str) -> Option<Self> {
        [
            TokenKind::Agent,
            TokenKind::Person,
            TokenKind::Auth,
            TokenKind::Resource,
        ]
        .into_iter()
        .find(|k| k.typ() == typ)
    }

    fn error_code(self) -> &'static str {
        match self {
            TokenKind::Agent => "invalid_agent_token",
            TokenKind::Person => "invalid_person_token",
            TokenKind::Auth => "invalid_auth_token",
            TokenKind::Resource => "invalid_resource_token",
        }
    }
}

/// Mint a token of `kind`, signed by `key` and naming it in `kid` by its
/// thumbprint. `claims` are written as given: the issuer decides them.
///
/// ```
/// use aauth_protocol::token::{mint, TokenKind};
/// use aauth_protocol::SigningKey;
///
/// let provider = SigningKey::generate();
/// let session = SigningKey::generate();
/// let agent_token = mint(&provider, TokenKind::Agent, &serde_json::json!({
///     "iss": "https://agents.example",
///     "dwk": "aauth-agent.json",
///     "sub": "aauth:travel@agents.example",
///     "iat": 1_791_000_000,
///     "exp": 1_791_003_600,
///     "cnf": { "jwk": session.public_jwk() },
/// }));
/// assert_eq!(agent_token.split('.').count(), 3);
/// ```
pub fn mint(key: &SigningKey, kind: TokenKind, claims: &Value) -> String {
    let header = json!({ "typ": kind.typ(), "kid": key.thumbprint() });
    jwt::encode(key, header.as_object().cloned().unwrap_or_default(), claims)
}

/// What [`verify_token`] checks a token against. Build one with
/// [`VerifyTokenOptions::new`].
#[derive(Clone, Debug)]
pub struct VerifyTokenOptions<'a> {
    /// The token, as the `jwt` parameter of `Signature-Key` carried it.
    pub jwt: &'a str,
    /// The thumbprint of the key that signed the HTTP request.
    pub http_signature_thumbprint: &'a str,
    /// This verifier's own server identifier: a person or auth token's `aud`.
    pub resource: &'a str,
    /// The kinds this call site accepts. A person token is identity, so a
    /// resource authorizing a request accepts only [`TokenKind::Auth`].
    pub accept: &'a [TokenKind],
    /// Seconds since the epoch.
    pub now: i64,
    /// How far ahead of `now` a token's `iat` may be.
    pub clock_tolerance: i64,
}

impl<'a> VerifyTokenOptions<'a> {
    /// Verify `jwt` now, with a 60-second tolerance for the issuer's clock.
    pub fn new(
        jwt: &'a str,
        http_signature_thumbprint: &'a str,
        resource: &'a str,
        accept: &'a [TokenKind],
    ) -> Self {
        Self {
            jwt,
            http_signature_thumbprint,
            resource,
            accept,
            now: crate::now_unix(),
            clock_tolerance: 60,
        }
    }
}

/// A token whose issuer's signature verified, bound to the key that signed
/// the request.
#[derive(Clone, Debug)]
#[non_exhaustive]
pub struct VerifiedToken {
    /// Which kind it is.
    pub kind: TokenKind,
    /// Its issuer.
    pub iss: String,
    /// The metadata document its issuer's keys were found through.
    pub dwk: String,
    /// The agent identifier, or the person's directed identifier.
    pub sub: String,
    /// The key the token binds, which signed the request.
    pub cnf_jwk: Jwk,
    /// When it was issued.
    pub iat: i64,
    /// When it expires.
    pub exp: i64,
    /// Every claim, for those without an accessor.
    pub claims: Map<String, Value>,
}

impl VerifiedToken {
    /// A non-empty string claim.
    pub fn claim_str(&self, name: &str) -> Option<&str> {
        self.claims
            .get(name)
            .and_then(Value::as_str)
            .filter(|s| !s.is_empty())
    }

    /// `jti`: the token's identifier at its issuer.
    pub fn jti(&self) -> Option<&str> {
        self.claim_str("jti")
    }

    /// An agent token's Person Server, or an auth token's.
    pub fn ps(&self) -> Option<&str> {
        self.claim_str("ps")
    }

    /// The mission the token was issued under.
    pub fn mission_s256(&self) -> Option<&str> {
        self.claim_str("mission_s256")
    }

    /// The person's organisation, at a multi-tenant Person Server.
    pub fn tenant(&self) -> Option<&str> {
        self.claim_str("tenant")
    }

    /// An auth token's scope.
    pub fn scope(&self) -> Option<&str> {
        self.claim_str("scope")
    }
}

fn aud_matches(aud: Option<&Value>, resource: &str) -> bool {
    match aud {
        Some(Value::String(a)) => a == resource,
        Some(Value::Array(list)) => list.iter().any(|a| a.as_str() == Some(resource)),
        _ => false,
    }
}

/// Verify an agent, person or auth token presented in `Signature-Key`.
///
/// In order: the `typ` is recognised and accepted here; the claims every
/// token of that kind needs are present; its `cnf.jwk` is the key that
/// signed the HTTP request; the issuer's key, found through `resolver`,
/// signed it; and only then its expiry, `iat` and `aud`. Nothing in the
/// claims is believed before the signature verifies, so `token_expired`
/// always means the named issuer minted a token that has run out.
///
/// Revocation is not checked here.
pub async fn verify_token(
    options: &VerifyTokenOptions<'_>,
    resolver: &dyn KeyResolver,
) -> Result<VerifiedToken, TokenError> {
    if options.accept.is_empty() {
        return Err(TokenError::new(
            "invalid_configuration",
            "accept names no token kinds",
        ));
    }
    if !is_server_identifier(options.resource) {
        return Err(TokenError::new(
            "invalid_configuration",
            format!("resource {} is not a server identifier", options.resource),
        ));
    }
    let decoded = jwt::decode(options.jwt)
        .map_err(|_| TokenError::new("malformed_token", "not a well-formed JWT"))?;
    let typ = decoded.typ().unwrap_or_default();
    let kind = TokenKind::from_typ(typ)
        .filter(|k| *k != TokenKind::Resource)
        .ok_or_else(|| {
            TokenError::new("unsupported_token_type", format!("unknown JWT typ {typ:?}"))
        })?;
    if !options.accept.contains(&kind) {
        return Err(TokenError::new(
            "token_type_not_accepted",
            format!("a {typ} is not accepted here"),
        ));
    }
    let code = kind.error_code();
    let alg = decoded.alg().unwrap_or_default();
    let algorithm = Algorithm::from_name(alg).ok_or_else(|| {
        TokenError::new(
            code,
            format!("JWT alg {alg:?} is not a fully-specified algorithm this verifier supports"),
        )
    })?;

    let required = |name: &str| {
        decoded
            .claim_str(name)
            .map(str::to_string)
            .ok_or_else(|| TokenError::new(code, format!("missing required claim: {name}")))
    };
    let iss = required("iss")?;
    let token_dwk = required("dwk")?;
    let number = |name: &str| {
        decoded
            .claims
            .get(name)
            .and_then(Value::as_i64)
            .ok_or_else(|| TokenError::new(code, format!("missing required claim: {name}")))
    };
    let iat = number("iat")?;
    let exp = number("exp")?;
    let expected_dwk: &[&str] = match kind {
        TokenKind::Agent => &[dwk::AGENT],
        TokenKind::Person => &[dwk::PERSON],
        _ => &[dwk::PERSON, dwk::ACCESS],
    };
    if !expected_dwk.contains(&token_dwk.as_str()) {
        return Err(TokenError::new(
            code,
            format!("unexpected dwk {token_dwk:?}"),
        ));
    }
    let cnf: Jwk = decoded
        .claims
        .get("cnf")
        .and_then(|c| c.get("jwk"))
        .cloned()
        .and_then(|j| serde_json::from_value(j).ok())
        .ok_or_else(|| TokenError::new(code, "missing required claim: cnf.jwk"))?;
    cnf.check_confirmation_key()
        .map_err(|e| TokenError::new(code, e.to_string()))?;
    let sub = required("sub")?;
    match kind {
        TokenKind::Person => {
            required("aud")?;
            required("jti")?;
        }
        TokenKind::Auth => {
            if decoded.claims.get("aud").is_none() {
                return Err(TokenError::new(code, "missing required claim: aud"));
            }
            required("ps")?;
        }
        _ => {}
    }

    let thumbprint = cnf
        .thumbprint()
        .map_err(|e| TokenError::new(code, e.to_string()))?;
    if !crate::constant_time_eq(&thumbprint, options.http_signature_thumbprint) {
        return Err(TokenError::new(
            "key_binding_failed",
            "cnf.jwk thumbprint does not match the HTTP signature key",
        ));
    }

    if !is_server_identifier(&iss) {
        return Err(TokenError::new(
            code,
            format!("iss is not a server identifier: {iss}"),
        ));
    }
    let jwks = resolver
        .jwks(&iss, &token_dwk)
        .await
        .map_err(|e| TokenError::new(code, e.to_string()))?;
    let key = match decoded.kid() {
        Some(kid) if !kid.is_empty() => jwks
            .keys
            .iter()
            .find(|k| k.kid.as_deref() == Some(kid))
            .ok_or_else(|| TokenError::new(code, format!("no key {kid:?} in the issuer's JWKS")))?,
        _ if jwks.keys.len() == 1 => &jwks.keys[0],
        _ => {
            return Err(TokenError::new(
                code,
                "the JWT names no kid and the issuer publishes more than one key",
            ))
        }
    };
    if key.alg.as_deref().is_some_and(|a| a != alg) {
        return Err(TokenError::new(
            code,
            "the issuer's key alg disagrees with the JWT's",
        ));
    }
    if !decoded.verify_signature(key, algorithm) {
        return Err(TokenError::new(code, "JWT signature verification failed"));
    }
    if options.now >= exp {
        return Err(TokenError::new("token_expired", "token has expired"));
    }
    if decoded
        .claims
        .get("nbf")
        .and_then(Value::as_i64)
        .is_some_and(|nbf| options.now < nbf)
    {
        return Err(TokenError::new(code, "token is not yet valid"));
    }
    if iat > options.now + options.clock_tolerance {
        return Err(TokenError::new(
            "clock_skew",
            format!(
                "token iat is {}s ahead of this verifier's clock",
                iat - options.now
            ),
        ));
    }
    if matches!(kind, TokenKind::Person | TokenKind::Auth)
        && !aud_matches(decoded.claims.get("aud"), options.resource)
    {
        return Err(TokenError::new(
            "aud_mismatch",
            "token aud is not this resource",
        ));
    }
    match kind {
        TokenKind::Agent => {
            if let Some(ps) = decoded.claim_str("ps") {
                if !is_server_identifier(ps) {
                    return Err(TokenError::new(
                        code,
                        format!("ps is not a server identifier: {ps}"),
                    ));
                }
            }
        }
        TokenKind::Person => {
            if decoded.claims.contains_key("scope") || decoded.claims.contains_key("account") {
                return Err(TokenError::new(
                    code,
                    "a person token MUST NOT carry scope or account",
                ));
            }
        }
        TokenKind::Auth => {
            if !decoded.claim_str("ps").is_some_and(is_server_identifier) {
                return Err(TokenError::new(code, "ps is not a server identifier"));
            }
        }
        TokenKind::Resource => {}
    }
    Ok(VerifiedToken {
        kind,
        iss,
        dwk: token_dwk,
        sub,
        cnf_jwk: cnf,
        iat,
        exp,
        claims: decoded.claims,
    })
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;
    use crate::discovery::{JwksResolver, StaticFetch};
    use std::sync::Arc;

    pub(crate) const AP: &str = "https://agents.example";

    pub(crate) fn provider(ap_key: &SigningKey) -> JwksResolver {
        let fetch = StaticFetch::default();
        fetch.insert(
            format!("{AP}/.well-known/aauth-agent.json"),
            json!({ "issuer": AP, "jwks_uri": format!("{AP}/jwks.json") }),
        );
        fetch.insert(
            format!("{AP}/jwks.json"),
            serde_json::to_value(ap_key.jwks()).unwrap(),
        );
        JwksResolver::new(Arc::new(fetch))
    }

    pub(crate) fn agent_token(ap_key: &SigningKey, session: &SigningKey, exp: i64) -> String {
        mint(
            ap_key,
            TokenKind::Agent,
            &json!({
                "iss": AP, "dwk": "aauth-agent.json", "sub": "aauth:travel@agents.example",
                "iat": 1_000, "exp": exp, "cnf": { "jwk": session.public_jwk() },
            }),
        )
    }

    fn options<'a>(jwt: &'a str, thumbprint: &'a str, now: i64) -> VerifyTokenOptions<'a> {
        VerifyTokenOptions {
            jwt,
            http_signature_thumbprint: thumbprint,
            resource: "https://ps.example",
            accept: &[TokenKind::Agent],
            now,
            clock_tolerance: 60,
        }
    }

    #[tokio::test]
    async fn an_agent_token_verifies_against_its_providers_keys() {
        let (ap, session) = (SigningKey::generate(), SigningKey::generate());
        let token = agent_token(&ap, &session, 5_000);
        let verified = verify_token(
            &options(&token, session.thumbprint(), 1_000),
            &provider(&ap),
        )
        .await
        .unwrap();
        assert_eq!(verified.sub, "aauth:travel@agents.example");
        assert_eq!(verified.kind, TokenKind::Agent);
    }

    #[tokio::test]
    async fn a_token_bound_to_another_key_is_refused() {
        let (ap, session) = (SigningKey::generate(), SigningKey::generate());
        let token = agent_token(&ap, &session, 5_000);
        let other = SigningKey::generate();
        let refused = verify_token(&options(&token, other.thumbprint(), 1_000), &provider(&ap))
            .await
            .unwrap_err();
        assert_eq!(refused.code, "key_binding_failed");
    }

    #[tokio::test]
    async fn expiry_is_judged_only_once_the_signature_verifies() {
        let (ap, session) = (SigningKey::generate(), SigningKey::generate());
        let expired = agent_token(&ap, &session, 1_500);
        let refused = verify_token(
            &options(&expired, session.thumbprint(), 2_000),
            &provider(&ap),
        )
        .await
        .unwrap_err();
        assert_eq!(refused.code, "token_expired");

        let forged = agent_token(&SigningKey::generate(), &session, 1_500);
        let refused = verify_token(
            &options(&forged, session.thumbprint(), 2_000),
            &provider(&ap),
        )
        .await
        .unwrap_err();
        assert_eq!(refused.code, "invalid_agent_token");
    }

    #[tokio::test]
    async fn a_kind_not_accepted_here_is_refused() {
        let (ap, session) = (SigningKey::generate(), SigningKey::generate());
        let token = agent_token(&ap, &session, 5_000);
        let mut opts = options(&token, session.thumbprint(), 1_000);
        opts.accept = &[TokenKind::Auth];
        let refused = verify_token(&opts, &provider(&ap)).await.unwrap_err();
        assert_eq!(refused.code, "token_type_not_accepted");
    }
}