78 lines
1.9 KiB
Go
78 lines
1.9 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"oidc-oauth2-server/services"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
)
|
|
|
|
type RegistrationHandler struct {
|
|
clientService *services.ClientService
|
|
}
|
|
|
|
func NewRegistrationHandler(clientService *services.ClientService) *RegistrationHandler {
|
|
return &RegistrationHandler{
|
|
clientService: clientService,
|
|
}
|
|
}
|
|
|
|
// Register 处理动态客户端注册
|
|
func (h *RegistrationHandler) Register(c *gin.Context) {
|
|
var req services.ClientRegistrationRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid_request", "error_description": err.Error()})
|
|
return
|
|
}
|
|
|
|
client, err := h.clientService.RegisterClient(&req)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid_request", "error_description": err.Error()})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusCreated, client)
|
|
}
|
|
|
|
// GetClient 获取客户端信息
|
|
func (h *RegistrationHandler) GetClient(c *gin.Context) {
|
|
clientID := c.Param("client_id")
|
|
client, err := h.clientService.GetClient(clientID)
|
|
if err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "not_found"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, client)
|
|
}
|
|
|
|
// UpdateClient 更新客户端信息
|
|
func (h *RegistrationHandler) UpdateClient(c *gin.Context) {
|
|
clientID := c.Param("client_id")
|
|
var req services.ClientRegistrationRequest
|
|
if err := c.ShouldBindJSON(&req); err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid_request"})
|
|
return
|
|
}
|
|
|
|
client, err := h.clientService.UpdateClient(clientID, &req)
|
|
if err != nil {
|
|
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid_request"})
|
|
return
|
|
}
|
|
|
|
c.JSON(http.StatusOK, client)
|
|
}
|
|
|
|
// DeleteClient 删除客户端
|
|
func (h *RegistrationHandler) DeleteClient(c *gin.Context) {
|
|
clientID := c.Param("client_id")
|
|
if err := h.clientService.DeleteClient(clientID); err != nil {
|
|
c.JSON(http.StatusNotFound, gin.H{"error": "not_found"})
|
|
return
|
|
}
|
|
|
|
c.Status(http.StatusNoContent)
|
|
}
|