(
input: OAuthCompleteInput,
)
| 1113 | // complete — redeem the session, exchange the code, mint the connection. |
| 1114 | // ----------------------------------------------------------------------- |
| 1115 | const complete = ( |
| 1116 | input: OAuthCompleteInput, |
| 1117 | ): Effect.Effect<Connection, OAuthCompleteError | OAuthSessionNotFoundError | StorageFailure> => |
| 1118 | Effect.gen(function* () { |
| 1119 | const sessionRow = yield* deps.fuma.use("oauth_session.findFirst", (db) => |
| 1120 | looseDb(db).findFirst("oauth_session", { |
| 1121 | where: (b: any) => b("state", "=", String(input.state)), |
| 1122 | }), |
| 1123 | ); |
| 1124 | if (!sessionRow) { |
| 1125 | return yield* new OAuthSessionNotFoundError({ state: input.state }); |
| 1126 | } |
| 1127 | const session = { |
| 1128 | owner: String(sessionRow.owner) as Owner, |
| 1129 | clientSlug: OAuthClientSlug.make(String(sessionRow.client_slug)), |
| 1130 | integration: IntegrationSlug.make(String(sessionRow.integration)), |
| 1131 | name: ConnectionName.make(String(sessionRow.name)), |
| 1132 | template: AuthTemplateSlug.make(String(sessionRow.template)), |
| 1133 | redirectUrl: String(sessionRow.redirect_url), |
| 1134 | pkceVerifier: sessionRow.pkce_verifier == null ? null : String(sessionRow.pkce_verifier), |
| 1135 | identityLabel: sessionRow.identity_label == null ? null : String(sessionRow.identity_label), |
| 1136 | expiresAt: Number(sessionRow.expires_at), |
| 1137 | // The scope set `start` requested (the integration's declared or |
| 1138 | // discovered scopes), persisted on the session payload. Drives the |
| 1139 | // recorded-scope fallback when the AS omits `scope`. Missing/legacy |
| 1140 | // payloads fall back to the client's scopes below. |
| 1141 | requestedScopes: requestedScopesFromPayload(sessionRow.payload), |
| 1142 | // The app's owner, recorded by `start` — reload the SAME app at |
| 1143 | // completion by explicit owner (no derivation). Defaults to the session |
| 1144 | // owner for same-owner connects. |
| 1145 | clientOwner: |
| 1146 | clientOwnerFromPayload(sessionRow.payload) ?? (String(sessionRow.owner) as Owner), |
| 1147 | }; |
| 1148 | |
| 1149 | // Expired sessions are not redeemable — drop + treat as not found. |
| 1150 | if (Number.isFinite(session.expiresAt) && session.expiresAt <= Date.now()) { |
| 1151 | yield* deleteSession(input.state); |
| 1152 | return yield* new OAuthSessionNotFoundError({ state: input.state }); |
| 1153 | } |
| 1154 | |
| 1155 | // Reload the SAME app `start` resolved, by its explicit recorded owner. |
| 1156 | const client = yield* loadClient(session.clientOwner, session.clientSlug); |
| 1157 | if (!client) { |
| 1158 | return yield* new OAuthCompleteError({ |
| 1159 | message: `OAuth client not found: ${session.clientSlug}`, |
| 1160 | restartRequired: true, |
| 1161 | }); |
| 1162 | } |
| 1163 | |
| 1164 | // The PKCE verifier is minted by `start` for every authorization_code |
| 1165 | // session. A null/missing one means a corrupt session row — exchanging |
| 1166 | // with an empty verifier would violate RFC 7636 and the AS would reject |
| 1167 | // it with an opaque error. Fail loudly + require a restart instead. |
| 1168 | if (session.pkceVerifier == null) { |
| 1169 | return yield* new OAuthCompleteError({ |
| 1170 | message: `OAuth session ${input.state} is missing its PKCE code verifier; restart the flow.`, |
| 1171 | restartRequired: true, |
| 1172 | }); |
nothing calls this directly
no test coverage detected