| 928 | // createClient — write the oauth_client row. |
| 929 | // ----------------------------------------------------------------------- |
| 930 | const createClient = ( |
| 931 | input: CreateOAuthClientInput, |
| 932 | ): Effect.Effect<OAuthClientSlug, OrgWriteDeniedError | StorageFailure> => |
| 933 | Effect.gen(function* () { |
| 934 | // The `first-party:` namespace is reserved for config-declared apps — a |
| 935 | // stored row under it would be shadowed by (or worse, impersonate) the |
| 936 | // host's own app. |
| 937 | if (isFirstPartyOAuthClientSlug(String(input.slug))) { |
| 938 | return yield* new StorageError({ |
| 939 | message: `OAuth client slug "${String(input.slug)}" uses the reserved first-party namespace.`, |
| 940 | cause: undefined, |
| 941 | }); |
| 942 | } |
| 943 | yield* deps.guardOrgWrite(input.owner); |
| 944 | yield* validateClientEndpoints(input, deps.endpointUrlPolicy); |
| 945 | if ( |
| 946 | input.tokenEndpointAuthMethod !== undefined && |
| 947 | input.tokenEndpointAuthMethod !== "body" && |
| 948 | input.clientSecret.length === 0 |
| 949 | ) { |
| 950 | return yield* new StorageError({ |
| 951 | message: "HTTP Basic token endpoint authentication requires a client secret.", |
| 952 | cause: undefined, |
| 953 | }); |
| 954 | } |
| 955 | const keys = yield* Effect.try({ |
| 956 | try: () => deps.ownedKeys(input.owner), |
| 957 | catch: (cause) => |
| 958 | new StorageError({ |
| 959 | message: "Cannot write oauth_client for owner without a subject", |
| 960 | cause, |
| 961 | }), |
| 962 | }); |
| 963 | const now = new Date(); |
| 964 | |
| 965 | // Resolve the out-of-band write up front, but do not mutate the provider |
| 966 | // until the database transaction commits. |
| 967 | let clientSecretItemIdValue: string | null = null; |
| 968 | let credentialWrite: CredentialWriteAttempt | null = null; |
| 969 | let secretWrite: CredentialWriteSnapshot | undefined; |
| 970 | if (input.clientSecret.length > 0) { |
| 971 | const provider = deps.defaultWritableProvider(); |
| 972 | if (!provider || !provider.set) { |
| 973 | return yield* new StorageError({ |
| 974 | message: |
| 975 | "No default writable credential provider is registered to store the OAuth client secret.", |
| 976 | cause: undefined, |
| 977 | }); |
| 978 | } |
| 979 | const attemptId = crypto.randomUUID(); |
| 980 | credentialWrite = makeCredentialWriteAttempt(deps.credentialWriteRuntimeId, attemptId); |
| 981 | clientSecretItemIdValue = credentialAttemptItemId( |
| 982 | clientSecretItemId(input.owner, input.slug), |
| 983 | attemptId, |
| 984 | ); |
| 985 | const itemId = ProviderItemId.make(clientSecretItemIdValue); |
| 986 | const [snapshot] = yield* snapshotCredentialWrites( |
| 987 | { ...provider, set: provider.set }, |