Crates.io | pykrete-jsonwebkey |
lib.rs | pykrete-jsonwebkey |
version | 0.4.1 |
source | src |
created_at | 2024-03-10 20:29:15.253771 |
updated_at | 2024-03-10 20:45:00.932197 |
description | JSON Web Key (JWK) (de)serialization, generation, and conversion. |
homepage | |
repository | https://github.com/CertainLach/jwk-rs |
max_upload_size | |
id | 1168913 |
size | 80,171 |
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.
Non-goals
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.
#[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::<TokenClaims>(&token, &my_jwk.key.to_decoding_key(), &validation).unwrap();
}
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.