feat(admin): 增加登录限流、审计与令牌退出 (#42)

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
fu500f
2026-10-31 00:50:23 +00:00
co-authored by Devin AI
parent 36f9c95fa7
commit 065198f227
11 changed files with 496 additions and 12 deletions
+61 -9
View File
@@ -6,12 +6,15 @@ package auth
import (
"context"
"crypto/hmac"
"crypto/rand"
"crypto/sha256"
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"net/http"
"strings"
"sync"
"time"
"golang.org/x/crypto/bcrypt"
@@ -23,11 +26,16 @@ type Authenticator struct {
passwordHash []byte
secret []byte
ttl time.Duration
mu sync.RWMutex
revoked map[string]int64
}
// New builds an Authenticator. passwordHash must be a bcrypt hash.
func New(username string, passwordHash, secret []byte, ttl time.Duration) *Authenticator {
return &Authenticator{username: username, passwordHash: passwordHash, secret: secret, ttl: ttl}
return &Authenticator{
username: username, passwordHash: passwordHash, secret: secret, ttl: ttl,
revoked: make(map[string]int64),
}
}
// ErrInvalidCredentials is returned when login fails.
@@ -49,6 +57,7 @@ func (a *Authenticator) Login(username, password string) (string, error) {
type claims struct {
Sub string `json:"sub"`
Exp int64 `json:"exp"`
JTI string `json:"jti"`
}
func b64(b []byte) string { return base64.RawURLEncoding.EncodeToString(b) }
@@ -61,7 +70,15 @@ func (a *Authenticator) sign(signingInput string) string {
func (a *Authenticator) issue(sub string) (string, error) {
header := b64([]byte(`{"alg":"HS256","typ":"JWT"}`))
payloadJSON, err := json.Marshal(claims{Sub: sub, Exp: time.Now().Add(a.ttl).Unix()})
randomID := make([]byte, 16)
if _, err := rand.Read(randomID); err != nil {
return "", err
}
payloadJSON, err := json.Marshal(claims{
Sub: sub,
Exp: time.Now().Add(a.ttl).Unix(),
JTI: hex.EncodeToString(randomID),
})
if err != nil {
return "", err
}
@@ -70,30 +87,65 @@ func (a *Authenticator) issue(sub string) (string, error) {
return signingInput + "." + a.sign(signingInput), nil
}
// Verify checks a token's signature and expiry, returning the subject.
func (a *Authenticator) Verify(token string) (string, error) {
func (a *Authenticator) verifyClaims(token string) (claims, error) {
parts := strings.Split(token, ".")
if len(parts) != 3 {
return "", errors.New("malformed token")
return claims{}, errors.New("malformed token")
}
signingInput := parts[0] + "." + parts[1]
if !hmac.Equal([]byte(a.sign(signingInput)), []byte(parts[2])) {
return "", errors.New("bad signature")
return claims{}, errors.New("bad signature")
}
payload, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
return "", err
return claims{}, err
}
var c claims
if err := json.Unmarshal(payload, &c); err != nil {
return "", err
return claims{}, err
}
if time.Now().Unix() >= c.Exp {
return "", errors.New("token expired")
return claims{}, errors.New("token expired")
}
if c.JTI == "" {
return claims{}, errors.New("token missing jti")
}
return c, nil
}
// Verify checks a token's signature, expiry, and revocation status.
func (a *Authenticator) Verify(token string) (string, error) {
c, err := a.verifyClaims(token)
if err != nil {
return "", err
}
a.mu.RLock()
exp, revoked := a.revoked[c.JTI]
a.mu.RUnlock()
if revoked && time.Now().Unix() < exp {
return "", errors.New("token revoked")
}
return c.Sub, nil
}
// Revoke verifies a token and records its identifier until it expires.
func (a *Authenticator) Revoke(token string) error {
c, err := a.verifyClaims(token)
if err != nil {
return err
}
now := time.Now().Unix()
a.mu.Lock()
defer a.mu.Unlock()
for jti, exp := range a.revoked {
if exp <= now {
delete(a.revoked, jti)
}
}
a.revoked[c.JTI] = c.Exp
return nil
}
type ctxKey int
const userKey ctxKey = 0