chronix_security/auth/
error.rs1#[derive(Debug, thiserror::Error)]
5pub enum AuthError {
6 #[error("invalid api key: {0}")]
8 InvalidApiKey(String),
9
10 #[error("api key expired: {0}")]
12 ExpiredApiKey(String),
13
14 #[error("invalid jwt: {0}")]
16 InvalidJwt(String),
17
18 #[error("jwt expired")]
20 ExpiredJwt,
21
22 #[error("invalid client certificate: {0}")]
24 InvalidCertificate(String),
25
26 #[error("no authentication provided")]
28 NoAuth,
29
30 #[error("encryption error: {0}")]
32 Encryption(String),
33
34 #[error("decryption error: {0}")]
36 Decryption(String),
37
38 #[error("key not found: {0}")]
40 KeyNotFound(String),
41
42 #[error("auth config error: {0}")]
44 Config(String),
45}
46
47impl AuthError {
48 #[must_use]
52 pub fn client_message(&self) -> &'static str {
53 match self {
54 Self::InvalidApiKey(_) | Self::InvalidJwt(_) | Self::InvalidCertificate(_) => {
55 "invalid credentials"
56 }
57 Self::ExpiredApiKey(_) | Self::ExpiredJwt => "credentials expired",
58 Self::NoAuth => "authentication required",
59 Self::Encryption(_) | Self::Decryption(_) | Self::KeyNotFound(_) => "internal error",
60 Self::Config(_) => "service misconfigured",
61 }
62 }
63
64 #[must_use]
66 pub fn is_auth_failure(&self) -> bool {
67 matches!(
68 self,
69 Self::InvalidApiKey(_)
70 | Self::InvalidJwt(_)
71 | Self::InvalidCertificate(_)
72 | Self::ExpiredApiKey(_)
73 | Self::ExpiredJwt
74 | Self::NoAuth
75 )
76 }
77}