mirror of
https://github.com/skyle1995/NetworkAuth.git
synced 2026-05-25 10:34:15 +08:00
Add the encrypt toolkit
This commit is contained in:
@@ -1,6 +1,8 @@
|
|||||||
package admin
|
package admin
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"net/http"
|
"net/http"
|
||||||
"networkDev/database"
|
"networkDev/database"
|
||||||
@@ -9,8 +11,6 @@ import (
|
|||||||
"networkDev/utils/encrypt"
|
"networkDev/utils/encrypt"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
"crypto/rand"
|
|
||||||
"encoding/hex"
|
|
||||||
|
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
@@ -281,8 +281,8 @@ func APIGenerateKeysHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
var req struct {
|
var req struct {
|
||||||
Side string `json:"side"` // submit | return
|
Side string `json:"side"` // submit | return
|
||||||
Algorithm int `json:"algorithm"` // 与 models.Algorithm* 对应
|
Algorithm int `json:"algorithm"` // 与 models.Algorithm* 对应
|
||||||
}
|
}
|
||||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||||
http.Error(w, "Invalid JSON", http.StatusBadRequest)
|
http.Error(w, "Invalid JSON", http.StatusBadRequest)
|
||||||
@@ -323,7 +323,7 @@ func APIGenerateKeysHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, "生成RSA密钥失败", http.StatusInternalServerError)
|
http.Error(w, "生成RSA密钥失败", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 转换为PEM格式
|
// 转换为PEM格式
|
||||||
publicKeyPEM, err := encrypt.PublicKeyToPEM(publicKey)
|
publicKeyPEM, err := encrypt.PublicKeyToPEM(publicKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -331,14 +331,14 @@ func APIGenerateKeysHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, "转换公钥格式失败", http.StatusInternalServerError)
|
http.Error(w, "转换公钥格式失败", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
privateKeyPEM, err := encrypt.PrivateKeyToPEM(privateKey)
|
privateKeyPEM, err := encrypt.PrivateKeyToPEM(privateKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithError(err).Error("Failed to convert private key to PEM")
|
logrus.WithError(err).Error("Failed to convert private key to PEM")
|
||||||
http.Error(w, "转换私钥格式失败", http.StatusInternalServerError)
|
http.Error(w, "转换私钥格式失败", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
result["public_key"] = publicKeyPEM
|
result["public_key"] = publicKeyPEM
|
||||||
result["private_key"] = privateKeyPEM
|
result["private_key"] = privateKeyPEM
|
||||||
case models.AlgorithmRSADynamic:
|
case models.AlgorithmRSADynamic:
|
||||||
@@ -349,7 +349,7 @@ func APIGenerateKeysHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
http.Error(w, "生成RSA动态密钥失败", http.StatusInternalServerError)
|
http.Error(w, "生成RSA动态密钥失败", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
result["public_key"] = publicKeyPEM
|
result["public_key"] = publicKeyPEM
|
||||||
result["private_key"] = privateKeyPEM
|
result["private_key"] = privateKeyPEM
|
||||||
case models.AlgorithmEasy:
|
case models.AlgorithmEasy:
|
||||||
@@ -377,62 +377,62 @@ func APIGenerateKeysHandler(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func APIResetKeyHandler(w http.ResponseWriter, r *http.Request) {
|
func APIResetKeyHandler(w http.ResponseWriter, r *http.Request) {
|
||||||
if r.Method != http.MethodPost {
|
if r.Method != http.MethodPost {
|
||||||
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
http.Error(w, "Method not allowed", http.StatusMethodNotAllowed)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var req struct {
|
var req struct {
|
||||||
ID uint `json:"id"`
|
ID uint `json:"id"`
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
||||||
http.Error(w, "Invalid JSON", http.StatusBadRequest)
|
http.Error(w, "Invalid JSON", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if req.ID == 0 {
|
if req.ID == 0 {
|
||||||
http.Error(w, "接口ID不能为空", http.StatusBadRequest)
|
http.Error(w, "接口ID不能为空", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
db, err := database.GetDB()
|
db, err := database.GetDB()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logrus.WithError(err).Error("Failed to get database connection")
|
logrus.WithError(err).Error("Failed to get database connection")
|
||||||
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
http.Error(w, "Internal server error", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var api models.API
|
var api models.API
|
||||||
if err := db.First(&api, req.ID).Error; err != nil {
|
if err := db.First(&api, req.ID).Error; err != nil {
|
||||||
http.Error(w, "接口不存在", http.StatusNotFound)
|
http.Error(w, "接口不存在", http.StatusNotFound)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// 生成新的16位大写十六进制密钥
|
// 生成新的16位大写十六进制密钥
|
||||||
bytes := make([]byte, 8)
|
bytes := make([]byte, 8)
|
||||||
if _, err := rand.Read(bytes); err != nil {
|
if _, err := rand.Read(bytes); err != nil {
|
||||||
logrus.WithError(err).Error("Failed to generate random API key")
|
logrus.WithError(err).Error("Failed to generate random API key")
|
||||||
http.Error(w, "生成密钥失败", http.StatusInternalServerError)
|
http.Error(w, "生成密钥失败", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
newKey := strings.ToUpper(hex.EncodeToString(bytes))
|
newKey := strings.ToUpper(hex.EncodeToString(bytes))
|
||||||
|
|
||||||
if err := db.Model(&api).Update("api_key", newKey).Error; err != nil {
|
if err := db.Model(&api).Update("api_key", newKey).Error; err != nil {
|
||||||
logrus.WithError(err).Error("Failed to update API key")
|
logrus.WithError(err).Error("Failed to update API key")
|
||||||
http.Error(w, "更新密钥失败", http.StatusInternalServerError)
|
http.Error(w, "更新密钥失败", http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
response := map[string]interface{}{
|
response := map[string]interface{}{
|
||||||
"success": true,
|
"success": true,
|
||||||
"message": "接口密钥重置成功",
|
"message": "接口密钥重置成功",
|
||||||
"data": map[string]interface{}{
|
"data": map[string]interface{}{
|
||||||
"id": api.ID,
|
"id": api.ID,
|
||||||
"api_key": newKey,
|
"api_key": newKey,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
json.NewEncoder(w).Encode(response)
|
json.NewEncoder(w).Encode(response)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user