(
input: MintOAuthConnectionInput,
)
| 3531 | // by the OAuth service) + produce the connection's tools. Mirrors |
| 3532 | // `connectionsCreate`'s upsert + tool-production, stamping the OAuth columns. |
| 3533 | const mintOAuthConnection = ( |
| 3534 | input: MintOAuthConnectionInput, |
| 3535 | ): Effect.Effect<Connection, StorageFailure> => |
| 3536 | Effect.gen(function* () { |
| 3537 | const name = connectionIdentifier(String(input.name)); |
| 3538 | yield* requireUserSubject(input.owner); |
| 3539 | const integrationRow = yield* findIntegrationRow(input.integration); |
| 3540 | if (!integrationRow) { |
| 3541 | return yield* new StorageError({ |
| 3542 | message: `Integration not found: ${input.integration}`, |
| 3543 | cause: undefined, |
| 3544 | }); |
| 3545 | } |
| 3546 | const keys = yield* Effect.try({ |
| 3547 | try: () => ownedKeys(input.owner), |
| 3548 | catch: (cause) => storageFailureFromUnknown("invalid owner", cause), |
| 3549 | }); |
| 3550 | const now = new Date(); |
| 3551 | const ref: ConnectionRef = { |
| 3552 | owner: input.owner, |
| 3553 | integration: input.integration, |
| 3554 | name, |
| 3555 | }; |
| 3556 | // Label precedence: an explicit (user-chosen) label always wins; a |
| 3557 | // derived label (OIDC claims) only FILLS an empty slot. Like |
| 3558 | // `description` below, a reconnect or token refresh must not erase a |
| 3559 | // label the user curated. Resolved once, used by every write below. |
| 3560 | let identityLabel: string | null = null; |
| 3561 | // The core-owned per-connection state this mint writes WHOLESALE: |
| 3562 | // whatever a previous grant recorded (a stale reauth verdict, an old |
| 3563 | // missing-scope set) describes a credential that no longer exists. |
| 3564 | const nextProviderState = { |
| 3565 | ...(input.missingOAuthScopes === undefined || input.missingOAuthScopes.length === 0 |
| 3566 | ? {} |
| 3567 | : { missingOAuthScopes: input.missingOAuthScopes }), |
| 3568 | ...(input.enterpriseManaged === undefined |
| 3569 | ? {} |
| 3570 | : { [ENTERPRISE_MANAGED_PROVIDER_STATE_KEY]: input.enterpriseManaged }), |
| 3571 | }; |
| 3572 | // Null, not `{}`, when this grant records nothing: an empty object would |
| 3573 | // read back as "state exists and is empty" on a column whose absence is |
| 3574 | // what every reader tests. |
| 3575 | const providerState = |
| 3576 | Object.keys(nextProviderState).length === 0 ? null : nextProviderState; |
| 3577 | yield* transaction( |
| 3578 | Effect.gen(function* () { |
| 3579 | const existing = yield* findConnectionRow(ref); |
| 3580 | const existingLabel = existing?.identity_label?.trim() ? existing.identity_label : null; |
| 3581 | identityLabel = |
| 3582 | input.identityLabel ?? existingLabel ?? input.derivedIdentityLabel ?? null; |
| 3583 | const set: Record<string, unknown> = { |
| 3584 | template: String(input.template), |
| 3585 | provider: input.provider, |
| 3586 | item_ids: { [PRIMARY_INPUT_VARIABLE]: input.itemId }, |
| 3587 | identity_label: identityLabel, |
| 3588 | oauth_client: String(input.oauthClient), |
| 3589 | oauth_client_owner: input.oauthClientOwner, |
| 3590 | refresh_item_id: input.refreshItemId, |
no test coverage detected