Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(engine): Add JSON Web Token (JWT) token generation and validation support #612

Merged
merged 9 commits into from
Apr 23, 2024

Conversation

zerosnacks
Copy link
Member

@zerosnacks zerosnacks commented Apr 23, 2024

Motivation

Closes #599

Solution

Implements JSON Web Token utilities in the rpc-types-engine crate. Only focuses on JWT token generation and validation, not the auth layer as the generated token is used as a string in bearer auth.

Largely uses the JWT secret implementation by Reth with some enhancements to documentation and using built-in get_current_timestamp. Notably does not implement loading from file, reading from file or writing to file as that is out of scope for Alloy I feel.

Regarding the defined limitations on the algorithm and length, I've kept that 1:1 with the Reth implementation to make sure it can be used in that context and the limitations seem reasonable enough for a more general case.

See for reference: https://github.com/paradigmxyz/reth/blob/79452eadaf4963f1e8d78a18b1f490d7c560aa54/crates/rpc/rpc/src/layers/jwt_secret.rs

Proposed API:

let secret = JwtSecret::random(); or JwtSecret::from_hex(...)

let claims = Claims::default();
let jwt = secret.encode(&claims).unwrap();
let result = secret.validate(&jwt);

assert!(matches!(result, Ok(())));

Where Claims::default() is a claim defined as: Claim { iat: get_current_timestamp(), exp: None }

The random() syntax is consistent with other places that uses it (Address::random(), etc..)

PR Checklist

  • Added Tests
  • Added Documentation
  • Breaking changes

@zerosnacks zerosnacks changed the title feat(engine): Add JSON Web Token (JWT) claims generation and validation support feat(engine): Add JSON Web Token (JWT) token generation and validation support Apr 23, 2024
Copy link
Member

@mattsse mattsse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

smol nit

crates/rpc-types-engine/src/jwt.rs Outdated Show resolved Hide resolved
@zerosnacks zerosnacks requested a review from mattsse April 23, 2024 15:42
@mattsse mattsse merged commit d68a6b7 into alloy-rs:main Apr 23, 2024
18 checks passed
ben186 pushed a commit to ben186/alloy that referenced this pull request Jul 27, 2024
…n support (alloy-rs#612)

* add basic layout

* port from Reth

* remove unused error

* add test for exp

* wrap up api

* make with_current_timestamp default

* avoid explicit casting

* use &str instead of String

* fix clippy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feature] Add JSON Web Token (JWT) token generation and validation support
2 participants