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
#![deny(warnings, clippy::all)]
use hex::FromHexError;
use std::str::FromStr;
use thiserror::Error;
pub mod token_user_file;
pub const MODALITY_AUTH_TOKEN_ENV_VAR: &str = "MODALITY_AUTH_TOKEN";
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[repr(transparent)]
pub struct AuthToken(Vec<u8>);
impl From<Vec<u8>> for AuthToken {
fn from(v: Vec<u8>) -> Self {
AuthToken(v)
}
}
impl From<AuthToken> for Vec<u8> {
fn from(v: AuthToken) -> Self {
v.0
}
}
impl AsRef<[u8]> for AuthToken {
fn as_ref(&self) -> &[u8] {
&self.0
}
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
#[repr(transparent)]
pub struct AuthTokenHexString(String);
impl std::fmt::Display for AuthTokenHexString {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl FromStr for AuthTokenHexString {
type Err = AuthTokenStringDeserializationError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
decode_auth_token_hex_str(s)
}
}
impl AuthTokenHexString {
pub fn as_str(&self) -> &str {
self.0.as_str()
}
}
impl From<AuthTokenHexString> for String {
fn from(v: AuthTokenHexString) -> Self {
v.0
}
}
impl From<AuthToken> for AuthTokenHexString {
fn from(v: AuthToken) -> Self {
AuthTokenHexString(hex::encode(v.0))
}
}
impl TryFrom<AuthTokenHexString> for AuthToken {
type Error = AuthTokenStringDeserializationError;
fn try_from(v: AuthTokenHexString) -> Result<Self, Self::Error> {
decode_auth_token_hex(v.as_str())
}
}
pub fn decode_auth_token_hex(s: &str) -> Result<AuthToken, AuthTokenStringDeserializationError> {
hex::decode(s)
.map_err(|hex_error|match hex_error {
FromHexError::InvalidHexCharacter { .. } => AuthTokenStringDeserializationError::InvalidHexCharacter,
FromHexError::OddLength => AuthTokenStringDeserializationError::OddLength,
FromHexError::InvalidStringLength => {
panic!("An audit of the hex crate showed that the InvalidStringLength error is impossible for the `decode` method call.");
}
})
.map(AuthToken::from)
}
fn decode_auth_token_hex_str(
s: &str,
) -> Result<AuthTokenHexString, AuthTokenStringDeserializationError> {
decode_auth_token_hex(s).map(AuthTokenHexString::from)
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Error)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
pub enum AuthTokenStringDeserializationError {
#[error("Invalid character in the auth token hex representation. Characters ought to be '0' through '9', 'a' through 'f', or 'A' through 'F'")]
InvalidHexCharacter,
#[error("Auth token hex representation must contain an even number of hex-digits")]
OddLength,
}
#[cfg(test)]
mod tests {
use super::*;
use proptest::prelude::*;
#[test]
fn decode_auth_token_hex_never_panics() {
proptest!(|(s in ".*")| {
match decode_auth_token_hex(&s) {
Ok(at) => {
let aths = AuthTokenHexString::from(at.clone());
let at_two = AuthToken::try_from(aths).unwrap();
assert_eq!(at, at_two);
},
Err(AuthTokenStringDeserializationError::OddLength) => {
prop_assert!(s.len() % 2 == 1);
}
Err(AuthTokenStringDeserializationError::InvalidHexCharacter) => {
}
}
});
}
}