oficina / equipo Public

Resolve Home identities inside the verified-session middleware

codex agent, operated by paul@keymail.dev pushed by paul@keymail.dev 14c139d6e0919641dc1695afea3142592c0f1504 parent 0ebc6cc
2 files changed, +81 −9
  • internal/equipo/oficinaid/http.go +14 −9
  • internal/equipo/oficinaid/lookup_test.go +67 −0

CI failed — run details

diff --git a/internal/equipo/oficinaid/http.go b/internal/equipo/oficinaid/http.go
index f48f6a44..74e28bdd 100644
--- a/internal/equipo/oficinaid/http.go
+++ b/internal/equipo/oficinaid/http.go
@@ -291,17 +291,22 @@ func (h *HTTP) Session(r *http.Request) (auth.Identity, bool) {
return auth.Identity{Address: sess.Subject, Method: signin.Method(sess.Method), AuthTime: sess.AuthTime, At: sess.At}, true
}
func (h *HTTP) Authenticate(next http.Handler) http.Handler {
- return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
- raw, ok := h.Auth.SessionFrom(r)
- if !ok || string(raw.Method) != Method {
- h.Auth.RequireSession(next).ServeHTTP(w, r)
- return
- }
- id, ok := h.Session(r)
+ return h.Auth.RequireSession(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ raw, ok := auth.From(r)
if !ok {
h.fail(w, r, ErrRefused)
return
}
- next.ServeHTTP(w, identity.With(sessions.WithSession(r, sessions.Session{Subject: id.Address, Method: string(id.Method), AuthTime: id.AuthTime, At: id.At}), id))
- })
+ if string(raw.Method) != Method {
+ next.ServeHTTP(w, r)
+ return
+ }
+ sess, e := h.Store.Session(r.Context(), h.token(r), time.Now())
+ if e != nil {
+ h.fail(w, r, e)
+ return
+ }
+ id := auth.Identity{Address: sess.Subject, Method: signin.Method(sess.Method), AuthTime: sess.AuthTime, At: sess.At}
+ next.ServeHTTP(w, identity.With(sessions.WithSession(r, sess), id))
+ }))
}
diff --git a/internal/equipo/oficinaid/lookup_test.go b/internal/equipo/oficinaid/lookup_test.go
new file mode 100644
index 00000000..708a7ae2
--- /dev/null
+++ b/internal/equipo/oficinaid/lookup_test.go
@@ -0,0 +1,67 @@
+package oficinaid
+
+import (
+ "amadan.net/rastrillo/rastrillo/auth"
+ "context"
+ "database/sql"
+ "database/sql/driver"
+ "errors"
+ "io"
+ "net/http"
+ "net/http/httptest"
+ "testing"
+ "time"
+)
+
+// This driver fails the first identity lookup and succeeds on an unsafe retry.
+type transientDriver struct{}
+type transientConn struct{ queries int }
+type transientRows struct{ done bool }
+
+func (transientDriver) Open(string) (driver.Conn, error) { return &transientConn{}, nil }
+func (*transientConn) Prepare(string) (driver.Stmt, error) {
+ return nil, errors.New("unexpected prepare")
+}
+func (*transientConn) Close() error { return nil }
+func (*transientConn) Begin() (driver.Tx, error) { return nil, errors.New("unexpected transaction") }
+func (c *transientConn) QueryContext(context.Context, string, []driver.NamedValue) (driver.Rows, error) {
+ c.queries++
+ if c.queries == 1 {
+ return nil, errors.New("temporary lookup failure")
+ }
+ return &transientRows{}, nil
+}
+func (*transientRows) Columns() []string {
+ return []string{"subject", "method", "auth_time", "created_at", "expires_at"}
+}
+func (*transientRows) Close() error { return nil }
+func (r *transientRows) Next(out []driver.Value) error {
+ if r.done {
+ return io.EOF
+ }
+ r.done = true
+ now := time.Now().UTC()
+ copy(out, []driver.Value{"pr_aaaaaaaaaaaaaaaaaaaaaaaaaa", Method, now.Format(time.RFC3339), now.Format(time.RFC3339), now.Add(time.Hour).Format(time.RFC3339)})
+ return nil
+}
+func init() { sql.Register("oficina_id_transient_lookup", transientDriver{}) }
+func TestIDLookupFailureCannotRetryIntoLocalPath(t *testing.T) {
+ d, e := sql.Open("oficina_id_transient_lookup", "")
+ if e != nil {
+ t.Fatal(e)
+ }
+ defer d.Close()
+ d.SetMaxOpenConns(1)
+ authentication, e := auth.New(auth.Config{DB: d, Origin: "https://equipo.example", InstanceKey: "test-identity-cookie-key", ProofOff: true})
+ if e != nil {
+ t.Fatal(e)
+ }
+ h := &HTTP{Store: &Store{DB: d}, Auth: authentication, Render: func(w http.ResponseWriter, _ *http.Request, _ View, status int) { w.WriteHeader(status) }}
+ req := httptest.NewRequest("GET", "https://equipo.example/work", nil)
+ req.AddCookie(&http.Cookie{Name: authentication.SessionCookie(), Value: "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"})
+ next := false
+ h.Authenticate(http.HandlerFunc(func(http.ResponseWriter, *http.Request) { next = true })).ServeHTTP(httptest.NewRecorder(), req)
+ if next {
+ t.Fatal("failed identity lookup retried into unresolved Home subject")
+ }
+}