rs-identity-model 0.6.2

Production-grade, RFC-compliant OIDC/OAuth2 client library for Rust
Documentation
//! Crate-wide error type for identity-model.

use thiserror::Error;

/// The unified error type returned by all fallible identity-model operations.
///
/// Every public function returns `Result<T, IdentityError>`.
#[derive(Debug, Error)]
pub enum IdentityError {
    /// An HTTP transport or status error.
    #[error("http error: {0}")]
    Http(String),

    /// A response body could not be deserialized as expected.
    #[error("deserialization error: {0}")]
    Deserialization(String),

    /// A token or document failed validation (signature, claims, issuer, etc.).
    #[error("validation error: {0}")]
    Validation(String),

    /// An ID Token failed one of the OpenID Connect ID-Token *profile* rules
    /// layered on top of standard JWT validation — the required `sub`, the
    /// `azp` authorized-party rules, or the opt-in `nonce` / `max_age` /
    /// `at_hash` / `c_hash` bindings (OIDC Core 1.0 §3.1.3.7 / §3.3.2.11). Kept
    /// distinct from [`IdentityError::Validation`] (signature/iss/aud/exp) so a
    /// caller can tell a profile violation apart from a base-JWT failure. The
    /// profile fails **closed**: an unknown or missing header `alg` on an
    /// `at_hash`/`c_hash` check is this error, never a silently skipped check.
    #[error("id token validation error: {0}")]
    IdTokenValidation(String),
    /// A caller-supplied claims validator (issue #603) rejected the token's
    /// claims. Carries a structured reason and the offending claim name (when
    /// the validator identified one), so a rejection surfaces *why* without
    /// parsing a message string. Kept distinct from [`IdentityError::Validation`]
    /// so a validation pipeline — and `combine_claims_validators` in `Any` mode —
    /// can tell a clean claims rejection from any other failure. Constructed from
    /// a [`crate::ClaimsValidationError`] via `From`.
    #[error("claims validation error: {reason}{}", .claim.as_deref().map(|c| format!(" (claim {c:?})")).unwrap_or_default())]
    ClaimsValidation {
        /// Why the claims were rejected.
        reason: String,
        /// The offending claim name, when the validator identified one.
        claim: Option<String>,
    },

    /// A client or builder was misconfigured (missing required fields).
    #[error("configuration error: {0}")]
    Configuration(String),

    /// A signing key with the requested `kid` was not found in the JWKS.
    #[error("key not found: {0}")]
    KeyNotFound(String),

    /// The token endpoint replied with a non-2xx OAuth 2.0 error response
    /// (RFC 6749 §5.2).
    #[error("token endpoint error {error:?}{}: HTTP {status}", .description.as_deref().map(|d| format!(": {d}")).unwrap_or_default())]
    TokenEndpoint {
        /// The RFC 6749 §5.2 `error` code, e.g. `invalid_client`.
        error: String,
        /// The human-readable `error_description`, if present.
        description: Option<String>,
        /// The `error_uri` pointing at documentation, if present.
        error_uri: Option<String>,
        /// The HTTP status of the error response.
        status: u16,
    },

    /// The UserInfo endpoint replied with a non-2xx status (OIDC Core 1.0
    /// §5.3.3). Carries the HTTP status and any `WWW-Authenticate` challenge a
    /// Bearer-protected resource returns to describe the failure (RFC 6750 §3).
    #[error("userinfo endpoint error: HTTP {status}{}", .www_authenticate.as_deref().map(|w| format!(": {w}")).unwrap_or_default())]
    UserInfo {
        /// The HTTP status of the error response.
        status: u16,
        /// The `WWW-Authenticate` challenge header, if present.
        www_authenticate: Option<String>,
        /// A short snippet of the response body for diagnostics.
        body: String,
    },
}

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

    #[test]
    fn display_messages() {
        assert_eq!(
            IdentityError::Validation("token expired".into()).to_string(),
            "validation error: token expired"
        );
        assert_eq!(
            IdentityError::KeyNotFound("abc".into()).to_string(),
            "key not found: abc"
        );
    }

    #[test]
    fn is_std_error() {
        // Confirms IdentityError participates in the std error ecosystem.
        fn assert_error<E: std::error::Error>(_: &E) {}
        assert_error(&IdentityError::Http("boom".into()));
    }
}