Skip to content

Commit 8f8a2b9

Browse files
committed
Implement TryFrom &Jwk for DecodingKey
1 parent 1456755 commit 8f8a2b9

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

examples/auth0.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
2121
return Err("No matching JWK found for the given kid".into());
2222
};
2323

24-
let decoding_key = DecodingKey::from_jwk(jwk)?;
25-
2624
let validation = {
2725
let mut validation = Validation::new(header.alg);
2826
validation.set_audience(&["https://dev-duzyayk4.eu.auth0.com/api/v2/"]);
@@ -31,7 +29,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
3129
};
3230

3331
let decoded_token =
34-
decode::<HashMap<String, serde_json::Value>>(TOKEN, &decoding_key, &validation)?;
32+
decode::<HashMap<String, serde_json::Value>>(TOKEN, &jwk.try_into()?, &validation)?;
3533

3634
println!("{:#?}", decoded_token);
3735

src/decoding.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,14 @@ impl DecodingKey {
251251
}
252252
}
253253

254+
impl TryFrom<&Jwk> for DecodingKey {
255+
type Error = crate::errors::Error;
256+
257+
fn try_from(jwk: &Jwk) -> Result<Self> {
258+
Self::from_jwk(jwk)
259+
}
260+
}
261+
254262
/// Decode and validate a JWT
255263
///
256264
/// If the token or its signature is invalid or the claims fail validation, it will return an error.

0 commit comments

Comments
 (0)