Skip to content

Commit 454d951

Browse files
committed
minor: Future
1 parent a0541d7 commit 454d951

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

leeway.go

+23-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package jwt
22

3-
import "time"
3+
import (
4+
"errors"
5+
"time"
6+
)
47

58
// Leeway adds validation for a leeway expiration time.
69
// If the token was not expired then a comparison between
@@ -18,3 +21,22 @@ func Leeway(leeway time.Duration) TokenValidatorFunc {
1821
return err
1922
}
2023
}
24+
25+
// Future adds a validation for the "iat" claim.
26+
// It checks if the token was issued in the future based on now+dur < iat.
27+
//
28+
// Example of use case: allow tokens that are going to be issued in the future,
29+
// for example a token that is going to be issued in 10 seconds from now.
30+
func Future(dur time.Duration) TokenValidatorFunc {
31+
return func(_ []byte, standardClaims Claims, err error) error {
32+
if errors.Is(err, ErrIssuedInTheFuture) {
33+
if Clock().Add(dur).Round(time.Second).Unix() < standardClaims.IssuedAt {
34+
return ErrIssuedInTheFuture
35+
}
36+
37+
return nil
38+
}
39+
40+
return err
41+
}
42+
}

0 commit comments

Comments
 (0)