1
0
mirror of https://github.com/BitskiCo/jwk-rs synced 2024-11-21 19:49:20 +00:00
Go to file
Nick Hynes 30ceb84639
Add CI
2020-07-13 23:24:18 +00:00
.github/workflows Add CI 2020-07-13 23:24:18 +00:00
src Add key generation 2020-07-13 23:07:02 +00:00
.gitignore Initial commit 2020-07-12 20:13:44 +00:00
.rustfmt.toml Initial commit 2020-07-12 20:13:44 +00:00
Cargo.toml Add key generation 2020-07-13 23:07:02 +00:00
LICENSE Initial commit 2020-07-12 20:13:44 +00:00
README.md Add key generation 2020-07-13 23:07:02 +00:00

jsonwebkey

JSON Web Key (JWK) (de)serialization, generation, and conversion.

Note: requires rustc nightly >= 1.45 for conveniences around fixed-size arrays.

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 for testing)

Non-goals

  • be a fully-featured JOSE framework

Example

extern crate jsonwebtoken as jwt;
extern crate jsonwebkey as jwk;

fn main() {
    let jwk_str = r#"{
        "kty": "EC",
        "d": "ZoKQ9j4dhIBlMRVrv-QG8P_T9sutv3_95eio9MtpgKg",
        "crv": "P-256",
        "x": "QOMHmv96tVlJv-uNqprnDSKIj5AiLTXKRomXYnav0N0",
        "y": "TjYZoHnctatEE6NCrKmXQdJJPnNzZEX8nBmZde3AY4k"
    }"#;
    let jwk = jwk::JsonWebKey::from_str(jwk_str).unwrap();
    let encoding_key = jwk::EncodingKey::from_ec_der(jwk.to_der().unwrap());
    let token = jwt::encode(&jwt::Header::default(), &() /* claims */, encoding_key).unwrap();
}

Features

  • 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.
  • jsonwebtoken - enables conversions to types in the jsonwebtoken crate.