mirror of
https://github.com/pommee/goaway.git
synced 2026-05-18 23:39:20 -05:00
fix: token improvements, dont refresh upon each request
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package api
|
package api
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
@@ -56,18 +57,22 @@ func (api *API) authMiddleware() gin.HandlerFunc {
|
|||||||
}
|
}
|
||||||
expiration := int64(exp)
|
expiration := int64(exp)
|
||||||
|
|
||||||
if now > expiration {
|
if now >= expiration {
|
||||||
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Token expired"})
|
c.AbortWithStatusJSON(http.StatusUnauthorized, gin.H{"error": "Token expired"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if now > expiration-int64(TokenDuration/2) {
|
halfDurationSeconds := int64(TokenDuration.Seconds() / 2)
|
||||||
|
timeUntilExpiration := expiration - now
|
||||||
|
|
||||||
|
if timeUntilExpiration <= halfDurationSeconds {
|
||||||
newToken, err := generateToken(username)
|
newToken, err := generateToken(username)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "Failed to renew token"})
|
c.AbortWithStatusJSON(http.StatusInternalServerError, gin.H{"error": "Failed to renew token"})
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
setAuthCookie(c.Writer, newToken)
|
setAuthCookie(c.Writer, newToken)
|
||||||
|
log.Debug("New token generated and cookie set")
|
||||||
}
|
}
|
||||||
|
|
||||||
c.Set("username", username)
|
c.Set("username", username)
|
||||||
@@ -77,6 +82,9 @@ func (api *API) authMiddleware() gin.HandlerFunc {
|
|||||||
|
|
||||||
func parseToken(tokenString string) (jwt.MapClaims, error) {
|
func parseToken(tokenString string) (jwt.MapClaims, error) {
|
||||||
token, err := jwt.Parse(tokenString, func(t *jwt.Token) (interface{}, error) {
|
token, err := jwt.Parse(tokenString, func(t *jwt.Token) (interface{}, error) {
|
||||||
|
if _, ok := t.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||||
|
return nil, fmt.Errorf("unexpected signing method: %v", t.Header["alg"])
|
||||||
|
}
|
||||||
return []byte(Secret), nil
|
return []byte(Secret), nil
|
||||||
})
|
})
|
||||||
if err != nil || !token.Valid {
|
if err != nil || !token.Valid {
|
||||||
@@ -85,7 +93,7 @@ func parseToken(tokenString string) (jwt.MapClaims, error) {
|
|||||||
|
|
||||||
claims, ok := token.Claims.(jwt.MapClaims)
|
claims, ok := token.Claims.(jwt.MapClaims)
|
||||||
if !ok {
|
if !ok {
|
||||||
return nil, err
|
return nil, fmt.Errorf("invalid token claims")
|
||||||
}
|
}
|
||||||
|
|
||||||
return claims, nil
|
return claims, nil
|
||||||
@@ -111,5 +119,6 @@ func setAuthCookie(w http.ResponseWriter, token string) {
|
|||||||
Secure: false,
|
Secure: false,
|
||||||
SameSite: http.SameSiteLaxMode,
|
SameSite: http.SameSiteLaxMode,
|
||||||
Expires: time.Now().Add(TokenDuration),
|
Expires: time.Now().Add(TokenDuration),
|
||||||
|
MaxAge: int(TokenDuration.Seconds()),
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user