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:
@@ -3,9 +3,10 @@ package appresult
|
||||
import (
|
||||
"bankline/internal/config"
|
||||
"fmt"
|
||||
"net/http"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/golang-jwt/jwt"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type UnsignedResponse struct {
|
||||
@@ -25,34 +26,79 @@ func respondWithError(c *gin.Context, code int, message interface{}) {
|
||||
c.AbortWithStatusJSON(code, gin.H{"error": message})
|
||||
}
|
||||
|
||||
// adminClaimsFromRequest returns admin JWT claims or ok=false.
|
||||
// Invalid/expired tokens clear auth cookies so GuestOnly and PageAuth agree.
|
||||
func adminClaimsFromRequest(c *gin.Context) (jwt.MapClaims, bool) {
|
||||
token := AccessTokenFromRequest(c)
|
||||
if token == "" {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
cfg := config.GetConfig()
|
||||
claims, err := TokenClaims(token, cfg.JwtKey)
|
||||
if err != nil {
|
||||
ClearAuthCookies(c)
|
||||
return nil, false
|
||||
}
|
||||
|
||||
isAdmin, _ := claims["admin"].(bool)
|
||||
if !isAdmin {
|
||||
ClearAuthCookies(c)
|
||||
return nil, false
|
||||
}
|
||||
|
||||
return claims, true
|
||||
}
|
||||
|
||||
func JwtTokenCheck() gin.HandlerFunc {
|
||||
|
||||
return func(c *gin.Context) {
|
||||
token := c.GetHeader("Authorization")
|
||||
|
||||
if token == "" {
|
||||
if AccessTokenFromRequest(c) == "" {
|
||||
respondWithError(c, 403, "Authorization header is missing")
|
||||
return
|
||||
}
|
||||
|
||||
cfg := config.GetConfig()
|
||||
claims, err := TokenClaims(token, cfg.JwtKey)
|
||||
if err != nil {
|
||||
claims, ok := adminClaimsFromRequest(c)
|
||||
if !ok {
|
||||
respondWithError(c, 401, "Token is expired")
|
||||
return
|
||||
}
|
||||
|
||||
isAdmin := claims["admin"].(bool)
|
||||
if isAdmin == false {
|
||||
respondWithError(c, 403, "Unauthorized")
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("id", fmt.Sprint(claims["id"]))
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// AdminPageAuth redirects unauthenticated browser requests to /admin/login.
|
||||
func AdminPageAuth() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
claims, ok := adminClaimsFromRequest(c)
|
||||
if !ok {
|
||||
ClearAuthCookies(c)
|
||||
c.Redirect(http.StatusFound, "/admin/login")
|
||||
c.Abort()
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("id", fmt.Sprint(claims["id"]))
|
||||
c.Next()
|
||||
}
|
||||
}
|
||||
|
||||
// AdminGuestOnly redirects already-authenticated users away from the login page.
|
||||
func AdminGuestOnly() gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
claims, ok := adminClaimsFromRequest(c)
|
||||
if !ok {
|
||||
c.Next()
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("id", fmt.Sprint(claims["id"]))
|
||||
c.Redirect(http.StatusFound, "/admin")
|
||||
c.Abort()
|
||||
}
|
||||
}
|
||||
|
||||
func TokenClaims(token, secretKey string) (jwt.MapClaims, error) {
|
||||
decoded, err := jwt.Parse(token, func(token *jwt.Token) (interface{}, error) {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user