oxabl_cache_format/audience.rs
1//! The client an ID token was issued to, from its `aud` and `azp` claims: the one rule that
2//! the server, the owner's client and the CLI apply (OpenID Connect Core 1.0, section
3//! 3.1.3.7, items 3 to 5). It lives here because this is the only crate both sides link.
4//!
5//! - `aud` is a string or an array of strings; anything else is refused.
6//! - The client id must be a member of `aud` and be accepted.
7//! - When `aud` has more than one member, `azp` must be present.
8//! - When `azp` is present, whatever `aud` holds, it is the client id: it must be a member
9//! of `aud` and be accepted. When several members of `aud` are accepted, `azp` decides.
10//!
11//! The other members of `aud` are not checked. With `azp` equal to the client id, the issuer
12//! issued the token to that client; an issuer such as Zitadel lists every application of a
13//! project in `aud`. Without the `azp` rule, a token minted for another application of the
14//! same project, which also holds this client id in `aud`, would pass.
15
16/// An `aud` or `azp` claim as the caller read it from the token's JSON.
17#[derive(Debug, Clone, PartialEq, Eq)]
18pub enum Claim<'t> {
19 Absent,
20 String(&'t str),
21 Strings(Vec<&'t str>),
22 /// Any other JSON value, or an array with a member that is not a string.
23 Other,
24}
25
26/// The client id the token was issued to, when `accepted` takes it; `None` refuses the
27/// token.
28pub fn issued_to<'t>(
29 aud: &Claim<'t>,
30 azp: &Claim<'t>,
31 accepted: impl Fn(&str) -> bool,
32) -> Option<&'t str> {
33 let members: &[&'t str] = match aud {
34 Claim::String(one) => std::slice::from_ref(one),
35 Claim::Strings(list) => list,
36 Claim::Absent | Claim::Other => return None,
37 };
38 let client = match (azp, members) {
39 (Claim::String(azp), _) => *azp,
40 (Claim::Absent, [only]) => *only,
41 _ => return None,
42 };
43 (members.contains(&client) && accepted(client)).then_some(client)
44}