Skip to content

Commit 0bb3cc1

Browse files
committed
fix leeway implementation and tests
1 parent 73d5a05 commit 0bb3cc1

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

token.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -282,8 +282,9 @@ func (t Token) Verify(issuer, subject, audience string, leeway time.Duration) er
282282

283283
// Valid checks if the token is valid yet.
284284
func (t Token) Valid(leeway time.Duration) bool {
285-
now := clock.Now().UTC().Add(leeway)
286-
return now.After(t.NotBefore)
285+
now := clock.Now().UTC()
286+
nbf := t.NotBefore.Add(-leeway)
287+
return nbf.Before(now)
287288
}
288289

289290
// Expired checks if the token has expired.
@@ -296,8 +297,9 @@ func (t Token) Expired(leeway time.Duration) bool {
296297
return false
297298
}
298299

299-
now := clock.Now().UTC().Add(leeway)
300-
return now.After(t.Expires)
300+
now := clock.Now().UTC()
301+
exp := t.Expires.Add(leeway)
302+
return now.After(exp)
301303
}
302304

303305
// buildHeader builds a new header map ready for signing.

token_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -433,10 +433,10 @@ func TestTokenExpired_True(t *testing.T) {
433433
}
434434
}
435435

436-
func TestTokenExpired_LeewayTrue(t *testing.T) {
436+
func TestTokenExpired_LeewayFalse(t *testing.T) {
437437
tkn := NewToken()
438438
tkn.Expires = tkn.IssuedAt.Add(1 * time.Hour)
439-
if !tkn.Expired(1 * time.Hour) {
439+
if tkn.Expired(1 * time.Hour) {
440440
t.Fatal("expected true, got false")
441441
}
442442
}

0 commit comments

Comments
 (0)