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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
use chrono::{DateTime, Duration, Utc};
use serde::{Deserialize, Serialize};

use crate::ValidationError;

/// Time-related validation options.
#[derive(Debug, Clone, Copy)]
pub struct TimeOptions {
    /// Leeway to use during validation.
    pub leeway: Duration,
    /// Current time to check against. If not set, the current time will be set to `Utc::now()`.
    pub current_time: Option<DateTime<Utc>>,
}

impl Default for TimeOptions {
    fn default() -> Self {
        Self {
            leeway: Duration::seconds(60),
            current_time: None,
        }
    }
}

/// A structure with no fields that can be used as a type parameter to `Claims`.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Empty {}

/// Claims encoded in a token.
///
/// Claims are comprised of a "standard" part (`exp`, `nbf` and `iat` claims as per [JWT spec]),
/// and custom fields. `iss`, `sub` and `aud` claims are not in the standard part
/// due to a variety of data types they can be reasonably represented by.
///
/// [JWT spec]: https://tools.ietf.org/html/rfc7519#section-4.1
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Claims<T> {
    /// Expiration date of the token.
    #[serde(
        rename = "exp",
        default,
        skip_serializing_if = "Option::is_none",
        with = "self::serde_timestamp"
    )]
    pub expiration_date: Option<DateTime<Utc>>,

    /// Minimum date at which token is valid.
    #[serde(
        rename = "nbf",
        default,
        skip_serializing_if = "Option::is_none",
        with = "self::serde_timestamp"
    )]
    pub not_before: Option<DateTime<Utc>>,

    /// Date of token issuance.
    #[serde(
        rename = "iat",
        default,
        skip_serializing_if = "Option::is_none",
        with = "self::serde_timestamp"
    )]
    pub issued_at: Option<DateTime<Utc>>,

    /// Custom claims.
    #[serde(flatten)]
    pub custom: T,
}

impl Claims<Empty> {
    /// Creates an empty claims instance.
    pub fn empty() -> Self {
        Self {
            expiration_date: None,
            not_before: None,
            issued_at: None,
            custom: Empty {},
        }
    }
}

impl<T> Claims<T> {
    /// Creates a new instance with the provided custom claims.
    pub fn new(custom_claims: T) -> Self {
        Self {
            expiration_date: None,
            not_before: None,
            issued_at: None,
            custom: custom_claims,
        }
    }

    /// Sets `expiration_date` claim so that the token has the specified `duration`.
    pub fn set_duration(self, duration: Duration) -> Self {
        Self {
            expiration_date: Some(Utc::now() + duration),
            ..self
        }
    }

    /// Atomically sets `issued_at` and `expiration_date` claims: first to the current time,
    /// and the second to match the specified `duration` of the token.
    pub fn set_duration_and_issuance(self, duration: Duration) -> Self {
        let issued_at = Utc::now();
        Self {
            expiration_date: Some(issued_at + duration),
            issued_at: Some(issued_at),
            ..self
        }
    }

    /// Sets the `nbf` claim.
    pub fn set_not_before(self, moment: DateTime<Utc>) -> Self {
        Self {
            not_before: Some(moment),
            ..self
        }
    }

    /// Validates the expiration claim.
    ///
    /// This method will return an error if the claims do not feature an expiration date,
    /// or if it is in the past (subject to the provided `options`).
    pub fn validate_expiration(&self, options: TimeOptions) -> Result<&Self, ValidationError> {
        if let Some(expiration) = self.expiration_date {
            let current_time = options.current_time.unwrap_or_else(Utc::now);
            if current_time > expiration + options.leeway {
                Err(ValidationError::Expired)
            } else {
                Ok(self)
            }
        } else {
            Err(ValidationError::NoClaim)
        }
    }

    /// Validates the maturity date (`nbf` claim).
    ///
    /// This method will return an error if the claims do not feature a maturity date,
    /// or if it is in the future (subject to the provided `options`).
    pub fn validate_maturity(&self, options: TimeOptions) -> Result<&Self, ValidationError> {
        if let Some(not_before) = self.not_before {
            let current_time = options.current_time.unwrap_or_else(Utc::now);
            if current_time < not_before - options.leeway {
                Err(ValidationError::NotMature)
            } else {
                Ok(self)
            }
        } else {
            Err(ValidationError::NoClaim)
        }
    }
}

mod serde_timestamp {
    use chrono::{offset::TimeZone, DateTime, Utc};
    use serde::{
        de::{Error as DeError, Visitor},
        Deserializer, Serializer,
    };

    use std::{convert::TryFrom, fmt};

    struct TimestampVisitor;

    impl<'de> Visitor<'de> for TimestampVisitor {
        type Value = DateTime<Utc>;

        fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
            formatter.write_str("UTC timestamp")
        }

        fn visit_i64<E>(self, value: i64) -> Result<Self::Value, E>
        where
            E: DeError,
        {
            Ok(Utc.timestamp(value, 0))
        }

        fn visit_u64<E>(self, value: u64) -> Result<Self::Value, E>
        where
            E: DeError,
        {
            let value = i64::try_from(value).map_err(DeError::custom)?;
            Ok(Utc.timestamp(value, 0))
        }
    }

    pub fn serialize<S: Serializer>(
        time: &Option<DateTime<Utc>>,
        serializer: S,
    ) -> Result<S::Ok, S::Error> {
        // `unwrap` is safe due to `skip_serializing_if` option
        serializer.serialize_i64(time.unwrap().timestamp())
    }

    pub fn deserialize<'de, D: Deserializer<'de>>(
        deserializer: D,
    ) -> Result<Option<DateTime<Utc>>, D::Error> {
        deserializer.deserialize_i64(TimestampVisitor).map(Some)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use assert_matches::assert_matches;

    #[test]
    fn empty_claims_can_be_serialized() {
        let mut claims = Claims::empty();
        assert!(serde_json::to_string(&claims).is_ok());
        assert!(serde_cbor::to_vec(&claims).is_ok());
        claims.expiration_date = Some(Utc::now());
        assert!(serde_json::to_string(&claims).is_ok());
        assert!(serde_cbor::to_vec(&claims).is_ok());
        claims.not_before = Some(Utc::now());
        assert!(serde_json::to_string(&claims).is_ok());
        assert!(serde_cbor::to_vec(&claims).is_ok());
    }

    #[test]
    fn expired_claim() {
        let mut claims = Claims::empty();
        assert_matches!(
            claims
                .validate_expiration(TimeOptions::default())
                .unwrap_err(),
            ValidationError::NoClaim
        );

        claims.expiration_date = Some(Utc::now() - Duration::hours(1));
        assert_matches!(
            claims
                .validate_expiration(TimeOptions::default())
                .unwrap_err(),
            ValidationError::Expired
        );

        claims.expiration_date = Some(Utc::now() - Duration::seconds(10));
        // With the default leeway, this claim is still valid.
        assert!(claims.validate_expiration(TimeOptions::default()).is_ok());
        // If we set leeway lower, then the claim will be considered expired.
        assert_matches!(
            claims
                .validate_expiration(TimeOptions {
                    leeway: Duration::seconds(5),
                    ..Default::default()
                })
                .unwrap_err(),
            ValidationError::Expired
        );
    }

    #[test]
    fn immature_claim() {
        let mut claims = Claims::empty();
        assert_matches!(
            claims
                .validate_maturity(TimeOptions::default())
                .unwrap_err(),
            ValidationError::NoClaim
        );

        claims.not_before = Some(Utc::now() + Duration::hours(1));
        assert_matches!(
            claims
                .validate_maturity(TimeOptions::default())
                .unwrap_err(),
            ValidationError::NotMature
        );

        claims.not_before = Some(Utc::now() + Duration::seconds(10));
        // With the default leeway, this claim is still valid.
        assert!(claims.validate_maturity(TimeOptions::default()).is_ok());
        // If we set leeway lower, then the claim will be considered expired.
        assert_matches!(
            claims
                .validate_maturity(TimeOptions {
                    leeway: Duration::seconds(5),
                    ..Default::default()
                })
                .unwrap_err(),
            ValidationError::NotMature
        );
    }
}