oxabl-cache-format 0.4.0

Byte formats for the oxabl-cache remote compile cache: frames, ids, limits.
Documentation
//! The client an ID token was issued to, from its `aud` and `azp` claims: the one rule that
//! the server, the owner's client and the CLI apply (OpenID Connect Core 1.0, section
//! 3.1.3.7, items 3 to 5). It lives here because this is the only crate both sides link.
//!
//! - `aud` is a string or an array of strings; anything else is refused.
//! - The client id must be a member of `aud` and be accepted.
//! - When `aud` has more than one member, `azp` must be present.
//! - When `azp` is present, whatever `aud` holds, it is the client id: it must be a member
//!   of `aud` and be accepted. When several members of `aud` are accepted, `azp` decides.
//!
//! The other members of `aud` are not checked. With `azp` equal to the client id, the issuer
//! issued the token to that client; an issuer such as Zitadel lists every application of a
//! project in `aud`. Without the `azp` rule, a token minted for another application of the
//! same project, which also holds this client id in `aud`, would pass.

/// An `aud` or `azp` claim as the caller read it from the token's JSON.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Claim<'t> {
    Absent,
    String(&'t str),
    Strings(Vec<&'t str>),
    /// Any other JSON value, or an array with a member that is not a string.
    Other,
}

/// The client id the token was issued to, when `accepted` takes it; `None` refuses the
/// token.
pub fn issued_to<'t>(
    aud: &Claim<'t>,
    azp: &Claim<'t>,
    accepted: impl Fn(&str) -> bool,
) -> Option<&'t str> {
    let members: &[&'t str] = match aud {
        Claim::String(one) => std::slice::from_ref(one),
        Claim::Strings(list) => list,
        Claim::Absent | Claim::Other => return None,
    };
    let client = match (azp, members) {
        (Claim::String(azp), _) => *azp,
        (Claim::Absent, [only]) => *only,
        _ => return None,
    };
    (members.contains(&client) && accepted(client)).then_some(client)
}