|
1 | 1 | package oidc
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "bytes" |
5 | 4 | "context"
|
6 | 5 | "encoding/base64"
|
7 | 6 | "encoding/json"
|
8 |
| - "errors" |
9 | 7 | "fmt"
|
10 | 8 | "io/ioutil"
|
11 | 9 | "net/http"
|
@@ -211,12 +209,29 @@ func (v *IDTokenVerifier) Verify(ctx context.Context, rawIDToken string) (*IDTok
|
211 | 209 | return nil, fmt.Errorf("oidc: malformed jwt: %v", err)
|
212 | 210 | }
|
213 | 211 |
|
214 |
| - // Throw out tokens with invalid claims before trying to verify the token. This lets |
215 |
| - // us do cheap checks before possibly re-syncing keys. |
216 |
| - payload, err := parseJWT(rawIDToken) |
| 212 | + switch len(jws.Signatures) { |
| 213 | + case 0: |
| 214 | + return nil, fmt.Errorf("oidc: id token not signed") |
| 215 | + case 1: |
| 216 | + default: |
| 217 | + return nil, fmt.Errorf("oidc: multiple signatures on id token not supported") |
| 218 | + } |
| 219 | + |
| 220 | + sig := jws.Signatures[0] |
| 221 | + supportedSigAlgs := v.config.SupportedSigningAlgs |
| 222 | + if len(supportedSigAlgs) == 0 { |
| 223 | + supportedSigAlgs = []string{RS256} |
| 224 | + } |
| 225 | + |
| 226 | + if !contains(supportedSigAlgs, sig.Header.Algorithm) { |
| 227 | + return nil, fmt.Errorf("oidc: id token signed with unsupported algorithm, expected %q got %q", supportedSigAlgs, sig.Header.Algorithm) |
| 228 | + } |
| 229 | + |
| 230 | + payload, err := v.keySet.VerifySignature(ctx, rawIDToken) |
217 | 231 | if err != nil {
|
218 |
| - return nil, fmt.Errorf("oidc: malformed jwt: %v", err) |
| 232 | + return nil, fmt.Errorf("failed to verify signature: %v", err) |
219 | 233 | }
|
| 234 | + |
220 | 235 | var token idToken
|
221 | 236 | if err := json.Unmarshal(payload, &token); err != nil {
|
222 | 237 | return nil, fmt.Errorf("oidc: failed to unmarshal claims: %v", err)
|
@@ -296,36 +311,7 @@ func (v *IDTokenVerifier) Verify(ctx context.Context, rawIDToken string) (*IDTok
|
296 | 311 | }
|
297 | 312 | }
|
298 | 313 |
|
299 |
| - switch len(jws.Signatures) { |
300 |
| - case 0: |
301 |
| - return nil, fmt.Errorf("oidc: id token not signed") |
302 |
| - case 1: |
303 |
| - default: |
304 |
| - return nil, fmt.Errorf("oidc: multiple signatures on id token not supported") |
305 |
| - } |
306 |
| - |
307 |
| - sig := jws.Signatures[0] |
308 |
| - supportedSigAlgs := v.config.SupportedSigningAlgs |
309 |
| - if len(supportedSigAlgs) == 0 { |
310 |
| - supportedSigAlgs = []string{RS256} |
311 |
| - } |
312 |
| - |
313 |
| - if !contains(supportedSigAlgs, sig.Header.Algorithm) { |
314 |
| - return nil, fmt.Errorf("oidc: id token signed with unsupported algorithm, expected %q got %q", supportedSigAlgs, sig.Header.Algorithm) |
315 |
| - } |
316 |
| - |
317 | 314 | t.sigAlgorithm = sig.Header.Algorithm
|
318 |
| - |
319 |
| - gotPayload, err := v.keySet.VerifySignature(ctx, rawIDToken) |
320 |
| - if err != nil { |
321 |
| - return nil, fmt.Errorf("failed to verify signature: %v", err) |
322 |
| - } |
323 |
| - |
324 |
| - // Ensure that the payload returned by the square actually matches the payload parsed earlier. |
325 |
| - if !bytes.Equal(gotPayload, payload) { |
326 |
| - return nil, errors.New("oidc: internal error, payload parsed did not match previous payload") |
327 |
| - } |
328 |
| - |
329 | 315 | return t, nil
|
330 | 316 | }
|
331 | 317 |
|
|
0 commit comments