tower_oauth2_resource_server/
error.rs1use std::{error::Error, fmt::Display};
2
3use http::{HeaderValue, Response, StatusCode, header::WWW_AUTHENTICATE};
4use jsonwebtoken::{Algorithm, jwk::KeyAlgorithm};
5
6#[derive(Clone, Debug, PartialEq)]
7pub enum StartupError {
8 InvalidParameter(String),
9 OidcDiscoveryFailed(String),
10}
11
12impl Display for StartupError {
13 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14 write!(f, "{:?}", self)
15 }
16}
17impl Error for StartupError {}
18
19#[derive(Clone, Debug, PartialEq)]
20pub enum JwkError {
21 FetchFailed,
22 ParseFailed,
23 MissingKeyId,
24 DecodingFailed,
25}
26
27impl Display for JwkError {
28 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29 write!(f, "{:?}", self)
30 }
31}
32impl Error for JwkError {}
33
34#[derive(Clone, Debug, PartialEq)]
35pub enum AuthError {
36 MissingAuthorizationHeader,
37 InvalidAuthorizationHeader,
38 ParseJwtError,
39 InvalidKeyId,
40 InvalidJwkAlgorithm(KeyAlgorithm),
41 MismatchingAlgorithm(Algorithm, Algorithm),
42 UnsupportedAlgorithm(Algorithm),
43 ValidationFailed {
44 reason: jsonwebtoken::errors::ErrorKind,
45 },
46 AuthorizerNotFound,
47}
48
49impl Display for AuthError {
50 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
51 write!(f, "{:?}", self)
52 }
53}
54impl Error for AuthError {}
55
56impl<B> From<AuthError> for Response<B>
57where
58 B: Default,
59{
60 fn from(e: AuthError) -> Self {
61 let mut response = Response::builder()
62 .status(StatusCode::UNAUTHORIZED)
63 .body(B::default())
64 .unwrap();
65 if e == AuthError::MissingAuthorizationHeader || e == AuthError::InvalidAuthorizationHeader
66 {
67 response
68 .headers_mut()
69 .insert(WWW_AUTHENTICATE, HeaderValue::from_str("Bearer").unwrap());
70 }
71 response
72 }
73}