(
row: ConnectionRow,
provider: CredentialProvider,
)
| 1502 | |
| 1503 | // Perform the actual refresh-token grant and persist the rotated material. |
| 1504 | const performTokenRefresh = ( |
| 1505 | row: ConnectionRow, |
| 1506 | provider: CredentialProvider, |
| 1507 | ): Effect.Effect<string | null, StorageFailure | CredentialResolutionError> => |
| 1508 | Effect.gen(function* () { |
| 1509 | const owner = row.owner as Owner; |
| 1510 | const reauth = (message: string): CredentialResolutionError => |
| 1511 | new CredentialResolutionError({ |
| 1512 | owner, |
| 1513 | integration: IntegrationSlug.make(row.integration), |
| 1514 | name: ConnectionName.make(row.name), |
| 1515 | message, |
| 1516 | reauthRequired: true, |
| 1517 | }); |
| 1518 | |
| 1519 | // Load the backing app by the owner STORED on the connection (a Personal |
| 1520 | // connection may be backed by a shared Workspace app) — no derivation. |
| 1521 | const clientOwner = (row.oauth_client_owner ?? row.owner) as Owner; |
| 1522 | const clientRow = yield* loadOAuthClientRow(clientOwner, String(row.oauth_client)); |
| 1523 | if (!clientRow) { |
| 1524 | return yield* reauth(`OAuth client "${row.oauth_client}" is no longer registered.`); |
| 1525 | } |
| 1526 | |
| 1527 | // The secret is stored in the provider (a vault item id), not inline. |
| 1528 | const clientSecret = clientRow.client_secret_item_id |
| 1529 | ? ((yield* provider.get(ProviderItemId.make(String(clientRow.client_secret_item_id)))) ?? |
| 1530 | "") |
| 1531 | : ""; |
| 1532 | // Re-request the scopes this connection was GRANTED (RFC 6749 §6: a |
| 1533 | // refresh must not exceed the originally-granted scope). Empty → omit |
| 1534 | // the param, which the AS treats as "same scopes as granted". |
| 1535 | const grantedScopes = row.oauth_scope |
| 1536 | ? String(row.oauth_scope).split(/\s+/).filter(Boolean) |
| 1537 | : []; |
| 1538 | |
| 1539 | // Refresh against the region the code was redeemed at when one was |
| 1540 | // recorded at connect time (multi-site providers like Datadog), else |
| 1541 | // the oauth_client's configured token endpoint. |
| 1542 | const tokenUrl = row.oauth_token_url |
| 1543 | ? String(row.oauth_token_url) |
| 1544 | : String(clientRow.token_url); |
| 1545 | |
| 1546 | // client_credentials (machine-to-machine) has NO refresh token — the |
| 1547 | // token is RE-MINTED from the client id/secret. The authorization_code |
| 1548 | // path below needs a stored refresh token. Branching on grant here is |
| 1549 | // what keeps a client_credentials connection (e.g. DealCloud) from |
| 1550 | // demanding a re-auth on a credential that has no human to re-auth. |
| 1551 | const token = |
| 1552 | String(clientRow.grant) === "client_credentials" |
| 1553 | ? yield* exchangeClientCredentials({ |
| 1554 | tokenUrl, |
| 1555 | clientId: String(clientRow.client_id), |
| 1556 | clientSecret, |
| 1557 | scopes: grantedScopes, |
| 1558 | resource: clientRow.resource ? String(clientRow.resource) : undefined, |
| 1559 | endpointUrlPolicy: config.oauthEndpointUrlPolicy, |
| 1560 | fetch: config.fetch, |
| 1561 | }).pipe( |
no test coverage detected