Skip to content

feat: parse with generic type T: DeserializeOwned #76

@carlocorradini

Description

@carlocorradini

Is your feature request related to a problem? Please describe.
Currently, the parse function returns Value, which is just a generic JSON value with no additional type information.
It would be very useful if the parse function accepts a generic type T: DeserializeOwned, with the default type being Value.
This allow users to parse the token directly into a known, strongly-typed struct, which is much more ergonomic to work with.
This doesn’t introduce a breaking change, because if no type is specified or inferred, the default will still be the original Value, preserving the existing behavior.

Describe the solution you'd like

#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(deny_unknown_fields)]
pub struct Claim {
    iat: DateTime<Utc>,
    nbf: DateTime<Utc>,
    exp: DateTime<Utc>,
    my_custom_claim: ComplexStruct,
}

let claim: Claim = PasetoParser::default().parse(&token, &key).unwrap();
let claim: serde_json::Value = PasetoParser::default().parse(&token, &key).unwrap();

Given a custom struct that represents the default/custom claims, deserialize it from the given token.
Adding #[serde(deny_unknown_fields)] ensures that deserialization will fail if the token contains any field not defined in the struct.

Not that internally we continue to use serde_json::Value to validate claims and related data but instead of returning it directly, we convert it using serde_json::from_value.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions