Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | 233x 233x 233x 233x 3x 233x 40x 40x 7x 7x 6x 33x 3x 8x 3x 30x 34x 31x | /**
* Exchange of the single-use authorization code GitLab sends to the callback.
*/
import type { OAuthConfig } from './config';
import type { GitLabOAuthApp } from './oauth-app';
import type { AuthCodeFlowState, GitLabTokenResponse } from './types';
import { sessionStore } from './session-store';
import { keepIssuedTokens } from './issued-tokens';
import { exchangeGitLabAuthCode } from './gitlab-device-flow';
/** A late or competing grant is retained for cleanup, never used for another callback. */
export class AccountLinkFlowUnavailableError extends Error {
constructor() {
super('The account-link flow is no longer available for this grant');
}
}
/**
* Exchange GitLab's single-use code and keep the tokens with the flow before anything
* else can fail. When a concurrent callback already spent the code, its stored tokens are
* used instead.
*/
export async function exchangeFlowCode(
state: string,
flow: AuthCodeFlowState,
code: string,
config: OAuthConfig,
app: GitLabOAuthApp,
): Promise<GitLabTokenResponse> {
let tokens: GitLabTokenResponse;
try {
tokens = await exchangeGitLabAuthCode(code, flow.callbackUri, config, app);
} catch (error: unknown) {
const current = await sessionStore.getAuthCodeFlow(state);
if (current?.gitlabTokens) return current.gitlabTokens;
throw error;
}
if (flow.link) {
let active: boolean | undefined;
await keepIssuedTokens(async () => {
active = await sessionStore.cacheLinkedFlowTokens(flow, tokens);
});
if (active === false) throw new AccountLinkFlowUnavailableError();
} else {
await keepIssuedTokens(() =>
sessionStore.storeAuthCodeFlow(state, { ...flow, gitlabTokens: tokens }),
);
}
return tokens;
}
|