extern crate jsonwebtoken; #[macro_use] extern crate serde_derive; extern crate chrono; use chrono::Utc; use jsonwebtoken::{decode, encode, sign, verify, Algorithm, Header, Key, Validation}; const RSA_ALGORITHMS: &[Algorithm] = &[ Algorithm::RS256, Algorithm::RS384, Algorithm::RS512, Algorithm::PS256, Algorithm::PS384, Algorithm::PS512, ]; #[derive(Debug, PartialEq, Clone, Serialize, Deserialize)] pub struct Claims { sub: String, company: String, exp: i64, } #[test] fn round_trip_sign_verification() { let privkey = include_bytes!("private_rsa_key.der"); for &alg in RSA_ALGORITHMS { let encrypted = sign("hello world", Key::Der(&privkey[..]), alg).unwrap(); let is_valid = verify(&encrypted, "hello world", Key::Der(include_bytes!("public_rsa_key.der")), alg) .unwrap(); assert!(is_valid); } } #[test] fn round_trip_claim() { let my_claims = Claims { sub: "b@b.com".to_string(), company: "ACME".to_string(), exp: Utc::now().timestamp() + 10000, }; let privkey = include_bytes!("private_rsa_key.der"); for &alg in RSA_ALGORITHMS { let token = encode(&Header::new(alg), &my_claims, Key::Der(&privkey[..])).unwrap(); let token_data = decode::( &token, Key::Der(include_bytes!("public_rsa_key.der")), &Validation::new(alg), ) .unwrap(); assert_eq!(my_claims, token_data.claims); assert!(token_data.header.kid.is_none()); } } #[test] #[should_panic(expected = "InvalidRsaKey")] fn fails_with_different_key_format() { let privkey = include_bytes!("private_rsa_key.der"); sign("hello world", Key::Pkcs8(&privkey[..]), Algorithm::RS256).unwrap(); } #[test] fn rsa_modulus_exponent() { let modulus: Vec = vec![ 0xc9, 0x11, 0x3a, 0xac, 0x7b, 0x8d, 0x47, 0x44, 0x1b, 0x1c, 0xed, 0xc7, 0xdc, 0xab, 0x76, 0xa4, 0xe2, 0x86, 0x56, 0x14, 0x2a, 0x19, 0x95, 0xc8, 0x9c, 0xe7, 0x6e, 0x40, 0xdc, 0x57, 0xce, 0xe2, 0xa5, 0xbd, 0x04, 0xcb, 0x51, 0x3b, 0xf8, 0x97, 0x8b, 0x20, 0x82, 0x1e, 0x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x6f, 0x2d, 0xa1, 0xdd, 0x96, 0x3c, 0x2d, 0x85, 0x46, 0xae, 0xa6, 0x57, 0x65, 0x37, 0x20, 0x9f, 0x6b, 0xa3, 0x9f, 0xcb, 0x8a, 0x8d, 0x72, 0xd9, 0x54, 0x3e, 0x53, 0x75, ]; let exponent: Vec = vec![0x01, 0x00, 0x01]; let privkey = include_bytes!("private_rsa_key.der"); let encrypted = sign("hello world", Key::Der(&privkey[..]), Algorithm::RS256).unwrap(); let is_valid = verify( &encrypted, "hello world", Key::ModulusExponent(&modulus, &exponent), Algorithm::RS256, ) .unwrap(); assert!(is_valid); }