axiam-sdk 1.2.2

Official Rust client SDK for AXIAM IAM
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
//! gRPC `validate_token` / `introspect_token` (CONTRACT.md §1.1.1, contract
//! 1.51) — the wrappers over `axiam.v1.TokenService` that §10.3 needed and no
//! SDK had.
//!
//! Until 1.51 §10.3 obliged an SDK validating over gRPC to read the response's
//! `cnf`, while §1's closed vocabulary permitted no method that could return
//! it: the stubs were generated here and wrapped by nothing. This module is
//! the wrapper, and its whole reason to exist is the confirmation.
//!
//! # Two tokens, kept apart
//!
//! Every call carries **two** credentials, and confusing them is the mistake
//! this API is shaped against (§1.1.1 rule 1):
//!
//! * the **caller's** own access token, which the [`AuthInterceptor`] puts in
//!   `authorization` exactly as for every other RPC, and which the server
//!   enforces like any other — including its own `cnf`;
//! * the **inspected** token, the one being asked about, which travels in the
//!   request message. It is a [`Sensitive`] argument with no default: nothing
//!   here falls back to the caller's token when it is omitted, because it
//!   cannot be omitted.
//!
//! # `valid` is not "usable as presented"
//!
//! `valid` / `active` mean the signature, expiry and tenant check out. When the
//! token carries `cnf`, whoever presented it to *you* must also prove
//! possession of the named key, against *your* connection — the AXIAM server
//! cannot, because the proof is not on the connection carrying this RPC
//! (§10.3 rule 2). So each result offers [`TokenValidation::status`], which
//! says which of the three cases this is without letting a caller treat a bound
//! token as a bearer one by reading one boolean, and
//! [`TokenValidation::verify_possession`], which applies §10.1 rule 9 with the
//! proofs you hold.
//!
//! Boundness is decided from `cnf` alone, never from `token_type`: the server
//! reports `"Bearer"` for a certificate-bound token, and only a `jkt`-bound one
//! is `"DPoP"` (§1.1.1 rule 5).

use std::future::Future;
use std::sync::Arc;

use tonic::Code;
use tonic::service::interceptor::InterceptedService;
use tonic::transport::Channel;
use uuid::Uuid;

use crate::grpc::client::{RefreshFn, status_to_axiam_error};
use crate::grpc::r#gen::token_service_client::TokenServiceClient;
use crate::grpc::r#gen::{
    CnfClaim as WireCnfClaim, IntrospectTokenRequest,
    IntrospectTokenResponse as WireIntrospectTokenResponse, RptPermission as WireRptPermission,
    ValidateTokenRequest, ValidateTokenResponse as WireValidateTokenResponse,
};
use crate::grpc::interceptor::AuthInterceptor;
use crate::token::{CnfClaim, PresentedProofs, TokenManager};
use crate::{AxiamError, Sensitive};

/// What a validation or introspection result means for the presenter — the
/// three cases §10.1 rule 9 and §10.3 distinguish.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum TokenStatus {
    /// `valid` / `active` is `false`: expired, badly signed, revoked, or a
    /// token of **another tenant** — which the server reports as inactive, not
    /// as an error (§1.1.1 rule 6).
    Inactive,
    /// Valid, and carries no `cnf`: an ordinary bearer token. Whoever holds it
    /// may use it.
    Bearer,
    /// Valid, **and sender-constrained**: it carries a `cnf` naming at least
    /// one method this SDK can check. Usable only by a presenter that proves
    /// possession — call `verify_possession` with the proofs from your own
    /// connection.
    SenderConstrained,
    /// Valid, but its `cnf` names **no** method this SDK can check — an empty
    /// `CnfClaim` message, which proto3 is the only way to spell. Refused, not
    /// read as unbound (§10.3 rule 3): `verify_possession` never accepts it.
    Unverifiable,
}

fn status_of(valid: bool, cnf: Option<&CnfClaim>) -> TokenStatus {
    match (valid, cnf) {
        (false, _) => TokenStatus::Inactive,
        (true, None) => TokenStatus::Bearer,
        (true, Some(c)) if c.names_nothing_checkable() => TokenStatus::Unverifiable,
        (true, Some(_)) => TokenStatus::SenderConstrained,
    }
}

