Skip to content
This repository has been archived by the owner on May 21, 2022. It is now read-only.

ParseWithClaims: getting json error when processing JWT that has multiple values for audience #348

Open
jeffkayser2 opened this issue Aug 7, 2019 · 17 comments · May be fixed by #355
Open

Comments

@jeffkayser2
Copy link

I am using JWT tokens generated by Auth0 and oauth2. The token payload looks like this:

{ "iss": "https://redacted.auth0.com/", "sub": "google-oauth2|redacted", "aud": [ "https://redacted:443/app/v1", "https://redacted.auth0.com/userinfo" ], "iat": 1565196366, "exp": 1565282766, "azp": "redacted", "scope": "openid profile email", "permissions": [ "admin" ] }

Because the audience is multi-valued, I am getting parsing error when trying to read the claims.

My parsing code looks like this:

type CustomClaimType struct { Permissions []string json:"permissions"Scope string json:"scope" jwt.StandardClaims }

. . .

// Now parse the token parsedToken, err := jwt.ParseWithClaims(bearerToken, &CustomClaimType{}, auth0ValidationKeyGetter) if err != nil { log.Printf("Got error parsing token: " + err.Error()) return nil, false, err }

When I run this, I get an error:

Got error parsing token: json: cannot unmarshal array into Go struct field CustomClaimType.aud of type string

@jeffkayser2
Copy link
Author

Issue348_Fix.zip

@jeffkayser2
Copy link
Author

I have fixed the code, but ran into an issue trying to push the change to github, so I attached a ZIP file of the changes instead.

@jugaltheshah
Copy link

jugaltheshah commented Aug 7, 2019

Having a similar issue using the jwt.StandardClaims with Audience defined as an array. Wouldn't this fix cause backwards compatibility issues @jeffkayser2?

I'm very new to Go so I don't know if this is possible, but it almost seems like Audience needs to be defined as Array | string, which also matches the spec: https://tools.ietf.org/html/rfc7519#section-4.1.3

@jeffkayser2
Copy link
Author

Good point. Could make it an interface{} instead. Would have to work through the various test cases to ensure that both Array and string claims are properly decoded and validated.

@dklesev
Copy link

dklesev commented Aug 27, 2019

@jeffkayser2 why you don't make a PR?

@jeffkayser2
Copy link
Author

I don't know git that well. Sorry. Here is a version that will work for Audiences that are both strings and []string.
Issue348v2.zip

@jeffkayser2
Copy link
Author

Ignore. Code works for string, but not []string.

@jeffkayser2
Copy link
Author

This version works for Audiences of type string and []string. Just an alternative.

Issue348v3.zip

Sorry for the lack of a pull request. I'm not that good at git yet.

@dklesev
Copy link

dklesev commented Aug 28, 2019

Refactored in favour of your solution (I think it's cleaner, hope it's ok for you me to push parts of your code), please see #355

@jeffkayser2
Copy link
Author

No problem! Thanks for doing that. One of these days, I'll figure out git. :-)

@friedrich
Copy link

Is this going to be fixed? Currently, RFC compliant tokens cannot be parsed because of this problem, which is a major issue in my opinion. Is there anything wrong with the proposed pull requests?

@alexksg
Copy link

alexksg commented Jan 20, 2020

Until now issue is not resolved.

@mileusna
Copy link

mileusna commented Feb 5, 2020

I took a sneak peek into v4.0.0 preview source code (https://github.com/dgrijalva/jwt-go/releases) and it looks like that v4 will fix this, but I don't know when it will be released.

@zbindenren
Copy link

I can confirm v4.0.0 preview solves this issue.

GSokol added a commit to GSokol/centrifuge that referenced this issue Apr 30, 2020
To fix jwt parsing problem (issue in jwt-go [centrifugal#348](dgrijalva/jwt-go#348)) emergency migrated to v4.
@chakch
Copy link

chakch commented Apr 30, 2020

Any stable release to fix this issue ?

@ivucica
Copy link

ivucica commented Aug 6, 2020

@dgrijalva Could you eventually put the contents of v4.0.0-preview into a subfolder named v4, so that neither the adoption of Go modules nor use of vendoring are required?

@trfv
Copy link

trfv commented Dec 14, 2021

[note]
you can use golang-jwt instead of jwt-go.

https://github.com/golang-jwt/jwt

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants