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

Change multi JWK Set min from 2 to 1 #88

Merged
merged 1 commit into from
May 13, 2023
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
4 changes: 2 additions & 2 deletions multiple.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ type MultipleJWKS struct {
// map then many refresh requests would take place each time a JWT is processed, this should be rate limited by
// RefreshRateLimit.
func GetMultiple(multiple map[string]Options, options MultipleOptions) (multiJWKS *MultipleJWKS, err error) {
if multiple == nil || len(multiple) < 2 {
return nil, fmt.Errorf("multiple JWKS must have two or more remote JWK Set resources: %w", ErrMultipleJWKSSize)
if len(multiple) < 1 {
return nil, fmt.Errorf("multiple JWKS must have one or more remote JWK Set resources: %w", ErrMultipleJWKSSize)
}

if options.KeySelector == nil {
Expand Down
40 changes: 40 additions & 0 deletions multiple_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,46 @@ func TestMultipleJWKS(t *testing.T) {
}
}

func TestMultipleJWKSSingle(t *testing.T) {
server1 := createTestServer([]byte(jwks1))
defer server1.Close()

const (
collisionJWT = "eyJhbGciOiJFZERTQSIsImtpZCI6ImNvbGxpc2lvbktJRCIsInR5cCI6IkpXVCJ9.e30.WXKmhyHjHQFXZ8dXfj07RvwKAgHB3EdGU1jeKUEY-wajgsRsHuhnotX1WqDSlngwGerEitnIcdMGViW_HNUCAA"
uniqueJWT = "eyJhbGciOiJFZERTQSIsImtpZCI6InVuaXF1ZUtJRCIsInR5cCI6IkpXVCJ9.e30.egdT5_vXYKIM7UfsyewYaR63tS9T9JvKwUJs7Srj6wG9JHXMvN9Ftq0rJGem07ESVtN5OtlcJOaMgSbtxnc6Bg"
)

m := map[string]keyfunc.Options{
server1.URL: {},
}

multiJWKS, err := keyfunc.GetMultiple(m, keyfunc.MultipleOptions{})
if err != nil {
t.Fatalf("failed to get multiple JWKS: %v", err)
}

token, err := jwt.Parse(collisionJWT, multiJWKS.Keyfunc)
if err != nil {
t.Fatalf("failed to parse collision JWT: %v", err)
}
if !token.Valid {
t.Fatalf("collision JWT is invalid")
}

token, err = jwt.Parse(uniqueJWT, multiJWKS.Keyfunc)
if err != nil {
t.Fatalf("failed to parse unique JWT: %v", err)
}
if !token.Valid {
t.Fatalf("unique JWT is invalid")
}

sets := multiJWKS.JWKSets()
if len(sets) != 1 {
t.Fatalf("expected 2 JWKS, got %d", len(sets))
}
}

func createTestServer(body []byte) *httptest.Server {
return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json")
Expand Down