(
owner: Owner,
slug: OAuthClientSlug,
)
| 1180 | // op never cascades into connections). |
| 1181 | // ----------------------------------------------------------------------- |
| 1182 | const removeClient = ( |
| 1183 | owner: Owner, |
| 1184 | slug: OAuthClientSlug, |
| 1185 | ): Effect.Effect<void, OrgWriteDeniedError | StorageFailure> => |
| 1186 | Effect.gen(function* () { |
| 1187 | // Config-declared apps have no row to remove; removing one is an env |
| 1188 | // change on the host, not a storage operation. Fail loudly rather than |
| 1189 | // returning a success that changed nothing. |
| 1190 | if (isFirstPartyOAuthClientSlug(String(slug))) { |
| 1191 | return yield* new StorageError({ |
| 1192 | message: `OAuth client "${String(slug)}" is a first-party app declared in host config; it cannot be removed through this surface.`, |
| 1193 | cause: undefined, |
| 1194 | }); |
| 1195 | } |
| 1196 | yield* deps.guardOrgWrite(owner); |
| 1197 | // "Is there an app at (owner, slug) right now?" — asked twice, for two |
| 1198 | // different reasons. Before the delete it says whether this call removes |
| 1199 | // anything at all; after the commit it says whether the secret key still |
| 1200 | // belongs to the app this call removed. |
| 1201 | const findClientRow = deps.fuma.use("oauth_client.findFirst", (db) => |
| 1202 | looseDb(db).findFirst("oauth_client", { |
| 1203 | where: (b: any) => b.and(b("owner", "=", owner), b("slug", "=", String(slug))), |
| 1204 | }), |
| 1205 | ); |
| 1206 | |
| 1207 | const removedRow = yield* deps.fuma.transaction( |
| 1208 | Effect.gen(function* () { |
| 1209 | const existing = yield* findClientRow; |
| 1210 | yield* deps.fuma |
| 1211 | .use("oauth_client.delete", (db) => |
| 1212 | looseDb(db).deleteMany("oauth_client", { |
| 1213 | where: (b: any) => b.and(b("owner", "=", owner), b("slug", "=", String(slug))), |
| 1214 | }), |
| 1215 | ) |
| 1216 | .pipe(Effect.asVoid); |
| 1217 | return existing; |
| 1218 | }), |
| 1219 | ); |
| 1220 | // Nothing matched, so this call removed nothing and owns no secret. The |
| 1221 | // idempotent no-op and the cross-subject miss both land here, and both |
| 1222 | // used to queue a delete of a key they never had a claim on. |
| 1223 | if (!removedRow) return; |
| 1224 | // Best-effort: drop the secret from the provider so it isn't orphaned. |
| 1225 | // |
| 1226 | // Deferred to the outermost commit. This function opens no transaction of |
| 1227 | // its own, but a caller can wrap it in one — and `provider.delete` reaches |
| 1228 | // a store that does not roll back with it. An abort would then restore the |
| 1229 | // client row while its secret stayed destroyed, leaving a client that |
| 1230 | // looks configured and can never authenticate again. Orphaning a secret is |
| 1231 | // recoverable; deleting one that is still referenced is not, so the |
| 1232 | // deletion waits until the row's removal is durable. With no transaction |
| 1233 | // active `afterCommit` runs it immediately, which is the behaviour this |
| 1234 | // path already had. |
| 1235 | const provider = deps.defaultWritableProvider(); |
| 1236 | const dropSecret = provider?.delete; |
| 1237 | if (provider && dropSecret) { |
| 1238 | yield* afterCommit( |
| 1239 | Effect.gen(function* () { |
nothing calls this directly
no test coverage detected