(
owner: Owner,
issuer: string | null,
)
| 745 | }; |
| 746 | |
| 747 | const dcrCandidatesForIssuer = ( |
| 748 | owner: Owner, |
| 749 | issuer: string | null, |
| 750 | ): Effect.Effect<readonly DcrReuseCandidate[], StorageFailure> => |
| 751 | deps.fuma |
| 752 | .use("oauth_client.findMany", (db) => |
| 753 | looseDb(db).findMany("oauth_client", { |
| 754 | where: (b: any) => b("owner", "=", owner), |
| 755 | }), |
| 756 | ) |
| 757 | .pipe( |
| 758 | Effect.map((rows) => { |
| 759 | const matches = rows.flatMap( |
| 760 | (row): readonly (DcrReuseCandidate & { readonly createdAt: number })[] => { |
| 761 | if (parseOAuthClientOrigin(row).kind !== "dynamic_client_registration") return []; |
| 762 | // A candidate matches only via a non-null, canonicalized stored |
| 763 | // issuer. The GC migration backfills origin_issuer on every |
| 764 | // surviving DCR row, so post-migration a null-issuer row is a |
| 765 | // transient (unmigrated) row; skipping it just mints one duplicate |
| 766 | // the migration then GCs, rather than reusing on a fuzzy token-host |
| 767 | // guess. |
| 768 | const rowIssuer = |
| 769 | row.origin_issuer == null ? null : canonicalIssuerUrl(String(row.origin_issuer)); |
| 770 | const issuerMatches = rowIssuer !== null && dcrIssuerMatches(rowIssuer, issuer); |
| 771 | if (!issuerMatches) return []; |
| 772 | return [ |
| 773 | { |
| 774 | slug: OAuthClientSlug.make(String(row.slug)), |
| 775 | resource: row.resource == null ? null : String(row.resource), |
| 776 | redirectUri: |
| 777 | row.origin_redirect_uri == null ? null : String(row.origin_redirect_uri), |
| 778 | createdAt: candidateCreatedAt(row.created_at), |
| 779 | }, |
| 780 | ]; |
| 781 | }, |
| 782 | ); |
| 783 | // Deterministic reuse order: oldest first, slug as a stable tiebreak |
| 784 | // when timestamps collide or are missing. Without this, which of |
| 785 | // several live duplicates sharing an (owner, issuer) gets reused is |
| 786 | // whatever order the storage backend returned rows in — the reuse |
| 787 | // pick must be stable across boots and backends. |
| 788 | return [...matches] |
| 789 | .sort( |
| 790 | (a, b) => |
| 791 | a.createdAt - b.createdAt || (a.slug < b.slug ? -1 : a.slug > b.slug ? 1 : 0), |
| 792 | ) |
| 793 | .map( |
| 794 | ({ slug, resource, redirectUri }): DcrReuseCandidate => ({ |
| 795 | slug, |
| 796 | resource, |
| 797 | redirectUri, |
| 798 | }), |
| 799 | ); |
| 800 | }), |
| 801 | ); |
| 802 | |
| 803 | const decideDcrClientReuse = ( |
| 804 | input: RegisterDynamicClientInput, |
no test coverage detected