Files
gochat/internal/handler/api/v1/platform_user_sso_handler.go
T
2026-06-04 15:44:48 +08:00

70 lines
2.4 KiB
Go

package v1
import (
"net/http"
"github.com/gin-gonic/gin"
"github.com/gochat/gochat/pkg/response"
)
// PlatformUserSSOHandler handles platform-level User SSO endpoints.
// Reference: Chatwoot Platform::Api::V1::UsersController#login, #token
type PlatformUserSSOHandler struct{}
// NewPlatformUserSSOHandler creates a new PlatformUserSSOHandler.
func NewPlatformUserSSOHandler() *PlatformUserSSOHandler {
return &PlatformUserSSOHandler{}
}
// GetSSOLink generates an SSO login link for a specific user.
// GET /platform/api/v1/users/:id/login
// Reference: Chatwoot Platform::Api::V1::UsersController#login
// Returns a URL that authenticates the user via SSO and redirects into Chatwoot.
func (h *PlatformUserSSOHandler) GetSSOLink(c *gin.Context) {
userID, err := parseUintParam(c, "id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid user ID")
return
}
// SSO link generation — in production, this would look up the user,
// generate a JWT or HMAC-based SSO token, and construct the redirect URL
// following Chatwoot's SSO protocol.
// Reference: Chatwoot User#generate_sso_link
response.OK(c, gin.H{
"url": "", // Would be populated with SSO redirect URL: e.g. https://app.chatwoot.com/auth/sso?token=xxx
"id": userID,
})
}
// GetSSOToken returns the SSO authentication token for a user.
// GET /platform/api/v1/users/:id/token
// Reference: Chatwoot Platform::Api::V1::UsersController#token
// Used after SSO login to verify the token and retrieve user info.
func (h *PlatformUserSSOHandler) GetSSOToken(c *gin.Context) {
userID, err := parseUintParam(c, "id")
if err != nil {
response.AbortWithStatusError(c, http.StatusBadRequest, response.ErrBadRequest, "invalid user ID")
return
}
// SSO token retrieval — in production, this would validate the SSO token
// and return user authentication details.
response.OK(c, gin.H{
"id": userID,
"token": "", // Would be populated with SSO auth token
})
}
// RegisterPlatformUserSSORoutes registers platform user SSO routes.
// Routes mirror Chatwoot's Platform::Api::V1::Users SSO endpoints:
// GET /platform/api/v1/users/:id/login → GetSSOLink
// GET /platform/api/v1/users/:id/token → GetSSOToken
func RegisterPlatformUserSSORoutes(g *gin.RouterGroup, h *PlatformUserSSOHandler) {
users := g.Group("/users")
{
users.GET("/:id/login", h.GetSSOLink)
users.GET("/:id/token", h.GetSSOToken)
}
}