perf(files): 优化私有文件鉴权与高成本查询隔离
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
package file
|
||||
|
||||
import (
|
||||
"crypto/hmac"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/json"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"hfb_sys/backend/internal/filekey"
|
||||
)
|
||||
|
||||
const accessTicketTTL = 5 * time.Minute
|
||||
|
||||
type accessTicketPayload struct {
|
||||
Version int `json:"v"`
|
||||
SubjectType string `json:"sub_type"`
|
||||
SubjectID uint64 `json:"sub_id"`
|
||||
TenantID uint64 `json:"tenant_id"`
|
||||
ObjectKey string `json:"key"`
|
||||
ExpiresAt int64 `json:"exp"`
|
||||
}
|
||||
|
||||
// AccessTicketManager 签发短期对象访问票据,避免相册逐图重复查询资源归属。
|
||||
type AccessTicketManager struct {
|
||||
secret []byte
|
||||
ttl time.Duration
|
||||
now func() time.Time
|
||||
}
|
||||
|
||||
func NewAccessTicketManager(secret string) *AccessTicketManager {
|
||||
sum := sha256.Sum256([]byte("hfb-file-access-ticket:" + secret))
|
||||
return &AccessTicketManager{
|
||||
secret: sum[:],
|
||||
ttl: accessTicketTTL,
|
||||
now: time.Now,
|
||||
}
|
||||
}
|
||||
|
||||
func (m *AccessTicketManager) Issue(subjectType string, subjectID, tenantID uint64, objectKey string) string {
|
||||
if m == nil || subjectID == 0 {
|
||||
return ""
|
||||
}
|
||||
canonical := filekey.Canonical(objectKey)
|
||||
if canonical == "" {
|
||||
return ""
|
||||
}
|
||||
payload, err := json.Marshal(accessTicketPayload{
|
||||
Version: 1,
|
||||
SubjectType: strings.TrimSpace(subjectType),
|
||||
SubjectID: subjectID,
|
||||
TenantID: tenantID,
|
||||
ObjectKey: canonical,
|
||||
ExpiresAt: m.now().Add(m.ttl).Unix(),
|
||||
})
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
signature := m.sign(payload)
|
||||
return base64.RawURLEncoding.EncodeToString(payload) + "." + base64.RawURLEncoding.EncodeToString(signature)
|
||||
}
|
||||
|
||||
func (m *AccessTicketManager) Valid(ticketText, subjectType string, subjectID, tenantID uint64, objectKey string) bool {
|
||||
if m == nil || subjectID == 0 || len(ticketText) == 0 || len(ticketText) > 4096 {
|
||||
return false
|
||||
}
|
||||
parts := strings.Split(ticketText, ".")
|
||||
if len(parts) != 2 {
|
||||
return false
|
||||
}
|
||||
payloadJSON, err := base64.RawURLEncoding.DecodeString(parts[0])
|
||||
if err != nil {
|
||||
return false
|
||||
}
|
||||
signature, err := base64.RawURLEncoding.DecodeString(parts[1])
|
||||
if err != nil || !hmac.Equal(signature, m.sign(payloadJSON)) {
|
||||
return false
|
||||
}
|
||||
var payload accessTicketPayload
|
||||
if err := json.Unmarshal(payloadJSON, &payload); err != nil {
|
||||
return false
|
||||
}
|
||||
canonical := filekey.Canonical(objectKey)
|
||||
return payload.Version == 1 &&
|
||||
payload.SubjectType == strings.TrimSpace(subjectType) &&
|
||||
payload.SubjectID == subjectID &&
|
||||
payload.TenantID == tenantID &&
|
||||
payload.ObjectKey == canonical &&
|
||||
payload.ExpiresAt > m.now().Unix()
|
||||
}
|
||||
|
||||
func (m *AccessTicketManager) sign(payload []byte) []byte {
|
||||
mac := hmac.New(sha256.New, m.secret)
|
||||
_, _ = mac.Write(payload)
|
||||
return mac.Sum(nil)
|
||||
}
|
||||
Reference in New Issue
Block a user