(
input: OAuthCompleteInput,
)
| 1250 | // complete — redeem the session, exchange the code, mint the connection. |
| 1251 | // ----------------------------------------------------------------------- |
| 1252 | const complete = ( |
| 1253 | input: OAuthCompleteInput, |
| 1254 | ): Effect.Effect<Connection, OAuthCompleteError | OAuthSessionNotFoundError | StorageFailure> => |
| 1255 | Effect.gen(function* () { |
| 1256 | const sessionRow = yield* deps.fuma.use("oauth_session.findFirst", (db) => |
| 1257 | looseDb(db).findFirst("oauth_session", { |
| 1258 | where: (b: any) => b("state", "=", String(input.state)), |
| 1259 | }), |
| 1260 | ); |
| 1261 | if (!sessionRow) { |
| 1262 | return yield* new OAuthSessionNotFoundError({ state: input.state }); |
| 1263 | } |
| 1264 | const session = { |
| 1265 | owner: String(sessionRow.owner) as Owner, |
| 1266 | clientSlug: OAuthClientSlug.make(String(sessionRow.client_slug)), |
| 1267 | integration: IntegrationSlug.make(String(sessionRow.integration)), |
| 1268 | name: ConnectionName.make(String(sessionRow.name)), |
| 1269 | template: AuthTemplateSlug.make(String(sessionRow.template)), |
| 1270 | redirectUrl: String(sessionRow.redirect_url), |
| 1271 | pkceVerifier: sessionRow.pkce_verifier == null ? null : String(sessionRow.pkce_verifier), |
| 1272 | identityLabel: sessionRow.identity_label == null ? null : String(sessionRow.identity_label), |
| 1273 | expiresAt: Number(sessionRow.expires_at), |
| 1274 | // The scope set `start` requested (the integration's declared or |
| 1275 | // discovered scopes), persisted on the session payload. Drives the |
| 1276 | // recorded-scope fallback when the AS omits `scope`. Missing/legacy |
| 1277 | // payloads fall back to the client's scopes below. |
| 1278 | requestedScopes: requestedScopesFromPayload(sessionRow.payload), |
| 1279 | // The app's owner, recorded by `start` — reload the SAME app at |
| 1280 | // completion by explicit owner (no derivation). Defaults to the session |
| 1281 | // owner for same-owner connects. |
| 1282 | clientOwner: |
| 1283 | clientOwnerFromPayload(sessionRow.payload) ?? (String(sessionRow.owner) as Owner), |
| 1284 | }; |
| 1285 | |
| 1286 | // Expired sessions are not redeemable — drop + treat as not found. |
| 1287 | if (Number.isFinite(session.expiresAt) && session.expiresAt <= Date.now()) { |
| 1288 | yield* deleteSession(input.state); |
| 1289 | return yield* new OAuthSessionNotFoundError({ state: input.state }); |
| 1290 | } |
| 1291 | |
| 1292 | // Reload the SAME app `start` resolved, by its explicit recorded owner. |
| 1293 | const client = yield* loadClient(session.clientOwner, session.clientSlug); |
| 1294 | if (!client) { |
| 1295 | return yield* new OAuthCompleteError({ |
| 1296 | message: `OAuth client not found: ${session.clientSlug}`, |
| 1297 | restartRequired: true, |
| 1298 | }); |
| 1299 | } |
| 1300 | |
| 1301 | // The PKCE verifier is minted by `start` for every authorization_code |
| 1302 | // session. A null/missing one means a corrupt session row — exchanging |
| 1303 | // with an empty verifier would violate RFC 7636 and the AS would reject |
| 1304 | // it with an opaque error. Fail loudly + require a restart instead. |
| 1305 | if (session.pkceVerifier == null) { |
| 1306 | return yield* new OAuthCompleteError({ |
| 1307 | message: `OAuth session ${input.state} is missing its PKCE code verifier; restart the flow.`, |
| 1308 | restartRequired: true, |
| 1309 | }); |
nothing calls this directly
no test coverage detected