(
input: RegisterDynamicClientInput,
issuer: string | null,
flowRedirectUri: string | null,
)
| 1110 | ); |
| 1111 | |
| 1112 | const decideDcrClientReuse = ( |
| 1113 | input: RegisterDynamicClientInput, |
| 1114 | issuer: string | null, |
| 1115 | flowRedirectUri: string | null, |
| 1116 | ): Effect.Effect< |
| 1117 | { |
| 1118 | readonly existingSlug: OAuthClientSlug | null; |
| 1119 | readonly registrationSlug: OAuthClientSlug; |
| 1120 | }, |
| 1121 | StorageFailure |
| 1122 | > => |
| 1123 | Effect.gen(function* () { |
| 1124 | const candidates = yield* dcrCandidatesForIssuer(input.owner, issuer); |
| 1125 | const resource = input.resource ?? null; |
| 1126 | // A candidate is reusable only when the callback it registered with the |
| 1127 | // AS still matches the current flow's callback — strict servers reject an |
| 1128 | // authorize request whose redirect_uri differs from the registration |
| 1129 | // (e.g. the callback origin changed after a sandbox was recreated while |
| 1130 | // the persisted client survived). A null stored redirect is a legacy row |
| 1131 | // predating the column: treated as matching so an upgrade doesn't |
| 1132 | // re-register every client whose callback never changed. A null FLOW |
| 1133 | // redirect has nothing to compare against, so it also reuses — the only |
| 1134 | // alternative is a fresh registration, which the missing-redirectUri |
| 1135 | // guard would fail. |
| 1136 | const redirectMatches = (candidate: DcrReuseCandidate): boolean => |
| 1137 | candidate.redirectUri === null || |
| 1138 | flowRedirectUri === null || |
| 1139 | candidate.redirectUri === flowRedirectUri; |
| 1140 | // A fresh registration must never take a slug an existing candidate |
| 1141 | // holds: `createClient` deletes any colliding (owner, slug) row first, |
| 1142 | // which would clobber a client that live connections still refresh |
| 1143 | // through (a redirect-mismatched client stays valid for refresh — the |
| 1144 | // token grant doesn't involve the redirect URI). |
| 1145 | const takenSlugs = new Set(candidates.map((client) => String(client.slug))); |
| 1146 | if (resource !== null) { |
| 1147 | const matchingResource = candidates.find((client) => client.resource === resource); |
| 1148 | if (matchingResource && redirectMatches(matchingResource)) { |
| 1149 | return { existingSlug: matchingResource.slug, registrationSlug: matchingResource.slug }; |
| 1150 | } |
| 1151 | const slug = uniqueDcrSlug( |
| 1152 | dcrClientSlug(issuer, candidates.length > 0 ? resource : null, input.slug), |
| 1153 | takenSlugs, |
| 1154 | ); |
| 1155 | return { |
| 1156 | existingSlug: null, |
| 1157 | registrationSlug: slug, |
| 1158 | }; |
| 1159 | } |
| 1160 | |
| 1161 | // Resource-less request: only reuse a resource-LESS candidate. A client |
| 1162 | // minted for a specific RFC 8707 resource must NOT be reused for a |
| 1163 | // resource-less flow (its tokens are bound to that resource), so when only |
| 1164 | // resource-scoped candidates exist we register a fresh resource-less client |
| 1165 | // rather than silently borrowing one (the old `?? candidates[0]` bug). |
| 1166 | const reusable = candidates.find( |
| 1167 | (client) => client.resource === null && redirectMatches(client), |
| 1168 | ); |
| 1169 | if (reusable) return { existingSlug: reusable.slug, registrationSlug: reusable.slug }; |
no test coverage detected