| 875 | // createClient — write the oauth_client row. |
| 876 | // ----------------------------------------------------------------------- |
| 877 | const createClient = ( |
| 878 | input: CreateOAuthClientInput, |
| 879 | ): Effect.Effect<OAuthClientSlug, OrgWriteDeniedError | StorageFailure> => |
| 880 | Effect.gen(function* () { |
| 881 | // The `first-party:` namespace is reserved for config-declared apps — a |
| 882 | // stored row under it would be shadowed by (or worse, impersonate) the |
| 883 | // host's own app. |
| 884 | if (isFirstPartyOAuthClientSlug(String(input.slug))) { |
| 885 | return yield* new StorageError({ |
| 886 | message: `OAuth client slug "${String(input.slug)}" uses the reserved first-party namespace.`, |
| 887 | cause: undefined, |
| 888 | }); |
| 889 | } |
| 890 | yield* deps.guardOrgWrite(input.owner); |
| 891 | yield* validateClientEndpoints(input, deps.endpointUrlPolicy); |
| 892 | if ( |
| 893 | input.tokenEndpointAuthMethod !== undefined && |
| 894 | input.tokenEndpointAuthMethod !== "body" && |
| 895 | input.clientSecret.length === 0 |
| 896 | ) { |
| 897 | return yield* new StorageError({ |
| 898 | message: "HTTP Basic token endpoint authentication requires a client secret.", |
| 899 | cause: undefined, |
| 900 | }); |
| 901 | } |
| 902 | const keys = yield* Effect.try({ |
| 903 | try: () => deps.ownedKeys(input.owner), |
| 904 | catch: (cause) => |
| 905 | new StorageError({ |
| 906 | message: "Cannot write oauth_client for owner without a subject", |
| 907 | cause, |
| 908 | }), |
| 909 | }); |
| 910 | const now = new Date(); |
| 911 | |
| 912 | // Resolve the out-of-band write up front, but do not mutate the provider |
| 913 | // until the database transaction commits. |
| 914 | let clientSecretItemIdValue: string | null = null; |
| 915 | let credentialWrite: CredentialWriteAttempt | null = null; |
| 916 | let secretWrite: CredentialWriteSnapshot | undefined; |
| 917 | if (input.clientSecret.length > 0) { |
| 918 | const provider = deps.defaultWritableProvider(); |
| 919 | if (!provider || !provider.set) { |
| 920 | return yield* new StorageError({ |
| 921 | message: |
| 922 | "No default writable credential provider is registered to store the OAuth client secret.", |
| 923 | cause: undefined, |
| 924 | }); |
| 925 | } |
| 926 | const attemptId = crypto.randomUUID(); |
| 927 | credentialWrite = makeCredentialWriteAttempt(deps.credentialWriteRuntimeId, attemptId); |
| 928 | clientSecretItemIdValue = credentialAttemptItemId( |
| 929 | clientSecretItemId(input.owner, input.slug), |
| 930 | attemptId, |
| 931 | ); |
| 932 | const itemId = ProviderItemId.make(clientSecretItemIdValue); |
| 933 | const [snapshot] = yield* snapshotCredentialWrites( |
| 934 | { ...provider, set: provider.set }, |