Skip to main content

wae_authentication/oauth2/
errors.rs

1//! OAuth2 错误类型定义
2
3use std::fmt;
4
5/// OAuth2 错误类型
6#[derive(Debug)]
7pub enum OAuth2Error {
8    /// 无效的授权码
9    InvalidAuthorizationCode,
10
11    /// 无效的重定向 URI
12    InvalidRedirectUri,
13
14    /// 无效的客户端 ID
15    InvalidClientId,
16
17    /// 无效的客户端密钥
18    InvalidClientSecret,
19
20    /// 无效的授权范围
21    InvalidScope(String),
22
23    /// 访问令牌无效
24    InvalidAccessToken,
25
26    /// 刷新令牌无效
27    InvalidRefreshToken,
28
29    /// 令牌已过期
30    TokenExpired,
31
32    /// 授权被拒绝
33    AccessDenied(String),
34
35    /// 不支持的授权类型
36    UnsupportedGrantType(String),
37
38    /// 不支持的响应类型
39    UnsupportedResponseType(String),
40
41    /// 状态不匹配
42    StateMismatch,
43
44    /// 配置错误
45    ConfigurationError(String),
46
47    /// 网络请求错误
48    RequestError(String),
49
50    /// 解析错误
51    ParseError(String),
52
53    /// 提供者错误
54    ProviderError(String),
55
56    /// 其他错误
57    Other(String),
58}
59
60impl fmt::Display for OAuth2Error {
61    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
62        match self {
63            OAuth2Error::InvalidAuthorizationCode => write!(f, "Invalid authorization code"),
64            OAuth2Error::InvalidRedirectUri => write!(f, "Invalid redirect URI"),
65            OAuth2Error::InvalidClientId => write!(f, "Invalid client ID"),
66            OAuth2Error::InvalidClientSecret => write!(f, "Invalid client secret"),
67            OAuth2Error::InvalidScope(scope) => write!(f, "Invalid scope: {}", scope),
68            OAuth2Error::InvalidAccessToken => write!(f, "Invalid access token"),
69            OAuth2Error::InvalidRefreshToken => write!(f, "Invalid refresh token"),
70            OAuth2Error::TokenExpired => write!(f, "Token has expired"),
71            OAuth2Error::AccessDenied(msg) => write!(f, "Authorization denied: {}", msg),
72            OAuth2Error::UnsupportedGrantType(grant_type) => {
73                write!(f, "Unsupported grant type: {}", grant_type)
74            }
75            OAuth2Error::UnsupportedResponseType(response_type) => {
76                write!(f, "Unsupported response type: {}", response_type)
77            }
78            OAuth2Error::StateMismatch => write!(f, "State mismatch"),
79            OAuth2Error::ConfigurationError(msg) => write!(f, "Configuration error: {}", msg),
80            OAuth2Error::RequestError(msg) => write!(f, "Request error: {}", msg),
81            OAuth2Error::ParseError(msg) => write!(f, "Parse error: {}", msg),
82            OAuth2Error::ProviderError(msg) => write!(f, "Provider error: {}", msg),
83            OAuth2Error::Other(msg) => write!(f, "OAuth2 error: {}", msg),
84        }
85    }
86}
87
88impl std::error::Error for OAuth2Error {}
89
90impl From<url::ParseError> for OAuth2Error {
91    fn from(err: url::ParseError) -> Self {
92        OAuth2Error::ParseError(err.to_string())
93    }
94}
95
96impl From<serde_json::Error> for OAuth2Error {
97    fn from(err: serde_json::Error) -> Self {
98        OAuth2Error::ParseError(err.to_string())
99    }
100}
101
102/// OAuth2 结果类型
103pub type OAuth2Result<T> = Result<T, OAuth2Error>;