Skip to content

Commit 0972257

Browse files
authored
Revert "feat: port clockskew support (#139)" (#184)
This reverts commit d489c99.
1 parent 1096e50 commit 0972257

File tree

7 files changed

+40
-149
lines changed

7 files changed

+40
-149
lines changed

claims.go

Lines changed: 23 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ import (
99
// Claims must just have a Valid method that determines
1010
// if the token is invalid for any supported reason
1111
type Claims interface {
12-
// Valid implements claim validation. The opts are function style options that can
13-
// be used to fine-tune the validation. The type used for the options is intentionally
14-
// un-exported, since its API and its naming is subject to change.
15-
Valid(opts ...validationOption) error
12+
Valid() error
1613
}
1714

1815
// RegisteredClaims are a structured version of the JWT Claims Set,
@@ -51,13 +48,13 @@ type RegisteredClaims struct {
5148
// There is no accounting for clock skew.
5249
// As well, if any of the above claims are not in the token, it will still
5350
// be considered a valid claim.
54-
func (c RegisteredClaims) Valid(opts ...validationOption) error {
51+
func (c RegisteredClaims) Valid() error {
5552
vErr := new(ValidationError)
5653
now := TimeFunc()
5754

5855
// The claims below are optional, by default, so if they are set to the
5956
// default value in Go, let's not fail the verification for them.
60-
if !c.VerifyExpiresAt(now, false, opts...) {
57+
if !c.VerifyExpiresAt(now, false) {
6158
delta := now.Sub(c.ExpiresAt.Time)
6259
vErr.Inner = fmt.Errorf("%s by %s", ErrTokenExpired, delta)
6360
vErr.Errors |= ValidationErrorExpired
@@ -68,7 +65,7 @@ func (c RegisteredClaims) Valid(opts ...validationOption) error {
6865
vErr.Errors |= ValidationErrorIssuedAt
6966
}
7067

71-
if !c.VerifyNotBefore(now, false, opts...) {
68+
if !c.VerifyNotBefore(now, false) {
7269
vErr.Inner = ErrTokenNotValidYet
7370
vErr.Errors |= ValidationErrorNotValidYet
7471
}
@@ -88,16 +85,12 @@ func (c *RegisteredClaims) VerifyAudience(cmp string, req bool) bool {
8885

8986
// VerifyExpiresAt compares the exp claim against cmp (cmp < exp).
9087
// If req is false, it will return true, if exp is unset.
91-
func (c *RegisteredClaims) VerifyExpiresAt(cmp time.Time, req bool, opts ...validationOption) bool {
92-
validator := validator{}
93-
for _, o := range opts {
94-
o(&validator)
95-
}
88+
func (c *RegisteredClaims) VerifyExpiresAt(cmp time.Time, req bool) bool {
9689
if c.ExpiresAt == nil {
97-
return verifyExp(nil, cmp, req, validator.leeway)
90+
return verifyExp(nil, cmp, req)
9891
}
9992

100-
return verifyExp(&c.ExpiresAt.Time, cmp, req, validator.leeway)
93+
return verifyExp(&c.ExpiresAt.Time, cmp, req)
10194
}
10295

10396
// VerifyIssuedAt compares the iat claim against cmp (cmp >= iat).
@@ -112,16 +105,12 @@ func (c *RegisteredClaims) VerifyIssuedAt(cmp time.Time, req bool) bool {
112105

113106
// VerifyNotBefore compares the nbf claim against cmp (cmp >= nbf).
114107
// If req is false, it will return true, if nbf is unset.
115-
func (c *RegisteredClaims) VerifyNotBefore(cmp time.Time, req bool, opts ...validationOption) bool {
116-
validator := validator{}
117-
for _, o := range opts {
118-
o(&validator)
119-
}
108+
func (c *RegisteredClaims) VerifyNotBefore(cmp time.Time, req bool) bool {
120109
if c.NotBefore == nil {
121-
return verifyNbf(nil, cmp, req, validator.leeway)
110+
return verifyNbf(nil, cmp, req)
122111
}
123112

124-
return verifyNbf(&c.NotBefore.Time, cmp, req, validator.leeway)
113+
return verifyNbf(&c.NotBefore.Time, cmp, req)
125114
}
126115

127116
// VerifyIssuer compares the iss claim against cmp.
@@ -152,13 +141,13 @@ type StandardClaims struct {
152141
// Valid validates time based claims "exp, iat, nbf". There is no accounting for clock skew.
153142
// As well, if any of the above claims are not in the token, it will still
154143
// be considered a valid claim.
155-
func (c StandardClaims) Valid(opts ...validationOption) error {
144+
func (c StandardClaims) Valid() error {
156145
vErr := new(ValidationError)
157146
now := TimeFunc().Unix()
158147

159148
// The claims below are optional, by default, so if they are set to the
160149
// default value in Go, let's not fail the verification for them.
161-
if !c.VerifyExpiresAt(now, false, opts...) {
150+
if !c.VerifyExpiresAt(now, false) {
162151
delta := time.Unix(now, 0).Sub(time.Unix(c.ExpiresAt, 0))
163152
vErr.Inner = fmt.Errorf("%s by %s", ErrTokenExpired, delta)
164153
vErr.Errors |= ValidationErrorExpired
@@ -169,7 +158,7 @@ func (c StandardClaims) Valid(opts ...validationOption) error {
169158
vErr.Errors |= ValidationErrorIssuedAt
170159
}
171160

172-
if !c.VerifyNotBefore(now, false, opts...) {
161+
if !c.VerifyNotBefore(now, false) {
173162
vErr.Inner = ErrTokenNotValidYet
174163
vErr.Errors |= ValidationErrorNotValidYet
175164
}
@@ -189,17 +178,13 @@ func (c *StandardClaims) VerifyAudience(cmp string, req bool) bool {
189178

190179
// VerifyExpiresAt compares the exp claim against cmp (cmp < exp).
191180
// If req is false, it will return true, if exp is unset.
192-
func (c *StandardClaims) VerifyExpiresAt(cmp int64, req bool, opts ...validationOption) bool {
193-
validator := validator{}
194-
for _, o := range opts {
195-
o(&validator)
196-
}
181+
func (c *StandardClaims) VerifyExpiresAt(cmp int64, req bool) bool {
197182
if c.ExpiresAt == 0 {
198-
return verifyExp(nil, time.Unix(cmp, 0), req, validator.leeway)
183+
return verifyExp(nil, time.Unix(cmp, 0), req)
199184
}
200185

201186
t := time.Unix(c.ExpiresAt, 0)
202-
return verifyExp(&t, time.Unix(cmp, 0), req, validator.leeway)
187+
return verifyExp(&t, time.Unix(cmp, 0), req)
203188
}
204189

205190
// VerifyIssuedAt compares the iat claim against cmp (cmp >= iat).
@@ -215,17 +200,13 @@ func (c *StandardClaims) VerifyIssuedAt(cmp int64, req bool) bool {
215200

216201
// VerifyNotBefore compares the nbf claim against cmp (cmp >= nbf).
217202
// If req is false, it will return true, if nbf is unset.
218-
func (c *StandardClaims) VerifyNotBefore(cmp int64, req bool, opts ...validationOption) bool {
219-
validator := validator{}
220-
for _, o := range opts {
221-
o(&validator)
222-
}
203+
func (c *StandardClaims) VerifyNotBefore(cmp int64, req bool) bool {
223204
if c.NotBefore == 0 {
224-
return verifyNbf(nil, time.Unix(cmp, 0), req, validator.leeway)
205+
return verifyNbf(nil, time.Unix(cmp, 0), req)
225206
}
226207

227208
t := time.Unix(c.NotBefore, 0)
228-
return verifyNbf(&t, time.Unix(cmp, 0), req, validator.leeway)
209+
return verifyNbf(&t, time.Unix(cmp, 0), req)
229210
}
230211

231212
// VerifyIssuer compares the iss claim against cmp.
@@ -259,11 +240,11 @@ func verifyAud(aud []string, cmp string, required bool) bool {
259240
return result
260241
}
261242

262-
func verifyExp(exp *time.Time, now time.Time, required bool, skew time.Duration) bool {
243+
func verifyExp(exp *time.Time, now time.Time, required bool) bool {
263244
if exp == nil {
264245
return !required
265246
}
266-
return now.Before((*exp).Add(+skew))
247+
return now.Before(*exp)
267248
}
268249

269250
func verifyIat(iat *time.Time, now time.Time, required bool) bool {
@@ -273,12 +254,11 @@ func verifyIat(iat *time.Time, now time.Time, required bool) bool {
273254
return now.After(*iat) || now.Equal(*iat)
274255
}
275256

276-
func verifyNbf(nbf *time.Time, now time.Time, required bool, skew time.Duration) bool {
257+
func verifyNbf(nbf *time.Time, now time.Time, required bool) bool {
277258
if nbf == nil {
278259
return !required
279260
}
280-
t := (*nbf).Add(-skew)
281-
return now.After(t) || now.Equal(t)
261+
return now.After(*nbf) || now.Equal(*nbf)
282262
}
283263

284264
func verifyIss(iss string, cmp string, required bool) bool {

go.mod

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
module github.com/golang-jwt/jwt/v4
22

33
go 1.16
4+
5+
retract (
6+
v4.4.0 // Contains a backwards incompatible change to the Claims interface.
7+
)

map_claims.go

Lines changed: 11 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,30 +34,25 @@ func (m MapClaims) VerifyAudience(cmp string, req bool) bool {
3434

3535
// VerifyExpiresAt compares the exp claim against cmp (cmp <= exp).
3636
// If req is false, it will return true, if exp is unset.
37-
func (m MapClaims) VerifyExpiresAt(cmp int64, req bool, opts ...validationOption) bool {
37+
func (m MapClaims) VerifyExpiresAt(cmp int64, req bool) bool {
3838
cmpTime := time.Unix(cmp, 0)
3939

4040
v, ok := m["exp"]
4141
if !ok {
4242
return !req
4343
}
4444

45-
validator := validator{}
46-
for _, o := range opts {
47-
o(&validator)
48-
}
49-
5045
switch exp := v.(type) {
5146
case float64:
5247
if exp == 0 {
53-
return verifyExp(nil, cmpTime, req, validator.leeway)
48+
return verifyExp(nil, cmpTime, req)
5449
}
5550

56-
return verifyExp(&newNumericDateFromSeconds(exp).Time, cmpTime, req, validator.leeway)
51+
return verifyExp(&newNumericDateFromSeconds(exp).Time, cmpTime, req)
5752
case json.Number:
5853
v, _ := exp.Float64()
5954

60-
return verifyExp(&newNumericDateFromSeconds(v).Time, cmpTime, req, validator.leeway)
55+
return verifyExp(&newNumericDateFromSeconds(v).Time, cmpTime, req)
6156
}
6257

6358
return false
@@ -91,30 +86,25 @@ func (m MapClaims) VerifyIssuedAt(cmp int64, req bool) bool {
9186

9287
// VerifyNotBefore compares the nbf claim against cmp (cmp >= nbf).
9388
// If req is false, it will return true, if nbf is unset.
94-
func (m MapClaims) VerifyNotBefore(cmp int64, req bool, opts ...validationOption) bool {
89+
func (m MapClaims) VerifyNotBefore(cmp int64, req bool) bool {
9590
cmpTime := time.Unix(cmp, 0)
9691

9792
v, ok := m["nbf"]
9893
if !ok {
9994
return !req
10095
}
10196

102-
validator := validator{}
103-
for _, o := range opts {
104-
o(&validator)
105-
}
106-
10797
switch nbf := v.(type) {
10898
case float64:
10999
if nbf == 0 {
110-
return verifyNbf(nil, cmpTime, req, validator.leeway)
100+
return verifyNbf(nil, cmpTime, req)
111101
}
112102

113-
return verifyNbf(&newNumericDateFromSeconds(nbf).Time, cmpTime, req, validator.leeway)
103+
return verifyNbf(&newNumericDateFromSeconds(nbf).Time, cmpTime, req)
114104
case json.Number:
115105
v, _ := nbf.Float64()
116106

117-
return verifyNbf(&newNumericDateFromSeconds(v).Time, cmpTime, req, validator.leeway)
107+
return verifyNbf(&newNumericDateFromSeconds(v).Time, cmpTime, req)
118108
}
119109

120110
return false
@@ -131,11 +121,11 @@ func (m MapClaims) VerifyIssuer(cmp string, req bool) bool {
131121
// There is no accounting for clock skew.
132122
// As well, if any of the above claims are not in the token, it will still
133123
// be considered a valid claim.
134-
func (m MapClaims) Valid(opts ...validationOption) error {
124+
func (m MapClaims) Valid() error {
135125
vErr := new(ValidationError)
136126
now := TimeFunc().Unix()
137127

138-
if !m.VerifyExpiresAt(now, false, opts...) {
128+
if !m.VerifyExpiresAt(now, false) {
139129
// TODO(oxisto): this should be replaced with ErrTokenExpired
140130
vErr.Inner = errors.New("Token is expired")
141131
vErr.Errors |= ValidationErrorExpired
@@ -147,7 +137,7 @@ func (m MapClaims) Valid(opts ...validationOption) error {
147137
vErr.Errors |= ValidationErrorIssuedAt
148138
}
149139

150-
if !m.VerifyNotBefore(now, false, opts...) {
140+
if !m.VerifyNotBefore(now, false) {
151141
// TODO(oxisto): this should be replaced with ErrTokenNotValidYet
152142
vErr.Inner = errors.New("Token is not valid yet")
153143
vErr.Errors |= ValidationErrorNotValidYet

parser.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,6 @@ type Parser struct {
2222
//
2323
// Deprecated: In future releases, this field will not be exported anymore and should be set with an option to NewParser instead.
2424
SkipClaimsValidation bool
25-
26-
validationOptions []validationOption
2725
}
2826

2927
// NewParser creates a new Parser with the specified options
@@ -84,7 +82,8 @@ func (p *Parser) ParseWithClaims(tokenString string, claims Claims, keyFunc Keyf
8482

8583
// Validate Claims
8684
if !p.SkipClaimsValidation {
87-
if err := token.Claims.Valid(p.validationOptions...); err != nil {
85+
if err := token.Claims.Valid(); err != nil {
86+
8887
// If the Claims Valid returned an error, check if it is a validation error,
8988
// If it was another error type, create a ValidationError with a generic ClaimsInvalid flag set
9089
if e, ok := err.(*ValidationError); !ok {

parser_option.go

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package jwt
22

3-
import "time"
4-
53
// ParserOption is used to implement functional-style options that modify the behavior of the parser. To add
64
// new options, just create a function (ideally beginning with With or Without) that returns an anonymous function that
75
// takes a *Parser type as input and manipulates its configuration accordingly.
@@ -29,10 +27,3 @@ func WithoutClaimsValidation() ParserOption {
2927
p.SkipClaimsValidation = true
3028
}
3129
}
32-
33-
// WithLeeway returns the ParserOption for specifying the leeway window.
34-
func WithLeeway(d time.Duration) ParserOption {
35-
return func(p *Parser) {
36-
p.validationOptions = append(p.validationOptions, withLeeway(d))
37-
}
38-
}

parser_test.go

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -78,28 +78,6 @@ var jwtTestData = []struct {
7878
nil,
7979
jwt.SigningMethodRS256,
8080
},
81-
{
82-
"basic expired with 60s skew",
83-
"", // autogen
84-
defaultKeyFunc,
85-
jwt.MapClaims{"foo": "bar", "exp": float64(time.Now().Unix() - 100)},
86-
false,
87-
jwt.ValidationErrorExpired,
88-
[]error{jwt.ErrTokenExpired},
89-
jwt.NewParser(jwt.WithLeeway(time.Minute)),
90-
jwt.SigningMethodRS256,
91-
},
92-
{
93-
"basic expired with 120s skew",
94-
"", // autogen
95-
defaultKeyFunc,
96-
jwt.MapClaims{"foo": "bar", "exp": float64(time.Now().Unix() - 100)},
97-
true,
98-
0,
99-
nil,
100-
jwt.NewParser(jwt.WithLeeway(2 * time.Minute)),
101-
jwt.SigningMethodRS256,
102-
},
10381
{
10482
"basic nbf",
10583
"", // autogen
@@ -111,28 +89,6 @@ var jwtTestData = []struct {
11189
nil,
11290
jwt.SigningMethodRS256,
11391
},
114-
{
115-
"basic nbf with 60s skew",
116-
"", // autogen
117-
defaultKeyFunc,
118-
jwt.MapClaims{"foo": "bar", "nbf": float64(time.Now().Unix() + 100)},
119-
false,
120-
jwt.ValidationErrorNotValidYet,
121-
[]error{jwt.ErrTokenNotValidYet},
122-
jwt.NewParser(jwt.WithLeeway(time.Minute)),
123-
jwt.SigningMethodRS256,
124-
},
125-
{
126-
"basic nbf with 120s skew",
127-
"", // autogen
128-
defaultKeyFunc,
129-
jwt.MapClaims{"foo": "bar", "nbf": float64(time.Now().Unix() + 100)},
130-
true,
131-
0,
132-
nil,
133-
jwt.NewParser(jwt.WithLeeway(2 * time.Minute)),
134-
jwt.SigningMethodRS256,
135-
},
13692
{
13793
"expired and nbf",
13894
"", // autogen

validator_option.go

Lines changed: 0 additions & 29 deletions
This file was deleted.

0 commit comments

Comments
 (0)