oficina / equipo Public

Preserve the initiating session through local reauthentication

codex agent, operated by paul@keymail.dev pushed by paul@keymail.dev fba289e676525482241628d7c651024990f2e560 parent ba0d062
7 files changed, +57 −8
  • .rastrillo/budgets.txt +1 −1
  • internal/equipo/auth.go +5 −1
  • internal/equipo/oficinaid/link.go +3 −0
  • internal/equipo/oficinaid/reauth.go +33 −0
  • internal/equipo/oficinaid/store_test.go +2 −1
  • internal/idtest/deletion_test.go +9 −1
  • internal/idtest/id_test.go +4 −4

CI failed — run details

diff --git a/.rastrillo/budgets.txt b/.rastrillo/budgets.txt
index c823f3bb..dcb7c3d3 100644
--- a/.rastrillo/budgets.txt
+++ b/.rastrillo/budgets.txt
@@ -14,7 +14,7 @@ perf GET /* skip Unknown private paths render 404; this fallback contains no scr
time internal/equipo/team 60s Plan 3 loaded CI measured 45.985s; niced serial reruns measured 18.079s and 22.031s. Keep migration, recovery, accounting and concurrency assertions; 60s leaves 30 percent headroom above loaded CI.
time internal/equipotest 300s Final fix package rerun measured 187.035s (review 172.4s); 300s gives 60 percent headroom for HTTP, sandbox, authority and final regression coverage.
-size internal/equipo 19708 - Web handlers and runtime copy, including Oficina ID route wiring and reviewed strings. Identity storage and HTTP flow live in internal/equipo/oficinaid.
+size internal/equipo 19712 - Web handlers and runtime copy, including Oficina ID route wiring and reviewed strings. Identity storage and HTTP flow live in internal/equipo/oficinaid.
size internal/wire 6097 - Protocol 21 Copy dispatch, result, task flags and downgrade fields share Encode.
size internal/equipotest - 13400 Task 21 repairs historical migration and conversation fixtures after Plan 3. Measured 13024 test lines; the 300s time ceiling is unchanged.
time internal/runner/checks 90s CI worker measured 44.553s (44.6s rounded);
diff --git a/internal/equipo/auth.go b/internal/equipo/auth.go
index b32cbb7e..b78395cd 100644
--- a/internal/equipo/auth.go
+++ b/internal/equipo/auth.go
@@ -50,7 +50,11 @@ func (a *application) requireMember(next http.Handler) http.Handler {
return h.RequireMember(next)
}
func (a *application) rememberSignIn(next http.HandlerFunc) http.HandlerFunc {
- return a.store.sessionHandler(a.auth).RememberSignIn(next)
+ handler := a.store.sessionHandler(a.auth).RememberSignIn(next)
+ if a.home != nil {
+ return a.home.LocalReauth(handler)
+ }
+ return handler
}
func privateUnavailable(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Cache-Control", "no-store")
diff --git a/internal/equipo/oficinaid/link.go b/internal/equipo/oficinaid/link.go
index 81ac51c4..3842ea56 100644
--- a/internal/equipo/oficinaid/link.go
+++ b/internal/equipo/oficinaid/link.go
@@ -88,6 +88,9 @@ func (s *Store) CompleteReauth(ctx context.Context, secret, token string, now ti
if e = tx.QueryRowContext(ctx, "SELECT session_epoch FROM members WHERE address=?", address).Scan(&epoch); e != nil || created.Unix() < epoch {
return "", "", ErrRefused
}
+ if _, e = tx.ExecContext(ctx, "DELETE FROM sessions WHERE token_hash=?", old); e != nil {
+ return "", "", e
+ }
var next string
if purpose == "link" {
next, e = s.Links.StartFreshOn(ctx, tx, local, "/account/home/confirm", now)
diff --git a/internal/equipo/oficinaid/reauth.go b/internal/equipo/oficinaid/reauth.go
new file mode 100644
index 00000000..9ff0bde9
--- /dev/null
+++ b/internal/equipo/oficinaid/reauth.go
@@ -0,0 +1,33 @@
+package oficinaid
+
+import (
+ "net/http"
+ "time"
+
+ "amadan.net/rastrillo/rastrillo/sessions"
+)
+
+// LocalReauth keeps the initiating session available to the link transaction.
+// The normal email handler still earns and mints the replacement session.
+// CompleteReauth rechecks and revokes the original together with its proof.
+func (h *HTTP) LocalReauth(next http.HandlerFunc) http.HandlerFunc {
+ return func(w http.ResponseWriter, r *http.Request) {
+ proof, token := cookie(r, reauthCookie), h.token(r)
+ if proof != "" && token != "" && (r.URL.Path == "/auth/callback" || r.Method == http.MethodPost) {
+ var one int
+ e := h.Store.DB.QueryRowContext(r.Context(), "SELECT 1 FROM equipo_id_reauth WHERE id_hash=? AND session_hash=? AND ready=0 AND expires>?", sessions.HashToken(proof), sessions.HashToken(token), time.Now().Unix()).Scan(&one)
+ if _, sessionErr := h.Store.Session(r.Context(), token, time.Now()); e == nil && sessionErr == nil {
+ nextRequest := r.Clone(r.Context())
+ nextRequest.Header = r.Header.Clone()
+ nextRequest.Header.Del("Cookie")
+ for _, c := range r.Cookies() {
+ if c.Name != h.Auth.SessionCookie() {
+ nextRequest.AddCookie(c)
+ }
+ }
+ r = nextRequest
+ }
+ }
+ next(w, r)
+ }
+}
diff --git a/internal/equipo/oficinaid/store_test.go b/internal/equipo/oficinaid/store_test.go
index 73959c8c..498bc370 100644
--- a/internal/equipo/oficinaid/store_test.go
+++ b/internal/equipo/oficinaid/store_test.go
@@ -299,6 +299,7 @@ func TestIDUnlinkRevokesReceiptsAndRelinkCannotRevive(t *testing.T) {
s, k := fixture(t)
now := time.Now().Truncate(time.Second)
local, home := establish(t, s, k, now)
+ survivor := seedLocal(t, s, "local-canary@example.invalid", now)
secret, handle := receive(t, s, k, "", "", "pr_aaaaaaaaaaaaaaaaaaaaaaaaaa", now, now)
proof, e := s.BeginReauth(context.Background(), local, "unlink", now)
if e != nil {
@@ -314,7 +315,7 @@ func TestIDUnlinkRevokesReceiptsAndRelinkCannotRevive(t *testing.T) {
if _, e = s.Session(context.Background(), home, now); e == nil {
t.Fatal("Home session survived unlink")
}
- if _, e = s.Session(context.Background(), local, now); e != nil {
+ if _, e = s.Session(context.Background(), survivor, now); e != nil {
t.Fatal("local session revoked")
}
establish(t, s, k, now)
diff --git a/internal/idtest/deletion_test.go b/internal/idtest/deletion_test.go
index bdc30c0e..6a180f6d 100644
--- a/internal/idtest/deletion_test.go
+++ b/internal/idtest/deletion_test.go
@@ -1,6 +1,7 @@
package idtest
import (
+ "amadan.net/rastrillo/rastrillo/sessions"
"net/url"
"testing"
"time"
@@ -40,14 +41,21 @@ func TestIDLocalReauthenticationUsesRealEmail(t *testing.T) {
old := f.local(t, address)
start := f.request("POST", "/account/home/reauth", f.origin, url.Values{"purpose": {"link"}}, old)
proof := cookie(t, start, "__Host-equipo-id-reauth")
- fresh := f.emailWith(t, start.Header().Get("Location"), old)
+ fresh := f.emailWith(t, start.Header().Get("Location"), old, proof)
if fresh.Value == old.Value {
t.Fatal("local reauthentication reused session")
}
+ var oldRows int
+ if e := f.db.Writer().QueryRow("SELECT count(*) FROM sessions WHERE token_hash=?", sessions.HashToken(old.Value)).Scan(&oldRows); e != nil || oldRows != 1 {
+ t.Fatal("original session lost before proof completion", oldRows, e)
+ }
end := f.request("GET", "/account/home/reauth-complete", "", nil, proof, fresh)
if end.Code != 303 || end.Header().Get("Location") != "/auth/home" {
t.Fatal("local proof did not start Home leg", end.Code, end.Body.String())
}
+ if e := f.db.Writer().QueryRow("SELECT count(*) FROM sessions WHERE token_hash=?", sessions.HashToken(old.Value)).Scan(&oldRows); e != nil || oldRows != 0 {
+ t.Fatal("original session was not rotated", oldRows, e)
+ }
done, _ := f.exchange(t, cookie(t, end, "__Host-equipo-id-init"), fresh, person)
if done.Code != 303 {
t.Fatal("fresh email then Home", done.Code)
diff --git a/internal/idtest/id_test.go b/internal/idtest/id_test.go
index 9080e26d..6ad2b42b 100644
--- a/internal/idtest/id_test.go
+++ b/internal/idtest/id_test.go
@@ -306,13 +306,13 @@ func fields(body, action string) url.Values {
return v
}
func (f *fixture) email(t *testing.T) { f.emailWith(t, "/signin?force=1", nil) }
-func (f *fixture) emailWith(t *testing.T, start string, original *http.Cookie) *http.Cookie {
+func (f *fixture) emailWith(t *testing.T, start string, original ...*http.Cookie) *http.Cookie {
t.Helper()
- page := f.request("GET", start, "", nil, original)
+ page := f.request("GET", start, "", nil, original...)
form := fields(page.Body.String(), "/auth/begin")
form.Set("address", address)
time.Sleep(pow.DefaultMinAge + 20*time.Millisecond)
- begin := f.request("POST", "/auth/begin", f.origin, form, original)
+ begin := f.request("POST", "/auth/begin", f.origin, form, original...)
if begin.Code != 303 {
t.Fatal("email begin", begin.Code, begin.Body.String())
}
@@ -333,7 +333,7 @@ func (f *fixture) emailWith(t *testing.T, start string, original *http.Cookie) *
if target == "" {
t.Fatal("no emailed link")
}
- jar := mergeCookies([]*http.Cookie{original}, page.Result().Cookies(), begin.Result().Cookies())
+ jar := mergeCookies(original, page.Result().Cookies(), begin.Result().Cookies())
confirm := f.request("GET", target, "", nil, jar...)
if confirm.Code != 200 {
t.Fatal("email confirmation", confirm.Code)