rs_identity_model/error.rs
1//! Crate-wide error type for identity-model.
2
3use thiserror::Error;
4
5/// The unified error type returned by all fallible identity-model operations.
6///
7/// Every public function returns `Result<T, IdentityError>`.
8#[derive(Debug, Error)]
9pub enum IdentityError {
10 /// An HTTP transport or status error.
11 #[error("http error: {0}")]
12 Http(String),
13
14 /// A response body could not be deserialized as expected.
15 #[error("deserialization error: {0}")]
16 Deserialization(String),
17
18 /// A token or document failed validation (signature, claims, issuer, etc.).
19 #[error("validation error: {0}")]
20 Validation(String),
21
22 /// An ID Token failed one of the OpenID Connect ID-Token *profile* rules
23 /// layered on top of standard JWT validation — the required `sub`, the
24 /// `azp` authorized-party rules, or the opt-in `nonce` / `max_age` /
25 /// `at_hash` / `c_hash` bindings (OIDC Core 1.0 §3.1.3.7 / §3.3.2.11). Kept
26 /// distinct from [`IdentityError::Validation`] (signature/iss/aud/exp) so a
27 /// caller can tell a profile violation apart from a base-JWT failure. The
28 /// profile fails **closed**: an unknown or missing header `alg` on an
29 /// `at_hash`/`c_hash` check is this error, never a silently skipped check.
30 #[error("id token validation error: {0}")]
31 IdTokenValidation(String),
32 /// A caller-supplied claims validator (issue #603) rejected the token's
33 /// claims. Carries a structured reason and the offending claim name (when
34 /// the validator identified one), so a rejection surfaces *why* without
35 /// parsing a message string. Kept distinct from [`IdentityError::Validation`]
36 /// so a validation pipeline — and `combine_claims_validators` in `Any` mode —
37 /// can tell a clean claims rejection from any other failure. Constructed from
38 /// a [`crate::ClaimsValidationError`] via `From`.
39 #[error("claims validation error: {reason}{}", .claim.as_deref().map(|c| format!(" (claim {c:?})")).unwrap_or_default())]
40 ClaimsValidation {
41 /// Why the claims were rejected.
42 reason: String,
43 /// The offending claim name, when the validator identified one.
44 claim: Option<String>,
45 },
46
47 /// A DPoP proof failed verification (RFC 9449 §4.3, DPOP-006).
48 ///
49 /// `field` names the offending proof member — `"alg"`, `"typ"`, `"jwk"`,
50 /// `"signature"`, `"jti"`, `"htm"`, `"htu"`, `"iat"`, `"ath"`, `"nonce"` — so
51 /// a caller can tell a method/URI mismatch from a signature or structural
52 /// failure without parsing a message string. Kept distinct from
53 /// [`IdentityError::Validation`] because a rejected proof is a
54 /// *sender-constraint* failure: the access token it accompanied may itself be
55 /// perfectly valid, and the right response is a `DPoP` challenge rather than a
56 /// plain token rejection.
57 #[error("dpop proof verification failed{}: {reason}", .field.as_deref().map(|f| format!(" for {f:?}")).unwrap_or_default())]
58 DpopVerification {
59 /// The offending proof member, when one can be named.
60 field: Option<String>,
61 /// Why the proof was rejected.
62 reason: String,
63 },
64
65 /// A client or builder was misconfigured (missing required fields).
66 #[error("configuration error: {0}")]
67 Configuration(String),
68
69 /// A signing key with the requested `kid` was not found in the JWKS.
70 #[error("key not found: {0}")]
71 KeyNotFound(String),
72
73 /// The token endpoint replied with a non-2xx OAuth 2.0 error response
74 /// (RFC 6749 §5.2).
75 #[error("token endpoint error {error:?}{}: HTTP {status}", .description.as_deref().map(|d| format!(": {d}")).unwrap_or_default())]
76 TokenEndpoint {
77 /// The RFC 6749 §5.2 `error` code, e.g. `invalid_client`.
78 error: String,
79 /// The human-readable `error_description`, if present.
80 description: Option<String>,
81 /// The `error_uri` pointing at documentation, if present.
82 error_uri: Option<String>,
83 /// The HTTP status of the error response.
84 status: u16,
85 },
86
87 /// The UserInfo endpoint replied with a non-2xx status (OIDC Core 1.0
88 /// §5.3.3). Carries the HTTP status and any `WWW-Authenticate` challenge a
89 /// Bearer-protected resource returns to describe the failure (RFC 6750 §3).
90 #[error("userinfo endpoint error: HTTP {status}{}", .www_authenticate.as_deref().map(|w| format!(": {w}")).unwrap_or_default())]
91 UserInfo {
92 /// The HTTP status of the error response.
93 status: u16,
94 /// The `WWW-Authenticate` challenge header, if present.
95 www_authenticate: Option<String>,
96 /// A short snippet of the response body for diagnostics.
97 body: String,
98 },
99}
100
101#[cfg(test)]
102mod tests {
103 use super::*;
104
105 #[test]
106 fn display_messages() {
107 assert_eq!(
108 IdentityError::Validation("token expired".into()).to_string(),
109 "validation error: token expired"
110 );
111 assert_eq!(
112 IdentityError::KeyNotFound("abc".into()).to_string(),
113 "key not found: abc"
114 );
115 assert_eq!(
116 IdentityError::Configuration("missing token_endpoint".into()).to_string(),
117 "configuration error: missing token_endpoint"
118 );
119 }
120
121 #[test]
122 fn is_std_error() {
123 // Confirms IdentityError participates in the std error ecosystem.
124 fn assert_error<E: std::error::Error>(_: &E) {}
125 assert_error(&IdentityError::Http("boom".into()));
126 }
127}