Skip to content

Commit 519ca34

Browse files
committed
docs(parser): clarify expiration validation behavior in PasetoParser
Add explicit documentation that PasetoParser::default() automatically validates expiration and not-before claims. Include example showing error handling for expired tokens. Changes: - Update README with new "Validating tokens and handling errors" section - Clarify PasetoParser::default() validates exp and nbf claims automatically - Document that PasetoParser::new() creates parser without automatic validations - Add example demonstrating error handling for expired tokens - Update struct-level and method documentation for clarity Closes #26
1 parent 80b77a9 commit 519ca34

3 files changed

Lines changed: 1095 additions & 1046 deletions

File tree

readme.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
- [Building and parsing tokens](#user-content-building-and-parsing-tokens)
2222
- [A default token](#user-content-a-default-token)
2323
- [A default parser](#user-content-a-default-parser)
24+
- [Validating tokens and handling errors](#user-content-validating-tokens-and-handling-errors)
2425
- [A token with a footer](#user-content-a-token-with-a-footer)
2526
- [A token with an implicit assertion (V3/V4 only)](#user-content-a-token-with-an-implicit-assertion-v3-or-v4-versioned-tokens-only)
2627
- [Setting a different expiration time](#user-content-setting-a-different-expiration-time)
@@ -101,6 +102,36 @@ Paseto is everything you love about JOSE (JWT, JWE, JWS) without any of the
101102
* Validates the [footer](https://github.com/paseto-standard/paseto-spec/tree/master/docs) if
102103
one was provided
103104
* 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)
105+
* Validates expiration (`exp`) and not-before (`nbf`) claims automatically
106+
* Returns a `GenericParserError` if validation fails (expired tokens, premature usage, invalid claims)
107+
108+
**Note**: `PasetoParser::default()` includes automatic expiration and not-before validation. Use `PasetoParser::new()` to construct a parser without these automatic validations.
109+
110+
### Validating tokens and handling errors
111+
112+
Token validation occurs during parsing. Expired tokens or tokens used before their `nbf` time return errors.
113+
114+
```rust
115+
use rusty_paseto::prelude::*;
116+
117+
let key = PasetoSymmetricKey::<V4, Local>::from(Key::from(b"wubbalubbadubdubwubbalubbadubdub"));
118+
119+
// Create a token that expires in the past
120+
let token = PasetoBuilder::<V4, Local>::default()
121+
.set_claim(ExpirationClaim::try_from("2019-01-01T00:00:00+00:00")?)
122+
.build(&key)?;
123+
124+
// Parsing the expired token returns an error
125+
match PasetoParser::<V4, Local>::default().parse(&token, &key) {
126+
Ok(json_value) => {
127+
println!("Token valid: {}", json_value);
128+
}
129+
Err(err) => {
130+
// This will print: "This token is expired"
131+
eprintln!("Token validation failed: {}", err);
132+
}
133+
}
134+
```
104135

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

src/lib.rs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -219,6 +219,10 @@
219219
//! * Validates the [footer](https://github.com/paseto-standard/paseto-spec/tree/master/docs) if
220220
//! one was provided
221221
//! * 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)
222+
//! * Validates expiration (`exp`) and not-before (`nbf`) claims automatically
223+
//! * Returns a [`GenericParserError`](generic::GenericParserError) if validation fails (expired tokens, premature usage, invalid claims)
224+
//!
225+
//! **Note**: `PasetoParser::default()` includes automatic expiration and not-before validation. Use `PasetoParser::new()` to construct a parser without these automatic validations.
222226
//!
223227
//! ## A token with a footer
224228
//!
@@ -572,9 +576,12 @@
572576
573577
// Compile-time checks for incompatible feature combinations
574578
// Multiple public features cause conflicting trait implementations for PasetoError
575-
#[cfg(all(feature = "v3_public", any(feature = "v1_public", feature = "v2_public", feature = "v4_public")))]
579+
#[cfg(all(
580+
feature = "v3_public",
581+
any(feature = "v1_public", feature = "v2_public", feature = "v4_public")
582+
))]
576583
compile_error!(
577-
"Cannot enable v3_public with other public features due to conflicting trait implementations. \n\
584+
"Cannot enable v3_public with other public features due to conflicting trait implementations. \n\
578585
Choose only ONE public feature: v1_public, v2_public, v3_public, or v4_public. \n\
579586
The PASETO specification recommends using a single version throughout your application. \n\
580587
See: https://github.com/rrrodzilla/rusty_paseto/issues/48"
@@ -590,7 +597,7 @@ compile_error!(
590597

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

0 commit comments

Comments
 (0)