(
input: MintOAuthConnectionInput,
)
| 3314 | // by the OAuth service) + produce the connection's tools. Mirrors |
| 3315 | // `connectionsCreate`'s upsert + tool-production, stamping the OAuth columns. |
| 3316 | const mintOAuthConnection = ( |
| 3317 | input: MintOAuthConnectionInput, |
| 3318 | ): Effect.Effect<Connection, StorageFailure> => |
| 3319 | Effect.gen(function* () { |
| 3320 | const name = connectionIdentifier(String(input.name)); |
| 3321 | yield* requireUserSubject(input.owner); |
| 3322 | const integrationRow = yield* findIntegrationRow(input.integration); |
| 3323 | if (!integrationRow) { |
| 3324 | return yield* new StorageError({ |
| 3325 | message: `Integration not found: ${input.integration}`, |
| 3326 | cause: undefined, |
| 3327 | }); |
| 3328 | } |
| 3329 | const keys = yield* Effect.try({ |
| 3330 | try: () => ownedKeys(input.owner), |
| 3331 | catch: (cause) => storageFailureFromUnknown("invalid owner", cause), |
| 3332 | }); |
| 3333 | const now = new Date(); |
| 3334 | const ref: ConnectionRef = { |
| 3335 | owner: input.owner, |
| 3336 | integration: input.integration, |
| 3337 | name, |
| 3338 | }; |
| 3339 | // Label precedence: an explicit (user-chosen) label always wins; a |
| 3340 | // derived label (OIDC claims) only FILLS an empty slot. Like |
| 3341 | // `description` below, a reconnect or token refresh must not erase a |
| 3342 | // label the user curated. Resolved once, used by every write below. |
| 3343 | let identityLabel: string | null = null; |
| 3344 | // The core-owned per-connection state this mint writes WHOLESALE: |
| 3345 | // whatever a previous grant recorded (a stale reauth verdict, an old |
| 3346 | // missing-scope set) describes a credential that no longer exists. |
| 3347 | const nextProviderState = { |
| 3348 | ...(input.missingOAuthScopes === undefined || input.missingOAuthScopes.length === 0 |
| 3349 | ? {} |
| 3350 | : { missingOAuthScopes: input.missingOAuthScopes }), |
| 3351 | ...(input.enterpriseManaged === undefined |
| 3352 | ? {} |
| 3353 | : { [ENTERPRISE_MANAGED_PROVIDER_STATE_KEY]: input.enterpriseManaged }), |
| 3354 | }; |
| 3355 | // Null, not `{}`, when this grant records nothing: an empty object would |
| 3356 | // read back as "state exists and is empty" on a column whose absence is |
| 3357 | // what every reader tests. |
| 3358 | const providerState = |
| 3359 | Object.keys(nextProviderState).length === 0 ? null : nextProviderState; |
| 3360 | yield* transaction( |
| 3361 | Effect.gen(function* () { |
| 3362 | const existing = yield* findConnectionRow(ref); |
| 3363 | const existingLabel = existing?.identity_label?.trim() ? existing.identity_label : null; |
| 3364 | identityLabel = |
| 3365 | input.identityLabel ?? existingLabel ?? input.derivedIdentityLabel ?? null; |
| 3366 | const set: Record<string, unknown> = { |
| 3367 | template: String(input.template), |
| 3368 | provider: input.provider, |
| 3369 | item_ids: { [PRIMARY_INPUT_VARIABLE]: input.itemId }, |
| 3370 | identity_label: identityLabel, |
| 3371 | oauth_client: String(input.oauthClient), |
| 3372 | oauth_client_owner: input.oauthClientOwner, |
| 3373 | refresh_item_id: input.refreshItemId, |
no test coverage detected