youhua
This commit is contained in:
@@ -5,6 +5,7 @@ import (
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
@@ -67,6 +68,11 @@ func (h *CredentialHandler) CreateCredential(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Trim whitespace from all values
|
||||
for k, v := range testData {
|
||||
testData[k] = strings.TrimSpace(v)
|
||||
}
|
||||
|
||||
// Validate fields for the provider type
|
||||
fields := config.CredentialTypeMeta(req.Type)
|
||||
for _, f := range fields {
|
||||
@@ -78,10 +84,23 @@ func (h *CredentialHandler) CreateCredential(c *gin.Context) {
|
||||
}
|
||||
}
|
||||
|
||||
// Cloudflare: require either API Token or Email+Key
|
||||
if req.Type == config.CredentialCloudflare {
|
||||
hasToken := testData["cf_api_token"] != ""
|
||||
hasEmailKey := testData["cf_email"] != "" && testData["cf_api_key"] != ""
|
||||
if !hasToken && !hasEmailKey {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "请填写 API Token,或者同时填写 Email 和 Global API Key"})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// Re-serialize trimmed data
|
||||
trimmedData, _ := json.Marshal(testData)
|
||||
|
||||
cred := &config.Credential{
|
||||
Name: req.Name,
|
||||
Type: req.Type,
|
||||
Data: req.Data,
|
||||
Data: string(trimmedData),
|
||||
}
|
||||
|
||||
if err := config.CredStore.Create(cred); err != nil {
|
||||
@@ -122,16 +141,28 @@ func (h *CredentialHandler) UpdateCredential(c *gin.Context) {
|
||||
existing.Type = req.Type
|
||||
}
|
||||
if req.Data != "" {
|
||||
// If it's the masked placeholder, keep existing data
|
||||
if req.Data == "********" {
|
||||
existing.Data = existing.Data
|
||||
} else {
|
||||
// If it's the masked placeholder, keep existing data unchanged
|
||||
if req.Data != "********" {
|
||||
var testData map[string]string
|
||||
if err := json.Unmarshal([]byte(req.Data), &testData); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "data must be valid JSON"})
|
||||
return
|
||||
}
|
||||
existing.Data = req.Data
|
||||
// Trim whitespace from all values
|
||||
for k, v := range testData {
|
||||
testData[k] = strings.TrimSpace(v)
|
||||
}
|
||||
// Cloudflare: require either API Token or Email+Key
|
||||
if existing.Type == config.CredentialCloudflare {
|
||||
hasToken := testData["cf_api_token"] != ""
|
||||
hasEmailKey := testData["cf_email"] != "" && testData["cf_api_key"] != ""
|
||||
if !hasToken && !hasEmailKey {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "请填写 API Token,或者同时填写 Email 和 Global API Key"})
|
||||
return
|
||||
}
|
||||
}
|
||||
trimmedData, _ := json.Marshal(testData)
|
||||
existing.Data = string(trimmedData)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user