diff --git a/.github/mutation/mutants.json b/.github/mutation/mutants.json index 3018dc37..80bbefdb 100644 --- a/.github/mutation/mutants.json +++ b/.github/mutation/mutants.json @@ -5802,5 +5802,17 @@ ], "run": "^TestLocalIssuerKeysBoundIssuer$", "why": "LocalIssuerKeys reports its own issuer as its binding" + }, + { + "id": "sqlstore-ciba-unknown-handle-wraps-notfound", + "file": "sqlstore/backchannel.go", + "old": "return fmt.Errorf(\"sqlstore: %s: %w (%w)\", op, notFound(\"handle\"), storage.ErrBackchannelAuthenticationNotFound)", + "new": "return fmt.Errorf(\"sqlstore: %s: %w (%v)\", op, notFound(\"handle\"), storage.ErrBackchannelAuthenticationNotFound)", + "module": "sqlstore", + "packages": [ + "." + ], + "run": "^TestBackchannelDecisionsAndExpiry$", + "why": "sqlstore wraps ErrBackchannelAuthenticationNotFound for an unknown CIBA handle" } ] diff --git a/examples/linked-accounts/go.mod b/examples/linked-accounts/go.mod index eedcaceb..ce3b62ee 100644 --- a/examples/linked-accounts/go.mod +++ b/examples/linked-accounts/go.mod @@ -3,7 +3,7 @@ module github.com/idfoundry/fapigo/examples/linked-accounts go 1.26.11 require ( - github.com/idfoundry/fapigo v0.84.0 + github.com/idfoundry/fapigo v0.86.0 github.com/idfoundry/fapigo/examples/internal/demokit v0.0.0-00010101000000-000000000000 github.com/idfoundry/fapigo/sqlstore v0.0.0-00010101000000-000000000000 modernc.org/sqlite v1.62.0 diff --git a/sqlstore/backchannel.go b/sqlstore/backchannel.go index 06ea8d32..069b37ce 100644 --- a/sqlstore/backchannel.go +++ b/sqlstore/backchannel.go @@ -131,7 +131,7 @@ func (s *BackchannelAuthenticationStore) LookupBackchannelAuthentication(ctx con handleHash[:]).Scan(&key, &clientID, &stored, &bound.deliveryMode, &bound.pollInterval, &bound.expires) switch { case errors.Is(err, sql.ErrNoRows): - return storage.LookedUpBackchannelAuthentication{}, fmt.Errorf("sqlstore: look up backchannel authentication: %w", notFound("handle")) + return storage.LookedUpBackchannelAuthentication{}, unknownBackchannelHandle("look up backchannel authentication") case err != nil: return storage.LookedUpBackchannelAuthentication{}, unavailable("look up backchannel authentication", err) } @@ -194,6 +194,14 @@ func (s *BackchannelAuthenticationStore) DecideBackchannelAuthentication(ctx con return out, nil } +// unknownBackchannelHandle is op's error for a handle the store holds no +// request under: ErrNotFound, also wrapping +// storage.ErrBackchannelAuthenticationNotFound so the server can tell a +// request that is gone from any other refusal. +func unknownBackchannelHandle(op string) error { + return fmt.Errorf("sqlstore: %s: %w (%w)", op, notFound("handle"), storage.ErrBackchannelAuthenticationNotFound) +} + // whyUndecided is DecideBackchannelAuthentication's error for a handle // its UPDATE didn't decide: unknown, or already decided. func (s *BackchannelAuthenticationStore) whyUndecided(ctx context.Context, handleHash [32]byte) error { @@ -202,7 +210,7 @@ func (s *BackchannelAuthenticationStore) whyUndecided(ctx context.Context, handl return unavailable("decide backchannel authentication", err) } if n == 0 { - return fmt.Errorf("sqlstore: decide backchannel authentication: %w", notFound("handle")) + return unknownBackchannelHandle("decide backchannel authentication") } return errBackchannelDecided } diff --git a/sqlstore/backchannel_test.go b/sqlstore/backchannel_test.go index 5dc59799..e7ccf8e2 100644 --- a/sqlstore/backchannel_test.go +++ b/sqlstore/backchannel_test.go @@ -75,7 +75,8 @@ func TestBackchannelPollInterval(t *testing.T) { }) } -// A decision for an unknown handle is ErrNotFound; for a decided one, +// A decision for an unknown handle is ErrNotFound, and +// storage.ErrBackchannelAuthenticationNotFound; for a decided one, // storage.ErrBackchannelAuthenticationAlreadyDecided. Lookup keeps answering after the decision, and an approval // polled once is spent even after the request expires. func TestBackchannelDecisionsAndExpiry(t *testing.T) { @@ -84,13 +85,16 @@ func TestBackchannelDecisionsAndExpiry(t *testing.T) { b := freshDB(t, dialect).Backchannel() r := cibaRequest("r", "poll") must(t, b.CreateBackchannelAuthentication(ctx, r)) - if _, err := b.DecideBackchannelAuthentication(ctx, storage.DecideBackchannelAuthentication{HandleHash: sha256.Sum256([]byte("unknown")), Status: storage.BackchannelAuthenticationDenied}); !errors.Is(err, sqlstore.ErrNotFound) { - t.Fatalf("DecideBackchannelAuthentication(unknown) = %v, want ErrNotFound", err) + if _, err := b.DecideBackchannelAuthentication(ctx, storage.DecideBackchannelAuthentication{HandleHash: sha256.Sum256([]byte("unknown")), Status: storage.BackchannelAuthenticationDenied}); !errors.Is(err, sqlstore.ErrNotFound) || !errors.Is(err, storage.ErrBackchannelAuthenticationNotFound) { + t.Fatalf("DecideBackchannelAuthentication(unknown) = %v, want ErrNotFound and ErrBackchannelAuthenticationNotFound", err) + } + if _, err := b.LookupBackchannelAuthentication(ctx, sha256.Sum256([]byte("unknown"))); !errors.Is(err, sqlstore.ErrNotFound) || !errors.Is(err, storage.ErrBackchannelAuthenticationNotFound) { + t.Fatalf("LookupBackchannelAuthentication(unknown) = %v, want ErrNotFound and ErrBackchannelAuthenticationNotFound", err) } approve := storage.DecideBackchannelAuthentication{HandleHash: r.HandleHash, Status: storage.BackchannelAuthenticationApproved, Grant: secretGrant} _, err := b.DecideBackchannelAuthentication(ctx, approve) must(t, err) - if _, err := b.DecideBackchannelAuthentication(ctx, approve); !errors.Is(err, storage.ErrBackchannelAuthenticationAlreadyDecided) || errors.Is(err, sqlstore.ErrNotFound) || errors.Is(err, storage.ErrStoreUnavailable) { + if _, err := b.DecideBackchannelAuthentication(ctx, approve); !errors.Is(err, storage.ErrBackchannelAuthenticationAlreadyDecided) || errors.Is(err, sqlstore.ErrNotFound) || errors.Is(err, storage.ErrBackchannelAuthenticationNotFound) || errors.Is(err, storage.ErrStoreUnavailable) { t.Fatalf("second DecideBackchannelAuthentication = %v, want ErrBackchannelAuthenticationAlreadyDecided, not ErrNotFound", err) } if got, err := b.LookupBackchannelAuthentication(ctx, r.HandleHash); err != nil || got.ClientID != "c" || string(got.Request) != string(secretGrant) { diff --git a/sqlstore/go.mod b/sqlstore/go.mod index e41fecea..2bf0d8ee 100644 --- a/sqlstore/go.mod +++ b/sqlstore/go.mod @@ -3,7 +3,7 @@ module github.com/idfoundry/fapigo/sqlstore go 1.26.11 require ( - github.com/idfoundry/fapigo v0.84.0 + github.com/idfoundry/fapigo v0.86.0 github.com/jackc/pgx/v5 v5.13.0 modernc.org/sqlite v1.62.0 )