rustbasic_passport/
error.rs1use serde::Serialize;
2use thiserror::Error as ThisError;
3
4#[derive(Debug, ThisError)]
5pub enum Error {
6 #[error("invalid client credentials")]
7 InvalidClient,
8 #[error("client not found")]
9 ClientNotFound,
10 #[error("invalid grant: {0}")]
11 InvalidGrant(String),
12 #[error("invalid request: {0}")]
13 InvalidRequest(String),
14 #[error("invalid scope: {0}")]
15 InvalidScope(String),
16 #[error("unauthorized: {0}")]
17 Unauthorized(String),
18 #[error("token has been revoked")]
19 TokenRevoked,
20 #[error("token has expired")]
21 TokenExpired,
22 #[error("storage error: {0}")]
23 Storage(String),
24 #[error("personal access client has not been configured")]
25 PersonalAccessClientNotFound,
26 #[error("unsupported grant type: {0}")]
27 UnknownGrant(String),
28 #[error("unsupported response type: {0}")]
29 UnsupportedResponseType(String),
30 #[error("redirect uri does not match the client")]
31 RedirectUriMismatch,
32 #[error("pkce verification failed")]
33 PkceVerificationFailed,
34 #[error("unsupported code challenge method: {0}")]
35 UnsupportedPkceMethod(String),
36 #[error("device code not approved yet")]
37 AuthorizationPending,
38 #[error("polling too frequently, slow down")]
39 SlowDown,
40 #[error("device code has expired")]
41 DeviceCodeExpired,
42 #[error("device authorization was denied by the user")]
43 AccessDenied,
44 #[error("jwt error: {0}")]
45 Jwt(#[from] jsonwebtoken::errors::Error),
46 #[error("rsa error: {0}")]
47 Rsa(#[from] rsa::errors::Error),
48 #[error("pkcs8 error: {0}")]
49 Pkcs8(#[from] rsa::pkcs8::Error),
50 #[error("spki error: {0}")]
51 Spki(#[from] rsa::pkcs8::spki::Error),
52 #[error("base64 error: {0}")]
53 Base64(#[from] base64::DecodeError),
54 #[error("io error: {0}")]
55 Io(#[from] std::io::Error),
56}
57
58#[derive(Debug, Clone, Serialize)]
59pub struct OAuthError {
60 pub error: &'static str,
61 #[serde(skip_serializing_if = "Option::is_none")]
62 pub error_description: Option<String>,
63}
64
65impl OAuthError {
66 pub fn status(&self) -> u16 {
67 match self.error {
68 "invalid_client" | "invalid_token" => 401,
69 _ => 400,
70 }
71 }
72}
73
74impl Error {
75 pub fn to_oauth_error(&self) -> OAuthError {
76 match self {
77 Error::InvalidClient => OAuthError {
78 error: "invalid_client",
79 error_description: Some(self.to_string()),
80 },
81 Error::UnknownGrant(g) => OAuthError {
82 error: "unsupported_grant_type",
83 error_description: Some(format!("grant type `{g}` is not supported")),
84 },
85 Error::UnsupportedResponseType(rt) => OAuthError {
86 error: "unsupported_response_type",
87 error_description: Some(format!("response type `{rt}` is not supported")),
88 },
89 Error::InvalidGrant(_)
90 | Error::RedirectUriMismatch
91 | Error::PkceVerificationFailed
92 | Error::UnsupportedPkceMethod(_) => OAuthError {
93 error: "invalid_grant",
94 error_description: Some(self.to_string()),
95 },
96 Error::InvalidScope(_) => OAuthError {
97 error: "invalid_scope",
98 error_description: Some(self.to_string()),
99 },
100 Error::InvalidRequest(_) => OAuthError {
101 error: "invalid_request",
102 error_description: Some(self.to_string()),
103 },
104 Error::Unauthorized(_) | Error::TokenRevoked | Error::TokenExpired => OAuthError {
105 error: "invalid_token",
106 error_description: Some(self.to_string()),
107 },
108 Error::AuthorizationPending => OAuthError {
109 error: "authorization_pending",
110 error_description: Some(self.to_string()),
111 },
112 Error::SlowDown => OAuthError {
113 error: "slow_down",
114 error_description: Some(self.to_string()),
115 },
116 Error::DeviceCodeExpired => OAuthError {
117 error: "expired_token",
118 error_description: Some(self.to_string()),
119 },
120 Error::AccessDenied => OAuthError {
121 error: "access_denied",
122 error_description: Some(self.to_string()),
123 },
124 _ => OAuthError {
125 error: "server_error",
126 error_description: Some(self.to_string()),
127 },
128 }
129 }
130}