Skip to content

Validating Google aud field throws an error #110

Closed

Description

@rokit

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.

Activity

  1. Keats commented on Nov 27, 2019

    @Keats
    Owner
  2. rokit commented on Nov 27, 2019

    @rokit
    Author

    Here is a failing test:

        #[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.

  3. rokit commented on Nov 27, 2019

    @rokit
    Author

    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);

  4. rokit commented on Nov 27, 2019

    @rokit
    Author

    The aud I'm getting from Google looks like this:
    "aud": "my-googleclientid1234.apps.googleusercontent.com"

  5. rokit commented on Nov 27, 2019

    @rokit
    Author

    I found this:
    https://tools.ietf.org/html/rfc7519#section-4.1.3

    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.

  6. Keats commented on Nov 28, 2019

    @Keats
    Owner

    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.

  7. added a commit that references this issue on Nov 28, 2019
    bfcfc1d
  8. Keats commented on Nov 28, 2019

    @Keats
    Owner

    Alpha 2 released with this bug fixed.

  9. rokit commented on Nov 28, 2019

    @rokit
    Author

    Awesome! Thanks Keats!

  10. added a commit that references this issue on Apr 13, 2023
    ea96357
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions