Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/linked-accounts/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/idfoundry/fapigo/examples/linked-accounts
go 1.26.11

require (
github.com/idfoundry/fapigo v0.74.0
github.com/idfoundry/fapigo v0.77.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
Expand Down
20 changes: 20 additions & 0 deletions sqlstore/contract_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sqlstore_test
import (
"context"
"testing"
"time"

"github.com/idfoundry/fapigo/sqlstore"
"github.com/idfoundry/fapigo/storage"
Expand Down Expand Up @@ -84,6 +85,25 @@ func TestGrantRegistryContract(t *testing.T) {
})
}

// TestGrantRegistryListsByKeyAlone checks that a keyed listing reads
// only that entry, not the subject's other grants with it.
func TestGrantRegistryListsByKeyAlone(t *testing.T) {
forEachDialect(t, func(t *testing.T, dialect sqlstore.Dialect) {
r := freshDB(t, dialect).GrantRegistry()
ctx := context.Background()
expires := time.Now().Add(time.Hour)
for _, key := range []string{"g1", "g2"} {
if err := r.RecordGrant(ctx, storage.NewGrantEntry{Key: key, SubjectKey: "s1", ClientID: "c", Record: []byte(`{}`), ExpiresAt: expires}); err != nil {
t.Fatal(err)
}
}
entries, err := r.ListGrants(ctx, storage.GrantListing{SubjectKey: "s1", Now: time.Now(), Key: "g1"})
if err != nil || len(entries) != 1 || entries[0].Key != "g1" {
t.Fatalf("ListGrants(s1, key g1) = %+v, %v, want g1 alone", entries, err)
}
})
}

func TestBackchannelInboxContract(t *testing.T) {
forEachDialect(t, func(t *testing.T, dialect sqlstore.Dialect) {
storage.TestBackchannelInboxContract(t, func() storage.BackchannelInbox { return freshDB(t, dialect).BackchannelInbox() })
Expand Down
3 changes: 3 additions & 0 deletions sqlstore/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,9 @@ func checkUnstorableKeyReads(t *testing.T, d *sqlstore.DB, key string) {
if got, err := d.GrantRegistry().ListGrants(ctx, storage.GrantListing{SubjectKey: key, Now: time.Now()}); err != nil || len(got) != 0 {
t.Errorf("ListGrants(%q) = %v, %v, want none", key, got, err)
}
if got, err := d.GrantRegistry().ListGrants(ctx, storage.GrantListing{SubjectKey: "s", Key: key, Now: time.Now()}); err != nil || len(got) != 0 {
t.Errorf("ListGrants(Key: %q) = %v, %v, want none", key, got, err)
}
if got, err := d.BackchannelInbox().ListBackchannelRequests(ctx, storage.BackchannelInboxListing{SubjectKey: key, Now: time.Now()}); err != nil || len(got) != 0 {
t.Errorf("ListBackchannelRequests(%q) = %v, %v, want none", key, got, err)
}
Expand Down
2 changes: 1 addition & 1 deletion sqlstore/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ module github.com/idfoundry/fapigo/sqlstore
go 1.26.11

require (
github.com/idfoundry/fapigo v0.74.0
github.com/idfoundry/fapigo v0.77.0
github.com/jackc/pgx/v5 v5.12.0
modernc.org/sqlite v1.62.0
)
Expand Down
13 changes: 9 additions & 4 deletions sqlstore/grant_registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,18 @@ func (r grantRegistry) RecordGrant(ctx context.Context, entry storage.NewGrantEn
return nil
}

// ListGrants implements storage.GrantRegistry: one indexed read.
// ListGrants implements storage.GrantRegistry: one indexed read. With
// listing.Key set it reads only that entry, by its primary key, so the
// read doesn't grow with the subject's other grants.
func (r grantRegistry) ListGrants(ctx context.Context, listing storage.GrantListing) ([]storage.GrantEntry, error) {
if !validTextKey(listing.SubjectKey) {
if !validTextKey(listing.SubjectKey) || (listing.Key != "" && !validTextKey(listing.Key)) {
return nil, nil
}
rows, err := r.d.db.QueryContext(ctx, r.d.q("SELECT entry_key, client_id, record, expires_at FROM {grant_registry} WHERE subject_key = ? AND expires_at > ?"), // NOSONAR: go:S2077 — a constant query of this package's; q substitutes only the validated TablePrefix and placeholders.
listing.SubjectKey, nanos(listing.Now))
query, args := "SELECT entry_key, client_id, record, expires_at FROM {grant_registry} WHERE subject_key = ? AND expires_at > ?", []any{listing.SubjectKey, nanos(listing.Now)}
if listing.Key != "" {
query, args = "SELECT entry_key, client_id, record, expires_at FROM {grant_registry} WHERE entry_key = ? AND subject_key = ? AND expires_at > ?", []any{listing.Key, listing.SubjectKey, nanos(listing.Now)}
}
rows, err := r.d.db.QueryContext(ctx, r.d.q(query), args...) // NOSONAR: go:S2077 — one of two constant queries of this package's; q substitutes only the validated TablePrefix and placeholders.
if err != nil {
return nil, unavailable(opListGrants, err)
}
Expand Down
Loading