Refs #3 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_017GpWAskjmYtZpuQ5JwYTX3
44 lines
964 B
Go
44 lines
964 B
Go
package auth
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
|
|
"github.com/google/uuid"
|
|
"github.com/pquerna/otp/totp"
|
|
"github.com/redis/go-redis/v9"
|
|
)
|
|
|
|
func TestTOTPRejectsReplay(t *testing.T) {
|
|
url := os.Getenv("UPTIME_TEST_REDIS_URL")
|
|
if url == "" {
|
|
t.Skip("UPTIME_TEST_REDIS_URL not set")
|
|
}
|
|
opts, _ := redis.ParseURL(url)
|
|
rdb := redis.NewClient(opts)
|
|
defer rdb.Close()
|
|
|
|
key, err := NewTOTPKey("a@example.test")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
now := time.Now()
|
|
code, _ := totp.GenerateCode(key.Secret(), now)
|
|
v := NewTOTPVerifier(rdb)
|
|
v.now = func() time.Time { return now }
|
|
user := uuid.New()
|
|
ctx := context.Background()
|
|
|
|
if ok, err := v.Verify(ctx, user, key.Secret(), code); !ok || err != nil {
|
|
t.Fatalf("first use rejected: %v", err)
|
|
}
|
|
if ok, _ := v.Verify(ctx, user, key.Secret(), code); ok {
|
|
t.Fatal("replayed code accepted")
|
|
}
|
|
if ok, _ := v.Verify(ctx, user, key.Secret(), "000000"); ok {
|
|
t.Fatal("wrong code accepted")
|
|
}
|
|
}
|