Skip to content

Commit 0f42df9

Browse files
authored
Allow 2 mins as acceptable skew for session JWTs (#24)
* Allow 2 mins as acceptable skew for session JWTs * Fix tests
1 parent be25931 commit 0f42df9

File tree

2 files changed

+4
-2
lines changed

2 files changed

+4
-2
lines changed

middleware.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"net/http"
77
"strings"
8+
"time"
89

910
"github.com/go-chi/jwtauth/v5"
1011
"github.com/lestrrat-go/jwx/v2/jwt"
@@ -46,7 +47,7 @@ func (o *Options) ApplyDefaults() {
4647

4748
func Session(cfg Options) func(next http.Handler) http.Handler {
4849
cfg.ApplyDefaults()
49-
auth := jwtauth.New("HS256", []byte(cfg.JWTSecret), nil)
50+
auth := jwtauth.New("HS256", []byte(cfg.JWTSecret), nil, jwt.WithAcceptableSkew(2*time.Minute))
5051

5152
return func(next http.Handler) http.Handler {
5253
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@@ -70,6 +71,7 @@ func Session(cfg Options) func(next http.Handler) http.Handler {
7071
}
7172
}
7273

74+
// Verify JWT token and validate its claims.
7375
token, err := jwtauth.VerifyRequest(auth, r, jwtauth.TokenFromHeader)
7476
if err != nil {
7577
if errors.Is(err, jwtauth.ErrExpired) {

middleware_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ func TestInvalid(t *testing.T) {
264264
assert.ErrorIs(t, err, proto.ErrUnauthorized)
265265

266266
// Expired JWT Token
267-
claims["exp"] = time.Now().Add(-time.Second).Unix()
267+
claims["exp"] = time.Now().Add(-5 * time.Minute).Unix() // Note: Session() middleware allows some skew.
268268
expiredJWT := authcontrol.S2SToken(JWTSecret, claims)
269269

270270
// Expired JWT Token valid method

0 commit comments

Comments
 (0)