Make login OAuth state single-use #193

2 changed files with 5 additions and 2 deletions
Showing only changes of commit 085456b65e - Show all commits
+2 -1
View File
@@ -889,11 +889,12 @@ func TestPendingStates(t *testing.T) {
t.Error("Consume of an unknown state = true") t.Error("Consume of an unknown state = true")
} }
for i := range maxPendingStates { for i := range maxPendingStates - 1 {
if err := p.Add(fmt.Sprint("s", i), now); err != nil { if err := p.Add(fmt.Sprint("s", i), now); err != nil {
t.Fatalf("Add %d: %v", i, err) t.Fatalf("Add %d: %v", i, err)
} }
} }
// "first" was consumed but has not expired, so it still counts.
if err := p.Add("over", now); !errors.Is(err, ErrTooManyPending) { if err := p.Add("over", now); !errors.Is(err, ErrTooManyPending) {
t.Errorf("Add past the bound = %v, want ErrTooManyPending", err) t.Errorf("Add past the bound = %v, want ErrTooManyPending", err)
} }
+3 -1
View File
@@ -200,7 +200,9 @@ func (p *PendingStates) Add(state string, now time.Time) error {
expired++ expired++
} }
p.order = p.order[expired:] p.order = p.order[expired:]
if len(p.states) >= maxPendingStates { // The bound counts every unexpired state, consumed or not: a consumed
// state leaves states at once but order only when it expires.
if len(p.order) >= maxPendingStates {
return ErrTooManyPending return ErrTooManyPending
} }
expires := now.Add(p.ttl) expires := now.Add(p.ttl)