Expose transaction-owned linking proof stages for Equipo
2 files changed,
+148
−28
connect/linkstore/atomic_test.go+52 −0connect/linkstore/flows.go+96 −28
CI passed — run details
diff --git a/connect/linkstore/atomic_test.go b/connect/linkstore/atomic_test.go| new file mode 100644 |
| index 0000000..fe2ecec |
| --- /dev/null |
| +++ b/connect/linkstore/atomic_test.go |
| @@ -0,0 +1,52 @@ |
| +package linkstore |
| + |
| +import ( |
| + "testing" |
| + "time" |
| +) |
| + |
| +func TestFreshStartRejectsEarlierHomeAuthentication(t *testing.T) { |
| + s, _ := fixture(t) |
| + l := local(1) |
| + secret, err := s.StartFresh(ctx, l, "/", testNow) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if err = s.BindNonce(ctx, secret, l, nonce(1), testNow); err != nil { |
| + t.Fatal(err) |
| + } |
| + _, err = s.Complete(ctx, secret, l, Home{Issuer: issuer, Subject: "person", Nonce: nonce(1), AuthTime: testNow.Add(-time.Second)}, testNow) |
| + refused(t, err) |
| +} |
| +func TestCompleteOnRollbackRetainsInitiation(t *testing.T) { |
| + s, _ := fixture(t) |
| + l := local(1) |
| + secret := start(t, s, l, nonce(1)) |
| + tx, err := s.db.Begin() |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + _, err = s.CompleteOn(ctx, tx, secret, l, Home{Issuer: issuer, Subject: "person", Nonce: nonce(1), AuthTime: testNow}, testNow) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + tx.Rollback() |
| + if _, err = s.Complete(ctx, secret, l, Home{Issuer: issuer, Subject: "person", Nonce: nonce(1), AuthTime: testNow}, testNow); err != nil { |
| + t.Fatal("rollback consumed initiation", err) |
| + } |
| +} |
| + |
| +func TestStartFreshOnRollsBack(t *testing.T) { |
| + s, _ := fixture(t) |
| + tx, e := s.db.Begin() |
| + if e != nil { |
| + t.Fatal(e) |
| + } |
| + if _, e = s.StartFreshOn(ctx, tx, local(1), "/", testNow); e != nil { |
| + t.Fatal(e) |
| + } |
| + tx.Rollback() |
| + if flowCount(t, s) != 0 { |
| + t.Fatal("initiation escaped transaction") |
| + } |
| +} |
diff --git a/connect/linkstore/flows.go b/connect/linkstore/flows.go| index 567fa3c..7c29d8a 100644 |
| --- a/connect/linkstore/flows.go |
| +++ b/connect/linkstore/flows.go |
| @@ -14,6 +14,7 @@ import ( |
| ) |
| type flow struct { |
| + Started int64 `json:"started,omitempty"` |
| PersonBinding string `json:"person_binding"` |
| Version int `json:"v"` |
| PersonID int64 `json:"person"` |
| @@ -102,30 +103,57 @@ func (s *Store) currentBinding(ctx context.Context, tx *sql.Tx, id int64) (strin |
| // Start is called only by an explicit same-origin local-account link POST. |
| // The returned secret belongs in a host-only Secure/HttpOnly/Lax cookie. |
| func (s *Store) Start(ctx context.Context, local Local, returnPath string, now time.Time) (string, error) { |
| - tx, high, err := s.begin(ctx, now) |
| + return s.start(ctx, local, returnPath, now, false) |
| +} |
| + |
| +// StartFresh requires Home authentication at or after this initiation. |
| +func (s *Store) StartFresh(ctx context.Context, local Local, returnPath string, now time.Time) (string, error) { |
| + return s.start(ctx, local, returnPath, now, true) |
| +} |
| +func (s *Store) start(ctx context.Context, local Local, returnPath string, now time.Time, fresh bool) (string, error) { |
| + tx, _, err := s.begin(ctx, now) |
| if err != nil { |
| return "", err |
| } |
| defer tx.Rollback() |
| + result, err := s.startOn(ctx, tx, local, returnPath, now, fresh) |
| + if err != nil && !errors.Is(err, ErrRefused) { |
| + return "", err |
| + } |
| + if e := commit(tx); e != nil { |
| + return "", e |
| + } |
| + return result, err |
| +} |
| + |
| +// StartFreshOn creates fresh linking intent inside the caller's transaction. |
| +func (s *Store) StartFreshOn(ctx context.Context, tx *sql.Tx, local Local, returnPath string, now time.Time) (string, error) { |
| + return s.startOn(ctx, tx, local, returnPath, now, true) |
| +} |
| +func (s *Store) startOn(ctx context.Context, tx *sql.Tx, local Local, returnPath string, now time.Time, fresh bool) (string, error) { |
| + high, err := s.clockOn(ctx, tx, now) |
| + if err != nil { |
| + return "", err |
| + } |
| if err = sweep(ctx, tx, high); err != nil { |
| return "", err |
| } |
| if !validLocal(local, now.Unix(), high) { |
| - return "", refuse(tx) |
| + return "", ErrRefused |
| } |
| binding, err := s.currentBinding(ctx, tx, local.PersonID) |
| if err != nil { |
| return "", err |
| } |
| if binding == "" { |
| - return "", refuse(tx) |
| + return "", ErrRefused |
| } |
| var count int |
| if err := tx.QueryRowContext(ctx, "SELECT COUNT(*) FROM home_link_flows").Scan(&count); err != nil { |
| return "", ErrStorage |
| } |
| if count >= maxFlows { |
| - return "", refuse(tx) |
| + return "", ErrRefused |
| } |
| secret, err := newSecret() |
| if err != nil { |
| @@ -133,13 +161,13 @@ func (s *Store) Start(ctx context.Context, local Local, returnPath string, now t |
| } |
| expires := min(now.Unix()+maxAge, local.AuthTime.Unix()+maxAge) |
| if expires <= high { |
| - return "", refuse(tx) |
| + return "", ErrRefused |
| } |
| f := flow{PersonBinding: binding, Version: 1, PersonID: local.PersonID, SessionHash: hash(local.SessionToken), LocalAuth: local.AuthTime.Unix(), Issuer: s.issuer, ReturnPath: safePath(returnPath), Expires: expires} |
| - if err := s.put(ctx, tx, "init", hash(secret), f); err != nil { |
| - return "", err |
| + if fresh { |
| + f.Started = now.Unix() |
| } |
| - if err := commit(tx); err != nil { |
| + if err := s.put(ctx, tx, "init", hash(secret), f); err != nil { |
| return "", err |
| } |
| return secret, nil |
| @@ -184,31 +212,47 @@ func (s *Store) BindNonce(ctx context.Context, secret string, local Local, nonce |
| // Complete consumes the initiation after the shared consumer has consumed and |
| // verified the Home assertion. It keeps the local Docs session intact. |
| func (s *Store) Complete(ctx context.Context, secret string, local Local, home Home, now time.Time) (string, error) { |
| + tx, _, err := s.begin(ctx, now) |
| + if err != nil { |
| + return "", err |
| + } |
| + defer tx.Rollback() |
| + result, err := s.CompleteOn(ctx, tx, secret, local, home, now) |
| + if err != nil && !errors.Is(err, ErrRefused) { |
| + return "", err |
| + } |
| + if e := commit(tx); e != nil { |
| + return "", e |
| + } |
| + return result, err |
| +} |
| + |
| +// CompleteOn uses the caller-owned writer transaction; it never commits. |
| +func (s *Store) CompleteOn(ctx context.Context, tx *sql.Tx, secret string, local Local, home Home, now time.Time) (string, error) { |
| id, ok := secretHash(secret) |
| if !ok { |
| return "", ErrRefused |
| } |
| - tx, high, err := s.begin(ctx, now) |
| + high, err := s.clockOn(ctx, tx, now) |
| if err != nil { |
| return "", err |
| } |
| - defer tx.Rollback() |
| f, err := s.load(ctx, tx, "init", id, high, true) |
| if errors.Is(err, ErrRefused) { |
| - return "", refuse(tx) |
| + return "", ErrRefused |
| } |
| if err != nil { |
| return "", err |
| } |
| - if !s.matches(f, local, now.Unix(), high) || !handoff.ValidNonce(home.Nonce) || f.NonceHash != hash(home.Nonce) || home.Issuer != s.issuer || !validSubject(home.Subject) || !fresh(home.AuthTime.Unix(), now.Unix(), high) { |
| - return "", refuse(tx) |
| + if !s.matches(f, local, now.Unix(), high) || !handoff.ValidNonce(home.Nonce) || f.NonceHash != hash(home.Nonce) || home.Issuer != s.issuer || !validSubject(home.Subject) || !fresh(home.AuthTime.Unix(), now.Unix(), high) || home.AuthTime.Unix() < f.Started { |
| + return "", ErrRefused |
| } |
| binding, err := s.currentBinding(ctx, tx, local.PersonID) |
| if err != nil { |
| return "", err |
| } |
| if binding == "" || !hmac.Equal([]byte(binding), []byte(f.PersonBinding)) { |
| - return "", refuse(tx) |
| + return "", ErrRefused |
| } |
| pending, err := newSecret() |
| if err != nil { |
| @@ -217,14 +261,11 @@ func (s *Store) Complete(ctx context.Context, secret string, local Local, home H |
| f.Subject, f.HomeAuth = home.Subject, home.AuthTime.Unix() |
| f.Expires = min(f.Expires, f.HomeAuth+maxAge) |
| if f.Expires <= high { |
| - return "", refuse(tx) |
| + return "", ErrRefused |
| } |
| if err := s.put(ctx, tx, "pending", hash(pending), f); err != nil { |
| return "", err |
| } |
| - if err := commit(tx); err != nil { |
| - return "", err |
| - } |
| return pending, nil |
| } |
| @@ -277,31 +318,47 @@ func (s *Store) Pending(ctx context.Context, secret string, local Local, now tim |
| // mapping write commit together. A separate proof for the same mapping is |
| // idempotent; a reused cookie or conflicting mapping is consumed and refused. |
| func (s *Store) Confirm(ctx context.Context, secret string, local Local, now time.Time) (Pending, error) { |
| + tx, _, err := s.begin(ctx, now) |
| + if err != nil { |
| + return Pending{}, err |
| + } |
| + defer tx.Rollback() |
| + result, err := s.ConfirmOn(ctx, tx, secret, local, now) |
| + if err != nil && !errors.Is(err, ErrRefused) { |
| + return Pending{}, err |
| + } |
| + if e := commit(tx); e != nil { |
| + return Pending{}, e |
| + } |
| + return result, err |
| +} |
| + |
| +// ConfirmOn uses the caller-owned writer transaction; it never commits. |
| +func (s *Store) ConfirmOn(ctx context.Context, tx *sql.Tx, secret string, local Local, now time.Time) (Pending, error) { |
| id, ok := secretHash(secret) |
| if !ok { |
| return Pending{}, ErrRefused |
| } |
| - tx, high, err := s.begin(ctx, now) |
| + high, err := s.clockOn(ctx, tx, now) |
| if err != nil { |
| return Pending{}, err |
| } |
| - defer tx.Rollback() |
| f, err := s.load(ctx, tx, "pending", id, high, true) |
| if errors.Is(err, ErrRefused) { |
| - return Pending{}, refuse(tx) |
| + return Pending{}, ErrRefused |
| } |
| if err != nil { |
| return Pending{}, err |
| } |
| if !s.matches(f, local, now.Unix(), high) || !validPending(f, now.Unix(), high) { |
| - return Pending{}, refuse(tx) |
| + return Pending{}, ErrRefused |
| } |
| binding, err := s.currentBinding(ctx, tx, local.PersonID) |
| if err != nil { |
| return Pending{}, err |
| } |
| if binding == "" || !hmac.Equal([]byte(binding), []byte(f.PersonBinding)) { |
| - return Pending{}, refuse(tx) |
| + return Pending{}, ErrRefused |
| } |
| _, err = tx.ExecContext(ctx, "INSERT INTO home_identity_links (issuer, subject, person_id, person_binding) VALUES (?, ?, ?, ?) ON CONFLICT DO NOTHING", s.issuer, f.Subject, f.PersonID, f.PersonBinding) |
| if err != nil { |
| @@ -311,16 +368,27 @@ func (s *Store) Confirm(ctx context.Context, secret string, local Local, now tim |
| var storedBinding string |
| err = tx.QueryRowContext(ctx, "SELECT person_id, person_binding FROM home_identity_links WHERE issuer = ? AND subject = ?", s.issuer, f.Subject).Scan(&person, &storedBinding) |
| if errors.Is(err, sql.ErrNoRows) { |
| - return Pending{}, refuse(tx) |
| + return Pending{}, ErrRefused |
| } |
| if err != nil { |
| return Pending{}, ErrStorage |
| } |
| if person != f.PersonID || !hmac.Equal([]byte(storedBinding), []byte(f.PersonBinding)) { |
| - return Pending{}, refuse(tx) |
| - } |
| - if err := commit(tx); err != nil { |
| - return Pending{}, err |
| + return Pending{}, ErrRefused |
| } |
| return pendingView(f), nil |
| } |
| + |
| +func (s *Store) clockOn(ctx context.Context, tx *sql.Tx, now time.Time) (int64, error) { |
| + if _, err := tx.ExecContext(ctx, "UPDATE home_link_clock SET high_water=MAX(high_water,?) WHERE id=1", now.Unix()); err != nil { |
| + return 0, ErrStorage |
| + } |
| + var high int64 |
| + if err := tx.QueryRowContext(ctx, "SELECT high_water FROM home_link_clock WHERE id=1").Scan(&high); err != nil { |
| + return 0, ErrStorage |
| + } |
| + if now.Unix() < high-30 { |
| + return 0, ErrRefused |
| + } |
| + return high, nil |
| +} |