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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
use std::error;
use std::fmt;
use std::time::{Duration, SystemTime};

use crate::base64;
use crate::Seperator;

#[derive(Debug)]
pub enum PayloadError {
    Serde(serde_json::Error),
    Base64(base64::DecodeError),
}

#[derive(Debug)]
pub struct SeperatorNotFound {
    pub seperator: Seperator,
}

/// Errors that can occur while unsigning a "signed value".
#[derive(Debug)]
pub enum BadSignature<'a> {
    /// A string was provided to unsign, but it did not contain
    /// the expected seperator.
    SeperatorNotFound { seperator: Seperator },
    /// The signature did not match what we expected it to be.
    SignatureMismatch { signature: &'a str, value: &'a str },
    /// The payload is invalid, e.g. it cannot be parsed.
    PayloadInvalid { value: &'a str, error: PayloadError },
}

/// Errors that can occur while unsigning a "signed value" using the timed signer.
#[derive(Debug)]
pub enum BadTimedSignature<'a> {
    /// A string was provided to unsign, but it did not contain
    /// the expected seperator.
    SeperatorNotFound { seperator: Seperator },
    /// The signature did not match what we expected it to be.
    SignatureMismatch { signature: &'a str, value: &'a str },
    /// The payload is invalid, e.g. it cannot be parsed.
    PayloadInvalid { value: &'a str, error: PayloadError },
    /// The timestamp is missing, but the value was signed with a correct
    /// secret key + salt.
    TimestampMissing { value: &'a str },
    /// The timestamp was present and signed, but we weren't able to parse it back to
    /// a SystemTime.
    TimestampInvalid { timestamp: &'a str },
    /// The timestamp expired - meaning that it was more than `max_age` ago.
    TimestampExpired {
        timestamp: SystemTime,
        max_age: Duration,
        value: &'a str,
    },
}

pub struct TimestampExpired<T> {
    pub timestamp: SystemTime,
    pub max_age: Duration,
    pub value: T,
}

/// Error that occurs when trying to construct a Seperator with
/// a char is in the base64 url-safe alphabet.
#[derive(Debug)]
pub struct InvalidSeperator(pub char);

impl<'a> fmt::Display for BadSignature<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            BadSignature::SeperatorNotFound { seperator } => {
                write!(f, "Seperator {:?} not found in value.", seperator.0)
            }
            BadSignature::SignatureMismatch { signature, .. } => {
                write!(f, "Signature {:?} does not match.", signature)
            }
            BadSignature::PayloadInvalid { error, .. } => {
                write!(f, "Payload cannot be parsed because {:?}.", error)
            }
        }
    }
}

impl<'a> error::Error for BadSignature<'a> {
    fn description(&self) -> &str {
        match *self {
            BadSignature::SeperatorNotFound { .. } => "seperator not found",
            BadSignature::SignatureMismatch { .. } => "signature does not match",
            BadSignature::PayloadInvalid { .. } => "payload invalid",
        }
    }

    fn cause(&self) -> Option<&error::Error> {
        None
    }
}

impl<'a> fmt::Display for BadTimedSignature<'a> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            BadTimedSignature::SeperatorNotFound { seperator, .. } => {
                write!(f, "Seperator {:?} not found in value.", seperator.0)
            }
            BadTimedSignature::SignatureMismatch { signature, .. } => {
                write!(f, "Signature {:?} does not match.", signature)
            }
            BadTimedSignature::PayloadInvalid { error, .. } => {
                write!(f, "Payload cannot be parsed because {:?}.", error)
            }
            BadTimedSignature::TimestampMissing { .. } => write!(f, "Timestamp missing"),
            BadTimedSignature::TimestampInvalid { timestamp } => {
                write!(f, "Timestamp {:?} is invalid", timestamp)
            }
            BadTimedSignature::TimestampExpired {
                timestamp, max_age, ..
            } => write!(
                f,
                "Timestamp {:?} is older than {:?} and is expired.",
                timestamp, max_age
            ),
        }
    }
}

impl<'a> error::Error for BadTimedSignature<'a> {
    fn description(&self) -> &str {
        match *self {
            BadTimedSignature::SeperatorNotFound { .. } => "seperator not found",
            BadTimedSignature::SignatureMismatch { .. } => "signature does not match",
            BadTimedSignature::TimestampMissing { .. } => "timestamp missing",
            BadTimedSignature::TimestampInvalid { .. } => "timestamp invalid",
            BadTimedSignature::TimestampExpired { .. } => "timestamp expired",
            BadTimedSignature::PayloadInvalid { .. } => "payload invalid",
        }
    }

    fn cause(&self) -> Option<&error::Error> {
        None
    }
}

impl<'a> From<BadSignature<'a>> for BadTimedSignature<'a> {
    fn from(bad_signature: BadSignature<'a>) -> Self {
        match bad_signature {
            BadSignature::SeperatorNotFound { seperator } => {
                BadTimedSignature::SeperatorNotFound { seperator }
            }
            BadSignature::SignatureMismatch { signature, value } => {
                BadTimedSignature::SignatureMismatch { signature, value }
            }
            BadSignature::PayloadInvalid { error, value } => {
                BadTimedSignature::PayloadInvalid { error, value }
            }
        }
    }
}

impl fmt::Display for InvalidSeperator {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "Seperator {:?} is in the base64 alphabet, and thus cannot be used",
            self.0
        )
    }
}

impl error::Error for InvalidSeperator {
    fn description(&self) -> &str {
        "invalid seperator"
    }

    fn cause(&self) -> Option<&error::Error> {
        None
    }
}

impl fmt::Display for SeperatorNotFound {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Seperator {:?} not found in value.", self.seperator)
    }
}

impl error::Error for SeperatorNotFound {
    fn description(&self) -> &str {
        "seperator not foundr"
    }

    fn cause(&self) -> Option<&error::Error> {
        None
    }
}

impl<'a> From<SeperatorNotFound> for BadSignature<'a> {
    fn from(error: SeperatorNotFound) -> Self {
        BadSignature::SeperatorNotFound {
            seperator: error.seperator,
        }
    }
}

impl<'a> From<SeperatorNotFound> for BadTimedSignature<'a> {
    fn from(error: SeperatorNotFound) -> Self {
        BadTimedSignature::SeperatorNotFound {
            seperator: error.seperator,
        }
    }
}

impl<T> fmt::Display for TimestampExpired<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "Timestamp {:?} is older than {:?} and is expired.",
            self.timestamp, self.max_age
        )
    }
}

impl<T> fmt::Debug for TimestampExpired<T> {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "TimestampExpired {{ max_age: {:?}, timestamp: {:?} }}",
            self.max_age, self.timestamp
        )
    }
}

impl<T> error::Error for TimestampExpired<T> {
    fn description(&self) -> &str {
        "timestamp expired"
    }

    fn cause(&self) -> Option<&error::Error> {
        None
    }
}

impl From<base64::DecodeError> for PayloadError {
    fn from(error: base64::DecodeError) -> Self {
        PayloadError::Base64(error)
    }
}

impl From<serde_json::Error> for PayloadError {
    fn from(error: serde_json::Error) -> Self {
        PayloadError::Serde(error)
    }
}