feat(login): add LOGIN_SERVICE_KEY_FILE authentication support (#11572)

This commit is contained in:
Mridang Agarwalla
2026-02-24 10:14:24 +00:00
committed by GitHub
parent 839a2d3b5b
commit 54095b64b1
5 changed files with 133 additions and 5 deletions
+36
View File
@@ -1,5 +1,16 @@
import { newSystemToken } from "@zitadel/client/node";
import { readFileSync } from "fs";
import { getLoginSystemUserId } from "./deployment";
/**
* Creates a signed JWT token using system user credentials from environment
* variables. The SYSTEM_USER_PRIVATE_KEY is expected to be base64-encoded and
* is decoded before signing. Requires AUDIENCE, SYSTEM_USER_ID, and
* SYSTEM_USER_PRIVATE_KEY to be set in the environment.
*
* @returns A signed JWT token string for authenticating API requests.
* @throws If the underlying token signing fails.
*/
export async function systemAPIToken() {
const token = {
audience: process.env.AUDIENCE,
@@ -15,3 +26,28 @@ export async function systemAPIToken() {
key: token.token,
});
}
/**
* Creates a signed JWT token by reading a private key from the file path
* specified in ZITADEL_LOGIN_SERVICE_KEY_FILE. The audience is resolved from
* AUDIENCE or falls back to ZITADEL_API_URL, and the subject is resolved via
* {@link getLoginSystemUserId}.
*
* @returns A signed JWT token string for authenticating API requests.
* @throws If the key file cannot be read or the token signing fails.
*/
export async function loginServiceKeyToken() {
const keyFile = process.env.ZITADEL_LOGIN_SERVICE_KEY_FILE!;
try {
const key = readFileSync(keyFile, "utf-8");
return newSystemToken({
audience: process.env.AUDIENCE || process.env.ZITADEL_API_URL,
subject: getLoginSystemUserId()!,
key: key,
});
} catch (err) {
throw new Error(`Failed to read login service key file "${keyFile}": ${err instanceof Error ? err.message : err}`);
}
}