Skip to content

Commit a6a4a06

Browse files
committed
Remove non-ctx version
1 parent 95ea691 commit a6a4a06

2 files changed

Lines changed: 10 additions & 30 deletions

File tree

server/auth/mw/jwtcheck/jwtcheck.go

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"net/http"
1010
"os"
1111
"strings"
12-
"time"
1312

1413
keyfunc "github.com/MicahParks/keyfunc/v3"
1514
"github.com/golang-jwt/jwt/v5"
@@ -39,22 +38,9 @@ func JWTMiddleware(jwtAudience string, jwtIssuer string, pubKeyPath string, useE
3938

4039
// JWTMiddlewareOIDC checks and pulls user information from JWT in Authorization header.
4140
// The JWKS keys are discovered from the issuer's OpenID Connect discovery endpoint.
42-
func JWTMiddlewareOIDC(jwtAudience string, jwtIssuer string, useEmailAsId bool) (func(http.Handler) http.Handler, error) {
43-
jwksURL, err := discoverJWKSURL(jwtIssuer)
44-
if err != nil {
45-
return nil, fmt.Errorf("OIDC discovery failed: %w", err)
46-
}
47-
kf, err := keyfunc.NewDefault([]string{jwksURL})
48-
if err != nil {
49-
return nil, fmt.Errorf("failed to create JWKS keyfunc: %w", err)
50-
}
51-
return newJWTHandler(kf.Keyfunc, jwtAudience, jwtIssuer, useEmailAsId), nil
52-
}
53-
54-
// JWTMiddlewareOIDCCtx is like JWTMiddlewareOIDC but accepts a context that controls
55-
// the lifetime of the background JWKS refresh goroutine.
56-
func JWTMiddlewareOIDCCtx(ctx context.Context, jwtAudience string, jwtIssuer string, useEmailAsId bool) (func(http.Handler) http.Handler, error) {
57-
jwksURL, err := discoverJWKSURLWithContext(ctx, jwtIssuer)
41+
// The context controls the lifetime of the background JWKS refresh goroutine.
42+
func JWTMiddlewareOIDC(ctx context.Context, jwtAudience string, jwtIssuer string, useEmailAsId bool) (func(http.Handler) http.Handler, error) {
43+
jwksURL, err := discoverJWKSURL(ctx, jwtIssuer)
5844
if err != nil {
5945
return nil, fmt.Errorf("OIDC discovery failed: %w", err)
6046
}
@@ -66,13 +52,7 @@ func JWTMiddlewareOIDCCtx(ctx context.Context, jwtAudience string, jwtIssuer str
6652
}
6753

6854
// discoverJWKSURL fetches the OIDC discovery document from the issuer and returns the jwks_uri.
69-
func discoverJWKSURL(issuer string) (string, error) {
70-
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
71-
defer cancel()
72-
return discoverJWKSURLWithContext(ctx, issuer)
73-
}
74-
75-
func discoverJWKSURLWithContext(ctx context.Context, issuer string) (string, error) {
55+
func discoverJWKSURL(ctx context.Context, issuer string) (string, error) {
7656
discoveryURL := strings.TrimRight(issuer, "/") + "/.well-known/openid-configuration"
7757
req, err := http.NewRequestWithContext(ctx, http.MethodGet, discoveryURL, nil)
7858
if err != nil {

server/auth/mw/jwtcheck/jwtcheck_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ func signToken(t *testing.T, key *rsa.PrivateKey, claims jwt.Claims) string {
3535
return s
3636
}
3737

38-
func TestDiscoverJWKSURLWithContext(t *testing.T) {
38+
func TestDiscoverJWKSURL(t *testing.T) {
3939
t.Run("valid discovery document", func(t *testing.T) {
4040
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
4141
assert.Equal(t, "/.well-known/openid-configuration", r.URL.Path)
@@ -44,7 +44,7 @@ func TestDiscoverJWKSURLWithContext(t *testing.T) {
4444
})
4545
}))
4646
defer srv.Close()
47-
url, err := discoverJWKSURLWithContext(context.Background(), srv.URL)
47+
url, err := discoverJWKSURL(context.Background(), srv.URL)
4848
assert.NoError(t, err)
4949
assert.Equal(t, "https://example.com/.well-known/jwks.json", url)
5050
})
@@ -54,7 +54,7 @@ func TestDiscoverJWKSURLWithContext(t *testing.T) {
5454
json.NewEncoder(w).Encode(map[string]string{"issuer": "https://example.com"})
5555
}))
5656
defer srv.Close()
57-
_, err := discoverJWKSURLWithContext(context.Background(), srv.URL)
57+
_, err := discoverJWKSURL(context.Background(), srv.URL)
5858
assert.ErrorContains(t, err, "missing jwks_uri")
5959
})
6060

@@ -63,7 +63,7 @@ func TestDiscoverJWKSURLWithContext(t *testing.T) {
6363
w.WriteHeader(http.StatusNotFound)
6464
}))
6565
defer srv.Close()
66-
_, err := discoverJWKSURLWithContext(context.Background(), srv.URL)
66+
_, err := discoverJWKSURL(context.Background(), srv.URL)
6767
assert.ErrorContains(t, err, "returned status 404")
6868
assert.ErrorContains(t, err, srv.URL)
6969
})
@@ -73,14 +73,14 @@ func TestDiscoverJWKSURLWithContext(t *testing.T) {
7373
w.Write([]byte("not json"))
7474
}))
7575
defer srv.Close()
76-
_, err := discoverJWKSURLWithContext(context.Background(), srv.URL)
76+
_, err := discoverJWKSURL(context.Background(), srv.URL)
7777
assert.ErrorContains(t, err, "failed to parse")
7878
})
7979

8080
t.Run("respects context cancellation", func(t *testing.T) {
8181
ctx, cancel := context.WithCancel(context.Background())
8282
cancel() // cancel immediately
83-
_, err := discoverJWKSURLWithContext(ctx, "http://localhost:0")
83+
_, err := discoverJWKSURL(ctx, "http://localhost:0")
8484
assert.Error(t, err)
8585
})
8686
}

0 commit comments

Comments
 (0)