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

oidc: use %w verb for wrapping errors #381

Merged
merged 1 commit into from
Jun 14, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions oidc/jwks.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (r *RemoteKeySet) verify(ctx context.Context, jws *jose.JSONWebSignature) (
// https://openid.net/specs/openid-connect-core-1_0.html#RotateSigKeys
keys, err := r.keysFromRemote(ctx)
if err != nil {
return nil, fmt.Errorf("fetching keys %v", err)
return nil, fmt.Errorf("fetching keys %w", err)
}

for _, key := range keys {
Expand Down Expand Up @@ -228,7 +228,7 @@ func (r *RemoteKeySet) updateKeys() ([]jose.JSONWebKey, error) {

resp, err := doRequest(r.ctx, req)
if err != nil {
return nil, fmt.Errorf("oidc: get keys failed %v", err)
return nil, fmt.Errorf("oidc: get keys failed %w", err)
}
defer resp.Body.Close()

Expand Down
38 changes: 37 additions & 1 deletion oidc/jwks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"crypto/rand"
"crypto/rsa"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -138,6 +139,41 @@ func TestMismatchedKeyID(t *testing.T) {
testKeyVerify(t, key2, bad, key1, key2)
}

func TestKeyVerifyContextCanceled(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

payload := []byte("a secret")

good := newECDSAKey(t)
jws, err := jose.ParseSigned(good.sign(t, payload))
if err != nil {
t.Fatal(err)
}

ch := make(chan struct{})
defer close(ch)

s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-ch
}))
defer s.Close()

rks := newRemoteKeySet(ctx, s.URL, nil)

cancel()

// Ensure the token verifies.
_, err = rks.verify(ctx, jws)
if err == nil {
t.Fatal("expected context canceled, got nil error")
}

if !errors.Is(err, context.Canceled) {
t.Errorf("expected error to be %q got %q", context.Canceled, err)
}
}

func testKeyVerify(t *testing.T, good, bad *signingKey, verification ...*signingKey) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
Expand Down Expand Up @@ -259,7 +295,7 @@ func BenchmarkVerify(b *testing.B) {

key := newRSAKey(b)

now := time.Date(2022, 01, 29, 0, 0, 0, 0, time.UTC)
rliebz marked this conversation as resolved.
Show resolved Hide resolved
now := time.Date(2022, 1, 29, 0, 0, 0, 0, time.UTC)
exp := now.Add(time.Hour)
payload := []byte(fmt.Sprintf(`{
"iss": "https://example.com",
Expand Down