1
0
mirror of https://github.com/BitskiCo/jwk-rs synced 2024-11-22 03:49:22 +00:00

Add jsonwebtoken conversions

This commit is contained in:
Nick Hynes 2020-07-13 22:11:10 +00:00
parent 8aaf3d71c7
commit fa22e01714
No known key found for this signature in database
GPG Key ID: 5B3463E9F1D73C83
3 changed files with 48 additions and 16 deletions

View File

@ -12,6 +12,7 @@ edition = "2018"
base64 = "0.12" base64 = "0.12"
bitflags = "1.2" bitflags = "1.2"
derive_more = "0.99" derive_more = "0.99"
jsonwebtoken = { version = "7.2", optional = true }
num-bigint = { version = "0.2", optional = true } num-bigint = { version = "0.2", optional = true }
paste = "0.1" paste = "0.1"
serde = { version = "1.0", features = ["derive"] } serde = { version = "1.0", features = ["derive"] }

View File

@ -40,3 +40,4 @@ fn main() {
* `convert` - enables `Key::{to_der, to_pem}`. * `convert` - enables `Key::{to_der, to_pem}`.
This pulls in the [yasna](https://crates.io/crates/yasna) crate. This pulls in the [yasna](https://crates.io/crates/yasna) crate.
* `jsonwebtoken` - enables conversions to types in the [jsonwebtoken](https://crates.io/crates/jsonwebtoken) crate.

View File

@ -36,9 +36,41 @@ pub struct JsonWebKey {
} }
impl JsonWebKey { impl JsonWebKey {
pub fn new(key: Key) -> Self {
Self {
key: box key,
key_use: None,
key_ops: KeyOps::empty(),
key_id: None,
algorithm: None,
}
}
pub fn set_algorithm(&mut self, alg: JsonWebAlgorithm) -> Result<(), Error> {
Self::validate_algorithm(alg, &*self.key)?;
self.algorithm = Some(alg);
Ok(())
}
pub fn from_slice(bytes: impl AsRef<[u8]>) -> Result<Self, Error> { pub fn from_slice(bytes: impl AsRef<[u8]>) -> Result<Self, Error> {
Ok(serde_json::from_slice(bytes.as_ref())?) Ok(serde_json::from_slice(bytes.as_ref())?)
} }
fn validate_algorithm(alg: JsonWebAlgorithm, key: &Key) -> Result<(), Error> {
use JsonWebAlgorithm::*;
use Key::*;
match (alg, key) {
(
ES256,
EC {
curve: Curve::P256 { .. },
},
)
| (RS256, RSA { .. })
| (HS256, Symmetric { .. }) => Ok(()),
_ => Err(Error::MismatchedAlgorithm),
}
}
} }
impl std::str::FromStr for JsonWebKey { impl std::str::FromStr for JsonWebKey {
@ -46,24 +78,11 @@ impl std::str::FromStr for JsonWebKey {
fn from_str(json: &str) -> Result<Self, Self::Err> { fn from_str(json: &str) -> Result<Self, Self::Err> {
let jwk = Self::from_slice(json.as_bytes())?; let jwk = Self::from_slice(json.as_bytes())?;
// Validate alg. let alg = match jwk.algorithm {
use JsonWebAlgorithm::*;
use Key::*;
let alg = match &jwk.algorithm {
Some(alg) => alg, Some(alg) => alg,
None => return Ok(jwk), None => return Ok(jwk),
}; };
match (alg, &*jwk.key) { Self::validate_algorithm(alg, &*jwk.key).map(|_| jwk)
(
ES256,
EC {
curve: Curve::P256 { .. },
},
)
| (RS256, RSA { .. })
| (HS256, Symmetric { .. }) => Ok(jwk),
_ => Err(Error::MismatchedAlgorithm),
}
} }
} }
@ -346,13 +365,24 @@ pub enum KeyUse {
Encryption, Encryption,
} }
#[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Zeroize)] #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize, Zeroize)]
pub enum JsonWebAlgorithm { pub enum JsonWebAlgorithm {
HS256, HS256,
RS256, RS256,
ES256, ES256,
} }
#[cfg(any(test, feature = "jsonwebtoken"))]
impl Into<jsonwebtoken::Algorithm> for JsonWebAlgorithm {
fn into(self) -> jsonwebtoken::Algorithm {
match self {
Self::HS256 => jsonwebtoken::Algorithm::HS256,
Self::ES256 => jsonwebtoken::Algorithm::ES256,
Self::RS256 => jsonwebtoken::Algorithm::RS256,
}
}
}
#[derive(Debug, thiserror::Error)] #[derive(Debug, thiserror::Error)]
pub enum Error { pub enum Error {
#[error(transparent)] #[error(transparent)]