instructure / paseto

A paseto implementation in rust.
MIT License
150 stars 13 forks source link

Basic local token usage is broken #37

Closed samuela closed 3 years ago

samuela commented 3 years ago

Describe the bug Building and validating a local token does not work.

To Reproduce

#[cfg(test)]
mod tests {
  use chrono::prelude::Utc;
  use chrono::Duration;

  #[test]
  fn paseto_build_validate() {
    let key = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
    let state = paseto::tokens::PasetoBuilder::new()
      .set_encryption_key(key.as_bytes())
      .set_expiration(&(Utc::now() + Duration::minutes(1)))
      .set_not_before(&Utc::now())
      .build()
      .expect("failed to construct paseto token");
    println!("{}", state);

    let validation = paseto::tokens::validate_local_token(
      &state,
      None,
      key.as_bytes(),
      &paseto::tokens::TimeBackend::Chrono,
    );
    println!("{:?}", validation);
    assert!(validation.is_ok());
  }
}

I'm getting

running 1 test
v2.local.c4IpI4S4kU-sb-wNW7mTmreWGhOLsO42SF0PDUuidfBGKmYiI6jQKqqa2RUnxzqK75moe8IjfNOROBw9c1QaYDzD1lGatPiEeoqt-D36Mw89wPlsB3dA3OwXako0Cu3Nrnc5svohjTRREiDDZOu3bPbYIxzlT58
Err(UnparseableTokenDate { claim_name: "iat" }

   0: failure::backtrace::internal::InternalBacktrace::new
             at /home/skainswo/.cargo/registry/src/github.com-1ecc6299db9ec823/failure-0.1.8/src/backtrace/internal.rs:46:44
   1: failure::backtrace::Backtrace::new
             at /home/skainswo/.cargo/registry/src/github.com-1ecc6299db9ec823/failure-0.1.8/src/backtrace/mod.rs:121:35
   2: <failure::error::error_impl::ErrorImpl as core::convert::From<F>>::from
             at /home/skainswo/.cargo/registry/src/github.com-1ecc6299db9ec823/failure-0.1.8/src/error/error_impl.rs:19:17
   3: <failure::error::Error as core::convert::From<F>>::from
             at /home/skainswo/.cargo/registry/src/github.com-1ecc6299db9ec823/failure-0.1.8/src/error/mod.rs:36:18
   4: paseto::tokens::validate_potential_json_blob
             at /home/skainswo/.cargo/registry/src/github.com-1ecc6299db9ec823/paseto-2.0.0+1.0.3/src/tokens/mod.rs:74:11
   5: paseto::tokens::validate_local_token
             at /home/skainswo/.cargo/registry/src/github.com-1ecc6299db9ec823/paseto-2.0.0+1.0.3/src/tokens/mod.rs:181:14
   6: api::auth::tests::paseto_build_validate
             at src/auth.rs:383:22
   7: api::auth::tests::paseto_build_validate::{{closure}}
             at src/auth.rs:373:3
   8: core::ops::function::FnOnce::call_once
             at /build/rustc-1.49.0-src/library/core/src/ops/function.rs:227:5
   9: test::__rust_begin_short_backtrace
  10: test::run_test::run_test_inner::{{closure}}
  11: std::sys_common::backtrace::__rust_begin_short_backtrace
  12: core::ops::function::FnOnce::call_once{{vtable.shim}}
  13: std::sys::unix::thread::Thread::new::thread_start
  14: start_thread
  15: __GI___clone
)
thread 'auth::tests::paseto_build_validate' panicked at 'assertion failed: validation.is_ok()', src/auth.rs:390:5
stack backtrace:
   0: std::panicking::begin_panic
             at /build/rustc-1.49.0-src/library/std/src/panicking.rs:521:12
   1: api::auth::tests::paseto_build_validate
             at ./src/auth.rs:390:5
   2: api::auth::tests::paseto_build_validate::{{closure}}
             at ./src/auth.rs:373:3
   3: core::ops::function::FnOnce::call_once
             at /build/rustc-1.49.0-src/library/core/src/ops/function.rs:227:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
test auth::tests::paseto_build_validate ... FAILED

failures:

failures:
    auth::tests::paseto_build_validate

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out

Versions (please complete the following information):

Additional context I can confirm this was working on paseto 1.0.7.

Mythra commented 3 years ago

Hey @samuela ,

Yep. Reading through the code, this certainly was a missed case when adding in support for the time crate that unfortunately we just simply did not have a test for. Sorry you ran into such a simple bug, I'll push a fix, and test to accompany it, and publish a patch version ASAP.

Thank you for your report!!!

Mythra commented 3 years ago

Alright, the fix has been published! Thank you again for reporting this.

samuela commented 3 years ago

Thanks so much @Mythra !