update problemu s hlavickami

This commit is contained in:
JiriUhlir
2026-07-20 15:27:53 +02:00
parent 8f7f8404c3
commit e87d4ce02b
+21 -2
View File
@@ -1024,12 +1024,25 @@ function applyCredentialHeaders(req: Request, body: GoogleRequestBody): void {
} }
async function applyServiceAccountAuthorization(req: Request, body: GoogleRequestBody): Promise<void> { async function applyServiceAccountAuthorization(req: Request, body: GoogleRequestBody): Promise<void> {
if (body.accessToken || body.accessTokenEnv || readBearerToken(req)) { const existingToken = body.accessToken || readEnvValue(body.accessTokenEnv) || readBearerToken(req);
if (existingToken) {
body.accessToken = existingToken;
return; return;
} }
if (body.accessTokenEnv) {
console.warn(`[google-service] accessTokenEnv "${body.accessTokenEnv}" is not set in the environment; falling back to other credentials.`);
}
const serviceAccountJson = readServiceAccountJson(req, body); const serviceAccountJson = readServiceAccountJson(req, body);
if (!serviceAccountJson) { if (!serviceAccountJson) {
if (!body.apiKey && !readEnvValue(body.apiKeyEnv) && !process.env.GOOGLE_API_KEY) {
throw new Error(
"No usable Google credential resolved. Provide an access token (Authorization: Bearer, accessToken, X-Google-Access-Token, or a populated accessTokenEnv), a service account JSON (serviceAccountJson, X-Google-Service-Account-Json, or a populated serviceAccountJsonEnv / GOOGLE_SERVICE_ACCOUNT_JSON), or an API key."
);
}
console.warn("[google-service] No access token or service account JSON resolved; calling Google with API key only.");
return; return;
} }
@@ -1048,13 +1061,19 @@ async function applyServiceAccountAuthorization(req: Request, body: GoogleReques
function readServiceAccountJson(req: Request, body?: GoogleRequestBody | ServiceAccountTokenBody): GoogleServiceAccountJson | undefined { function readServiceAccountJson(req: Request, body?: GoogleRequestBody | ServiceAccountTokenBody): GoogleServiceAccountJson | undefined {
const bodyValue = body && "serviceAccountJson" in body ? body.serviceAccountJson : undefined; const bodyValue = body && "serviceAccountJson" in body ? body.serviceAccountJson : undefined;
const envName = body && "serviceAccountJsonEnv" in body ? body.serviceAccountJsonEnv : undefined; const envName = body && "serviceAccountJsonEnv" in body ? body.serviceAccountJsonEnv : undefined;
const headerEnvName = req.header("x-google-service-account-json-env");
const raw = const raw =
bodyValue || bodyValue ||
req.header("x-google-service-account-json") || req.header("x-google-service-account-json") ||
readEnvValue(envName) || readEnvValue(envName) ||
readEnvValue(req.header("x-google-service-account-json-env")) || readEnvValue(headerEnvName) ||
process.env.GOOGLE_SERVICE_ACCOUNT_JSON; process.env.GOOGLE_SERVICE_ACCOUNT_JSON;
const requestedEnvName = envName || headerEnvName;
if (requestedEnvName && !readEnvValue(requestedEnvName)) {
console.warn(`[google-service] serviceAccountJsonEnv "${requestedEnvName}" is not set in the environment.`);
}
if (!raw) { if (!raw) {
return undefined; return undefined;
} }