qbo_auth: discovery-документ, клиент API с разбором Fault, отключение с отзывом токена; анкета и требования Intuit
All checks were successful
ci / test (push) Successful in 39s
ci / deploy (push) Successful in 2m41s

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
2026-11-19 20:18:06 +03:00
parent 98930c5b40
commit 46d0fbab6d
12 changed files with 482 additions and 53 deletions

View File

@@ -24,13 +24,13 @@ export class TokenError extends Error {
}
}
export function authorizeUrl(p: { clientId: string; redirectUri: string; scope: string; state: string }): string {
export function authorizeUrl(p: { clientId: string; redirectUri: string; scope: string; state: string; base?: string }): string {
const q = new URLSearchParams({ client_id: p.clientId, response_type: "code", scope: p.scope, redirect_uri: p.redirectUri, state: p.state });
return `${AUTHORIZE_URL}?${q.toString()}`;
return `${p.base ?? AUTHORIZE_URL}?${q.toString()}`;
}
async function tokenCall(fetchFn: FetchFn, creds: { clientId: string; clientSecret: string }, params: Record<string, string>): Promise<TokenSet> {
const r = await fetchFn(TOKEN_URL, {
async function tokenCall(fetchFn: FetchFn, creds: { clientId: string; clientSecret: string }, params: Record<string, string>, tokenUrl: string): Promise<TokenSet> {
const r = await fetchFn(tokenUrl, {
method: "POST",
headers: {
Authorization: "Basic " + Buffer.from(`${creds.clientId}:${creds.clientSecret}`).toString("base64"),
@@ -51,7 +51,24 @@ async function tokenCall(fetchFn: FetchFn, creds: { clientId: string; clientSecr
return { accessToken: at, refreshToken: rt, expiresIn: Number(j.expires_in ?? 3600), refreshExpiresIn: Number(j.x_refresh_token_expires_in ?? 8640000) };
}
export const exchangeCode = (f: FetchFn, c: { clientId: string; clientSecret: string }, code: string, redirectUri: string) =>
tokenCall(f, c, { grant_type: "authorization_code", code, redirect_uri: redirectUri });
export const refreshTokens = (f: FetchFn, c: { clientId: string; clientSecret: string }, refreshToken: string) =>
tokenCall(f, c, { grant_type: "refresh_token", refresh_token: refreshToken });
export const exchangeCode = (f: FetchFn, c: { clientId: string; clientSecret: string }, code: string, redirectUri: string, tokenUrl: string = TOKEN_URL) =>
tokenCall(f, c, { grant_type: "authorization_code", code, redirect_uri: redirectUri }, tokenUrl);
export const refreshTokens = (f: FetchFn, c: { clientId: string; clientSecret: string }, refreshToken: string, tokenUrl: string = TOKEN_URL) =>
tokenCall(f, c, { grant_type: "refresh_token", refresh_token: refreshToken }, tokenUrl);
/** Отзыв токена у Intuit (revocation endpoint из discovery-документа). Успех: 200. */
export async function revokeToken(fetchFn: FetchFn, creds: { clientId: string; clientSecret: string }, token: string, revokeUrl: string): Promise<void> {
const r = await fetchFn(revokeUrl, {
method: "POST",
headers: {
Authorization: "Basic " + Buffer.from(`${creds.clientId}:${creds.clientSecret}`).toString("base64"),
"Content-Type": "application/json",
Accept: "application/json",
},
body: JSON.stringify({ token }),
});
if (!r.ok) {
const tid = intuitTid(r);
throw new Error(`Intuit revoke: ${r.status}${tid ? ` intuit_tid=${tid}` : ""}`);
}
}