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

Support array in "aud" claim #445

Open
milin-wish opened this issue Jan 18, 2021 · 5 comments
Open

Support array in "aud" claim #445

milin-wish opened this issue Jan 18, 2021 · 5 comments

Comments

@milin-wish
Copy link

Hi,

According to the spec, "In the general case, the "aud" value is an array of case-sensitive strings, each containing a StringOrURI value." Can you update this library to support an array in the "aud" value?

Thanks,
Mike

@Schalex1998
Copy link

Hey,

we have the same issue when going from v3 to v4.

Best,
Alex

@quetzyg
Copy link

quetzyg commented Feb 26, 2021

I just ran into this issue today. If I added this functionality, would you accept a pull request, @dgrijalva?

Scrap that, I just realised that the v4 branch has this sorted here.

@Schalex1998
Copy link

Schalex1998 commented Feb 26, 2021

jwt.Parse(token, func(token *jwt.Token) (i interface{}, e error) {
	return rsaPublicKey, nil
}, jwt.WithoutAudienceValidation())

we just removed the validation as workaround btw

@quetzyg
Copy link

quetzyg commented Feb 26, 2021

jwt.Parse(token, func(token *jwt.Token) (i interface{}, e error) {
	return rsaPublicKey, nil
}, jwt.WithoutAudienceValidation())

we just removed the validation as workaround btw

That's just wrong.

@quetzyg
Copy link

quetzyg commented Feb 26, 2021

For those that are stuck in version 3.x for the time being, this work around does the trick:

func verifyAudience(claims jwt.MapClaims, audience string) bool {
	original := claims["aud"]

	switch aud := claims["aud"].(type) {
	case string:
		return claims.VerifyAudience(audience, true)
	case []interface{}:
		for _, val := range aud {
			if s, ok := val.(string); ok {
				claims["aud"] = s

				if claims.VerifyAudience(audience, true) {
					claims["aud"] = original

					return true
				}
			}
		}
	}

	claims["aud"] = original

	return false
}

So instead of calling the VerifyAudience() method of jwt.MapClaims like:

valid := claims.VerifyAudience("some.audience", true);

it would instead be:

valid := verifyAudience(claims, "some.audience");

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

No branches or pull requests

3 participants