fn verify_possession_of(
    valid: bool,
    cnf: Option<&CnfClaim>,
    proofs: PresentedProofs<'_>,
) -> Result<(), AxiamError> {
    if !valid {
        return Err(AxiamError::auth(
            "the token is not valid (expired, revoked, badly signed, or of another tenant)",
        ));
    }
    match cnf {
        None => Ok(()),
        Some(cnf) => cnf.verify(proofs),
    }
}

/// `proto3` cannot tell an absent string from an empty one, so an empty member
/// is an absent member. The message itself being absent is the distinction
/// that survives the wire, and is kept: `None` is unbound, `Some` with both
/// members `None` is the §10.3 rule 3 case.
fn cnf_from_wire(wire: Option<WireCnfClaim>) -> Option<CnfClaim> {
    wire.map(|c| CnfClaim {
        x5t_s256: non_empty(c.x5t_s256),
        jkt: non_empty(c.jkt),
    })
}

fn non_empty(value: String) -> Option<String> {
    if value.is_empty() { None } else { Some(value) }
}

/// The result of [`TokenGrpcClient::validate_token`] — every field of
/// `ValidateTokenResponse` (§1.1.1 rule 3).
///
/// **`valid` is not permission to proceed.** Read [`Self::status`], or call
/// [`Self::verify_possession`]; see the module documentation.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct TokenValidation {
    /// The server's verdict on signature, expiry and tenant — and nothing
    /// more. `false` for a token of another tenant, with every other field
    /// empty (§1.1.1 rule 6).
    pub valid: bool,
    /// Subject UUID; empty when not valid.
    pub subject_id: String,
    /// Tenant UUID; empty when not valid.
    pub tenant_id: String,
    /// Organization UUID; empty when not valid.
    pub org_id: String,
    /// Expiry, seconds since the epoch; `0` when not valid.
    pub exp: i64,
    /// The RFC 7800 confirmation. `None` means **unbound**; `Some` with both
    /// members `None` is an empty confirmation, which is refused, never read
    /// as unbound (§10.3 rule 3). Always carried, whether or not the caller
    /// looks at it — the field is the reason this method exists.
    pub cnf: Option<CnfClaim>,
    /// `"Bearer"` or `"DPoP"`. **Does not say whether the token is bound**: a
    /// certificate-bound token is `"Bearer"` (§1.1.1 rule 5). Use
    /// [`Self::status`].
    pub token_type: String,
}

impl TokenValidation {
    /// Which of the §10.1 rule 9 cases this is, decided from `valid` and `cnf`
    /// — never from `token_type`.
    #[must_use]
    pub fn status(&self) -> TokenStatus {
        status_of(self.valid, self.cnf.as_ref())
    }

    /// Whether the **presenter** may use this token: `valid`, and every sender
    /// constraint its `cnf` names satisfied by `proofs` (§10.1 rule 9, §10.3
    /// rule 2).
    ///
    /// `proofs` are what *your* connection established — the peer
    /// certificate's thumbprint, a DPoP proof you verified — never values taken
    /// from a request header the caller controls. An unbound valid token is
    /// accepted with or without proofs.
    ///
    /// # Errors
    ///
    /// [`AxiamError::Auth`] when the token is not valid, or when its `cnf` is
    /// not satisfied — including an empty `cnf`, which nothing satisfies.
    pub fn verify_possession(&self, proofs: PresentedProofs<'_>) -> Result<(), AxiamError> {
        verify_possession_of(self.valid, self.cnf.as_ref(), proofs)
    }
}

impl From<WireValidateTokenResponse> for TokenValidation {
    fn from(wire: WireValidateTokenResponse) -> Self {
        Self {
            valid: wire.valid,
            subject_id: wire.subject_id,
            tenant_id: wire.tenant_id,
            org_id: wire.org_id,
            exp: wire.exp,
            cnf: cnf_from_wire(wire.cnf),
            token_type: wire.token_type,
        }
    }
}

/// A UMA 2.0 permission carried by an RPT (CONTRACT.md §20) — one entry of
/// [`TokenIntrospection::permissions`].
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct RptPermission {
    /// Resource UUID.
    pub resource_id: String,
    /// Scopes granted on that resource.
    pub resource_scopes: Vec<String>,
    /// Expiry of this permission, seconds since the epoch.
    pub exp: i64,
}

impl From<WireRptPermission> for RptPermission {
    fn from(wire: WireRptPermission) -> Self {
        Self {
            resource_id: wire.resource_id,
            resource_scopes: wire.resource_scopes,
            exp: wire.exp,
        }
    }
}

