Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Package JWT: allow clients to optionally enforce matching of aud, iss and sub claims #419

Merged
merged 6 commits into from
May 16, 2024

Conversation

techmanmike
Copy link

@techmanmike techmanmike commented May 15, 2024

Purpose: Allow clients to enforce matching aud, iss and sub claims

Changes:

  • created DecoderParserOptions with MustMatchAudience, MustMatchIssuer and MustMatchSubject methods
  • updated jwt/README.md

@techmanmike techmanmike changed the title allow clients to optional enforce matching of aud iss and sub claims Pacakge JWT: allow clients to optional enforce matching of aud, iss and sub claims May 15, 2024
@techmanmike techmanmike changed the title Pacakge JWT: allow clients to optional enforce matching of aud, iss and sub claims Pacakge JWT: allow clients to optionally enforce matching of aud, iss and sub claims May 15, 2024
@techmanmike techmanmike changed the title Pacakge JWT: allow clients to optionally enforce matching of aud, iss and sub claims Package JWT: allow clients to optionally enforce matching of aud, iss and sub claims May 15, 2024
@techmanmike techmanmike marked this pull request as ready for review May 15, 2024 23:18
// token or the `aud` claim is missing.
func MustMatchAudience(aud string) DecoderParserOption {
return func(p *decoderParser) {
p.expectedAud = aud
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

jwt.io seems to parse our example token as an array of strings
Screenshot 2024-05-16 at 2 02 56 PM
Do we need to cater for mutliple audiences? or would we only ever expect 1 aud value?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes - the encoded claims can specify multiple audiences, but the decoder just checks that the aud that is "itself" is in that list.

See decoder.go line 123:

if parserOptions.expectedAud != "" {
opts = append(opts, jwt.WithAudience(parserOptions.expectedAud))
}

That "jwt.WithAudience()" goes onto be implemented inside go-jwt as:

func (v *Validator) verifyAudience(claims Claims, cmp string, required bool) error {
aud, err := claims.GetAudience()
if err != nil {
return err
}

if len(aud) == 0 {
	return errorIfRequired(required, "aud")
}

// use a var here to keep constant time compare when looping over a number of claims
result := false

var stringClaims string
for _, a := range aud {
	if subtle.ConstantTimeCompare([]byte(a), []byte(cmp)) != 0 {
		result = true
	}
	stringClaims = stringClaims + a
}

// case where "" is sent in one or many aud claims
if stringClaims == "" {
	return errorIfRequired(required, "aud")
}

return errorIfFalse(result, ErrTokenInvalidAudience)

}

Which loops through all the audiences and check that "this" audience is in that list.

Make sense?

AndrewMcCraeCA
AndrewMcCraeCA previously approved these changes May 16, 2024
Copy link
Contributor

@AndrewMcCraeCA AndrewMcCraeCA left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left one comment but otherwise looks good

Copy link

Package Line Rate Health
github.com/cultureamp/ca-go/cipher 79%
github.com/cultureamp/ca-go/env 95%
github.com/cultureamp/ca-go/jwt 87%
github.com/cultureamp/ca-go/kafka/consumer 82%
github.com/cultureamp/ca-go/launchdarkly 71%
github.com/cultureamp/ca-go/launchdarkly/evaluationcontext 96%
github.com/cultureamp/ca-go/log 93%
github.com/cultureamp/ca-go/ref 100%
github.com/cultureamp/ca-go/request 100%
github.com/cultureamp/ca-go/secrets 41%
github.com/cultureamp/ca-go/sentry 99%
Summary 88% (1876 / 2128)

@techmanmike techmanmike enabled auto-merge (squash) May 16, 2024 05:22
@techmanmike techmanmike merged commit 5fc8487 into main May 16, 2024
7 checks passed
@techmanmike techmanmike deleted the jwt-iss-aud-sub-validation branch May 16, 2024 05:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants