(
input: OAuthCompleteInput,
options?: OAuthCompleteOptions,
)
| 2154 | // complete — redeem the session, exchange the code, mint the connection. |
| 2155 | // ----------------------------------------------------------------------- |
| 2156 | const complete = ( |
| 2157 | input: OAuthCompleteInput, |
| 2158 | options?: OAuthCompleteOptions, |
| 2159 | ): Effect.Effect< |
| 2160 | Connection, |
| 2161 | OAuthCompleteError | OAuthSessionNotFoundError | OrgWriteDeniedError | StorageFailure |
| 2162 | > => |
| 2163 | Effect.gen(function* () { |
| 2164 | const sessionRow = yield* deps.fuma.use("oauth_session.findFirst", (db) => |
| 2165 | looseDb(db).findFirst("oauth_session", { |
| 2166 | where: (b: any) => b("state", "=", String(input.state)), |
| 2167 | }), |
| 2168 | ); |
| 2169 | if (!sessionRow) { |
| 2170 | return yield* new OAuthSessionNotFoundError({ state: input.state }); |
| 2171 | } |
| 2172 | const session = { |
| 2173 | owner: String(sessionRow.owner) as Owner, |
| 2174 | clientSlug: OAuthClientSlug.make(String(sessionRow.client_slug)), |
| 2175 | integration: IntegrationSlug.make(String(sessionRow.integration)), |
| 2176 | name: ConnectionName.make(String(sessionRow.name)), |
| 2177 | template: AuthTemplateSlug.make(String(sessionRow.template)), |
| 2178 | redirectUrl: String(sessionRow.redirect_url), |
| 2179 | pkceVerifier: sessionRow.pkce_verifier == null ? null : String(sessionRow.pkce_verifier), |
| 2180 | identityLabel: sessionRow.identity_label == null ? null : String(sessionRow.identity_label), |
| 2181 | expiresAt: Number(sessionRow.expires_at), |
| 2182 | // The scope set `start` requested (the integration's declared or |
| 2183 | // discovered scopes), persisted on the session payload. Drives the |
| 2184 | // recorded-scope fallback when the AS omits `scope`. Missing/legacy |
| 2185 | // payloads fall back to the client's scopes below. |
| 2186 | requestedScopes: requestedScopesFromPayload(sessionRow.payload), |
| 2187 | // The app's owner, recorded by `start` — reload the SAME app at |
| 2188 | // completion by explicit owner (no derivation). Defaults to the session |
| 2189 | // owner for same-owner connects. |
| 2190 | clientOwner: |
| 2191 | clientOwnerFromPayload(sessionRow.payload) ?? (String(sessionRow.owner) as Owner), |
| 2192 | }; |
| 2193 | |
| 2194 | // Annotate as soon as the session resolves the flow's identity, so even |
| 2195 | // a completion that fails at the exchange still says WHOSE connect died. |
| 2196 | yield* Effect.annotateCurrentSpan({ |
| 2197 | "executor.integration": String(session.integration), |
| 2198 | "executor.connection": String(session.name), |
| 2199 | "executor.template": String(session.template), |
| 2200 | "executor.oauth.client": String(session.clientSlug), |
| 2201 | "executor.oauth.client_first_party": isFirstPartyOAuthClientSlug( |
| 2202 | String(session.clientSlug), |
| 2203 | ), |
| 2204 | }); |
| 2205 | |
| 2206 | // Expired sessions are not redeemable — drop + treat as not found. |
| 2207 | if (Number.isFinite(session.expiresAt) && session.expiresAt <= Date.now()) { |
| 2208 | yield* deleteSession(input.state); |
| 2209 | return yield* new OAuthSessionNotFoundError({ state: input.state }); |
| 2210 | } |
| 2211 | |
| 2212 | // Reload the SAME app `start` resolved, by its explicit recorded owner. |
| 2213 | const client = yield* loadClient(session.clientOwner, session.clientSlug); |
nothing calls this directly
no test coverage detected