hidekatsu-izuno / josekit-rs

JOSE (Javascript Object Signing and Encryption) library for Rust (based on OpenSSL).
Apache License 2.0
74 stars 31 forks source link

Allow deserialization of JwtPayload #8

Closed 0xpr03 closed 2 years ago

0xpr03 commented 2 years ago

Hello, I'd like to propose adding a method for deserialization of the JwtPayload.

Background: I want to use josekit to verify the basic claims (exp,aud,sub,iss) and then deserialize multiple (other) claims that I expect to exist, using them as part of the API. Doing that is currently very code intense and a lot of error handling:

let name: String = payload.claim("name")
        .ok_or(AuthError::MissingClaim("name"))
        .and_then(|v|josekit::Value::deserialize(v).map_err(AuthError::from))?;
let delete_after: Option<u32> = match payload.claim("delete_after").map(Deserialize::deserialize) {
         None => None,
         Some(Ok(i)) => Some(i),
         Some(Err(_)) => return Err(AuthError::InvalidClaim("delete_after")),
};

As the JwtPayload contains the inner parts of json::Value::Object, I'd like to do something like

#[derive(Deserialize)]
struct ExpectedClaims {
    name: String,
    should_timeout: Option<u32>,
    ...
}
// handle invalid/missing claims at once
let my_claims: ExpectedClaims = payload.deserialize()?;

The less API intrusive way would be providing a into_inner(self) -> Map<String,Value> using the into_inner(self) -> Map<String,Value> function. I just found the function 😅. But I think this could be expanded by directly returning json::Value::Object.

let claims: josekit::Map<String,josekit::Value>  = payload.into();
let val = serde_json::Value::Object(claims);
let my_claims: ExpectedClaims = serde_json::from_value(val)?;

Any thoughts ? Am I missing something here ? If you would like to, I can open a PR for one of them.

0xpr03 commented 2 years ago

Updated a bit to reflect the necessary stuff.

hidekatsu-izuno commented 2 years ago

Thank you for your request. But your suggestion is superfluous. I don't think this feature is nessecity.