Skip to content

Commit 6e66008

Browse files
committed
Remove StandardClaims in favor of RegisteredClaims (#235)
This PR removes the old legacy standard claims, which have been deprecated since the beginning of the `v4` module in favor of the newer `RegisteredClaims`. Removing them before any further changes to the validation API is quite useful, as less code needs to be adapated.
1 parent a90858a commit 6e66008

File tree

3 files changed

+5
-118
lines changed

3 files changed

+5
-118
lines changed

claims.go

-96
Original file line numberDiff line numberDiff line change
@@ -119,102 +119,6 @@ func (c *RegisteredClaims) VerifyIssuer(cmp string, req bool) bool {
119119
return verifyIss(c.Issuer, cmp, req)
120120
}
121121

122-
// StandardClaims are a structured version of the JWT Claims Set, as referenced at
123-
// https://datatracker.ietf.org/doc/html/rfc7519#section-4. They do not follow the
124-
// specification exactly, since they were based on an earlier draft of the
125-
// specification and not updated. The main difference is that they only
126-
// support integer-based date fields and singular audiences. This might lead to
127-
// incompatibilities with other JWT implementations. The use of this is discouraged, instead
128-
// the newer RegisteredClaims struct should be used.
129-
//
130-
// Deprecated: Use RegisteredClaims instead for a forward-compatible way to access registered claims in a struct.
131-
type StandardClaims struct {
132-
Audience string `json:"aud,omitempty"`
133-
ExpiresAt int64 `json:"exp,omitempty"`
134-
Id string `json:"jti,omitempty"`
135-
IssuedAt int64 `json:"iat,omitempty"`
136-
Issuer string `json:"iss,omitempty"`
137-
NotBefore int64 `json:"nbf,omitempty"`
138-
Subject string `json:"sub,omitempty"`
139-
}
140-
141-
// Valid validates time based claims "exp, iat, nbf". There is no accounting for clock skew.
142-
// As well, if any of the above claims are not in the token, it will still
143-
// be considered a valid claim.
144-
func (c StandardClaims) Valid() error {
145-
vErr := new(ValidationError)
146-
now := TimeFunc().Unix()
147-
148-
// The claims below are optional, by default, so if they are set to the
149-
// default value in Go, let's not fail the verification for them.
150-
if !c.VerifyExpiresAt(now, false) {
151-
delta := time.Unix(now, 0).Sub(time.Unix(c.ExpiresAt, 0))
152-
vErr.Inner = fmt.Errorf("%s by %s", ErrTokenExpired, delta)
153-
vErr.Errors |= ValidationErrorExpired
154-
}
155-
156-
if !c.VerifyIssuedAt(now, false) {
157-
vErr.Inner = ErrTokenUsedBeforeIssued
158-
vErr.Errors |= ValidationErrorIssuedAt
159-
}
160-
161-
if !c.VerifyNotBefore(now, false) {
162-
vErr.Inner = ErrTokenNotValidYet
163-
vErr.Errors |= ValidationErrorNotValidYet
164-
}
165-
166-
if vErr.valid() {
167-
return nil
168-
}
169-
170-
return vErr
171-
}
172-
173-
// VerifyAudience compares the aud claim against cmp.
174-
// If required is false, this method will return true if the value matches or is unset
175-
func (c *StandardClaims) VerifyAudience(cmp string, req bool) bool {
176-
return verifyAud([]string{c.Audience}, cmp, req)
177-
}
178-
179-
// VerifyExpiresAt compares the exp claim against cmp (cmp < exp).
180-
// If req is false, it will return true, if exp is unset.
181-
func (c *StandardClaims) VerifyExpiresAt(cmp int64, req bool) bool {
182-
if c.ExpiresAt == 0 {
183-
return verifyExp(nil, time.Unix(cmp, 0), req)
184-
}
185-
186-
t := time.Unix(c.ExpiresAt, 0)
187-
return verifyExp(&t, time.Unix(cmp, 0), req)
188-
}
189-
190-
// VerifyIssuedAt compares the iat claim against cmp (cmp >= iat).
191-
// If req is false, it will return true, if iat is unset.
192-
func (c *StandardClaims) VerifyIssuedAt(cmp int64, req bool) bool {
193-
if c.IssuedAt == 0 {
194-
return verifyIat(nil, time.Unix(cmp, 0), req)
195-
}
196-
197-
t := time.Unix(c.IssuedAt, 0)
198-
return verifyIat(&t, time.Unix(cmp, 0), req)
199-
}
200-
201-
// VerifyNotBefore compares the nbf claim against cmp (cmp >= nbf).
202-
// If req is false, it will return true, if nbf is unset.
203-
func (c *StandardClaims) VerifyNotBefore(cmp int64, req bool) bool {
204-
if c.NotBefore == 0 {
205-
return verifyNbf(nil, time.Unix(cmp, 0), req)
206-
}
207-
208-
t := time.Unix(c.NotBefore, 0)
209-
return verifyNbf(&t, time.Unix(cmp, 0), req)
210-
}
211-
212-
// VerifyIssuer compares the iss claim against cmp.
213-
// If required is false, this method will return true if the value matches or is unset
214-
func (c *StandardClaims) VerifyIssuer(cmp string, req bool) bool {
215-
return verifyIss(c.Issuer, cmp, req)
216-
}
217-
218122
// ----- helpers
219123

220124
func verifyAud(aud []string, cmp string, required bool) bool {

parser_test.go

+3-20
Original file line numberDiff line numberDiff line change
@@ -199,19 +199,6 @@ var jwtTestData = []struct {
199199
&jwt.Parser{UseJSONNumber: true},
200200
jwt.SigningMethodRS256,
201201
},
202-
{
203-
"Standard Claims",
204-
"",
205-
defaultKeyFunc,
206-
&jwt.StandardClaims{
207-
ExpiresAt: time.Now().Add(time.Second * 10).Unix(),
208-
},
209-
true,
210-
0,
211-
nil,
212-
&jwt.Parser{UseJSONNumber: true},
213-
jwt.SigningMethodRS256,
214-
},
215202
{
216203
"JSON Number - basic expired",
217204
"", // autogen
@@ -360,8 +347,6 @@ func TestParser_Parse(t *testing.T) {
360347
switch data.claims.(type) {
361348
case jwt.MapClaims:
362349
token, err = parser.ParseWithClaims(data.tokenString, jwt.MapClaims{}, data.keyfunc)
363-
case *jwt.StandardClaims:
364-
token, err = parser.ParseWithClaims(data.tokenString, &jwt.StandardClaims{}, data.keyfunc)
365350
case *jwt.RegisteredClaims:
366351
token, err = parser.ParseWithClaims(data.tokenString, &jwt.RegisteredClaims{}, data.keyfunc)
367352
}
@@ -454,8 +439,6 @@ func TestParser_ParseUnverified(t *testing.T) {
454439
switch data.claims.(type) {
455440
case jwt.MapClaims:
456441
token, _, err = parser.ParseUnverified(data.tokenString, jwt.MapClaims{})
457-
case *jwt.StandardClaims:
458-
token, _, err = parser.ParseUnverified(data.tokenString, &jwt.StandardClaims{})
459442
case *jwt.RegisteredClaims:
460443
token, _, err = parser.ParseUnverified(data.tokenString, &jwt.RegisteredClaims{})
461444
}
@@ -695,9 +678,9 @@ func BenchmarkParseUnverified(b *testing.B) {
695678
b.Run("map_claims", func(b *testing.B) {
696679
benchmarkParsing(b, parser, data.tokenString, jwt.MapClaims{})
697680
})
698-
case *jwt.StandardClaims:
699-
b.Run("standard_claims", func(b *testing.B) {
700-
benchmarkParsing(b, parser, data.tokenString, &jwt.StandardClaims{})
681+
case *jwt.RegisteredClaims:
682+
b.Run("registered_claims", func(b *testing.B) {
683+
benchmarkParsing(b, parser, data.tokenString, &jwt.RegisteredClaims{})
701684
})
702685
}
703686
}

token_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestToken_SigningString(t1 *testing.T) {
3030
"typ": "JWT",
3131
"alg": jwt.SigningMethodHS256.Alg(),
3232
},
33-
Claims: jwt.StandardClaims{},
33+
Claims: jwt.RegisteredClaims{},
3434
Signature: "",
3535
Valid: false,
3636
},
@@ -67,7 +67,7 @@ func BenchmarkToken_SigningString(b *testing.B) {
6767
"typ": "JWT",
6868
"alg": jwt.SigningMethodHS256.Alg(),
6969
},
70-
Claims: jwt.StandardClaims{},
70+
Claims: jwt.RegisteredClaims{},
7171
}
7272
b.Run("BenchmarkToken_SigningString", func(b *testing.B) {
7373
b.ResetTimer()

0 commit comments

Comments
 (0)