Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

node: set JWT expiry to 60 seconds #25416

Merged
merged 2 commits into from
Jul 28, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
node: set JWT expiry to 60 seconds
  • Loading branch information
MariusVanDerWijden committed Jul 27, 2022
commit f6f75f9a1c970b2c39bc48cb82aa6563375a1078
6 changes: 4 additions & 2 deletions node/jwt_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"github.com/golang-jwt/jwt/v4"
)

const expiryTime = 60 * time.Second

type jwtHandler struct {
keyFunc func(token *jwt.Token) (interface{}, error)
next http.Handler
Expand Down Expand Up @@ -68,9 +70,9 @@ func (handler *jwtHandler) ServeHTTP(out http.ResponseWriter, r *http.Request) {
http.Error(out, "token is expired", http.StatusForbidden)
case claims.IssuedAt == nil:
http.Error(out, "missing issued-at", http.StatusForbidden)
case time.Since(claims.IssuedAt.Time) > 5*time.Second:
case time.Since(claims.IssuedAt.Time) > expiryTime:
http.Error(out, "stale token", http.StatusForbidden)
case time.Until(claims.IssuedAt.Time) > 5*time.Second:
case time.Until(claims.IssuedAt.Time) > expiryTime:
http.Error(out, "future token", http.StatusForbidden)
default:
handler.next.ServeHTTP(out, r)
Expand Down
4 changes: 2 additions & 2 deletions node/rpcstack_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,11 @@ func TestJWT(t *testing.T) {
expFail := []func() string{
// future
func() string {
return fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix() + 6}))
return fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix() + int64(expiryTime.Seconds()) + 1}))
},
// stale
func() string {
return fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix() - 6}))
return fmt.Sprintf("Bearer %v", issueToken(secret, nil, testClaim{"iat": time.Now().Unix() - int64(expiryTime.Seconds()) - 1}))
},
// wrong algo
func() string {
Expand Down