feat(auth): enhance security with token hashing and sync integration

- Add token hash fields to Master and Key models for indexed lookups
- Implement SyncService integration in admin and master handlers
- Update master key validation with backward-compatible digest lookup
- Hash child keys in database and store token digests for Redis sync
- Add master metadata sync to Redis for balancer validation
- Ensure backward compatibility with legacy rows during migration
This commit is contained in:
zenfun
2025-12-05 00:17:22 +08:00
parent 8645b22b83
commit 25e5e105b3
7 changed files with 123 additions and 41 deletions

View File

@@ -9,10 +9,11 @@ import (
type AdminHandler struct {
masterService *service.MasterService
syncService *service.SyncService
}
func NewAdminHandler(masterService *service.MasterService) *AdminHandler {
return &AdminHandler{masterService: masterService}
func NewAdminHandler(masterService *service.MasterService, syncService *service.SyncService) *AdminHandler {
return &AdminHandler{masterService: masterService, syncService: syncService}
}
type CreateMasterRequest struct {
@@ -43,6 +44,11 @@ func (h *AdminHandler) CreateMaster(c *gin.Context) {
return
}
if err := h.syncService.SyncMaster(master); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to sync master key", "details": err.Error()})
return
}
c.JSON(http.StatusCreated, gin.H{
"id": master.ID,
"name": master.Name,

View File

@@ -11,10 +11,11 @@ import (
type MasterHandler struct {
masterService *service.MasterService
syncService *service.SyncService
}
func NewMasterHandler(masterService *service.MasterService) *MasterHandler {
return &MasterHandler{masterService: masterService}
func NewMasterHandler(masterService *service.MasterService, syncService *service.SyncService) *MasterHandler {
return &MasterHandler{masterService: masterService, syncService: syncService}
}
type IssueChildKeyRequest struct {
@@ -55,6 +56,11 @@ func (h *MasterHandler) IssueChildKey(c *gin.Context) {
return
}
if err := h.syncService.SyncKey(key); err != nil {
c.JSON(http.StatusInternalServerError, gin.H{"error": "failed to sync child key", "details": err.Error()})
return
}
c.JSON(http.StatusCreated, gin.H{
"id": key.ID,
"key_secret": rawChildKey,