Fix admin login reload loop from stale localStorage tokens.
Clear client tokens on the login page, gate admin pages with shared cookie JWT checks, and clear Secure cookies reliably behind the proxy. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -15,6 +15,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/minio/minio-go/v7"
|
||||
@@ -27,8 +28,9 @@ import (
|
||||
)
|
||||
|
||||
const (
|
||||
loginURL = "/login"
|
||||
tokenURL = "/token"
|
||||
loginURL = "/login"
|
||||
logoutURL = "/logout"
|
||||
tokenURL = "/token"
|
||||
|
||||
getProfileURL = "/get-profile"
|
||||
fileUploadURL = "/file"
|
||||
@@ -49,11 +51,11 @@ func NewHandler(logger *logging.Logger, repository Repository) handlers.Handler
|
||||
|
||||
func (h *handler) Register(router *gin.RouterGroup) {
|
||||
router.POST(loginURL, h.login)
|
||||
router.POST(logoutURL, h.logout)
|
||||
router.GET(getProfileURL, appresult.JwtTokenCheck(), h.getProfile)
|
||||
router.POST(fileUploadURL, appresult.JwtTokenCheck(), h.fileUpload)
|
||||
router.DELETE(fileRemoveURL, appresult.JwtTokenCheck(), h.DeleteObject)
|
||||
router.POST(tokenURL, h.token)
|
||||
|
||||
}
|
||||
|
||||
// =================================================================================
|
||||
@@ -100,11 +102,20 @@ func (h *handler) login(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
appresult.SetAuthCookies(c, newTokenPair["accessToken"], newTokenPair["refreshToken"])
|
||||
|
||||
successResult := appresult.Success
|
||||
successResult.Data = newTokenPair
|
||||
|
||||
c.JSON(http.StatusOK, successResult)
|
||||
return
|
||||
}
|
||||
|
||||
// logout clears HttpOnly auth cookies.
|
||||
func (h *handler) logout(c *gin.Context) {
|
||||
appresult.ClearAuthCookies(c)
|
||||
successResult := appresult.Success
|
||||
successResult.Data = ""
|
||||
c.JSON(http.StatusOK, successResult)
|
||||
}
|
||||
|
||||
// getProfile godoc
|
||||
@@ -292,47 +303,57 @@ func DeleteItemInMinio(ctx context.Context, minioClient *minio.Client, value str
|
||||
// @Success 200 {string} string true "token"
|
||||
// @Router /admin/token [post]
|
||||
func (h *handler) token(c *gin.Context) {
|
||||
var (
|
||||
err error
|
||||
tokenReq TokenReqBody
|
||||
)
|
||||
var tokenReq TokenReqBody
|
||||
|
||||
if err = c.ShouldBindJSON(&tokenReq); err != nil {
|
||||
// Body is optional when refresh token is in HttpOnly cookie.
|
||||
_ = c.ShouldBindJSON(&tokenReq)
|
||||
refresh := strings.TrimSpace(tokenReq.RefreshToken)
|
||||
if refresh == "" {
|
||||
refresh = appresult.RefreshTokenFromCookie(c)
|
||||
}
|
||||
if refresh == "" {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrMissingParam)
|
||||
return
|
||||
}
|
||||
|
||||
token, err := jwt.Parse(tokenReq.RefreshToken, func(token *jwt.Token) (interface{}, error) {
|
||||
token, err := jwt.Parse(refresh, func(token *jwt.Token) (interface{}, error) {
|
||||
if _, ok := token.Method.(*jwt.SigningMethodHMAC); !ok {
|
||||
return nil, fmt.Errorf("unexpected signing method: %v", token.Header["alg"])
|
||||
}
|
||||
return []byte(utils.SecretKey), nil
|
||||
})
|
||||
if err != nil || token == nil || !token.Valid {
|
||||
appresult.ClearAuthCookies(c)
|
||||
c.JSON(http.StatusUnauthorized, appresult.ErrNotAcceptable)
|
||||
return
|
||||
}
|
||||
|
||||
if claims, ok := token.Claims.(jwt.MapClaims); ok && token.Valid {
|
||||
claims, ok := token.Claims.(jwt.MapClaims)
|
||||
if !ok {
|
||||
c.JSON(http.StatusUnauthorized, appresult.ErrNotAcceptable)
|
||||
return
|
||||
}
|
||||
|
||||
res, err := h.repository.CheckAdmin(context.TODO(), int(claims["id"].(float64)))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
res, err := h.repository.CheckAdmin(context.TODO(), int(claims["id"].(float64)))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
if int(claims["id"].(float64)) == res.Id {
|
||||
|
||||
newTokenPair, err := utils.GenerateTokenPair(int(claims["id"].(float64)))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrNotAcceptable)
|
||||
return
|
||||
}
|
||||
|
||||
successResult := appresult.Success
|
||||
successResult.Data = newTokenPair
|
||||
c.JSON(http.StatusOK, successResult)
|
||||
return
|
||||
}
|
||||
if int(claims["id"].(float64)) != res.Id {
|
||||
c.JSON(http.StatusInternalServerError, appresult.ErrInternalServer)
|
||||
return
|
||||
}
|
||||
|
||||
return
|
||||
newTokenPair, err := utils.GenerateTokenPair(int(claims["id"].(float64)))
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, appresult.ErrNotAcceptable)
|
||||
return
|
||||
}
|
||||
|
||||
appresult.SetAuthCookies(c, newTokenPair["accessToken"], newTokenPair["refreshToken"])
|
||||
|
||||
successResult := appresult.Success
|
||||
successResult.Data = newTokenPair
|
||||
c.JSON(http.StatusOK, successResult)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user