feat(admin): 增加登录限流、审计与令牌退出 (#42)
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
package adminhandler
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/baicai2026-baicai/goods/api/internal/auth"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
)
|
||||
|
||||
func TestLoginLimiterBlocksByIPAndUsername(t *testing.T) {
|
||||
limiter := newLoginLimiter(2, 3, time.Minute)
|
||||
limiter.recordFailure("192.0.2.1", "Admin")
|
||||
limiter.recordFailure("192.0.2.1", "admin")
|
||||
|
||||
blocked, retry := limiter.blocked("192.0.2.1", "admin")
|
||||
if !blocked || retry <= 0 {
|
||||
t.Fatalf("IP limit: blocked=%v retry=%v, want blocked with retry", blocked, retry)
|
||||
}
|
||||
|
||||
limiter.recordFailure("192.0.2.2", "admin")
|
||||
blocked, retry = limiter.blocked("192.0.2.2", "ADMIN")
|
||||
if !blocked || retry <= 0 {
|
||||
t.Fatalf("username limit: blocked=%v retry=%v, want blocked with retry", blocked, retry)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginBlockedReturnsRetryAfterWithoutCheckingPassword(t *testing.T) {
|
||||
h := New(nil, nil, "/ping", nil)
|
||||
h.loginLimit = newLoginLimiter(1, 1, 2*time.Second)
|
||||
h.loginLimit.recordFailure("192.0.2.10", "admin")
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/ping/api/login", strings.NewReader(
|
||||
`{"username":"admin","password":"irrelevant"}`,
|
||||
))
|
||||
req.RemoteAddr = "192.0.2.10:1234"
|
||||
recorder := httptest.NewRecorder()
|
||||
h.Login(recorder, req)
|
||||
|
||||
if recorder.Code != http.StatusTooManyRequests {
|
||||
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusTooManyRequests)
|
||||
}
|
||||
if retryAfter := recorder.Header().Get("Retry-After"); retryAfter != "2" && retryAfter != "1" {
|
||||
t.Fatalf("Retry-After = %q, want 1 or 2 seconds", retryAfter)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginFailureRecordsIPAndUsername(t *testing.T) {
|
||||
passwordHash, err := bcrypt.GenerateFromPassword([]byte("s3cret"), bcrypt.MinCost)
|
||||
if err != nil {
|
||||
t.Fatalf("hash password: %v", err)
|
||||
}
|
||||
h := New(nil, auth.New("admin", passwordHash, []byte("secret"), time.Hour), "/ping", nil)
|
||||
req := httptest.NewRequest(http.MethodPost, "/ping/api/login", strings.NewReader(
|
||||
`{"username":"admin","password":"wrong"}`,
|
||||
))
|
||||
req.RemoteAddr = "192.0.2.15:1234"
|
||||
recorder := httptest.NewRecorder()
|
||||
h.Login(recorder, req)
|
||||
|
||||
if recorder.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusUnauthorized)
|
||||
}
|
||||
if got := len(h.loginLimit.ipFailures["192.0.2.15"]); got != 1 {
|
||||
t.Fatalf("IP failure count = %d, want 1", got)
|
||||
}
|
||||
if got := len(h.loginLimit.usernameFailures["admin"]); got != 1 {
|
||||
t.Fatalf("username failure count = %d, want 1", got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginSuccessClearsIPFailures(t *testing.T) {
|
||||
passwordHash, err := bcrypt.GenerateFromPassword([]byte("s3cret"), bcrypt.MinCost)
|
||||
if err != nil {
|
||||
t.Fatalf("hash password: %v", err)
|
||||
}
|
||||
h := New(nil, auth.New("admin", passwordHash, []byte("secret"), time.Hour), "/ping", nil)
|
||||
h.loginLimit.recordFailure("192.0.2.20", "admin")
|
||||
h.loginLimit.recordFailure("192.0.2.20", "admin")
|
||||
|
||||
req := httptest.NewRequest(http.MethodPost, "/ping/api/login", strings.NewReader(
|
||||
`{"username":"admin","password":"s3cret"}`,
|
||||
))
|
||||
req.RemoteAddr = "192.0.2.20:1234"
|
||||
recorder := httptest.NewRecorder()
|
||||
h.Login(recorder, req)
|
||||
|
||||
if recorder.Code != http.StatusOK {
|
||||
t.Fatalf("status = %d, want %d: %s", recorder.Code, http.StatusOK, recorder.Body.String())
|
||||
}
|
||||
if blocked, _ := h.loginLimit.blocked("192.0.2.20", "admin"); blocked {
|
||||
t.Fatal("successful login did not clear IP failures")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoginLimiterRecoversAfterWindow(t *testing.T) {
|
||||
limiter := newLoginLimiter(1, 1, 20*time.Millisecond)
|
||||
limiter.recordFailure("192.0.2.30", "admin")
|
||||
if blocked, _ := limiter.blocked("192.0.2.30", "admin"); !blocked {
|
||||
t.Fatal("expected login to be blocked before window expires")
|
||||
}
|
||||
|
||||
time.Sleep(30 * time.Millisecond)
|
||||
if blocked, _ := limiter.blocked("192.0.2.30", "admin"); blocked {
|
||||
t.Fatal("login remained blocked after window expired")
|
||||
}
|
||||
|
||||
limiter.mu.Lock()
|
||||
defer limiter.mu.Unlock()
|
||||
if len(limiter.ipFailures) != 0 || len(limiter.usernameFailures) != 0 {
|
||||
t.Fatalf("expired entries remain: ip=%d username=%d",
|
||||
len(limiter.ipFailures), len(limiter.usernameFailures))
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogoutRevokesCurrentToken(t *testing.T) {
|
||||
passwordHash, err := bcrypt.GenerateFromPassword([]byte("s3cret"), bcrypt.MinCost)
|
||||
if err != nil {
|
||||
t.Fatalf("hash password: %v", err)
|
||||
}
|
||||
authn := auth.New("admin", passwordHash, []byte("secret"), time.Hour)
|
||||
token, err := authn.Login("admin", "s3cret")
|
||||
if err != nil {
|
||||
t.Fatalf("login: %v", err)
|
||||
}
|
||||
h := New(nil, authn, "/ping", nil)
|
||||
req := httptest.NewRequest(http.MethodPost, "/ping/api/logout", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
recorder := httptest.NewRecorder()
|
||||
h.Router().ServeHTTP(recorder, req)
|
||||
|
||||
if recorder.Code != http.StatusNoContent {
|
||||
t.Fatalf("logout status = %d, want %d", recorder.Code, http.StatusNoContent)
|
||||
}
|
||||
if _, err := authn.Verify(token); err == nil {
|
||||
t.Fatal("logout did not revoke token")
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user