13e8e73da7
The browser UI signs in with a Discord authorization code grant (identify + guilds.members.read) instead of a shared password. Guild membership is the gate; the owner's Discord ID is the only identity allowed in while registration is closed. Sessions become rows in a sessions table with opaque random ids — the cookie carries only the id, looked up and expiry-checked per request — so deleting a row revokes a session. HMAC cookie signing, its derived key, and WEB_PASSWORD are gone, and no replacement signing secret is introduced (ADR-0002). Discord's API base is configurable (DISCORD_API_BASE); the full flow is tested through the real router against a local stub, including the form-encoded token exchange Discord rejects if sent as JSON.
57 lines
1.5 KiB
Go
57 lines
1.5 KiB
Go
package web
|
|
|
|
import (
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestOAuthStateSingleUse(t *testing.T) {
|
|
s := newOAuthStates()
|
|
s.put("st", time.Now().Add(time.Minute))
|
|
if !s.take("st") {
|
|
t.Fatal("take of a fresh state = false, want true")
|
|
}
|
|
if s.take("st") {
|
|
t.Fatal("take of a consumed state = true, want false")
|
|
}
|
|
}
|
|
|
|
func TestOAuthStateUnknownOrExpired(t *testing.T) {
|
|
s := newOAuthStates()
|
|
if s.take("never-seen") {
|
|
t.Fatal("take of an unknown state = true, want false")
|
|
}
|
|
s.put("stale", time.Now().Add(-time.Minute))
|
|
if s.take("stale") {
|
|
t.Fatal("take of an expired state = true, want false")
|
|
}
|
|
}
|
|
|
|
// The map is capped: a flood of starts evicts the oldest states, and consumed
|
|
// states (which leave the FIFO behind) must not defeat the cap.
|
|
func TestOAuthStateEviction(t *testing.T) {
|
|
s := newOAuthStates()
|
|
key := func(i, salt int) string {
|
|
return string(rune('a'+i%26)) + string(rune('0'+i/26+salt*16))
|
|
}
|
|
now := time.Now().Add(time.Hour)
|
|
for i := 0; i < maxStates*2; i++ {
|
|
s.put(key(i, 0), now)
|
|
}
|
|
if got := len(s.expiry); got != maxStates {
|
|
t.Fatalf("states after a flood = %d, want %d", got, maxStates)
|
|
}
|
|
|
|
// Consume everything, then flood again: the map stays bounded and the
|
|
// eviction loop pops the stale FIFO entries instead of stalling.
|
|
for state := range s.expiry {
|
|
s.take(state)
|
|
}
|
|
for i := 0; i < maxStates; i++ {
|
|
s.put(key(i, 1), now)
|
|
}
|
|
if got := len(s.expiry); got != maxStates {
|
|
t.Fatalf("states after consume+flood = %d, want %d", got, maxStates)
|
|
}
|
|
}
|