rusty_paseto
A type-driven, ergonomic implementation of the PASETO protocol for secure stateless tokens.
PASETO: Platform-Agnostic Security Tokens
Paseto is everything you love about JOSE (JWT, JWE, JWS) without any of the many design deficits that plague the JOSE standards.
Roadmap and Current Feature Status
| APIs, Tests & Documentation | v1.local | v1.public | v2.local | v2.public | v3.local | v3.public | v4.local | v4.public |
|---|---|---|---|---|---|---|---|---|
| PASETO Token Builder | :green_circle: | :green_circle: | :green_circle: | :green_circle: | :green_circle: | :black_circle: | :green_circle: | :green_circle: |
| PASETO Token Parser | :green_circle: | :green_circle: | :green_circle: | :green_circle: | :green_circle: | :black_circle: | :green_circle: | :green_circle: |
| Flexible Claim Validation | :green_circle: | :green_circle: | :green_circle: | :green_circle: | :green_circle: | :black_circle: | :green_circle: | :green_circle: |
| Generic Token Builder | :green_circle: | :green_circle: | :green_circle: | :green_circle: | :green_circle: | :black_circle: | :green_circle: | :green_circle: |
| Generic Token Parser | :green_circle: | :green_circle: | :green_circle: | :green_circle: | :green_circle: | :black_circle: | :green_circle: | :green_circle: |
| Encryption/Signing | :green_circle: | :green_circle: | :green_circle: | :green_circle: | :green_circle: | :black_circle: | :green_circle: | :green_circle: |
| Decryption/Verification | :green_circle: | :green_circle: | :green_circle: | :green_circle: | :green_circle: | :black_circle: | :green_circle: | :green_circle: |
| PASETO Test vectors | :green_circle: | :green_circle: | :green_circle: | :green_circle: | :green_circle: | :black_circle: | :green_circle: | :green_circle: |
| Documentation | :black_circle: | :black_circle: | :orange_circle: | :black_circle: | :black_circle: | :black_circle: | :black_circle: | :black_circle: |
| Feature | Status |
|---|---|
| Feature gates | :black_circle: |
| PASERK support | :black_circle: |
Usage
// at the top of your source file
use *;
Examples: Building and parsing tokens
Here's a basic, default token:
use *;
// create a key specifying the PASETO version and purpose
let key = from;
// use a default token builder with the same PASETO version and purpose
let token = default.build?;
// token is a String in the form: "v4.local.encoded-payload"
A default token
- Has no footer
- Has no implicit assertion for V3 or V4 versioned tokens
- Expires in 1 hour after creation (due to an included default ExpirationClaim)
- Contains an IssuedAtClaim defaulting to the current utc time the token was created
- Contains a NotBeforeClaim defaulting to the current utc time the token was created
You can parse and validate an existing token with the following:
let key = from;
// now we can parse and validate the token with a parser that returns a serde_json::Value
let json_value = default.parse?;
//the ExpirationClaim
assert!;
//the IssuedAtClaim
assert!;
A default parser
- Validates the token structure and decryptes the payload or verifies the signature of the content
- Validates the footer if one was provided
- Validates the implicit assertion if one was provided (for V3 or V4 versioned tokens only)
A token with a footer
PASETO tokens can have an optional footer. In rusty_paseto we have strict types for most things.
So we can extend the previous example to add a footer to the token by using code like the
following:
use *;
let key = from;
let token = default
// note how we set the footer here
.set_footer
.build?;
// token is now a String in the form: "v4.local.encoded-payload.footer"
And parse it by passing in the same expected footer
// now we can parse and validate the token with a parser that returns a serde_json::Value
let json_value = default
.set_footer
.parse?;
//the ExpirationClaim
assert!;
//the IssuedAtClaim
assert!;
A token with an implicit assertion (V3 or V4 versioned tokens only)
Version 3 (V3) and Version 4 (V4) PASETO tokens can have an optional implicit assertion. So we can extend the previous example to add an implicit assertion to the token by using code like the following:
use *;
let key = from;
let token = default
.set_footer
// note how we set the implicit assertion here
.set_implicit_assertion
.build?;
// token is now a String in the form: "v4.local.encoded-payload.footer"
And parse it by passing in the same expected implicit assertion
// now we can parse and validate the token with a parser that returns a serde_json::Value
let json_value = default
.set_footer
.set_implicit_assertion
.parse?;
Setting a different expiration time
As mentioned, default tokens expire 1 hour from creation time. You can set your own expiration time by adding an ExpirationClaim which takes an ISO 8601 (Rfc3339) compliant datetime string.
Note: claims taking an ISO 8601 (Rfc3339) string use the TryFrom trait and return a Result<(),PasetoClaimError>
# use *;
// must include
use TryFrom;
let key = from;
// real-world example using the time crate to expire 5 minutes from now
let token = default
// note the TryFrom implmentation for ExpirationClaim
//.set_claim(ExpirationClaim::try_from("2019-01-01T00:00:00+00:00")?)
.set_claim
.set_footer
.build?;
// token is a String in the form: "v4.local.encoded-payload.footer"
Tokens that never expire
A 1 hour ExpirationClaim is set by default because the use case for non-expiring tokens in the world of security tokens is fairly limited. Omitting an expiration claim or forgetting to require one when processing them is almost certainly an oversight rather than a deliberate choice.
When it is a deliberate choice, you have the opportunity to deliberately remove this claim from the Builder. The method call required to do so ensures readers of the code understand the implicit risk.
let token = default
.set_claim
// even if you set an expiration claim (as above) it will be ignored
// due to the method call below
.set_no_expiration_danger_acknowledged
.build?;
Setting PASETO Claims
The PASETO specification includes seven reserved claims which you can set with their explicit types:
// real-world example using the time crate to prevent the token from being used before 2
// minutes from now
let in_2_minutes = .format?;
let token = default
//json payload key: "exp"
.set_claim
//json payload key: "iat"
// the IssueAtClaim is automatically set to UTC NOW by default
// but you can override it here
// .set_claim(IssuedAtClaim::try_from("2019-01-01T00:00:00+00:00")?)
//json payload key: "nbf"
//don't use this token before two minutes after UTC NOW
.set_claim
//json payload key: "aud"
.set_claim
//json payload key: "sub"
.set_claim
//json payload key: "iss"
.set_claim
//json payload key: "jti"
.set_claim
.build?;
Setting your own Custom Claims
The CustomClaim struct takes a tuple in the form of (key: String, value: T) where T is any
serializable type
Note: CustomClaims use the TryFrom trait and return a Result<(), PasetoClaimError> if you attempt to use one of the reserved PASETO keys in your CustomClaim
let token = default
.set_claim
.set_claim
.build?;
This throws an error:
// "exp" is a reserved PASETO claim key, you should use the ExpirationClaim type
let token = default
.set_claim
.build?;
Validating claims
rusty_paseto allows for flexible claim validation at parse time
Checking claims
Let's see how we can check particular claims exist with expected values.
// use a default token builder with the same PASETO version and purpose
let token = default
.set_claim
.set_claim
.set_claim
.build?;
default
// you can check any claim even custom claims
.check_claim
.check_claim
.check_claim
.parse?;
// no need for the assertions below since the check_claim methods
// above accomplish the same but at parse time!
//assert_eq!(json_value["sub"], "Get schwifty");
//assert_eq!(json_value["Contestant"], "Earth");
//assert_eq!(json_value["Universe"], 137);
Custom validation
What if we have more complex validation requirements? You can pass in a reference to a closure which receives the key and value of the claim you want to validate so you can implement any validation logic you choose.
Let's see how we can validate our tokens only contain universes with prime numbers:
// use a default token builder with the same PASETO version and purpose
let token = default
.set_claim
.set_claim
.set_claim
.build?;
default
.check_claim
.check_claim
.validate_claim
.parse?;
This token will fail to parse with the validation code above:
// 136 is not a prime number
let token = default
.set_claim
.build?;
Acknowledgments
If the API of this crate doesn't suit your tastes, check out the other PASETO implementations in the Rust ecosystem which inspired rusty_paseto:
- paseto - by Cynthia Coan
- pasetors - by Johannes
Questions?
File an issue or hit me up on Twitter!