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
//! 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 DPoP proof failed verification (RFC 9449 §4.3, DPOP-006).
///
/// `field` names the offending proof member — `"alg"`, `"typ"`, `"jwk"`,
/// `"signature"`, `"jti"`, `"htm"`, `"htu"`, `"iat"`, `"ath"`, `"nonce"` — so
/// a caller can tell a method/URI mismatch from a signature or structural
/// failure without parsing a message string. Kept distinct from
/// [`IdentityError::Validation`] because a rejected proof is a
/// *sender-constraint* failure: the access token it accompanied may itself be
/// perfectly valid, and the right response is a `DPoP` challenge rather than a
/// plain token rejection.
#[error("dpop proof verification failed{}: {reason}", .field.as_deref().map(|f| format!(" for {f:?}")).unwrap_or_default())]
DpopVerification {
/// The offending proof member, when one can be named.
field: Option<String>,
/// Why the proof was rejected.
reason: 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"
);
assert_eq!(
IdentityError::Configuration("missing token_endpoint".into()).to_string(),
"configuration error: missing token_endpoint"
);
}
#[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()));
}
}