1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//! # confirm_email
//!
//! `confirm_email` is a lightweight Rust crate for generating and validating
//! URL-safe, encrypted email confirmation tokens with configurable expiry.
//!
//! ## Purpose
//! The `confirm_email` crate addresses a common requirement in user registration systems: verifying that users have access to the email addresses they provide. When users register for an account, the system needs to confirm their email address before fully activating their account or granting access to certain features.
//!
//! This library generates secure, encrypted tokens that can be embedded in confirmation emails sent to users. When users click the confirmation link, the application validates the token to verify the email address and complete the registration process. The tokens contain the user's email address and an expiration timestamp, ensuring that confirmation links remain valid only for a specified period.
//!
//! The crate is designed for applications that need reliable email verification without the complexity of managing token storage in databases or external services. All token information is self-contained and cryptographically protected, making the system both secure and stateless.
//! ## Features
//!
//! - Generate a token containing an email address and expiration timestamp, encrypted
//! and encoded as a compact string.
//! - Configure token validity duration (default: 1 day).
//! - Validate and decrypt a token, returning the original email or a descriptive error
//! if the token is invalid or expired.
//!
//! ## Quickstart
//!
//! Add this to your `Cargo.toml`:
//!
//! ```toml
//! [dependencies]
//! confirm_email = "0.1"
//! ```
//!
//! ## Usage
//!
//! ```rust
//! use confirm_email::{generate_token, generate_token_with_expiration, validate_token};
//! use confirm_email::error::Error;
//!
//! // 1. Generate a token with default expiry (1 day):
//! let token = generate_token(
//! "user@example.com".to_string(),
//! "super_secret_key".to_string(),
//! ).expect("Error generating token with default validity");
//!
//! // 2. Generate a token with custom expiry (e.g., 3600 seconds = 1 hour):
//! let hour_token = generate_token_with_expiration(
//! "user@example.com".to_string(),
//! "super_secret_key".to_string(),
//! 3600,
//! ).expect("Error generating token with custom validity");
//!
//! // 3. Validate and decrypt the token:
//! match validate_token(token.clone(), "super_secret_key".to_string()) {
//! Ok(email) => println!("Confirmed email: {}", email),
//! Err(Error::Expired(ts)) => eprintln!("Token expired at {}", ts),
//! Err(e) => eprintln!("Invalid token: {}", e),
//! }
//! ```
//!
//! ## API
//!
//! - [`generate_token(email, key)`]
//! Generate a token for `email` using `key`, valid for the default duration (1 day).
//!
//! - [`generate_token_with_expiration(email, key, exp_seconds)`]
//! Generate a token for `email` using `key`, valid for `exp_seconds` seconds.
//!
//! - [`validate(token, key)`]
//! Decrypt and verify `token` with `key`. Returns the original email on success or
//! an [`Other`] on failure (`Expired`, `Invalid`, etc.).
//!
//! ## License
//!
//! MIT
use crate;
use crateError;
use crateExpired;
use cratePayload;
use ;
/// Contains helper functions for encryption and decryption
/// Contains definitions for the possible errors
/// Contains the definition of the token's payload
/// Contains tests
const DEFAULT_VALIDITY_DAYS: i64 = 1;
/// Generates a token with a specified expiration time in seconds.
///
/// This function creates a token that expires after the specified number of seconds. It uses
/// the provided email and key to generate the token.
///
/// # Arguments
///
/// * `email` - A `String` representing the user's email address.
/// * `key` - A `String` representing the encryption key.
/// * `exp_seconds` - An `i64` representing the number of seconds until the token expires. **Must be greater than 1**
///
/// # Returns
///
/// A `Result<String, Error>` that contains the generated token if successful, or an error otherwise.
///
/// # Examples
///
/// ```
/// // Example of generating a token with custom expiration (e.g., 3600 seconds = 1 hour)
/// use confirm_email::{generate_token_with_expiration, validate_token};
///
/// let token = generate_token_with_expiration("user@example.com".to_string(), "secret_key".to_string(), 3600).unwrap();
/// assert!(validate_token(token, "secret_key".to_string()).is_ok());
/// ```
/// Generates a token with the default expiration time.
///
/// This function creates a token that expires after the default number of days (1 day). It uses
/// the provided email and key to generate the token.
///
/// # Arguments
///
/// * `email` - A `String` representing the user's email address.
/// * `key` - A `String` representing the encryption key.
///
/// # Returns
///
/// A `Result<String, Error>` that contains the generated token if successful, or an error otherwise.
///
/// # Examples
///
/// ```
/// // Example of generating a token with default validity (1 day)
/// use confirm_email::{generate_token, validate_token};
///
/// let token = generate_token("user@example.com".to_string(), "secret_key".to_string()).unwrap();
/// assert!(validate_token(token, "secret_key".to_string()).is_ok());
/// ```
/// Parse the token and if valid returns the corresponding email.
///
/// This function takes an encrypted token and decryption key, decrypts the token,
/// verifies its expiration time, and returns the associated email address if valid.
///
/// # Arguments
///
/// * `token` - A `String` containing the encrypted token to validate.
/// * `key` - A `String` representing the decryption key.
///
/// # Returns
///
/// A `Result<String, Error>` that contains the user's email address if validation succeeds,
/// or an error if validation fails due to invalid token format, expiration, or decryption issues.
///
/// # Examples
///
/// ```
/// // Example of successful validation
/// use confirm_email::{generate_token, generate_token_with_expiration, validate_token};
///
/// let token = generate_token("user@example.com".to_string(), "secret_key".to_string()).unwrap();
/// assert_eq!(validate_token(token, "secret_key".to_string()).unwrap(), "user@example.com");
/// ```