Files
zitadel/apps/login/src/lib/api.ts
T
RamonandGitHub 057fb8dff2 fix(login): prettier setup (#11798)
# Which Problems Are Solved
Invalid formatting in the login codebase due to invalid prettier
configuration

# How the Problems Are Solved
Fxed .prettierignore wildcards for login source files

# Additional Changes
Removed .prettierrc file because there is already a prettier.config.mjs
file

# Additional Context
2026-03-10 13:30:20 +00:00

52 lines
1.7 KiB
TypeScript

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,
userID: process.env.SYSTEM_USER_ID,
token: Buffer.from(process.env.SYSTEM_USER_PRIVATE_KEY, "base64").toString("utf-8"),
};
return newSystemToken({
audience: token.audience,
subject: token.userID,
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}`);
}
}