https://github.com/nhynes/jwk-rs
Rust JSON Web Keys π¦πΈπ
https://github.com/nhynes/jwk-rs
jwk jwt rust
Last synced: 3 months ago
JSON representation
Rust JSON Web Keys π¦πΈπ
- Host: GitHub
- URL: https://github.com/nhynes/jwk-rs
- Owner: nhynes
- License: mit
- Created: 2020-07-12T18:57:24.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2023-12-17T20:47:35.000Z (over 1 year ago)
- Last Synced: 2025-04-10T03:52:43.415Z (3 months ago)
- Topics: jwk, jwt, rust
- Language: Rust
- Homepage:
- Size: 60.5 KB
- Stars: 35
- Watchers: 1
- Forks: 19
- Open Issues: 10
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jsonwebkey
[](https://crates.io/crates/jsonwebkey)
[](https://docs.rs/jsonwebkey)
[](https://codecov.io/gh/nhynes/jwk-rs)*[JSON Web Key (JWK)](https://tools.ietf.org/html/rfc7517#section-4.3) (de)serialization, generation, and conversion.*
**Goals**
tl;dr: get keys into a format that can be used by other crates; be as safe as possible while doing so.
- Serialization and deserialization of _Required_ and _Recommended_ key types (HS256, RS256, ES256)
- Conversion to PEM for interop with existing JWT libraries (e.g., [jsonwebtoken](https://crates.io/crates/jsonwebtoken))
- Key generation (particularly useful for testing)**Non-goals**
- be a fully-featured JOSE framework
## Examples
### Deserializing from JSON
```rust
extern crate jsonwebkey as jwk;
// Generated using https://mkjwk.org/.
let jwt_str = r#"{
"kty": "oct",
"use": "sig",
"kid": "my signing key",
"k": "Wpj30SfkzM_m0Sa_B2NqNw",
"alg": "HS256"
}"#;
let the_jwk: jwk::JsonWebKey = jwt_str.parse().unwrap();
println!("{:#?}", the_jwk); // looks like `jwt_str` but with reordered fields.
```### Using with other crates
```rust
#[cfg(all(feature = "generate", feature = "jwt-convert"))] {
extern crate jsonwebtoken as jwt;
extern crate jsonwebkey as jwk;#[derive(serde::Serialize, serde::Deserialize)]
struct TokenClaims {
exp: usize
}let mut my_jwk = jwk::JsonWebKey::new(jwk::Key::generate_p256());
my_jwk.set_algorithm(jwk::Algorithm::ES256);let alg: jwt::Algorithm = my_jwk.algorithm.unwrap().into();
let token = jwt::encode(
&jwt::Header::new(alg),
&TokenClaims { exp: 1492 },
&my_jwk.key.to_encoding_key(),
).unwrap();let mut validation = jwt::Validation::new(alg);
validation.validate_exp = false;
jwt::decode::(&token, &my_jwk.key.to_decoding_key(), &validation).unwrap();
}
```## Features
* `pkcs-convert` - enables `Key::{to_der, to_pem}`.
This pulls in the [yasna](https://crates.io/crates/yasna) crate.
* `generate` - enables `Key::{generate_p256, generate_symmetric}`.
This pulls in the [p256](https://crates.io/crates/p256) and [rand](https://crates.io/crates/rand) crates.
* `jwt-convert` - enables conversions to types in the
[jsonwebtoken](https://crates.io/crates/jsonwebtoken) crate.