feat(admin): 增加登录限流、审计与令牌退出 (#42)
Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
package auth
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
@@ -60,3 +66,87 @@ func TestVerifyRejectsExpired(t *testing.T) {
|
||||
t.Fatal("expected expired token error")
|
||||
}
|
||||
}
|
||||
|
||||
func TestIssuedTokensHaveUniqueRandomJTI(t *testing.T) {
|
||||
a := newTestAuth(t, time.Hour)
|
||||
first, err := a.Login("admin", "s3cret")
|
||||
if err != nil {
|
||||
t.Fatalf("first login: %v", err)
|
||||
}
|
||||
second, err := a.Login("admin", "s3cret")
|
||||
if err != nil {
|
||||
t.Fatalf("second login: %v", err)
|
||||
}
|
||||
|
||||
readJTI := func(token string) string {
|
||||
t.Helper()
|
||||
parts := strings.Split(token, ".")
|
||||
if len(parts) != 3 {
|
||||
t.Fatalf("token has %d parts, want 3", len(parts))
|
||||
}
|
||||
payload, err := base64.RawURLEncoding.DecodeString(parts[1])
|
||||
if err != nil {
|
||||
t.Fatalf("decode payload: %v", err)
|
||||
}
|
||||
var c claims
|
||||
if err := json.Unmarshal(payload, &c); err != nil {
|
||||
t.Fatalf("unmarshal claims: %v", err)
|
||||
}
|
||||
randomID, err := hex.DecodeString(c.JTI)
|
||||
if err != nil {
|
||||
t.Fatalf("decode jti: %v", err)
|
||||
}
|
||||
if len(randomID) != 16 {
|
||||
t.Fatalf("jti length = %d bytes, want 16", len(randomID))
|
||||
}
|
||||
return c.JTI
|
||||
}
|
||||
|
||||
if firstID, secondID := readJTI(first), readJTI(second); firstID == secondID {
|
||||
t.Fatal("issued tokens have the same jti")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRevokeRejectsTokenInVerifyAndMiddleware(t *testing.T) {
|
||||
a := newTestAuth(t, time.Hour)
|
||||
token, err := a.Login("admin", "s3cret")
|
||||
if err != nil {
|
||||
t.Fatalf("login: %v", err)
|
||||
}
|
||||
if err := a.Revoke(token); err != nil {
|
||||
t.Fatalf("revoke: %v", err)
|
||||
}
|
||||
if _, err := a.Verify(token); err == nil {
|
||||
t.Fatal("Verify accepted revoked token")
|
||||
}
|
||||
|
||||
nextCalled := false
|
||||
handler := a.Middleware(http.HandlerFunc(func(http.ResponseWriter, *http.Request) {
|
||||
nextCalled = true
|
||||
}))
|
||||
req := httptest.NewRequest(http.MethodGet, "/", nil)
|
||||
req.Header.Set("Authorization", "Bearer "+token)
|
||||
recorder := httptest.NewRecorder()
|
||||
handler.ServeHTTP(recorder, req)
|
||||
if recorder.Code != http.StatusUnauthorized {
|
||||
t.Fatalf("status = %d, want %d", recorder.Code, http.StatusUnauthorized)
|
||||
}
|
||||
if nextCalled {
|
||||
t.Fatal("middleware called next for revoked token")
|
||||
}
|
||||
}
|
||||
|
||||
func TestRevokePrunesExpiredEntries(t *testing.T) {
|
||||
a := newTestAuth(t, time.Hour)
|
||||
a.revoked["expired"] = time.Now().Add(-time.Minute).Unix()
|
||||
token, err := a.Login("admin", "s3cret")
|
||||
if err != nil {
|
||||
t.Fatalf("login: %v", err)
|
||||
}
|
||||
if err := a.Revoke(token); err != nil {
|
||||
t.Fatalf("revoke: %v", err)
|
||||
}
|
||||
if _, ok := a.revoked["expired"]; ok {
|
||||
t.Fatal("Revoke retained an expired revocation")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user