Skip to content

Commit

Permalink
Merge pull request #260 from ericchiang/ctx
Browse files Browse the repository at this point in the history
oidc: separate NewProvider context and Verifier context
  • Loading branch information
ericchiang authored Jul 17, 2020
2 parents 5d97e1d + e30e73e commit 7a14ede
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
13 changes: 12 additions & 1 deletion oidc/oidc.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,17 @@ func ClientContext(ctx context.Context, client *http.Client) context.Context {
return context.WithValue(ctx, oauth2.HTTPClient, client)
}

// cloneContext copies a context's bag-of-values into a new context that isn't
// associated with its cancelation. This is used to initialize remote keys sets
// which run in the background and aren't associated with the initial context.
func cloneContext(ctx context.Context) context.Context {
cp := context.Background()
if c, ok := ctx.Value(oauth2.HTTPClient).(*http.Client); ok {
cp = ClientContext(cp, c)
}
return cp
}

func doRequest(ctx context.Context, req *http.Request) (*http.Response, error) {
client := http.DefaultClient
if c, ok := ctx.Value(oauth2.HTTPClient).(*http.Client); ok {
Expand Down Expand Up @@ -153,7 +164,7 @@ func NewProvider(ctx context.Context, issuer string) (*Provider, error) {
userInfoURL: p.UserInfoURL,
algorithms: algs,
rawClaims: body,
remoteKeySet: NewRemoteKeySet(ctx, p.JWKSURL),
remoteKeySet: NewRemoteKeySet(cloneContext(ctx), p.JWKSURL),
}, nil
}

Expand Down
13 changes: 13 additions & 0 deletions oidc/oidc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,19 @@ func TestNewProvider(t *testing.T) {
}
}

func TestCloneContext(t *testing.T) {
ctx := context.Background()
if _, ok := cloneContext(ctx).Value(oauth2.HTTPClient).(*http.Client); ok {
t.Errorf("cloneContext(): expected no *http.Client from empty context")
}

c := &http.Client{}
ctx = ClientContext(ctx, c)
if got, ok := cloneContext(ctx).Value(oauth2.HTTPClient).(*http.Client); !ok || c != got {
t.Errorf("cloneContext(): expected *http.Client from context")
}
}

type testServer struct {
contentType string
userInfo string
Expand Down

0 comments on commit 7a14ede

Please sign in to comment.