Files
OmniRoute/open-sse/services/tokenRefresh/providers/gitlabDuo.ts
Diego Rodrigues de Sa e Souza 5e96d52544 refactor(sse): extract per-provider token-refresh functions from tokenRefresh.ts (#7817)
Split the 13 export async function refresh<Provider>Token() implementations
out of open-sse/services/tokenRefresh.ts (2249 lines, frozen) into their own
co-located leaf modules under open-sse/services/tokenRefresh/providers/, with
shared OAuth-error classification (extractOAuthErrorCode/readRefreshErrorBody)
and the form-body builder moved to tokenRefresh/shared.ts. tokenRefresh.ts now
keeps only the cross-provider orchestrator (refreshAccessToken dispatcher,
getAccessToken dedup/mutex/CAS-guard layers, refreshWithRetry circuit
breaker) and re-exports every previously-public symbol so no importer needed
to change (open-sse/index.ts, executors, src/sse/services/tokenRefresh.ts,
tests). File shrinks 2249 -> 989 lines; every new leaf is well under the
800-line cap.

Pure move, zero behavior change — verified via typecheck:core, check:cycles,
eslint (0 new any), file-size/complexity/cognitive-complexity ratchets (all
under baseline), and the full token-refresh test surface (token-refresh-*,
oauth-providers-*, executor-{kiro,github,gitlab,codex,antigravity,default-base},
token-health-check*, kiro-external-idp, codebuddy-cn-provider,
windsurf-devin-executors, grok-cli-*, agy-*, xai/zed-oauth-provider,
deepseek-web-autorefresh, ghe-copilot). Updated the 8 structural (text-based)
assertions in oauth-providers-error-handling.test.ts that read function
bodies straight from tokenRefresh.ts to read from the new per-provider files
instead — same assertions, new location.

The provider-module split was originally proposed by KooshaPari in PR #7338
against a base too old to merge cleanly; redone here from scratch against the
current release/v3.8.49 tip, credit preserved via co-authorship.

Co-authored-by: KooshaPari <KooshaPari@users.noreply.github.com>
2026-07-20 00:05:25 -03:00

104 lines
3.4 KiB
TypeScript

// @ts-nocheck
// Extracted from open-sse/services/tokenRefresh.ts — see ../shared.ts for
// provenance notes (ported idea from KooshaPari's PR #7338, redone on tip).
import { PROVIDERS } from "../../../config/constants.ts";
import { runWithProxyContext } from "../../../utils/proxyFetch.ts";
import { buildGitLabOAuthEndpoints, resolveGitLabOAuthBaseUrl } from "@/lib/oauth/gitlab";
import { buildFormParams, type RefreshLogger } from "../shared.ts";
/**
* Specialized refresh for GitLab Duo OAuth tokens.
* Token URL is instance-specific; resolves from providerSpecificData.baseUrl.
* Uses PKCE authorization_code flow initially but refresh_token grant does NOT
* require code_verifier — only client_id + refresh_token.
* On invalid_grant (revoked/expired refresh token) returns the unrecoverable sentinel.
*/
export async function refreshGitLabDuoToken(
refreshToken: string,
providerSpecificData: Record<string, unknown> | null | undefined,
log: RefreshLogger,
proxyConfig: unknown = null
) {
if (!refreshToken) {
log?.warn?.("TOKEN_REFRESH", "No refresh token for GitLab Duo");
return null;
}
const baseUrl = resolveGitLabOAuthBaseUrl(providerSpecificData);
const endpoints = buildGitLabOAuthEndpoints(baseUrl);
const tokenUrl = endpoints.tokenUrl;
// client_id from providerSpecificData (stored at login) or fall back to PROVIDERS config
const clientId =
(providerSpecificData?.clientId as string) ||
PROVIDERS["gitlab-duo"]?.clientId ||
process.env.GITLAB_DUO_OAUTH_CLIENT_ID ||
process.env.GITLAB_OAUTH_CLIENT_ID ||
"";
try {
const response = await runWithProxyContext(proxyConfig, () =>
fetch(tokenUrl, {
method: "POST",
headers: {
"Content-Type": "application/x-www-form-urlencoded",
Accept: "application/json",
},
body: buildFormParams({
grant_type: "refresh_token",
refresh_token: refreshToken,
client_id: clientId,
}),
})
);
if (!response.ok) {
const errorText = await response.text();
// Detect unrecoverable token — GitLab returns standard OAuth2 error codes.
try {
const errorBody = JSON.parse(errorText);
const errorCode = errorBody.error;
if (errorCode === "invalid_grant" || errorCode === "invalid_request") {
log?.error?.(
"TOKEN_REFRESH",
"GitLab Duo refresh token invalid. Re-authentication required.",
{
errorCode,
}
);
return { error: "unrecoverable_refresh_error", code: errorCode };
}
} catch {
// not JSON — fall through
}
log?.error?.("TOKEN_REFRESH", "Failed to refresh GitLab Duo token", {
status: response.status,
error: errorText.slice(0, 200),
});
return null;
}
const tokens = await response.json();
log?.info?.("TOKEN_REFRESH", "Successfully refreshed GitLab Duo token", {
hasNewAccessToken: !!tokens.access_token,
hasNewRefreshToken: !!tokens.refresh_token,
expiresIn: tokens.expires_in,
});
return {
accessToken: tokens.access_token,
refreshToken: tokens.refresh_token || refreshToken,
expiresIn: tokens.expires_in,
};
} catch (error) {
log?.error?.(
"TOKEN_REFRESH",
`Network error refreshing GitLab Duo token: ${error instanceof Error ? error.message : String(error)}`
);
return null;
}
}