|
9 | 9 | "crypto/rand" |
10 | 10 | "crypto/rsa" |
11 | 11 | "encoding/json" |
| 12 | + "errors" |
12 | 13 | "fmt" |
13 | 14 | "net/http" |
14 | 15 | "net/http/httptest" |
@@ -138,6 +139,41 @@ func TestMismatchedKeyID(t *testing.T) { |
138 | 139 | testKeyVerify(t, key2, bad, key1, key2) |
139 | 140 | } |
140 | 141 |
|
| 142 | +func TestKeyVerifyContextCanceled(t *testing.T) { |
| 143 | + ctx, cancel := context.WithCancel(context.Background()) |
| 144 | + defer cancel() |
| 145 | + |
| 146 | + payload := []byte("a secret") |
| 147 | + |
| 148 | + good := newECDSAKey(t) |
| 149 | + jws, err := jose.ParseSigned(good.sign(t, payload)) |
| 150 | + if err != nil { |
| 151 | + t.Fatal(err) |
| 152 | + } |
| 153 | + |
| 154 | + ch := make(chan struct{}) |
| 155 | + defer close(ch) |
| 156 | + |
| 157 | + s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { |
| 158 | + <-ch |
| 159 | + })) |
| 160 | + defer s.Close() |
| 161 | + |
| 162 | + rks := newRemoteKeySet(ctx, s.URL, nil) |
| 163 | + |
| 164 | + cancel() |
| 165 | + |
| 166 | + // Ensure the token verifies. |
| 167 | + _, err = rks.verify(ctx, jws) |
| 168 | + if err == nil { |
| 169 | + t.Fatal("expected context canceled, got nil error") |
| 170 | + } |
| 171 | + |
| 172 | + if !errors.Is(err, context.Canceled) { |
| 173 | + t.Errorf("expected error to be %q got %q", context.Canceled, err) |
| 174 | + } |
| 175 | +} |
| 176 | + |
141 | 177 | func testKeyVerify(t *testing.T, good, bad *signingKey, verification ...*signingKey) { |
142 | 178 | ctx, cancel := context.WithCancel(context.Background()) |
143 | 179 | defer cancel() |
@@ -259,7 +295,7 @@ func BenchmarkVerify(b *testing.B) { |
259 | 295 |
|
260 | 296 | key := newRSAKey(b) |
261 | 297 |
|
262 | | - now := time.Date(2022, 01, 29, 0, 0, 0, 0, time.UTC) |
| 298 | + now := time.Date(2022, 1, 29, 0, 0, 0, 0, time.UTC) |
263 | 299 | exp := now.Add(time.Hour) |
264 | 300 | payload := []byte(fmt.Sprintf(`{ |
265 | 301 | "iss": "https://example.com", |
|
0 commit comments