chore: update auth duration

This commit is contained in:
Steven 2023-07-28 22:53:32 +08:00
parent 4e3ca8ceb4
commit d798b2c5fb
2 changed files with 5 additions and 9 deletions

View File

@ -23,14 +23,11 @@ const (
apiTokenDuration = 2 * time.Hour apiTokenDuration = 2 * time.Hour
accessTokenDuration = 24 * time.Hour accessTokenDuration = 24 * time.Hour
refreshTokenDuration = 7 * 24 * time.Hour refreshTokenDuration = 7 * 24 * time.Hour
// RefreshThresholdDuration is the threshold duration for refreshing token.
RefreshThresholdDuration = 1 * time.Hour
// CookieExpDuration expires slightly earlier than the jwt expiration. Client would be logged out if the user // CookieExpDuration expires slightly earlier than the jwt expiration. Client would be logged out if the user
// cookie expires, thus the client would always logout first before attempting to make a request with the expired jwt. // cookie expires, thus the client would always logout first before attempting to make a request with the expired jwt.
// Suppose we have a valid refresh token, we will refresh the token in 2 cases: // Suppose we have a valid refresh token, we will refresh the token in the following cases:
// 1. The access token is about to expire in <<refreshThresholdDuration>> // 1. The access token has already expired, we refresh the token so that the ongoing request can pass through.
// 2. The access token has already expired, we refresh the token so that the ongoing request can pass through.
CookieExpDuration = refreshTokenDuration - 1*time.Minute CookieExpDuration = refreshTokenDuration - 1*time.Minute
// AccessTokenCookieName is the cookie name of access token. // AccessTokenCookieName is the cookie name of access token.
AccessTokenCookieName = "slash.access-token" AccessTokenCookieName = "slash.access-token"

View File

@ -5,7 +5,6 @@ import (
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/boojack/slash/api/v1/auth" "github.com/boojack/slash/api/v1/auth"
"github.com/boojack/slash/internal/util" "github.com/boojack/slash/internal/util"
@ -105,12 +104,12 @@ func JWTMiddleware(server *APIV1Service, next echo.HandlerFunc, secret string) e
return nil, errors.Errorf("unexpected access token kid=%v", t.Header["kid"]) return nil, errors.Errorf("unexpected access token kid=%v", t.Header["kid"])
}) })
generateToken := time.Until(claims.ExpiresAt.Time) < auth.RefreshThresholdDuration generateToken := false
if err != nil { if err != nil {
var ve *jwt.ValidationError var ve *jwt.ValidationError
if errors.As(err, &ve) { if errors.As(err, &ve) {
// If expiration error is the only error, we will clear the err // If expiration error is the only error, we will ignore the err
// and generate new access token and refresh token // and generate new access token and refresh token.
if ve.Errors == jwt.ValidationErrorExpired { if ve.Errors == jwt.ValidationErrorExpired {
generateToken = true generateToken = true
} }