| 844 | // createClient — write the oauth_client row. |
| 845 | // ----------------------------------------------------------------------- |
| 846 | const createClient = ( |
| 847 | input: CreateOAuthClientInput, |
| 848 | ): Effect.Effect<OAuthClientSlug, StorageFailure> => |
| 849 | Effect.gen(function* () { |
| 850 | // The `first-party:` namespace is reserved for config-declared apps — a |
| 851 | // stored row under it would be shadowed by (or worse, impersonate) the |
| 852 | // host's own app. |
| 853 | if (isFirstPartyOAuthClientSlug(String(input.slug))) { |
| 854 | return yield* new StorageError({ |
| 855 | message: `OAuth client slug "${String(input.slug)}" uses the reserved first-party namespace.`, |
| 856 | cause: undefined, |
| 857 | }); |
| 858 | } |
| 859 | yield* validateClientEndpoints(input, deps.endpointUrlPolicy); |
| 860 | const keys = yield* Effect.try({ |
| 861 | try: () => deps.ownedKeys(input.owner), |
| 862 | catch: (cause) => |
| 863 | new StorageError({ |
| 864 | message: "Cannot write oauth_client for owner without a subject", |
| 865 | cause, |
| 866 | }), |
| 867 | }); |
| 868 | const now = new Date(); |
| 869 | |
| 870 | // Store the secret out-of-band in the default writable provider; the row |
| 871 | // keeps only its item id. A public/PKCE client (empty secret) stores null |
| 872 | // — there is no plaintext column to fall back to (the schema dropped it). |
| 873 | let clientSecretItemIdValue: string | null = null; |
| 874 | if (input.clientSecret.length > 0) { |
| 875 | const provider = deps.defaultWritableProvider(); |
| 876 | if (!provider || !provider.set) { |
| 877 | return yield* new StorageError({ |
| 878 | message: |
| 879 | "No default writable credential provider is registered to store the OAuth client secret.", |
| 880 | cause: undefined, |
| 881 | }); |
| 882 | } |
| 883 | clientSecretItemIdValue = clientSecretItemId(input.owner, input.slug); |
| 884 | yield* provider.set(ProviderItemId.make(clientSecretItemIdValue), input.clientSecret); |
| 885 | } |
| 886 | |
| 887 | yield* deps.fuma |
| 888 | .use("oauth_client.deleteExisting", (db) => |
| 889 | looseDb(db).deleteMany("oauth_client", { |
| 890 | where: (b: any) => |
| 891 | b.and(b("owner", "=", input.owner), b("slug", "=", String(input.slug))), |
| 892 | }), |
| 893 | ) |
| 894 | .pipe(Effect.catch(() => Effect.void)); |
| 895 | yield* deps.fuma.use("oauth_client.create", (db) => |
| 896 | looseDb(db).create("oauth_client", { |
| 897 | tenant: keys.tenant, |
| 898 | owner: keys.owner, |
| 899 | subject: keys.subject, |
| 900 | slug: String(input.slug), |
| 901 | authorization_url: input.authorizationUrl, |
| 902 | token_url: input.tokenUrl, |
| 903 | grant: input.grant, |