(
input: CreateOAuthClientInput,
)
| 637 | // createClient — write the oauth_client row. |
| 638 | // ----------------------------------------------------------------------- |
| 639 | const createClient = ( |
| 640 | input: CreateOAuthClientInput, |
| 641 | ): Effect.Effect<OAuthClientSlug, StorageFailure> => |
| 642 | Effect.gen(function* () { |
| 643 | // The `first-party:` namespace is reserved for config-declared apps — a |
| 644 | // stored row under it would be shadowed by (or worse, impersonate) the |
| 645 | // host's own app. |
| 646 | if (isFirstPartyOAuthClientSlug(String(input.slug))) { |
| 647 | return yield* new StorageError({ |
| 648 | message: `OAuth client slug "${String(input.slug)}" uses the reserved first-party namespace.`, |
| 649 | cause: undefined, |
| 650 | }); |
| 651 | } |
| 652 | yield* validateClientEndpoints(input, deps.endpointUrlPolicy); |
| 653 | const keys = yield* Effect.try({ |
| 654 | try: () => deps.ownedKeys(input.owner), |
| 655 | catch: (cause) => |
| 656 | new StorageError({ |
| 657 | message: "Cannot write oauth_client for owner without a subject", |
| 658 | cause, |
| 659 | }), |
| 660 | }); |
| 661 | const now = new Date(); |
| 662 | |
| 663 | // Store the secret out-of-band in the default writable provider; the row |
| 664 | // keeps only its item id. A public/PKCE client (empty secret) stores null |
| 665 | // — there is no plaintext column to fall back to (the schema dropped it). |
| 666 | let clientSecretItemIdValue: string | null = null; |
| 667 | if (input.clientSecret.length > 0) { |
| 668 | const provider = deps.defaultWritableProvider(); |
| 669 | if (!provider || !provider.set) { |
| 670 | return yield* new StorageError({ |
| 671 | message: |
| 672 | "No default writable credential provider is registered to store the OAuth client secret.", |
| 673 | cause: undefined, |
| 674 | }); |
| 675 | } |
| 676 | clientSecretItemIdValue = clientSecretItemId(input.owner, input.slug); |
| 677 | yield* provider.set(ProviderItemId.make(clientSecretItemIdValue), input.clientSecret); |
| 678 | } |
| 679 | |
| 680 | yield* deps.fuma |
| 681 | .use("oauth_client.deleteExisting", (db) => |
| 682 | looseDb(db).deleteMany("oauth_client", { |
| 683 | where: (b: any) => |
| 684 | b.and(b("owner", "=", input.owner), b("slug", "=", String(input.slug))), |
| 685 | }), |
| 686 | ) |
| 687 | .pipe(Effect.catch(() => Effect.void)); |
| 688 | yield* deps.fuma.use("oauth_client.create", (db) => |
| 689 | looseDb(db).create("oauth_client", { |
| 690 | tenant: keys.tenant, |
| 691 | owner: keys.owner, |
| 692 | subject: keys.subject, |
| 693 | slug: String(input.slug), |
| 694 | authorization_url: input.authorizationUrl, |
| 695 | token_url: input.tokenUrl, |
| 696 | grant: input.grant, |
no test coverage detected