8000 Add support for multiple JWKS urls by johnlanda · Pull Request #128 · hashicorp/cap · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add support for multiple JWKS urls #128

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

Merged
merged 4 commits into from
Feb 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
8000
Diff view
Diff view
38 changes: 29 additions & 9 deletions jwt/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,27 @@ import (
const DefaultLeewaySeconds = 150

// Validator validates JSON Web Tokens (JWT) by providing signature
// verification and claims set validation.
// verification and claims set validation. Validator can contain either
// a single or multiple KeySets and will attempt to verify the JWT by iterating
// through the configured KeySets.
type Validator struct {
keySet KeySet
keySets []KeySet
}

// NewValidator returns a Validator that uses the given KeySet to verify JWT signatures.
func NewValidator(keySet KeySet) (*Validator, error) {
if keySet == nil {
return nil, errors.New("keySet must not be nil")
func NewValidator(keySets ...KeySet) (*Validator, error) {
if len(keySets) <= 0 {
return nil, errors.New("must provide at least one key set")
}

for _, keySet := range keySets {
if keySet == nil {
return nil, errors.New("keySet must not be nil")
}
}

return &Validator{
keySet: keySet,
keySets: keySets,
}, nil
}

Expand Down Expand Up @@ -116,9 +124,21 @@ func (v *Validator) ValidateAllowMissingIatNbfExp(ctx context.Context, token str
}

func (v *Validator) validateAll(ctx context.Context, token string, expected Expected, allowMissingIatExpNbf bool) (map[string]interface{}, error) {
// First, verify the signature to ensure subsequent validation is against verified claims
allClaims, err := v.keySet.VerifySignature(ctx, token)
if err != nil {
var allClaims map[string]interface{}
var err error

// Ensure that the token is signed by at least one of the given key sets
var tokenVerified bool
for _, keySet := range v.keySets {
// First, verify the signature to ensure subsequent validation is against verified claims
allClaims, err = keySet.VerifySignature(ctx, token)
if err == nil {
tokenVerified = true
break
}
}

if !tokenVerified {
return nil, fmt.Errorf("error verifying token signature: %w", err)
}

Expand Down
Loading
0