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
//! Claims Validate

use std::error::Error;
use std::fmt::{Display, Formatter};

use serde::Serialize;
use serde_json as json;

use crate::time;

pub struct IssuedAtTime;

pub struct NotBeforeTime;

pub struct ExpiredTime;

pub struct ExpectIss<'a>(pub &'a str);

pub struct ExpectSub<'a>(pub &'a str);

pub struct ExpectAud<'a>(pub &'a str);

pub struct ExpectJti<'a>(pub &'a str);

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum ValidateError {
    /// Claim "iss" does not match
    InvalidIss,
    /// Claim "sub" does not match
    InvalidSub,
    /// Claim "aud" does not match
    InvalidAud,
    /// Claim "jti" does not match
    InvalidJti,
    /// Now before the issued time
    InvalidIat,
    /// Token not active
    NotBefore,
    /// Token expired
    TokenExpiredAt(u64),
}

impl Display for ValidateError {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        match self {
            ValidateError::InvalidIss => f.write_str("Invalid iss"),
            ValidateError::InvalidSub => f.write_str("Invalid sub"),
            ValidateError::InvalidAud => f.write_str("Invalid aud"),
            ValidateError::InvalidJti => f.write_str("Invalid jti"),
            ValidateError::InvalidIat => f.write_str("Invalid iat"),
            ValidateError::NotBefore => f.write_str("Used before nbf"),
            ValidateError::TokenExpiredAt(time) => write!(f, "Token expired at {}", time),
        }
    }
}

impl Error for ValidateError {}

pub trait Validation<C: ?Sized> {
    type Error;

    fn validate(&self, claims: &C) -> Result<(), Self::Error>;
}

impl<T: Serialize> Validation<T> for IssuedAtTime {
    type Error = ValidateError;

    fn validate(&self, claims: &T) -> Result<(), Self::Error> {
        let claims = json::to_value(claims).ok();
        claims.and_then(|x| x["iat"].as_u64())
            .filter(|&x| x <= time::now_secs())
            .ok_or(ValidateError::InvalidIat)
            .map(|_| ())
    }
}

impl<T: Serialize> Validation<T> for NotBeforeTime {
    type Error = ValidateError;

    fn validate(&self, claims: &T) -> Result<(), Self::Error> {
        let claims = json::to_value(claims).ok();
        claims.and_then(|x| x["nbf"].as_u64())
            .filter(|&x| x <= time::now_secs())
            .ok_or(ValidateError::NotBefore)
            .map(|_| ())
    }
}

impl<T: Serialize> Validation<T> for ExpiredTime {
    type Error = ValidateError;

    fn validate(&self, claims: &T) -> Result<(), Self::Error> {
        let claims = json::to_value(claims).ok();
        claims.and_then(|x| x["exp"].as_u64())
            .ok_or(ValidateError::TokenExpiredAt(0))
            .and_then(|x| if x <= time::now_secs() { Err(ValidateError::TokenExpiredAt(x)) } else { Ok(x) })
            .map(|_| ())
    }
}

trait ExpectValidation<'a> {
    /// (claim_name, expected_value, error)
    fn expect(&self) -> (&'static str, &'a str, ValidateError);
}

impl<'a, T: ExpectValidation<'a>, C: Serialize> Validation<C> for T {
    type Error = ValidateError;

    fn validate(&self, claims: &C) -> Result<(), Self::Error> {
        let (claim_name, expected_value, error) = self.expect();
        let claims = json::to_value(claims).ok();
        claims.as_ref()
            .and_then(|x| x[claim_name].as_str())
            .filter(|x| x == &expected_value)
            .ok_or(error)
            .map(|_| ())
    }
}

impl<'a> ExpectValidation<'a> for ExpectIss<'a> {
    #[inline]
    fn expect(&self) -> (&'static str, &'a str, ValidateError) {
        ("iss", self.0, ValidateError::InvalidIss)
    }
}

impl<'a> ExpectValidation<'a> for ExpectSub<'a> {
    #[inline]
    fn expect(&self) -> (&'static str, &'a str, ValidateError) {
        ("sub", self.0, ValidateError::InvalidSub)
    }
}

impl<'a> ExpectValidation<'a> for ExpectAud<'a> {
    #[inline]
    fn expect(&self) -> (&'static str, &'a str, ValidateError) {
        ("aud", self.0, ValidateError::InvalidAud)
    }
}

impl<'a> ExpectValidation<'a> for ExpectJti<'a> {
    #[inline]
    fn expect(&self) -> (&'static str, &'a str, ValidateError) {
        ("jti", self.0, ValidateError::InvalidJti)
    }
}

pub trait Validate {
    #[inline]
    fn validate<V: Validation<Self>>(&self, validation: V) -> Result<(), V::Error> {
        validation.validate(self)
    }
}

impl<T> Validate for T {}