/// The result of [`TokenGrpcClient::introspect_token`] — every field of
/// `IntrospectTokenResponse`, the RFC 7662 set (§1.1.1 rule 3).
///
/// **`active` is not permission to proceed.** Read [`Self::status`], or call
/// [`Self::verify_possession`]; see the module documentation.
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct TokenIntrospection {
    /// Signature, expiry and tenant check out — and nothing more. `false` for a
    /// token of another tenant (§1.1.1 rule 6).
    pub active: bool,
    /// Subject UUID.
    pub sub: String,
    /// Tenant UUID.
    pub tenant_id: String,
    /// Organization UUID.
    pub org_id: String,
    /// Issuer.
    pub iss: String,
    /// Issued-at, seconds since the epoch.
    pub iat: i64,
    /// Expiry, seconds since the epoch.
    pub exp: i64,
    /// Token id.
    pub jti: String,
    /// Space-separated granted scopes; `None` when the token carries none.
    pub scope: Option<String>,
    /// The client the token was issued to, when it was issued to one.
    pub client_id: Option<String>,
    /// `"Bearer"` or `"DPoP"`. **Does not say whether the token is bound** —
    /// see [`TokenValidation::token_type`].
    pub token_type: String,
    /// The RFC 7800 confirmation; `None` is unbound, and an empty one is
    /// refused (§10.3 rule 3). See [`TokenValidation::cnf`].
    pub cnf: Option<CnfClaim>,
    /// UMA 2.0 RPT permissions (§20); empty for any other token.
    pub permissions: Vec<RptPermission>,
    /// The foreign issuer whose subject token bought this one (token exchange,
    /// §15.7), when there was one.
    pub ext_exchange_iss: Option<String>,
}

impl TokenIntrospection {
    /// As [`TokenValidation::status`].
    #[must_use]
    pub fn status(&self) -> TokenStatus {
        status_of(self.active, self.cnf.as_ref())
    }

    /// As [`TokenValidation::verify_possession`].
    ///
    /// # Errors
    ///
    /// [`AxiamError::Auth`] when the token is not active, or its `cnf` is not
    /// satisfied by `proofs`.
    pub fn verify_possession(&self, proofs: PresentedProofs<'_>) -> Result<(), AxiamError> {
        verify_possession_of(self.active, self.cnf.as_ref(), proofs)
    }
}

impl From<WireIntrospectTokenResponse> for TokenIntrospection {
    fn from(wire: WireIntrospectTokenResponse) -> Self {
        Self {
            active: wire.active,
            sub: wire.sub,
            tenant_id: wire.tenant_id,
            org_id: wire.org_id,
            iss: wire.iss,
            iat: wire.iat,
            exp: wire.exp,
            jti: wire.jti,
            scope: non_empty(wire.scope),
            client_id: non_empty(wire.client_id),
            token_type: wire.token_type,
            cnf: cnf_from_wire(wire.cnf),
            permissions: wire.permissions.into_iter().map(Into::into).collect(),
            ext_exchange_iss: non_empty(wire.ext_exchange_iss),
        }
    }
}

type InnerClient = TokenServiceClient<InterceptedService<Channel, AuthInterceptor>>;

/// gRPC transport client for `TokenService` — `validate_token` and
/// `introspect_token` (CONTRACT.md §1.1.1).
///
/// Built like [`crate::grpc::UserInfoGrpcClient`]: on the shared
/// lazily-connected [`Channel`], with the same [`AuthInterceptor`] putting the
/// caller's token and `x-tenant-id` on every RPC, and the same
/// `UNAUTHENTICATED` → single-flight refresh → retry-once (§9), which applies
/// to the **caller's** token only.
#[derive(Clone)]
pub struct TokenGrpcClient {
    inner: InnerClient,
    token_manager: Arc<TokenManager>,
    tenant_id: Uuid,
    refresh_fn: RefreshFn,
}

impl std::fmt::Debug for TokenGrpcClient {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("TokenGrpcClient")
            .field("tenant_id", &self.tenant_id)
            .finish_non_exhaustive()
    }
}

