Skip to content

Commit d12567f

Browse files
palsivertsenhiranya911
authored andcommitted
Move token signature verification after fields (#175)
Token signature validation can be an expensive operation. For tokens with invalid fields it is not necessary to check the signature and it is therfore moved to the bottom.
1 parent a291d33 commit d12567f

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

auth/auth.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,9 +220,6 @@ func (c *Client) VerifyIDToken(ctx context.Context, idToken string) (*Token, err
220220
return nil, fmt.Errorf("id token must be a non-empty string")
221221
}
222222

223-
if err := verifyToken(ctx, idToken, c.keySource); err != nil {
224-
return nil, err
225-
}
226223
segments := strings.Split(idToken, ".")
227224

228225
var (
@@ -281,6 +278,13 @@ func (c *Client) VerifyIDToken(ctx context.Context, idToken string) (*Token, err
281278
return nil, err
282279
}
283280
payload.UID = payload.Subject
281+
282+
// Verifying the signature requires syncronized access to a key store and
283+
// potentially issues a http request. Validating the fields of the token is
284+
// cheaper and invalid tokens will fail faster.
285+
if err := verifyToken(ctx, idToken, c.keySource); err != nil {
286+
return nil, err
287+
}
284288
return &payload, nil
285289
}
286290

0 commit comments

Comments
 (0)