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
I have the following code that creates a validation struct and decodes a token:
let aud =
"my-googleclientid1234.apps.googleusercontent.com".to_string();
let mut aud_hashset = HashSet::new();
aud_hashset.insert(aud);
let validation = Validation {
algorithms: vec![Algorithm::RS256],
aud: Some(aud_hashset),
..Validation::default()
};
let token_data_rsa = jwt::decode_rsa_components::<GooglePayload>(&token, &n, &e, &validation);
But it throws an error: Err(Error(Json(Error("invalid type: string \"my-googleclientid1234.apps.googleusercontent.com\", expected a sequence", line: 0, column: 0))))
If I don't try to validate the aud field, then the token decodes as expected.
#[test]
fn aud_use_validation_struct() {
let mut claims = Map::new();
claims.insert("aud".to_string(), to_value("my-googleclientid1234.apps.googleusercontent.com").unwrap());
let aud = "my-googleclientid1234.apps.googleusercontent.com".to_string();
let mut aud_hashset = std::collections::HashSet::new();
aud_hashset.insert(aud);
let validation = Validation {
aud: Some(aud_hashset),
validate_exp: false,
..Validation::default()
};
let res = validate(&claims, &validation);
println!("{:?}", res);
assert!(res.is_ok());
}
I threw the println in there so you could see the error I'm seeing.
I thought the reason might be because I was adding the aud field while creating the Validation struct as opposed to doing it this way: validation.set_audience(&["my-googleclientid1234.apps.googleusercontent.com"]);
but even the above is not working in my own project.
Wait a minute. I see to_value is taking an array now: claims.insert("aud".to_string(), to_value(["everyone"]).unwrap());
Sorry, I must have missed that since I initially copied the test from the master branch which doesn't have it in an array like the next branch does. Never mind, it always took an array. I must have changed it but don't remember.
In that case, how should I be structuring my GooglePayload? I thought aud took a single String value. Honestly, the use of a collection (HashSet) is confusing me for that reason.
I tried this:
GooglePayload {
...
aud: String, // also tried aud: HashSet<String> and aud: Vec<String>
...
}
I think the issue is Serde can't decode the token to my GooglePayload struct. I confirmed that the error is happening on this line: jwt::decode_rsa_components::<GooglePayload>(&token, &n, &e, &validation);
I don't think it's right that the aud should always be a collection. Per the spec, it can be a single value. I think that's why Serde can't decode into it. Google is giving me an aud value that decodes to a single String, my GooglePayload is expecting a single String, but jsonwebtoken is encoding the aud claim to a HashSet...if I'm understanding things correctly, which I may not be.
In theory the set should be internal since it can have multiple values but not necessarily as you mention. If you only have one aud you still need to pass it as an array right now but it should be treated a single value internally. I'll check later.
Activity
Keats commentedon Nov 27, 2019
In the v7 alpha? Can you come up with a failing test in https://github.com/Keats/jsonwebtoken/blob/next/src/validation.rs ?
rokit commentedon Nov 27, 2019
Here is a failing test:
I threw the
printlnin there so you could see the error I'm seeing.I thought the reason might be because I was adding the
audfield while creating the Validation struct as opposed to doing it this way:validation.set_audience(&["my-googleclientid1234.apps.googleusercontent.com"]);but even the above is not working in my own project.
rokit commentedon Nov 27, 2019
Wait a minute. I see
to_valueis taking an array now:claims.insert("aud".to_string(), to_value(["everyone"]).unwrap());Sorry, I must have missed that
since I initially copied the test from the master branch which doesn't have it in an array like the. Never mind, it always took an array. I must have changed it but don't remember.nextbranch doesIn that case, how should I be structuring my
GooglePayload? I thoughtaudtook a singleStringvalue. Honestly, the use of a collection (HashSet) is confusing me for that reason.I tried this:
I think the issue is Serde can't decode the token to my
GooglePayloadstruct. I confirmed that the error is happening on this line:jwt::decode_rsa_components::<GooglePayload>(&token, &n, &e, &validation);rokit commentedon Nov 27, 2019
The
audI'm getting from Google looks like this:"aud": "my-googleclientid1234.apps.googleusercontent.com"rokit commentedon Nov 27, 2019
I found this:
https://tools.ietf.org/html/rfc7519#section-4.1.3
I don't think it's right that the
audshould always be a collection. Per the spec, it can be a single value. I think that's why Serde can't decode into it. Google is giving me anaudvalue that decodes to a single String, myGooglePayloadis expecting a single String, butjsonwebtokenis encoding theaudclaim to a HashSet...if I'm understanding things correctly, which I may not be.Keats commentedon Nov 28, 2019
In theory the set should be internal since it can have multiple values but not necessarily as you mention. If you only have one
audyou still need to pass it as an array right now but it should be treated a single value internally. I'll check later.Handle aud not being a sequence
Keats commentedon Nov 28, 2019
Alpha 2 released with this bug fixed.
rokit commentedon Nov 28, 2019
Awesome! Thanks Keats!
Handle aud not being a sequence