(
input: MintOAuthConnectionInput,
)
| 4052 | // purpose: reconnect/refresh re-mints the SAME connection, stamping the |
| 4053 | // OAuth columns. |
| 4054 | const mintOAuthConnection = ( |
| 4055 | input: MintOAuthConnectionInput, |
| 4056 | ): Effect.Effect<Connection, StorageFailure> => |
| 4057 | Effect.gen(function* () { |
| 4058 | const name = connectionIdentifier(String(input.name)); |
| 4059 | yield* requireUserSubject(input.owner); |
| 4060 | const integrationRow = yield* findIntegrationRow(input.integration); |
| 4061 | if (!integrationRow) { |
| 4062 | return yield* new StorageError({ |
| 4063 | message: `Integration not found: ${input.integration}`, |
| 4064 | cause: undefined, |
| 4065 | }); |
| 4066 | } |
| 4067 | const keys = yield* Effect.try({ |
| 4068 | try: () => ownedKeys(input.owner), |
| 4069 | catch: (cause) => storageFailureFromUnknown("invalid owner", cause), |
| 4070 | }); |
| 4071 | const now = new Date(); |
| 4072 | const ref: ConnectionRef = { |
| 4073 | owner: input.owner, |
| 4074 | integration: input.integration, |
| 4075 | name, |
| 4076 | }; |
| 4077 | // Label precedence: an explicit (user-chosen) label always wins; a |
| 4078 | // derived label (OIDC claims) only FILLS an empty slot. Like |
| 4079 | // `description` below, a reconnect or token refresh must not erase a |
| 4080 | // label the user curated. Resolved once, used by every write below. |
| 4081 | let identityLabel: string | null = null; |
| 4082 | // The core-owned per-connection state this mint writes WHOLESALE: |
| 4083 | // whatever a previous grant recorded (a stale reauth verdict, an old |
| 4084 | // missing-scope set) describes a credential that no longer exists. |
| 4085 | const nextProviderState = { |
| 4086 | ...(input.missingOAuthScopes === undefined || input.missingOAuthScopes.length === 0 |
| 4087 | ? {} |
| 4088 | : { missingOAuthScopes: input.missingOAuthScopes }), |
| 4089 | ...(input.enterpriseManaged === undefined |
| 4090 | ? {} |
| 4091 | : { [ENTERPRISE_MANAGED_PROVIDER_STATE_KEY]: input.enterpriseManaged }), |
| 4092 | }; |
| 4093 | // Null, not `{}`, when this grant records nothing: an empty object would |
| 4094 | // read back as "state exists and is empty" on a column whose absence is |
| 4095 | // what every reader tests. |
| 4096 | const providerState = |
| 4097 | Object.keys(nextProviderState).length === 0 ? null : nextProviderState; |
| 4098 | yield* transaction( |
| 4099 | Effect.gen(function* () { |
| 4100 | const existing = yield* findConnectionRow(ref); |
| 4101 | const existingLabel = existing?.identity_label?.trim() ? existing.identity_label : null; |
| 4102 | identityLabel = |
| 4103 | input.identityLabel ?? existingLabel ?? input.derivedIdentityLabel ?? null; |
| 4104 | const set: Record<string, unknown> = { |
| 4105 | template: String(input.template), |
| 4106 | provider: input.provider, |
| 4107 | item_ids: { [PRIMARY_INPUT_VARIABLE]: input.itemId }, |
| 4108 | identity_label: identityLabel, |
| 4109 | oauth_client: String(input.oauthClient), |
| 4110 | oauth_client_owner: input.oauthClientOwner, |
| 4111 | refresh_item_id: input.refreshItemId, |
no test coverage detected