Skip to content

Commit

Permalink
Allow 2 mins as acceptable skew for session JWTs (#24)
Browse files Browse the repository at this point in the history
* Allow 2 mins as acceptable skew for session JWTs

* Fix tests
  • Loading branch information
VojtechVitek authored Nov 13, 2024
1 parent be25931 commit 0f42df9
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 2 deletions.
4 changes: 3 additions & 1 deletion middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"net/http"
"strings"
"time"

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

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

return func(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand All @@ -70,6 +71,7 @@ func Session(cfg Options) func(next http.Handler) http.Handler {
}
}

// Verify JWT token and validate its claims.
token, err := jwtauth.VerifyRequest(auth, r, jwtauth.TokenFromHeader)
if err != nil {
if errors.Is(err, jwtauth.ErrExpired) {
Expand Down
2 changes: 1 addition & 1 deletion middleware_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,7 @@ func TestInvalid(t *testing.T) {
assert.ErrorIs(t, err, proto.ErrUnauthorized)

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

// Expired JWT Token valid method
Expand Down

0 comments on commit 0f42df9

Please sign in to comment.