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
use crate::time::*;
use std::{fmt, ops::Deref, str::FromStr};
use uuid::Uuid;
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub struct Nonce(Uuid);
impl Nonce {
pub const STR_LEN: usize = 32;
pub fn new() -> Self {
Self(Uuid::new_v4())
}
}
impl From<Uuid> for Nonce {
fn from(from: Uuid) -> Self {
Self(from)
}
}
impl From<Nonce> for Uuid {
fn from(from: Nonce) -> Self {
from.0
}
}
impl AsRef<Uuid> for Nonce {
fn as_ref(&self) -> &Uuid {
&self.0
}
}
impl Deref for Nonce {
type Target = Uuid;
fn deref(&self) -> &Uuid {
&self.0
}
}
#[derive(Debug)]
pub struct NonceParseError;
impl fmt::Display for NonceParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), std::fmt::Error> {
write!(f, "Invalid Nonce")
}
}
impl FromStr for Nonce {
type Err = NonceParseError;
fn from_str(nonce_str: &str) -> Result<Self, Self::Err> {
nonce_str
.parse::<Uuid>()
.map(Into::into)
.map_err(|_| NonceParseError)
}
}
impl fmt::Display for Nonce {
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), std::fmt::Error> {
write!(f, "{}", self.0.to_simple_ref().to_string())
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct EmailNonce {
pub email: String,
pub nonce: Nonce,
}
pub type ActualTokenLen = usize;
pub type NonceString = String;
#[derive(Debug)]
pub enum EmailNonceDecodingError {
Bs58(bs58::decode::Error),
Utf8(std::string::FromUtf8Error),
TooShort(ActualTokenLen),
Parse(NonceString, NonceParseError),
}
impl EmailNonce {
pub fn encode_to_string(&self) -> String {
let nonce = self.nonce.to_string();
debug_assert_eq!(Nonce::STR_LEN, nonce.len());
let mut concat = String::with_capacity(self.email.len() + nonce.len());
concat += &self.email;
concat += &nonce;
bs58::encode(concat).into_string()
}
pub fn decode_from_str(encoded: &str) -> Result<EmailNonce, EmailNonceDecodingError> {
let decoded = bs58::decode(encoded)
.into_vec()
.map_err(EmailNonceDecodingError::Bs58)?;
let mut concat = String::from_utf8(decoded).map_err(EmailNonceDecodingError::Utf8)?;
if concat.len() < Nonce::STR_LEN {
return Err(EmailNonceDecodingError::TooShort(concat.len()));
}
let email_len = concat.len() - Nonce::STR_LEN;
let nonce_slice: &str = &concat[email_len..];
let nonce = nonce_slice
.parse::<Nonce>()
.map_err(|err| EmailNonceDecodingError::Parse(nonce_slice.into(), err))?;
concat.truncate(email_len);
let email = concat;
Ok(Self { email, nonce })
}
}
#[derive(Debug, Clone, Eq, PartialEq)]
pub struct UserToken {
pub email_nonce: EmailNonce,
pub expires_at: Timestamp,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn encode_decode_email_nonce() {
let example = EmailNonce {
email: "test@example.com".into(),
nonce: Nonce::new(),
};
let encoded = example.encode_to_string();
let decoded = EmailNonce::decode_from_str(&encoded).unwrap();
assert_eq!(example, decoded);
}
#[test]
fn encode_decode_email_nonce_with_empty_email() {
let example = EmailNonce {
email: "".into(),
nonce: Nonce::new(),
};
let encoded = example.encode_to_string();
let decoded = EmailNonce::decode_from_str(&encoded).unwrap();
assert_eq!(example, decoded);
}
#[test]
fn decode_empty_email_nonce() {
assert!(EmailNonce::decode_from_str("").is_err());
}
#[test]
fn should_generate_unique_instances() {
let n1 = Nonce::new();
let n2 = Nonce::new();
assert_ne!(n1, n2);
}
#[test]
fn should_convert_from_to_string() {
let n1 = Nonce::new();
let s1 = n1.to_string();
assert_eq!(Nonce::STR_LEN, s1.len());
let n2 = s1.parse::<Nonce>().unwrap();
assert_eq!(n1, n2);
assert_eq!(s1, n2.to_string());
}
}