rusty_paseto/generic/claims/
mod.rs

1use serde_json::Value;
2use std::collections::HashMap;
3
4mod audience_claim;
5mod custom_claim;
6mod error;
7mod expiration_claim;
8mod issued_at_claim;
9mod issuer_claim;
10mod not_before_claim;
11mod subject_claim;
12mod token_identifier_claim;
13mod traits;
14
15pub use audience_claim::AudienceClaim;
16pub use custom_claim::CustomClaim;
17pub use error::PasetoClaimError;
18pub use expiration_claim::ExpirationClaim;
19pub use issued_at_claim::IssuedAtClaim;
20pub use issuer_claim::IssuerClaim;
21pub use not_before_claim::NotBeforeClaim;
22pub use subject_claim::SubjectClaim;
23pub use token_identifier_claim::TokenIdentifierClaim;
24pub use traits::PasetoClaim;
25///A type for creating generic claim validation functions
26pub type ValidatorFn = dyn Fn(&str, &Value) -> Result<(), PasetoClaimError>;
27///A type for tracking claims in a token
28pub type ValidatorMap = HashMap<String, Box<ValidatorFn>>;
29
30#[cfg(test)]
31mod unit_tests {
32  //TODO: need more comprehensive tests than these to flesh out the additionl error types
33  use super::*;
34  use anyhow::Result;
35  //use chrono::prelude::*;
36  use std::convert::TryFrom;
37  use time::format_description::well_known::Rfc3339;
38
39  #[test]
40  fn test_expiration_claim() -> Result<()> {
41    // setup
42    // a good time format
43    let now = time::OffsetDateTime::now_utc().format(&Rfc3339)?;
44
45    assert!(ExpirationClaim::try_from("hello").is_err());
46    let claim = ExpirationClaim::try_from(now);
47    assert!(claim.is_ok());
48    let claim = claim.unwrap();
49
50    assert_eq!(claim.get_key(), "exp");
51
52    Ok(())
53  }
54
55  #[test]
56  fn test_not_before_claim() -> Result<()> {
57    // setup
58    // a good time format
59    let now = time::OffsetDateTime::now_utc().format(&Rfc3339)?;
60
61    assert!(NotBeforeClaim::try_from("hello").is_err());
62    let claim = NotBeforeClaim::try_from(now);
63    assert!(claim.is_ok());
64    let claim = claim.unwrap();
65
66    assert_eq!(claim.get_key(), "nbf");
67
68    Ok(())
69  }
70
71  #[test]
72  fn test_issued_at_claim() -> Result<()> {
73    // setup
74    // a good time format
75    let now = time::OffsetDateTime::now_utc().format(&Rfc3339)?;
76
77    assert!(IssuedAtClaim::try_from("hello").is_err());
78    let claim = IssuedAtClaim::try_from(now);
79    assert!(claim.is_ok());
80    let claim = claim.unwrap();
81
82    assert_eq!(claim.get_key(), "iat");
83
84    Ok(())
85  }
86  #[test]
87  fn test_token_identifier_claim() {
88    // setup
89    let borrowed_str = String::from("hello world");
90    let claim = TokenIdentifierClaim::from(borrowed_str.as_str());
91
92    //verify
93    assert_eq!("jti", claim.get_key());
94  }
95
96  #[test]
97  fn test_audience_claim() {
98    // setup
99    let borrowed_str = String::from("hello world");
100    let claim = AudienceClaim::from(borrowed_str.as_str());
101
102    //verify
103    assert_eq!("aud", claim.get_key());
104  }
105
106  #[test]
107  fn test_subject_claim() {
108    // setup
109    let borrowed_str = String::from("hello world");
110    let claim = SubjectClaim::from(borrowed_str.as_str());
111
112    //verify
113    assert_eq!("sub", claim.get_key());
114  }
115
116  #[test]
117  fn test_iss_claim() {
118    // setup
119    let borrowed_str = String::from("hello world");
120    let claim = IssuerClaim::from(borrowed_str.as_str());
121
122    //verify
123    assert_eq!("iss", claim.get_key());
124  }
125
126  #[test]
127  fn test_basic_custom_claim() -> Result<()> {
128    let borrowed_str = String::from("universe");
129    let claim = CustomClaim::try_from((borrowed_str.as_str(), 137))?;
130    // setup
131    //verify
132
133    assert_eq!(claim.get_key(), "universe");
134    let (_, v) = claim.as_ref();
135    assert_eq!(v, &137);
136    Ok(())
137  }
138
139  #[test]
140  fn test_restricted_custom_claim() {
141    // setup
142    //verify
143    assert!(CustomClaim::try_from(("iss", 137)).is_err());
144    assert!(CustomClaim::try_from(("sub", 137)).is_err());
145    assert!(CustomClaim::try_from(("aud", 137)).is_err());
146    assert!(CustomClaim::try_from(("exp", 137)).is_err());
147    assert!(CustomClaim::try_from(("nbf", 137)).is_err());
148    assert!(CustomClaim::try_from(("iat", 137)).is_err());
149    assert!(CustomClaim::try_from(("jti", 137)).is_err());
150    assert!(CustomClaim::try_from(("i'm good tho", true)).is_ok());
151  }
152}