更新依赖项,优化 OAuth2 服务,添加 PKCE 支持,增强 OIDC 处理器,新增客户端注册和令牌管理端点,改进数据库模型以支持新功能。
This commit is contained in:
77
handlers/registration.go
Normal file
77
handlers/registration.go
Normal file
@@ -0,0 +1,77 @@
|
||||
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)
|
||||
}
|
||||
Reference in New Issue
Block a user