Skip to content

Conflict in validation::validate #51

Closed

Description

@fundon

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 } }

From https://github.com/Keats/jsonwebtoken/blob/master/src/validation.rs#L121-L147

    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.

Activity

  1. added a commit that references this issue on Jul 25, 2018
    5528497
  2. Keats commented on Jul 25, 2018

    @Keats
    Owner

    Sorry, completely missed that issue. It should be fixed in the PR above and in the next release

  3. added a commit that references this issue on Apr 13, 2023
    fcb1be9
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions