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

fix: add support for multiple jwks keys per endpoint #753

Merged
merged 6 commits into from
Feb 7, 2025
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
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- fix: CORS: allow all request headers [#741](https://github.com/hypermodeinc/modus/pull/741)
- fix: accept long base64 strings [#742](https://github.com/hypermodeinc/modus/pull/742)
- fix: improve dgraph auth header passing [#752](https://github.com/hypermodeinc/modus/pull/752)
- fix: jwks endpoint should use key ID if available [#730](https://github.com/hypermodeinc/modus/pull/753)

## 2025-01-24 - Runtime 0.17.1

Expand Down
41 changes: 22 additions & 19 deletions runtime/middleware/jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,34 +205,37 @@ func jwksEndpointsJsonToKeys(ctx context.Context, jwksEndpointsJson string) (map
return nil, err
}
keys := make(map[string]any)
for key, value := range jwksEndpoints {
for endpointKey, value := range jwksEndpoints {
jwks, err := jwk.Fetch(ctx, value)
if err != nil {
return nil, err
}

jwkKey, exists := jwks.Key(0)
if !exists {
return nil, errors.New("No keys found in JWKS for key: " + key)
}
for it := jwks.Keys(ctx); it.Next(ctx); {
jwkKey := it.Pair().Value.(jwk.Key)
var rawKey any
if err := jwkKey.Raw(&rawKey); err != nil {
return nil, err
}

var rawKey any
err = jwkKey.Raw(&rawKey)
if err != nil {
return nil, err
}
// Marshal the raw key into DER-encoded PKIX format
derBytes, err := x509.MarshalPKIXPublicKey(rawKey)
if err != nil {
return nil, err
}

// Marshal the raw key into DER-encoded PKIX format
derBytes, err := x509.MarshalPKIXPublicKey(rawKey)
if err != nil {
return nil, err
}
pubKey, err := x509.ParsePKIXPublicKey(derBytes)
if err != nil {
return nil, err
}

pubKey, err := x509.ParsePKIXPublicKey(derBytes)
if err != nil {
return nil, err
// Use a combination of endpoint key and key ID (if available) as the map key
keyID := endpointKey
if kid, exists := jwkKey.Get("kid"); exists {
keyID = endpointKey + "_" + kid.(string)
}
keys[keyID] = pubKey
}
keys[key] = pubKey
}
return keys, nil
}