Keats / jsonwebtoken

JWT lib in rust
MIT License
1.69k stars 271 forks source link

Some other errors #169

Closed zhuxiujia closed 3 years ago

zhuxiujia commented 3 years ago
#[derive(Debug, Serialize, Deserialize)]
struct Claims {
    pub exp: usize,
}
#[test]
fn test2(){
    let key = b"secret";
    let my_claims =
        Claims { exp: 24 * 60 * 60 * 1000 };
    let token = match encode(&Header::default(), &my_claims, &EncodingKey::from_secret(key)) {
        Ok(t) => t,
        Err(_) => panic!(), // in practice you would return the error
    };

    let validation = Validation {  ..Validation::default() };
    let token_data = match decode::<Claims>(&token, &DecodingKey::from_secret(key), &validation) {
        Ok(c) => c,
        Err(err) => match *err.kind() {
            ErrorKind::InvalidToken => panic!("Token is invalid"), // Example on how to handle a specific error
            ErrorKind::InvalidIssuer => panic!("Issuer is invalid"), // Example on how to handle a specific error
            _ => panic!("Some other errors"),
        },
    };
    println!("{:?}", token_data.claims);
    println!("{:?}", token_data.header);
}

///result
Some other errors
thread 'domain::vo::jwt::test2' panicked at 'Some other errors', src\domain\vo\jwt.rs:83:18
stack backtrace:
   0: std::panicking::begin_panic<str*>
             at C:\Users\zxj_pc\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\std\src\panicking.rs:521
   1: abs_admin::domain::vo::jwt::test2
             at .\src\domain\vo\jwt.rs:83
   2: abs_admin::domain::vo::jwt::test2::{{closure}}
             at .\src\domain\vo\jwt.rs:68
   3: core::ops::function::FnOnce::call_once<closure-0,tuple<>>
             at C:\Users\zxj_pc\.rustup\toolchains\stable-x86_64-pc-windows-msvc\lib\rustlib\src\rust\library\core\src\ops\function.rs:227
   4: core::ops::function::FnOnce::call_once
             at /rustc/e1884a8e3c3e813aada8254edfa120e85bf5ffca\library\core\src\ops\function.rs:227
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
Keats commented 3 years ago

? You can pattern match on all the error kinds to see what the issue is, or print err

zhuxiujia commented 3 years ago

Claims { exp: 24 60 60 * 1000 }; will be parse fail

but

Claims { exp: 10000000 };

will be success

zhuxiujia commented 3 years ago

you can try

Claims { exp: 24 };

this code also parse fail for“some other error”,can you fix that?