common_access_token/error.rs
1//! Error types for the Common Access Token library.
2//!
3//! This module defines the error types used throughout the library.
4//! It provides a comprehensive set of errors that can occur during
5//! token generation, validation, and processing.
6//!
7//! The main `Error` enum implements the standard `Error` trait and
8//! provides detailed error messages for debugging and troubleshooting.
9
10use thiserror::Error;
11
12/// Errors that can occur during CAT token operations.
13///
14/// This enum represents all the possible errors that can occur when
15/// generating, validating, or processing CAT tokens. Each variant
16/// provides specific information about what went wrong.
17///
18/// # Examples
19///
20/// ```
21/// use common_access_token::{Cat, CatOptions, CatValidationOptions, CatValidationType};
22/// use std::collections::HashMap;
23///
24/// // Create a CAT instance with a key
25/// let key = hex::decode("403697de87af64611c1d32a05dab0fe1fcb715a86ab435f1ec99192d79569388").unwrap();
26/// let cat = Cat::new(CatOptions {
27/// keys: HashMap::from([("key-1".to_string(), key)]),
28/// expect_cwt_tag: true,
29/// });
30///
31/// // Try to validate an invalid token
32/// let result = cat.validate(
33/// "invalid-token",
34/// CatValidationType::Mac,
35/// &CatValidationOptions {
36/// issuer: "issuer".to_string(),
37/// audience: None,
38/// },
39/// );
40///
41/// // Handle the error
42/// match result {
43/// Ok(_) => println!("Token is valid"),
44/// Err(err) => println!("Error: {}", err),
45/// }
46/// ```
47#[derive(Error, Debug)]
48pub enum Error {
49 #[error("CBOR encoding error: {0}")]
50 CborEncoding(#[from] ciborium::ser::Error<std::io::Error>),
51
52 #[error("CBOR decoding error: {0}")]
53 CborDecoding(#[from] ciborium::de::Error<std::io::Error>),
54
55 #[error("IO error: {0}")]
56 Io(#[from] std::io::Error),
57
58 #[error("Base64 decoding error: {0}")]
59 Base64Decoding(#[from] base64::DecodeError),
60
61 #[error("JSON error: {0}")]
62 Json(#[from] serde_json::Error),
63
64 #[error("Key not found: {0}")]
65 KeyNotFound(String),
66
67 #[error("Invalid issuer: expected {expected}, got {actual}")]
68 InvalidIssuer { expected: String, actual: String },
69
70 #[error("Token expired")]
71 TokenExpired,
72
73 #[error("Token not yet active")]
74 TokenNotActive,
75
76 #[error("Invalid audience: {0:?}")]
77 InvalidAudience(Vec<String>),
78
79 #[error("Invalid claim type")]
80 InvalidClaimType,
81
82 #[error("Tag mismatch")]
83 TagMismatch,
84
85 #[error("Expected CWT tag")]
86 ExpectedCwtTag,
87
88 #[error("Failed to MAC token")]
89 FailedToMac,
90
91 #[error("Unsupported validation type")]
92 UnsupportedValidationType,
93
94 #[error("Unable to parse token")]
95 UnableToParseToken,
96
97 #[error("Missing options for MAC validation")]
98 MissingOptionsForMacValidation,
99}