Files
nginx-ui/api/user/short_token.go
0xJacky fb37c94276 feat: implement short token endpoint for WebSocket authentication
- Added `InitTokenRouter` to define the `/token/short` endpoint for issuing short tokens.
- Created `IssueShortToken` function to handle short token generation and response.
- Updated WebSocket middleware to require short token for authentication, preventing CSWSH attacks.
- Modified user store and login handling to integrate short token functionality.
- Enhanced documentation to reflect changes in WebSocket security requirements.
2026-04-02 00:06:04 +08:00

35 lines
818 B
Go

package user
import (
"net/http"
"github.com/0xJacky/Nginx-UI/api"
"github.com/0xJacky/Nginx-UI/internal/middleware"
"github.com/0xJacky/Nginx-UI/internal/user"
"github.com/gin-gonic/gin"
"github.com/uozi-tech/cosy"
)
// IssueShortToken creates a short token for WebSocket authentication.
// Requires both JWT (via AuthRequired) and the session-binding cookie.
func IssueShortToken(c *gin.Context) {
sessionCookie, err := c.Cookie(middleware.SecureSessionCookieName)
if err != nil || sessionCookie == "" {
c.JSON(http.StatusForbidden, gin.H{
"message": "Session binding cookie required",
})
return
}
u := api.CurrentUser(c)
shortToken, err := user.GenerateShortToken(u.ID)
if err != nil {
cosy.ErrHandler(c, err)
return
}
c.JSON(http.StatusOK, gin.H{
"short_token": shortToken,
})
}