1
0
mirror of https://github.com/BitskiCo/jwk-rs synced 2024-11-25 05:19:22 +00:00
jwk-rs/README.md

73 lines
3.4 KiB
Markdown
Raw Normal View History

2020-07-12 18:57:57 +00:00
# jsonwebkey
2020-07-14 00:14:08 +00:00
[![crates.io](https://img.shields.io/crates/v/jsonwebkey.svg?color=fc8d62&logo=rust)](https://crates.io/crates/jsonwebkey)
[![docs.rs](https://img.shields.io/badge/docs.rs-jsonwebkey-66c2a5?labelColor=555555&logoColor=white&logo=data:image/svg+xml;base64,PHN2ZyByb2xlPSJpbWciIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgdmlld0JveD0iMCAwIDUxMiA1MTIiPjxwYXRoIGZpbGw9IiNmNWY1ZjUiIGQ9Ik00ODguNiAyNTAuMkwzOTIgMjE0VjEwNS41YzAtMTUtOS4zLTI4LjQtMjMuNC0zMy43bC0xMDAtMzcuNWMtOC4xLTMuMS0xNy4xLTMuMS0yNS4zIDBsLTEwMCAzNy41Yy0xNC4xIDUuMy0yMy40IDE4LjctMjMuNCAzMy43VjIxNGwtOTYuNiAzNi4yQzkuMyAyNTUuNSAwIDI2OC45IDAgMjgzLjlWMzk0YzAgMTMuNiA3LjcgMjYuMSAxOS45IDMyLjJsMTAwIDUwYzEwLjEgNS4xIDIyLjEgNS4xIDMyLjIgMGwxMDMuOS01MiAxMDMuOSA1MmMxMC4xIDUuMSAyMi4xIDUuMSAzMi4yIDBsMTAwLTUwYzEyLjItNi4xIDE5LjktMTguNiAxOS45LTMyLjJWMjgzLjljMC0xNS05LjMtMjguNC0yMy40LTMzLjd6TTM1OCAyMTQuOGwtODUgMzEuOXYtNjguMmw4NS0zN3Y3My4zek0xNTQgMTA0LjFsMTAyLTM4LjIgMTAyIDM4LjJ2LjZsLTEwMiA0MS40LTEwMi00MS40di0uNnptODQgMjkxLjFsLTg1IDQyLjV2LTc5LjFsODUtMzguOHY3NS40em0wLTExMmwtMTAyIDQxLjQtMTAyLTQxLjR2LS42bDEwMi0zOC4yIDEwMiAzOC4ydi42em0yNDAgMTEybC04NSA0Mi41di03OS4xbDg1LTM4Ljh2NzUuNHptMC0xMTJsLTEwMiA0MS40LTEwMi00MS40di0uNmwxMDItMzguMiAxMDIgMzguMnYuNnoiPjwvcGF0aD48L3N2Zz4K)](https://docs.rs/jsonwebkey)
[![codecov](https://codecov.io/gh/nhynes/jwk-rs/branch/master/graph/badge.svg)](https://codecov.io/gh/nhynes/jwk-rs)
2020-07-12 21:23:06 +00:00
*[JSON Web Key (JWK)](https://tools.ietf.org/html/rfc7517#section-4.3) (de)serialization, generation, and conversion.*
2020-07-12 18:57:57 +00:00
2020-07-12 21:23:06 +00:00
**Goals**
tl;dr: get keys into a format that can be used by other crates; be as safe as possible while doing so.
2020-07-12 18:57:57 +00:00
2020-07-13 23:51:09 +00:00
- 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)
2020-07-12 21:23:06 +00:00
**Non-goals**
2020-07-13 23:51:09 +00:00
- be a fully-featured JOSE framework
2020-07-12 21:23:06 +00:00
2020-07-13 23:51:09 +00:00
## 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"
}"#;
2020-07-16 02:32:11 +00:00
let the_jwk: jwk::JsonWebKey = jwt_str.parse().unwrap();
println!("{:#?}", the_jwk); // looks like `jwt_str` but with reordered fields.
2020-07-13 23:51:09 +00:00
```
### Using with other crates
2020-07-12 21:23:06 +00:00
```rust
2020-07-16 02:32:11 +00:00
#[cfg(all(feature = "generate", feature = "jwt-convert"))] {
2020-07-12 21:23:06 +00:00
extern crate jsonwebtoken as jwt;
extern crate jsonwebkey as jwk;
2020-07-13 23:51:09 +00:00
#[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);
2020-07-16 02:32:11 +00:00
let alg: jwt::Algorithm = my_jwk.algorithm.unwrap().into();
2020-07-13 23:51:09 +00:00
let token = jwt::encode(
2020-07-16 02:32:11 +00:00
&jwt::Header::new(alg),
2020-07-13 23:51:09 +00:00
&TokenClaims {},
2020-07-16 02:32:11 +00:00
&my_jwk.key.to_encoding_key(),
2020-07-13 23:51:09 +00:00
).unwrap();
2020-07-16 02:32:11 +00:00
let mut validation = jwt::Validation::new(alg);
2020-07-13 23:51:09 +00:00
validation.validate_exp = false;
2020-07-16 02:32:11 +00:00
jwt::decode::<TokenClaims>(&token, &my_jwk.key.to_decoding_key(), &validation).unwrap();
}
2020-07-12 21:23:06 +00:00
```
2020-07-13 20:46:42 +00:00
## Features
2020-07-16 02:32:11 +00:00
* `pkcs-convert` - enables `Key::{to_der, to_pem}`.
This pulls in the [yasna](https://crates.io/crates/yasna) crate.
2020-07-13 23:07:02 +00:00
* `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.
2020-07-16 02:32:11 +00:00
* `jwt-convert` - enables conversions to types in the
[jsonwebtoken](https://crates.io/crates/jsonwebtoken) crate.