chore: refactor simplify gog token extraction
This commit is contained in:
parent
a7bfe30d88
commit
ec4088c76c
@ -3,11 +3,6 @@ import fs from "node:fs";
|
|||||||
import os from "node:os";
|
import os from "node:os";
|
||||||
import path from "node:path";
|
import path from "node:path";
|
||||||
|
|
||||||
type GogTokenEntry = {
|
|
||||||
account?: string;
|
|
||||||
refreshToken: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
const tokenCache = new Map<string, string>();
|
const tokenCache = new Map<string, string>();
|
||||||
|
|
||||||
function resolveWildcardJsonFile(
|
function resolveWildcardJsonFile(
|
||||||
@ -106,20 +101,8 @@ export function resolveGogCredentialsFile(params: {
|
|||||||
return resolveGogJsonFile(params, "credentials");
|
return resolveGogJsonFile(params, "credentials");
|
||||||
}
|
}
|
||||||
|
|
||||||
function looksLikeRefreshToken(token: string): boolean {
|
function extractRefreshToken(value: unknown): string | null {
|
||||||
const trimmed = token.trim();
|
if (!value || typeof value !== "object") return null;
|
||||||
if (!trimmed) return false;
|
|
||||||
if (trimmed.startsWith("ya29.")) return false;
|
|
||||||
if (trimmed.startsWith("1//")) return true;
|
|
||||||
return trimmed.length > 30;
|
|
||||||
}
|
|
||||||
|
|
||||||
function collectTokens(value: unknown, out: GogTokenEntry[]) {
|
|
||||||
if (!value || typeof value !== "object") return;
|
|
||||||
if (Array.isArray(value)) {
|
|
||||||
for (const entry of value) collectTokens(entry, out);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
const record = value as Record<string, unknown>;
|
const record = value as Record<string, unknown>;
|
||||||
const refreshToken =
|
const refreshToken =
|
||||||
typeof record.refresh_token === "string"
|
typeof record.refresh_token === "string"
|
||||||
@ -127,20 +110,11 @@ function collectTokens(value: unknown, out: GogTokenEntry[]) {
|
|||||||
: typeof record.refreshToken === "string"
|
: typeof record.refreshToken === "string"
|
||||||
? record.refreshToken
|
? record.refreshToken
|
||||||
: undefined;
|
: undefined;
|
||||||
if (refreshToken && looksLikeRefreshToken(refreshToken)) {
|
const trimmed = refreshToken?.trim();
|
||||||
const account =
|
if (!trimmed) return null;
|
||||||
typeof record.email === "string"
|
if (trimmed.startsWith("ya29.")) return null;
|
||||||
? record.email
|
if (trimmed.startsWith("1//")) return trimmed;
|
||||||
: typeof record.account === "string"
|
return trimmed.length > 30 ? trimmed : null;
|
||||||
? record.account
|
|
||||||
: typeof record.user === "string"
|
|
||||||
? record.user
|
|
||||||
: undefined;
|
|
||||||
out.push({ account, refreshToken });
|
|
||||||
}
|
|
||||||
for (const entry of Object.values(record)) {
|
|
||||||
collectTokens(entry, out);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function parseTokenEmails(value: unknown): string[] {
|
function parseTokenEmails(value: unknown): string[] {
|
||||||
@ -234,27 +208,18 @@ export function readGogRefreshTokenSync(params: {
|
|||||||
env,
|
env,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
const parsed = readJsonFile(outPath);
|
||||||
|
const token = extractRefreshToken(parsed);
|
||||||
|
if (!token) return null;
|
||||||
|
tokenCache.set(cacheKey, token);
|
||||||
|
return token;
|
||||||
} catch {
|
} catch {
|
||||||
|
return null;
|
||||||
|
} finally {
|
||||||
try {
|
try {
|
||||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
fs.rmSync(tmpDir, { recursive: true, force: true });
|
||||||
} catch {
|
} catch {
|
||||||
// ignore cleanup errors
|
// ignore cleanup errors
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
const parsed = readJsonFile(outPath);
|
|
||||||
try {
|
|
||||||
fs.rmSync(tmpDir, { recursive: true, force: true });
|
|
||||||
} catch {
|
|
||||||
// ignore cleanup errors
|
|
||||||
}
|
|
||||||
|
|
||||||
const tokens: GogTokenEntry[] = [];
|
|
||||||
if (parsed) collectTokens(parsed, tokens);
|
|
||||||
const token = tokens[0]?.refreshToken?.trim();
|
|
||||||
if (!token) return null;
|
|
||||||
|
|
||||||
tokenCache.set(cacheKey, token);
|
|
||||||
return token;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user