mirror of
https://github.com/EZ-Api/ez-api.git
synced 2026-01-13 17:47:51 +00:00
refactor(service): remove legacy routing and unused code
Removes the legacy route table maintenance logic from the sync service that populated deprecated Redis keys. Additionally, deletes the unused TokenService and KeyDTO files to reduce technical debt.
This commit is contained in:
@@ -1,10 +0,0 @@
|
|||||||
package dto
|
|
||||||
|
|
||||||
// KeyDTO defines payload for key creation/update.
|
|
||||||
type KeyDTO struct {
|
|
||||||
Group string `json:"group"`
|
|
||||||
KeySecret string `json:"key_secret"`
|
|
||||||
Balance float64 `json:"balance"`
|
|
||||||
Status string `json:"status"`
|
|
||||||
Weight int `json:"weight"`
|
|
||||||
}
|
|
||||||
@@ -366,21 +366,6 @@ func (s *SyncService) writeProvidersSnapshot(ctx context.Context, pipe redis.Pip
|
|||||||
return fmt.Errorf("marshal provider %d: %w", k.ID, err)
|
return fmt.Errorf("marshal provider %d: %w", k.ID, err)
|
||||||
}
|
}
|
||||||
pipe.HSet(ctx, "config:providers", fmt.Sprintf("%d", k.ID), payload)
|
pipe.HSet(ctx, "config:providers", fmt.Sprintf("%d", k.ID), payload)
|
||||||
|
|
||||||
// Legacy route table maintenance for compatibility.
|
|
||||||
for _, m := range models {
|
|
||||||
if m == "" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if snap.Status != "active" {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
if snap.BanUntil > 0 && time.Now().Unix() < snap.BanUntil {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
routeKey := fmt.Sprintf("route:group:%s:%s", groupName, m)
|
|
||||||
pipe.SAdd(ctx, routeKey, k.ID)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,76 +0,0 @@
|
|||||||
package service
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"strconv"
|
|
||||||
|
|
||||||
"github.com/ez-api/foundation/tokenhash"
|
|
||||||
"github.com/redis/go-redis/v9"
|
|
||||||
)
|
|
||||||
|
|
||||||
type TokenService struct {
|
|
||||||
rdb *redis.Client
|
|
||||||
}
|
|
||||||
|
|
||||||
func NewTokenService(rdb *redis.Client) *TokenService {
|
|
||||||
return &TokenService{rdb: rdb}
|
|
||||||
}
|
|
||||||
|
|
||||||
type TokenInfo struct {
|
|
||||||
MasterID uint
|
|
||||||
IssuedAtEpoch int64
|
|
||||||
Status string
|
|
||||||
Group string
|
|
||||||
}
|
|
||||||
|
|
||||||
// ValidateToken checks a child key against Redis for validity.
|
|
||||||
// This is designed to be called by the data plane (balancer).
|
|
||||||
func (s *TokenService) ValidateToken(ctx context.Context, token string) (*TokenInfo, error) {
|
|
||||||
tokenHash := tokenhash.HashToken(token)
|
|
||||||
tokenKey := fmt.Sprintf("auth:token:%s", tokenHash)
|
|
||||||
|
|
||||||
// 1. Get token metadata from Redis
|
|
||||||
tokenData, err := s.rdb.HGetAll(ctx, tokenKey).Result()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to get token data: %w", err)
|
|
||||||
}
|
|
||||||
if len(tokenData) == 0 {
|
|
||||||
return nil, errors.New("token not found")
|
|
||||||
}
|
|
||||||
|
|
||||||
if tokenData["status"] != "active" {
|
|
||||||
return nil, errors.New("token is not active")
|
|
||||||
}
|
|
||||||
|
|
||||||
masterID, _ := strconv.ParseUint(tokenData["master_id"], 10, 64)
|
|
||||||
issuedAtEpoch, _ := strconv.ParseInt(tokenData["issued_at_epoch"], 10, 64)
|
|
||||||
|
|
||||||
// 2. Get master metadata from Redis
|
|
||||||
masterKey := fmt.Sprintf("auth:master:%d", masterID)
|
|
||||||
masterData, err := s.rdb.HGetAll(ctx, masterKey).Result()
|
|
||||||
if err != nil {
|
|
||||||
return nil, fmt.Errorf("failed to get master metadata: %w", err)
|
|
||||||
}
|
|
||||||
if len(masterData) == 0 {
|
|
||||||
return nil, errors.New("master metadata not found")
|
|
||||||
}
|
|
||||||
masterStatus := masterData["status"]
|
|
||||||
if masterStatus != "" && masterStatus != "active" {
|
|
||||||
return nil, errors.New("master is not active")
|
|
||||||
}
|
|
||||||
masterEpoch, _ := strconv.ParseInt(masterData["epoch"], 10, 64)
|
|
||||||
|
|
||||||
// 3. Core Epoch Validation
|
|
||||||
if issuedAtEpoch < masterEpoch {
|
|
||||||
return nil, errors.New("token revoked due to master key rotation")
|
|
||||||
}
|
|
||||||
|
|
||||||
return &TokenInfo{
|
|
||||||
MasterID: uint(masterID),
|
|
||||||
IssuedAtEpoch: issuedAtEpoch,
|
|
||||||
Status: tokenData["status"],
|
|
||||||
Group: tokenData["group"],
|
|
||||||
}, nil
|
|
||||||
}
|
|
||||||
Reference in New Issue
Block a user