feat(auth): implement master key authentication system with child key issuance

Add admin and master authentication layers with JWT support. Replace direct
key creation with hierarchical master/child key system. Update database
schema to support master accounts with configurable limits and epoch-based
key revocation. Add health check endpoint with system status monitoring.

BREAKING CHANGE: Removed direct POST /keys endpoint in favor of master-based
key issuance through /v1/tokens. Database migration requires dropping old User
table and creating Master table with new relationships.
This commit is contained in:
zenfun
2025-12-05 00:16:47 +08:00
parent 5360cc6f1a
commit 8645b22b83
16 changed files with 618 additions and 229 deletions

View File

@@ -0,0 +1,54 @@
package api
import (
"net/http"
"github.com/ez-api/ez-api/internal/service"
"github.com/gin-gonic/gin"
)
type AdminHandler struct {
masterService *service.MasterService
}
func NewAdminHandler(masterService *service.MasterService) *AdminHandler {
return &AdminHandler{masterService: masterService}
}
type CreateMasterRequest struct {
Name string `json:"name" binding:"required"`
Group string `json:"group" binding:"required"`
MaxChildKeys int `json:"max_child_keys"`
GlobalQPS int `json:"global_qps"`
}
func (h *AdminHandler) CreateMaster(c *gin.Context) {
var req CreateMasterRequest
if err := c.ShouldBindJSON(&req); err != nil {
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
return
}
// Use defaults if not provided
if req.MaxChildKeys == 0 {
req.MaxChildKeys = 5
}
if req.GlobalQPS == 0 {
req.GlobalQPS = 3
}
master, rawMasterKey, err := h.masterService.CreateMaster(req.Name, req.Group, req.MaxChildKeys, req.GlobalQPS)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to create master key", "details": err.Error()})
return
}
c.JSON(http.StatusCreated, gin.H{
"id": master.ID,
"name": master.Name,
"group": master.Group,
"master_key": rawMasterKey, // Only show this on creation
"max_child_keys": master.MaxChildKeys,
"global_qps": master.GlobalQPS,
})
}