1
0
mirror of https://github.com/BitskiCo/jwk-rs synced 2024-12-29 12:08:50 +00:00
Go to file
2022-04-01 19:32:56 -07:00
.github/workflows Rename default branch to main 2022-04-01 19:27:03 -07:00
src Upgrade p256 2022-04-01 19:32:56 -07:00
.gitignore Initial commit 2020-07-12 20:13:44 +00:00
.rustfmt.toml Initial commit 2020-07-12 20:13:44 +00:00
.tarpaulin.toml Improve test coverage 2020-07-16 03:50:14 +00:00
Cargo.toml Upgrade p256 2022-04-01 19:32:56 -07:00
LICENSE Initial commit 2020-07-12 20:13:44 +00:00
README.md Use stable Rust 2021-01-25 22:14:44 +03:00
rust-toolchain.toml Configure rust-toolchain.toml 2022-04-01 19:30:46 -07:00

jsonwebkey

crates.io docs.rs codecov

JSON Web Key (JWK) (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)
  • Key generation (particularly useful for testing)

Non-goals

  • be a fully-featured JOSE framework

Examples

Deserializing from JSON

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

#[cfg(all(feature = "generate", feature = "jwt-convert"))] {
extern crate jsonwebtoken as jwt;
extern crate jsonwebkey as jwk;

#[derive(serde::Serialize, serde::Deserialize)]
struct TokenClaims {}

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 {},
    &my_jwk.key.to_encoding_key(),
).unwrap();

let mut validation = jwt::Validation::new(alg);
validation.validate_exp = false;
jwt::decode::<TokenClaims>(&token, &my_jwk.key.to_decoding_key(), &validation).unwrap();
}

Features

  • pkcs-convert - enables Key::{to_der, to_pem}. This pulls in the yasna crate.
  • generate - enables Key::{generate_p256, generate_symmetric}. This pulls in the p256 and rand crates.
  • jwt-convert - enables conversions to types in the jsonwebtoken crate.