Repository navigation
Closed
Description
main.rs
extern crate jsonwebtoken;
#[macro_use]
extern crate serde_derive;
extern crate serde;
use jsonwebtoken::{decode, encode, Header, Validation};
#[derive(Debug, Serialize, Deserialize)]
struct Meta {
id: i32,
}
fn main() {
let v = Validation {
leeway: 5,
validate_exp: true,
iss: Some("iss no check".to_string()),
sub: Some("sub no check".to_string()),
..Validation::default()
};
let meta = Meta { id: 32 };
let token = encode(&Header::default(), &meta, "secret".as_ref()).unwrap();
println!("{:#?}", v);
println!("{:}", token);
if let Ok(new_meta) = decode::<Meta>(&token, "secret".as_ref(), &v) {
println!("{}", "succed");
println!("{:?}", new_meta);
} else {
println!("{}", "failed");
}
}output
Validation {
leeway: 5,
validate_exp: true,
validate_iat: true,
validate_nbf: true,
aud: None,
iss: Some(
"iss no check"
),
sub: Some(
"sub no check"
),
algorithms: [
HS256
]
}
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MzJ9.BtzaL8AzJJtMOqPWZM2sSmOfTSje5osCILeWGEpWqBA
{
"id": Number(
32
)
}
Validation {
leeway: 5,
validate_exp: true,
validate_iat: true,
validate_nbf: true,
aud: None,
iss: Some(
"iss no check"
),
sub: Some(
"sub no check"
),
algorithms: [
HS256
]
}
succed
TokenData { header: Header { typ: Some("JWT"), alg: HS256, cty: None, jku: None, kid: None, x5u: None, x5t: None }, claims: Meta { id: 32 } }
if let Some(exp) = claims.get("exp") {
if options.validate_exp && from_value::<i64>(exp.clone())? < now - options.leeway {
return Err(ErrorKind::ExpiredSignature.into());
}
}
if let Some(nbf) = claims.get("nbf") {
if options.validate_nbf && from_value::<i64>(nbf.clone())? > now + options.leeway {
return Err(ErrorKind::ImmatureSignature.into());
}
}
if let Some(iss) = claims.get("iss") {
if let Some(ref correct_iss) = options.iss {
if from_value::<String>(iss.clone())? != *correct_iss {
return Err(ErrorKind::InvalidIssuer.into());
}
}
}
if let Some(sub) = claims.get("sub") {
if let Some(ref correct_sub) = options.sub {
if from_value::<String>(sub.clone())? != *correct_sub {
return Err(ErrorKind::InvalidSubject.into());
}
}
}Should check options.validate_exp then echeck claims.get("exp"), the others are probably the same.
Metadata
Metadata
Assignees
Labels
No labels
Activity
Fix validation not working properly
Keats commentedon Jul 25, 2018
Sorry, completely missed that issue. It should be fixed in the PR above and in the next release
Fix validation not working properly