You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
According to the JWT introduction, claims should correspond to fields within the JWT payload. However, when passing a value to the claims parameter, it serializes as an embedded object under the claims key in the JWT payload instead of merging with the predefined claims. This behavior differs from the expected result of integrating the custom claims directly into the payload.
Here’s the definition of the claims structure from src/auth/jwt.rs:
/// Represents the claims associated with a user JWT.#[derive(Debug,Serialize,Deserialize)]pubstructUserClaims{pubpid:String,exp:u64,pubclaims:Option<Value>,}
To Reproduce
#[derive(Debug,Default,Deserialize,Serialize)]pubstructUserJwtClaims{pubroles:Vec<String>,}fnmain() -> Result<(),Box<dynError>>{let custom_claims = Some(serde_json::to_value(UserJwtClaims{roles:vec!["admin".to_owned(),"non-admin".to_owned()];}).unwrap());let res = auth::jwt::JWT::new("PqRwLF2rhHe8J22oBeHy").generate_token(&604800,"PID".to_string(), custom_claims)?;}
I believe this can be resolved with a small adjustment to use the #[serde(flatten)] attribute in the UserClaims struct:
/// Represents the claims associated with a user JWT.#[derive(Debug,Serialize,Deserialize)]pubstructUserClaims{pubpid:String,exp:u64,#[serde(flatten)]pubclaims:Option<Value>,}
This change would allow the claims to merge seamlessly with the predefined claims, achieving the expected behavior.
The text was updated successfully, but these errors were encountered:
/// Represents the claims associated with a user JWT.#[derive(Debug,Serialize,Deserialize)]pubstructUserClaims{pubpid:String,exp:u64,#[serde(default, flatten)]pubclaims:HashMap<String,Value>,}
instead. with your proposal, if the Value is not a map (for example an integer) I wonder how will it behave with flatten
I would like to address this issue. Note that this will involve a breaking change, because of replacement of Option<Value> to serde_json::Map<String,Value>
Description
I am building an RBAC system using the built-in JWT support. When using the claims parameter in src/auth/jwt.rs, I encountered unexpected behavior.
Here's the function signature in question:
According to the JWT introduction, claims should correspond to fields within the JWT payload. However, when passing a value to the claims parameter, it serializes as an embedded object under the claims key in the JWT payload instead of merging with the predefined claims. This behavior differs from the expected result of integrating the custom claims directly into the payload.
Here’s the definition of the claims structure from src/auth/jwt.rs:
To Reproduce
Expected Behavior
I expected the following JWT payload:
Instead, the actual JWT payload looks like this:
Environment:
Additional Context
I believe this can be resolved with a small adjustment to use the #[serde(flatten)] attribute in the UserClaims struct:
This change would allow the claims to merge seamlessly with the predefined claims, achieving the expected behavior.
The text was updated successfully, but these errors were encountered: