Revoke a bearer Base fails to store, and say to start again
If storing the bearer from an approved exchange failed (a database error, or the browser's request cancelled), Base answered "connected" while keeping nothing, leaving a live credential it did not know about. Storing now runs on its own clock (context.WithoutCancel, 10s). Unless the same device is already connected, any failure to store revokes the device in Equipo (bounded, best effort), logs it, forgets the link and returns "failed": the connect page says "That didn't work. Start again."
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+72
−28
docs/copy-pending.md+2 −1internal/web/ask.go+3 −2internal/web/ask_connect.go+40 −24internal/web/templates/ask_connect.html+1 −1internal/webtest/ask_test.go+26 −0
diff --git a/docs/copy-pending.md b/docs/copy-pending.md| index 13a57d2..7cb2447 100644 |
| --- a/docs/copy-pending.md |
| +++ b/docs/copy-pending.md |
| @@ -252,7 +252,8 @@ Review 5 (pending): "Ask Base anything", the chat with Equipo's coordinator. |
| | internal/web/templates/ask_connect.html | Not connected, body | Connect Equipo, and its coordinator answers you in Base. | |
| | internal/web/templates/ask_connect.html | Code expired, body | That code expired. Start again to get a new one. | |
| | internal/web/templates/ask_connect.html | Equipo refused the exchange (403), body | Equipo didn't accept that request. Start again. | |
| -| internal/web/templates/ask_connect.html | Expired or refused, button | Start again | |
| +| internal/web/templates/ask_connect.html | Base could not keep the connection Equipo approved (it revokes it), body | That didn't work. Start again. | |
| +| internal/web/templates/ask_connect.html | Expired, refused or failed, button | Start again | |
| | internal/web/templates/ask_connect.html | Connect page, back button | Back | |
| | internal/web/ask_connect.go | Equipo did not answer when starting | Equipo isn't answering. Try again in a minute. | |
| | internal/web/ask_connect.go | Equipo did not answer while polling | Equipo isn't answering. Base will keep trying. | |
diff --git a/internal/web/ask.go b/internal/web/ask.go| index 9c17fa8..289ea11 100644 |
| --- a/internal/web/ask.go |
| +++ b/internal/web/ask.go |
| @@ -44,13 +44,14 @@ func newAsker(origin, instanceKey, name string) *asker { |
| // The states of a member's chat. Connect: nothing connected; pending: a |
| // sign-in code waits for approval in Equipo; expired and refused: that code |
| -// can no longer be approved; ready: a live bearer; reconnect: Equipo |
| +// can no longer be approved; failed: Base could not keep the bearer it got; ready: a live bearer; reconnect: Equipo |
| // refused the bearer or it expired; unavailable: Equipo did not answer. |
| const ( |
| askConnect = "connect" |
| askPending = "pending" |
| askExpired = "expired" |
| askRefused = "refused" |
| + askFailed = "failed" |
| askReady = "ready" |
| askReconnect = "reconnect" |
| askUnavailable = "unavailable" |
| @@ -140,7 +141,7 @@ func (v askView) Shows(states string) bool { |
| // homeState is the state Home and Settings show, read without calling |
| // Equipo: a code that can no longer be approved is as good as none. |
| func homeState(state string) string { |
| - if state == askExpired || state == askRefused { |
| + if state == askExpired || state == askRefused || state == askFailed { |
| return askConnect |
| } |
| return state |
diff --git a/internal/web/ask_connect.go b/internal/web/ask_connect.go| index d218fea..4272334 100644 |
| --- a/internal/web/ask_connect.go |
| +++ b/internal/web/ask_connect.go |
| @@ -104,7 +104,7 @@ func (a *app) askConnectPage(w http.ResponseWriter, r *http.Request) { |
| return |
| } |
| v := connectView{State: state, Next: next, Code: l.UserCode, Verify: l.VerificationURI} |
| - if ended := q.Get("ended"); state == askConnect && (ended == askExpired || ended == askRefused) { |
| + if ended := q.Get("ended"); state == askConnect && (ended == askExpired || ended == askRefused || ended == askFailed) { |
| v.State = ended |
| } |
| if q.Get("problem") != "" && state == askPending { |
| @@ -136,7 +136,7 @@ func (a *app) askConnectCheck(w http.ResponseWriter, r *http.Request) { |
| switch { |
| case state == askReady: |
| target = askNext[next] |
| - case state == askExpired || state == askRefused: |
| + case state == askExpired || state == askRefused || state == askFailed: |
| target += "&ended=" + state |
| case problem: |
| target += "&problem=1" |
| @@ -181,38 +181,54 @@ func (a *app) poll(ctx context.Context, me store.Member, l store.EquipoLink, sec |
| case err != nil: |
| return askPending, err |
| } |
| + return a.keep(ctx, me, l, sec, cred), nil |
| +} |
| + |
| +// storeTimeout bounds storing a new bearer, and revoking it if that fails. |
| +const storeTimeout = 10 * time.Second |
| + |
| +// keep stores the bearer an exchange brought back, on its own clock: a |
| +// browser that went away must not leave a credential Base lost. The link |
| +// must still be this pending device. If it is this device and already |
| +// connected (a duplicate exchange won), that connection stays. Otherwise |
| +// (disconnected, replaced, or the store failed) the device is revoked in |
| +// Equipo, and the person is told to start again (askFailed) or, if they |
| +// disconnected, sees that. |
| +func (a *app) keep(ctx context.Context, me store.Member, l store.EquipoLink, sec deviceSecrets, cred equipo.Credential) string { |
| + ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), storeTimeout) |
| + defer cancel() |
| sec.Token = cred.Token |
| + l.Revision = cred.Revision |
| sealed, err := a.sealSecrets(me.ID, sec) |
| - if err != nil { |
| - return askPending, err |
| - } |
| - stored, err := store.ConnectEquipoLink(ctx, a.db, me.ID, l.DeviceID, sealed, cred.ExpiresAt, cred.Revision) |
| - if err != nil || stored { |
| - return askReady, err |
| + if err == nil { |
| + var stored bool |
| + if stored, err = store.ConnectEquipoLink(ctx, a.db, me.ID, l.DeviceID, sealed, cred.ExpiresAt, cred.Revision); stored { |
| + return askReady |
| + } |
| } |
| - return a.unstored(ctx, me, l, sec, cred) |
| -} |
| - |
| -// unstored handles a bearer the link would not take. If the link is |
| -// still this device it is already connected (a duplicate exchange won), |
| -// and that connection stays. If the device was disconnected or replaced, |
| -// the bearer is revoked, so nothing live is left behind. |
| -func (a *app) unstored(ctx context.Context, me store.Member, l store.EquipoLink, sec deviceSecrets, cred equipo.Credential) (string, error) { |
| - now, found, err := store.EquipoLinkFor(ctx, a.db, me.ID) |
| - if err != nil { |
| - return askPending, err |
| + if err == nil { |
| + now, found, e := store.EquipoLinkFor(ctx, a.db, me.ID) |
| + if e == nil && found && now.DeviceID == l.DeviceID && now.State == store.EquipoConnected { |
| + return askReady |
| + } |
| + if err = e; err == nil { |
| + a.revoke(ctx, l, sec) |
| + return askConnect |
| + } |
| } |
| - if found && now.DeviceID == l.DeviceID { |
| - return askReady, nil |
| + a.logger.Error("ask: store bearer", "device", l.DeviceID, "err", err) |
| + a.revoke(ctx, l, sec) |
| + if e := store.DeleteEquipoLink(ctx, a.db, me.ID, l.DeviceID); e != nil { |
| + a.logger.Error("ask: forget device", "device", l.DeviceID, "err", e) |
| } |
| - l.Revision = cred.Revision |
| - a.revoke(context.WithoutCancel(ctx), l, sec) |
| - return askConnect, nil |
| + return askFailed |
| } |
| // revoke revokes the member's device in Equipo, as itself. It is best |
| // effort: the bearer is about to be forgotten either way. |
| func (a *app) revoke(ctx context.Context, l store.EquipoLink, sec deviceSecrets) { |
| + ctx, cancel := context.WithTimeout(context.WithoutCancel(ctx), storeTimeout) |
| + defer cancel() |
| err := a.ask.client.RevokeDevice(ctx, sec.Token, l.DeviceID, l.Revision) |
| if err != nil && !errors.Is(err, equipo.ErrUnauthenticated) { |
| a.logger.Warn("ask: revoke device", "device", l.DeviceID, "err", err) |
diff --git a/internal/web/templates/ask_connect.html b/internal/web/templates/ask_connect.html| index 752356d..2d06b5e 100644 |
| --- a/internal/web/templates/ask_connect.html |
| +++ b/internal/web/templates/ask_connect.html |
| @@ -9,7 +9,7 @@ |
| <p class="alt">Once you've approved it, come back and press Check again.</p> |
| <form method="post" action="/ask/connect/check" data-connect-check><input type="hidden" name="next" value="{{.Next}}"><button class="btn ghost full" type="submit">Check again</button></form> |
| <script type="module" src="{{asset "static/ask-connect.js"}}"></script> |
| -{{else}}<p>{{if eq .State "expired"}}That code expired. Start again to get a new one.{{else if eq .State "refused"}}Equipo didn't accept that request. Start again.{{else if eq .State "reconnect"}}Base's connection to Equipo expired or was removed.{{else}}Connect Equipo, and its coordinator answers you in Base.{{end}}</p> |
| +{{else}}<p>{{if eq .State "expired"}}That code expired. Start again to get a new one.{{else if eq .State "refused"}}Equipo didn't accept that request. Start again.{{else if eq .State "failed"}}That didn't work. Start again.{{else if eq .State "reconnect"}}Base's connection to Equipo expired or was removed.{{else}}Connect Equipo, and its coordinator answers you in Base.{{end}}</p> |
| <form method="post" action="/ask/connect"><input type="hidden" name="next" value="{{.Next}}"><button class="btn full" type="submit">{{if eq .State "connect"}}Connect Equipo{{else if eq .State "reconnect"}}Reconnect Equipo{{else}}Start again{{end}}</button></form> |
| <a class="btn ghost full" href="{{.Back}}">Back</a> |
| {{end}}{{with .Problem}}<p class="status" role="alert">{{.}}</p>{{end}} |
diff --git a/internal/webtest/ask_test.go b/internal/webtest/ask_test.go| index d26a7c2..3d3cda0 100644 |
| --- a/internal/webtest/ask_test.go |
| +++ b/internal/webtest/ask_test.go |
| @@ -574,3 +574,29 @@ func TestDuplicateExchangeKeepsConnection(t *testing.T) { |
| t.Fatalf("link: %+v %v", got, found) |
| } |
| } |
| + |
| +// If Base cannot store the bearer, it revokes the device in Equipo and |
| +// says to start again: never "connected" with a credential it lost. |
| +func TestConnectStoreFailureRevokes(t *testing.T) { |
| + a, f := newAskApp(t) |
| + r := a.post("/ask/connect", url.Values{"next": {"settings"}}) |
| + m := codeRE.FindStringSubmatch(a.get(r.Location()).Body) |
| + if err := f.Approve(m[1]); err != nil { |
| + t.Fatal(err) |
| + } |
| + l, _, _ := store.EquipoLinkFor(context.Background(), a.DB, a.ownerID()) |
| + if _, err := a.DB.Exec(`CREATE TRIGGER no_bearer BEFORE UPDATE OF state ON equipo_links |
| + WHEN NEW.state = 'connected' BEGIN SELECT RAISE(ABORT, 'disk full'); END`); err != nil { |
| + t.Fatal(err) |
| + } |
| + c := a.check("settings") |
| + if c.Code != 303 || c.Location() == "/settings" || !strings.Contains(c.Location(), "ended=failed") { |
| + t.Fatalf("check: %d %q, want start again", c.Code, c.Location()) |
| + } |
| + if !f.Revoked(l.DeviceID) { |
| + t.Fatal("the device whose bearer was lost was not revoked") |
| + } |
| + if page := a.get(c.Location()).Body; !strings.Contains(page, "That didn't work. Start again.") { |
| + t.Fatalf("page:\n%s", page) |
| + } |
| +} |