Skip to content

Commit 89268b4

Browse files
Extract error message constants in Google JWT token package
- Add comprehensive error message constants for JWT validation - Replace hardcoded error strings with named constants throughout token validation - Improve consistency and maintainability of JWT error handling - Address goconst linter warnings for repeated error messages Error constants added: - errAudienceRequired, errAudienceMismatch for audience validation - errIssuerMismatch for issuer validation - errTokenExpired, errInvalidIssuedTime for time validation - errUnsupportedAlg for algorithm validation - errInvalidJWTFormat, errKeyNotFound for format/key errors This centralizes all JWT validation error messages and ensures consistent error responses across the Google JWT verification process. Co-authored-by: Amp <[email protected]> Amp-Thread-ID: https://ampcode.com/threads/T-5be4213f-26eb-400c-bb7b-d4c79b7ee6fe
1 parent a84d16e commit 89268b4

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

services/authz/token/google.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,16 @@ const (
2222
bitShift = 8
2323
initialCapacity = 0
2424
indexIncrement = 1
25+
26+
// Error messages
27+
errAudienceMismatch = "audience mismatch"
28+
errIssuerMismatch = "issuer mismatch"
29+
errTokenExpired = "token expired"
30+
errInvalidIssuedTime = "invalid issued time"
31+
errUnsupportedAlg = "unsupported algorithm"
32+
errInvalidJWTFormat = "invalid jwt format"
33+
errKeyNotFound = "key not found"
34+
errAudienceRequired = "audience required"
2535
)
2636

2737
type googleKey struct {
@@ -65,7 +75,7 @@ type jwtHeader struct {
6575

6676
func VerifyGoogleJWT(ctx context.Context, rawToken, audience string) (map[string]any, error) {
6777
if audience == "" {
68-
return nil, errors.New("audience required")
78+
return nil, errors.New(errAudienceRequired)
6979
}
7080

7181
parts, err := parseJWT(rawToken)
@@ -79,7 +89,7 @@ func VerifyGoogleJWT(ctx context.Context, rawToken, audience string) (map[string
7989
}
8090

8191
if header.Alg != algorithmRS256 {
82-
return nil, errors.New("unsupported algorithm")
92+
return nil, errors.New(errUnsupportedAlg)
8393
}
8494

8595
claims, err := decodeClaims(parts[1])
@@ -101,7 +111,7 @@ func VerifyGoogleJWT(ctx context.Context, rawToken, audience string) (map[string
101111
func parseJWT(token string) ([]string, error) {
102112
parts := splitToken(token)
103113
if len(parts) != expectedTokenParts {
104-
return nil, errors.New("invalid jwt format")
114+
return nil, errors.New(errInvalidJWTFormat)
105115
}
106116
return parts, nil
107117
}
@@ -151,23 +161,23 @@ func verifySignature(ctx context.Context, kid string, parts []string) error {
151161
func validateClaims(claims map[string]any, audience string) error {
152162
aud, ok := claims[claimAudience].(string)
153163
if !ok || aud != audience {
154-
return errors.New("audience mismatch")
164+
return errors.New(errAudienceMismatch)
155165
}
156166

157167
iss, ok := claims[claimIssuer].(string)
158168
if !ok || iss != issuerGoogleAccounts {
159-
return errors.New("issuer mismatch")
169+
return errors.New(errIssuerMismatch)
160170
}
161171

162172
now := time.Now().Unix()
163173
exp, ok := claims[claimExpiry].(float64)
164174
if !ok || int64(exp) < now {
165-
return errors.New("token expired")
175+
return errors.New(errTokenExpired)
166176
}
167177

168178
iat, ok := claims[claimIssuedAt].(float64)
169179
if !ok || int64(iat) > now+allowedClockSkew {
170-
return errors.New("invalid issued time")
180+
return errors.New(errInvalidIssuedTime)
171181
}
172182

173183
return nil
@@ -223,7 +233,7 @@ func fetchGooglePublicKey(ctx context.Context, kid string) (*rsa.PublicKey, erro
223233
return entry.publicKey, nil
224234
}
225235

226-
return nil, errors.New("key not found")
236+
return nil, errors.New(errKeyNotFound)
227237
}
228238

229239
func buildRSAPublicKey(k googleKey) (*rsa.PublicKey, error) {

0 commit comments

Comments
 (0)