Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
- [Building and parsing tokens](#user-content-building-and-parsing-tokens)
- [A default token](#user-content-a-default-token)
- [A default parser](#user-content-a-default-parser)
- [Validating tokens and handling errors](#user-content-validating-tokens-and-handling-errors)
- [A token with a footer](#user-content-a-token-with-a-footer)
- [A token with an implicit assertion (V3/V4 only)](#user-content-a-token-with-an-implicit-assertion-v3-or-v4-versioned-tokens-only)
- [Setting a different expiration time](#user-content-setting-a-different-expiration-time)
Expand Down Expand Up @@ -101,6 +102,36 @@ Paseto is everything you love about JOSE (JWT, JWE, JWS) without any of the
* Validates the [footer](https://github.com/paseto-standard/paseto-spec/tree/master/docs) if
one was provided
* Validates the [implicit assertion](https://github.com/paseto-standard/paseto-spec/tree/master/docs) if one was provided (for V3 or V4 versioned tokens only)
* Validates expiration (`exp`) and not-before (`nbf`) claims automatically
* Returns a `GenericParserError` if validation fails (expired tokens, premature usage, invalid claims)

**Note**: `PasetoParser::default()` includes automatic expiration and not-before validation. Use `PasetoParser::new()` to construct a parser without these automatic validations.

### Validating tokens and handling errors

Token validation occurs during parsing. Expired tokens or tokens used before their `nbf` time return errors.

```rust
use rusty_paseto::prelude::*;

let key = PasetoSymmetricKey::<V4, Local>::from(Key::from(b"wubbalubbadubdubwubbalubbadubdub"));

// Create a token that expires in the past
let token = PasetoBuilder::<V4, Local>::default()
.set_claim(ExpirationClaim::try_from("2019-01-01T00:00:00+00:00")?)
.build(&key)?;

// Parsing the expired token returns an error
match PasetoParser::<V4, Local>::default().parse(&token, &key) {
Ok(json_value) => {
println!("Token valid: {}", json_value);
}
Err(err) => {
// This will print: "This token is expired"
eprintln!("Token validation failed: {}", err);
}
}
```

<h6 align="right"><a href="#user-content-table-of-contents">back to toc</a></h6>

Expand Down
13 changes: 10 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,10 @@
//! * Validates the [footer](https://github.com/paseto-standard/paseto-spec/tree/master/docs) if
//! one was provided
//! * Validates the [implicit assertion](https://github.com/paseto-standard/paseto-spec/tree/master/docs) if one was provided (for V3 or V4 versioned tokens only)
//! * Validates expiration (`exp`) and not-before (`nbf`) claims automatically
//! * Returns a [`GenericParserError`](generic::GenericParserError) if validation fails (expired tokens, premature usage, invalid claims)
//!
//! **Note**: `PasetoParser::default()` includes automatic expiration and not-before validation. Use `PasetoParser::new()` to construct a parser without these automatic validations.
//!
//! ## A token with a footer
//!
Expand Down Expand Up @@ -572,9 +576,12 @@

// Compile-time checks for incompatible feature combinations
// Multiple public features cause conflicting trait implementations for PasetoError
#[cfg(all(feature = "v3_public", any(feature = "v1_public", feature = "v2_public", feature = "v4_public")))]
#[cfg(all(
feature = "v3_public",
any(feature = "v1_public", feature = "v2_public", feature = "v4_public")
))]
compile_error!(
"Cannot enable v3_public with other public features due to conflicting trait implementations. \n\
"Cannot enable v3_public with other public features due to conflicting trait implementations. \n\
Choose only ONE public feature: v1_public, v2_public, v3_public, or v4_public. \n\
The PASETO specification recommends using a single version throughout your application. \n\
See: https://github.com/rrrodzilla/rusty_paseto/issues/48"
Expand All @@ -590,7 +597,7 @@ compile_error!(

#[cfg(all(feature = "v2_public", feature = "v4_public"))]
compile_error!(
"Cannot enable both v2_public and v4_public due to conflicting trait implementations. \n\
"Cannot enable both v2_public and v4_public due to conflicting trait implementations. \n\
Choose only ONE public feature: v1_public, v2_public, v3_public, or v4_public. \n\
The PASETO specification recommends using a single version throughout your application. \n\
See: https://github.com/rrrodzilla/rusty_paseto/issues/48"
Expand Down
Loading
Loading