feat(key): extend key metadata and validation

This commit is contained in:
zenfun
2025-12-19 21:24:24 +08:00
parent 5e98368428
commit 524f8c5a4e
6 changed files with 351 additions and 71 deletions

View File

@@ -425,13 +425,31 @@ func (h *AdminHandler) IssueChildKeyForMaster(c *gin.Context) {
return
}
key, rawChildKey, err := h.masterService.IssueChildKeyAsAdmin(masterID, req.Group, req.Scopes)
modelLimits := strings.TrimSpace(req.ModelLimits)
modelLimitsEnabled := false
if req.ModelLimitsEnabled != nil {
modelLimitsEnabled = *req.ModelLimitsEnabled
} else if modelLimits != "" {
modelLimitsEnabled = true
}
key, rawChildKey, err := h.masterService.IssueChildKeyAsAdmin(masterID, service.IssueKeyOptions{
Group: strings.TrimSpace(req.Group),
Scopes: strings.TrimSpace(req.Scopes),
ModelLimits: modelLimits,
ModelLimitsEnabled: modelLimitsEnabled,
ExpiresAt: req.ExpiresAt,
AllowIPs: strings.TrimSpace(req.AllowIPs),
DenyIPs: strings.TrimSpace(req.DenyIPs),
})
if err != nil {
switch {
case errors.Is(err, service.ErrMasterNotFound):
c.JSON(http.StatusNotFound, gin.H{"error": err.Error()})
case errors.Is(err, service.ErrMasterNotActive):
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
case errors.Is(err, service.ErrModelLimitForbidden):
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
case errors.Is(err, service.ErrChildKeyGroupForbidden):
c.JSON(http.StatusForbidden, gin.H{"error": err.Error()})
case errors.Is(err, service.ErrChildKeyLimitReached):