Skip to main content

wae_authentication/saml/
errors.rs

1//! SAML 错误类型定义
2
3use std::fmt;
4
5/// SAML 错误类型
6#[derive(Debug)]
7pub enum SamlError {
8    /// 无效的 SAML 请求
9    InvalidRequest(String),
10
11    /// 无效的 SAML 响应
12    InvalidResponse(String),
13
14    /// 无效的断言
15    InvalidAssertion(String),
16
17    /// 签名验证失败
18    SignatureVerificationFailed(String),
19
20    /// 缺少签名
21    MissingSignature,
22
23    /// 证书错误
24    CertificateError(String),
25
26    /// XML 解析错误
27    XmlParsingError(String),
28
29    /// Base64 解码错误
30    Base64DecodeError(String),
31
32    /// 压缩/解压错误
33    CompressionError(String),
34
35    /// 时间验证失败
36    TimeValidationError(String),
37
38    /// 断言已过期
39    AssertionExpired,
40
41    /// 断言尚未生效
42    AssertionNotYetValid,
43
44    /// 受众验证失败
45    AudienceValidationFailed { expected: String, actual: String },
46
47    /// 发行人验证失败
48    IssuerValidationFailed { expected: String, actual: String },
49
50    /// 目标验证失败
51    DestinationValidationFailed,
52
53    /// 重放攻击检测
54    ReplayAttackDetected,
55
56    /// 配置错误
57    ConfigurationError(String),
58
59    /// 元数据错误
60    MetadataError(String),
61
62    /// 绑定不支持
63    UnsupportedBinding(String),
64
65    /// 名称 ID 格式不支持
66    UnsupportedNameIdFormat(String),
67
68    /// 其他错误
69    Other(String),
70}
71
72impl fmt::Display for SamlError {
73    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
74        match self {
75            SamlError::InvalidRequest(msg) => write!(f, "Invalid SAML request: {}", msg),
76            SamlError::InvalidResponse(msg) => write!(f, "Invalid SAML response: {}", msg),
77            SamlError::InvalidAssertion(msg) => write!(f, "Invalid assertion: {}", msg),
78            SamlError::SignatureVerificationFailed(msg) => {
79                write!(f, "Signature verification failed: {}", msg)
80            }
81            SamlError::MissingSignature => write!(f, "Missing signature"),
82            SamlError::CertificateError(msg) => write!(f, "Certificate error: {}", msg),
83            SamlError::XmlParsingError(msg) => write!(f, "XML parsing error: {}", msg),
84            SamlError::Base64DecodeError(msg) => write!(f, "Base64 decode error: {}", msg),
85            SamlError::CompressionError(msg) => write!(f, "Compression error: {}", msg),
86            SamlError::TimeValidationError(msg) => write!(f, "Time validation failed: {}", msg),
87            SamlError::AssertionExpired => write!(f, "Assertion has expired"),
88            SamlError::AssertionNotYetValid => write!(f, "Assertion is not yet valid"),
89            SamlError::AudienceValidationFailed { expected, actual } => {
90                write!(f, "Audience validation failed: expected {}, got {}", expected, actual)
91            }
92            SamlError::IssuerValidationFailed { expected, actual } => {
93                write!(f, "Issuer validation failed: expected {}, got {}", expected, actual)
94            }
95            SamlError::DestinationValidationFailed => write!(f, "Destination validation failed"),
96            SamlError::ReplayAttackDetected => write!(f, "Replay attack detected"),
97            SamlError::ConfigurationError(msg) => write!(f, "Configuration error: {}", msg),
98            SamlError::MetadataError(msg) => write!(f, "Metadata error: {}", msg),
99            SamlError::UnsupportedBinding(binding) => write!(f, "Unsupported binding: {}", binding),
100            SamlError::UnsupportedNameIdFormat(format) => {
101                write!(f, "Unsupported name ID format: {}", format)
102            }
103            SamlError::Other(msg) => write!(f, "SAML error: {}", msg),
104        }
105    }
106}
107
108impl std::error::Error for SamlError {}
109
110/// SAML 结果类型
111pub type SamlResult<T> = Result<T, SamlError>;