Merge branch 'devin/1783582849-image-cookie-rolling'

This commit is contained in:
2026-07-09 19:24:37 +08:00
2 changed files with 27 additions and 7 deletions
+24 -4
View File
@@ -3,6 +3,7 @@ package middleware
import ( import (
"net/http" "net/http"
"backend/internal/config"
"backend/internal/service" "backend/internal/service"
"github.com/gin-gonic/gin" "github.com/gin-gonic/gin"
) )
@@ -10,12 +11,13 @@ import (
const currentUserKey = "current_user" const currentUserKey = "current_user"
const currentSessionKey = "current_session" const currentSessionKey = "current_session"
func RequireSession(auth *service.AuthService) gin.HandlerFunc { func RequireSession(auth *service.AuthService, cfg *config.Config) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
cookieToken := readCookie(c, cfg.SessionCookieName)
user, session, err := auth.CurrentUserFromRequest( user, session, err := auth.CurrentUserFromRequest(
c.Request.Context(), c.Request.Context(),
c.GetHeader("Authorization"), c.GetHeader("Authorization"),
readCookie(c, "vivid_session"), cookieToken,
) )
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"detail": "failed to validate session"}) c.JSON(http.StatusInternalServerError, gin.H{"detail": "failed to validate session"})
@@ -27,18 +29,20 @@ func RequireSession(auth *service.AuthService) gin.HandlerFunc {
c.Abort() c.Abort()
return return
} }
refreshSessionCookie(c, cfg, cookieToken)
c.Set(currentUserKey, user) c.Set(currentUserKey, user)
c.Set(currentSessionKey, session) c.Set(currentSessionKey, session)
c.Next() c.Next()
} }
} }
func RequireAdminSession(auth *service.AuthService) gin.HandlerFunc { func RequireAdminSession(auth *service.AuthService, cfg *config.Config) gin.HandlerFunc {
return func(c *gin.Context) { return func(c *gin.Context) {
cookieToken := readCookie(c, cfg.SessionCookieName)
user, session, err := auth.CurrentUserFromRequest( user, session, err := auth.CurrentUserFromRequest(
c.Request.Context(), c.Request.Context(),
c.GetHeader("Authorization"), c.GetHeader("Authorization"),
readCookie(c, "vivid_session"), cookieToken,
) )
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"detail": "failed to validate session"}) c.JSON(http.StatusInternalServerError, gin.H{"detail": "failed to validate session"})
@@ -55,12 +59,28 @@ func RequireAdminSession(auth *service.AuthService) gin.HandlerFunc {
c.Abort() c.Abort()
return return
} }
refreshSessionCookie(c, cfg, cookieToken)
c.Set(currentUserKey, user) c.Set(currentUserKey, user)
c.Set(currentSessionKey, session) c.Set(currentSessionKey, session)
c.Next() c.Next()
} }
} }
// refreshSessionCookie rolls the browser session cookie forward on each
// authenticated request that carried it. The server-side session already slides
// its TTL on use, but the cookie's Max-Age was frozen at login — so it would
// lapse mid-session and break cookie-only auth (e.g. <img> loads of private
// images) even while the SPA still looks logged in via its Bearer token. Only
// refresh when the request actually presented the cookie (a pure Bearer/API-key
// caller has none to roll).
func refreshSessionCookie(c *gin.Context, cfg *config.Config, cookieToken string) {
if cookieToken == "" {
return
}
c.SetSameSite(http.SameSiteLaxMode)
c.SetCookie(cfg.SessionCookieName, cookieToken, int(cfg.SessionTTL.Seconds()), "/", "", cfg.CookieSecure, true)
}
func readCookie(c *gin.Context, name string) string { func readCookie(c *gin.Context, name string) string {
v, err := c.Cookie(name) v, err := c.Cookie(name)
if err != nil { if err != nil {
+3 -3
View File
@@ -81,7 +81,7 @@ func New(cfg *config.Config, auth *service.AuthService, handlers Handlers) *gin.
} }
userAuthed := engine.Group("/admin/api") userAuthed := engine.Group("/admin/api")
userAuthed.Use(middleware.RequireSession(auth)) userAuthed.Use(middleware.RequireSession(auth, cfg))
{ {
userAuthed.GET("/logs", handlers.UserGen.Logs) userAuthed.GET("/logs", handlers.UserGen.Logs)
userAuthed.POST("/generate", handlers.UserGen.Generate) userAuthed.POST("/generate", handlers.UserGen.Generate)
@@ -99,7 +99,7 @@ func New(cfg *config.Config, auth *service.AuthService, handlers Handlers) *gin.
} }
authed := engine.Group("/admin/api") authed := engine.Group("/admin/api")
authed.Use(middleware.RequireAdminSession(auth)) authed.Use(middleware.RequireAdminSession(auth, cfg))
{ {
authed.GET("/dashboard", handlers.AdminRead.Dashboard) authed.GET("/dashboard", handlers.AdminRead.Dashboard)
authed.GET("/users", handlers.AdminRead.Users) authed.GET("/users", handlers.AdminRead.Users)
@@ -186,7 +186,7 @@ func New(cfg *config.Config, auth *service.AuthService, handlers Handlers) *gin.
} }
} }
authGroup.Use(middleware.RequireSession(auth)) authGroup.Use(middleware.RequireSession(auth, cfg))
{ {
authGroup.GET("/me", handlers.Auth.Me) authGroup.GET("/me", handlers.Auth.Me)
authGroup.GET("/invites", handlers.Auth.Invites) authGroup.GET("/invites", handlers.Auth.Invites)