impl TokenGrpcClient {
    /// Wrap a shared [`Channel`] with the auth/tenant interceptor. Share the
    /// same `TokenManager` as the rest of the client so the caller's token is
    /// the one a `login()` produced; `refresh_fn` follows
    /// [`crate::grpc::AuthzGrpcClient::new`].
    pub fn new(
        channel: Channel,
        token_manager: Arc<TokenManager>,
        tenant_id: Uuid,
        refresh_fn: RefreshFn,
    ) -> Self {
        let interceptor = AuthInterceptor::new(Arc::clone(&token_manager), tenant_id);
        Self {
            inner: TokenServiceClient::with_interceptor(channel, interceptor),
            token_manager,
            tenant_id,
            refresh_fn,
        }
    }

    /// The tenant UUID this client was constructed with.
    pub fn tenant_id(&self) -> Uuid {
        self.tenant_id
    }

    /// `TokenService/ValidateToken` — validate `token` (signature, expiry,
    /// tenant) and return every field the server sends, `cnf` included.
    ///
    /// `token` is the token being **asked about**, not the caller's own; the
    /// caller's token authenticates the call and comes from the shared
    /// `TokenManager`.
    ///
    /// # Errors
    ///
    /// * [`AxiamError::Auth`], **with no wire call**, when this client holds no
    ///   caller token (§1.1.1 rule 2) — log in first.
    /// * The §2 mapping of any other gRPC status. A token of another tenant is
    ///   **not** an error: it comes back with `valid: false` (rule 6).
    pub async fn validate_token(
        &self,
        token: &Sensitive<String>,
    ) -> Result<TokenValidation, AxiamError> {
        self.require_caller_token("validate_token")?;
        let request = || ValidateTokenRequest {
            access_token: token.expose().clone(),
        };
        self.call(|| {
            let mut client = self.inner.clone();
            let request = request();
            async move { client.validate_token(request).await }
        })
        .await
        .map(Into::into)
    }

    /// `TokenService/IntrospectToken` — the RFC 7662 view of `token`, every
    /// field included, `cnf` among them.
    ///
    /// Same two-token rule and the same errors as [`Self::validate_token`].
    ///
    /// # Errors
    ///
    /// As [`Self::validate_token`].
    pub async fn introspect_token(
        &self,
        token: &Sensitive<String>,
    ) -> Result<TokenIntrospection, AxiamError> {
        self.require_caller_token("introspect_token")?;
        let request = || IntrospectTokenRequest {
            access_token: token.expose().clone(),
        };
        self.call(|| {
            let mut client = self.inner.clone();
            let request = request();
            async move { client.introspect_token(request).await }
        })
        .await
        .map(Into::into)
    }

    /// §1.1 rule 3 via §1.1.1 rule 2: no caller token, no wire call.
    ///
    /// The interceptor would also refuse, with `UNAUTHENTICATED` — but that
    /// status is what sends a call to the §9 guard, and a guard handed no token
    /// has nothing to refresh. Refusing here names the mistake instead.
    fn require_caller_token(&self, operation: &str) -> Result<(), AxiamError> {
        if self.token_manager.cached_access_token().is_none() {
            return Err(AxiamError::auth(format!(
                "{operation}: no caller token — call login() first (the token being inspected is \
                 a separate argument and never authenticates the call)"
            )));
        }
        Ok(())
    }

    /// One attempt, and on `UNAUTHENTICATED` one single-flight refresh of the
    /// **caller's** token and one retry (§9).
    async fn call<T, F, Fut>(&self, attempt: F) -> Result<T, AxiamError>
    where
        F: Fn() -> Fut,
        Fut: Future<Output = Result<tonic::Response<T>, tonic::Status>>,
    {
        match attempt().await {
            Ok(response) => Ok(response.into_inner()),
            Err(status) if status.code() == Code::Unauthenticated => {
                // CONTRACT 1.52 N4.5 (C-12): see
                // `AuthzGrpcClient::refresh_and_retry` — a credential with no
                // refresh token is never refreshed, and surfaces the
                // server's own message rather than the guard's.
                if !self.token_manager.has_refresh_token().await {
                    return Err(status_to_axiam_error(status));
                }
                let observed = self
                    .token_manager
                    .cached_access_token()
                    .map(|t| t.expose().clone())
                    .unwrap_or_default();
                let refresh_fn = Arc::clone(&self.refresh_fn);
                self.token_manager
                    .refresh_if_needed(&observed, move |refresh_token| refresh_fn(refresh_token))
                    .await?;
                attempt()
                    .await
                    .map(tonic::Response::into_inner)
                    .map_err(status_to_axiam_error)
            }
            Err(status) => Err(status_to_axiam_error(status)),
        }
    }
}