Skip to content

Commit 07baf17

Browse files
authored
Prevent resource leak in keyfunc initialization (#382)
Signed-off-by: Micah Parks <[email protected]>
1 parent e101759 commit 07baf17

File tree

1 file changed

+13
-7
lines changed

1 file changed

+13
-7
lines changed

pkg/client/acr/acr.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const (
2626
)
2727

2828
type Client struct {
29+
Keyfunc keyfunc.Keyfunc
2930
Options
3031

3132
cacheMu sync.Mutex
@@ -62,7 +63,17 @@ func New(opts Options) (*Client, error) {
6263
return nil, errors.New("cannot specify refresh token as well as username/password")
6364
}
6465

66+
var k keyfunc.Keyfunc
67+
var err error
68+
if opts.JWKSURI != "" {
69+
k, err = keyfunc.NewDefaultCtx(context.TODO(), []string{opts.JWKSURI})
70+
if err != nil {
71+
return nil, fmt.Errorf("failed to create keyfunc: %w", err)
72+
}
73+
}
74+
6575
return &Client{
76+
Keyfunc: k,
6677
Options: opts,
6778
cachedACRClient: make(map[string]*acrClient),
6879
}, nil
@@ -266,13 +277,8 @@ func (c *Client) getTokenExpiration(tokenString string) (time.Time, error) {
266277
jwtParser := jwt.NewParser(jwt.WithoutClaimsValidation())
267278
var token *jwt.Token
268279
var err error
269-
if c.JWKSURI != "" {
270-
var k keyfunc.Keyfunc
271-
k, err = keyfunc.NewDefaultCtx(context.TODO(), []string{c.JWKSURI})
272-
if err != nil {
273-
return time.Time{}, err
274-
}
275-
token, err = jwtParser.Parse(tokenString, k.Keyfunc)
280+
if c.Keyfunc != nil {
281+
token, err = jwtParser.Parse(tokenString, c.Keyfunc.Keyfunc)
276282
} else {
277283
token, _, err = jwtParser.ParseUnverified(tokenString, jwt.MapClaims{})
278284
}

0 commit comments

Comments
 (0)