UNPKG

firebase-tools

Version:
96 lines (95 loc) 4.49 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.command = void 0; const clc = require("colorette"); const node_crypto_1 = require("node:crypto"); const os = require("os"); const command_1 = require("../command"); const projectUtils_1 = require("../projectUtils"); const api_1 = require("../appcheck/api"); const appcheck_prompts_1 = require("./appcheck-prompts"); const requireAuth_1 = require("../requireAuth"); const utils_1 = require("../utils"); const error_1 = require("../error"); const prompt_1 = require("../prompt"); const logger_1 = require("../logger"); exports.command = new command_1.Command("appcheck:debugtokens:create [debugToken]") .description("generate and register an App Check debug token for an app") .option("--app <appId>", "the app id of your Firebase app") .option("--display-name <displayName>", "display name for the debug token") .option("--force", "overwrite existing debug token if it has the same display name without prompting") .before(requireAuth_1.requireAuth) .action(async (debugToken, options) => { const { appId } = await (0, appcheck_prompts_1.getOrPromptProjectAndAppId)(options); const projectNumber = await (0, projectUtils_1.needProjectNumber)(options); let token = debugToken; if (!token) { if (!options.nonInteractive) { const action = await (0, prompt_1.select)({ message: "Would you like to register a token generated by the SDK, or create a new one?", choices: [ { name: "Create a new debug token", value: "generate" }, { name: "Register a token generated by the SDK", value: "sdk" }, ], }); if (action === "sdk") { token = await (0, prompt_1.password)({ message: "Enter the debug token generated by the SDK:", validate: (val) => (val.trim().length > 0 ? true : "Debug token cannot be empty."), }); } else { token = (0, node_crypto_1.randomUUID)(); } } else { token = (0, node_crypto_1.randomUUID)(); } } let displayName = options.displayName; const defaultName = `CLI Debug Token (${os.hostname() || "Unknown Host"})`; if (!displayName) { if (!options.nonInteractive) { displayName = await (0, prompt_1.input)({ message: "What would you like to call this debug token?", default: defaultName, }); } else { displayName = defaultName; } } const existingTokens = await (0, api_1.listDebugTokens)(projectNumber, appId); const matchingTokens = existingTokens.filter((t) => t.displayName === displayName); if (matchingTokens.length > 0) { let shouldOverwrite = options.force; if (!shouldOverwrite) { if (options.nonInteractive) { throw new error_1.FirebaseError(`A token with the display name "${displayName}" already exists. Must pass --force to overwrite in non-interactive mode.`, { exit: 1 }); } shouldOverwrite = await (0, prompt_1.confirm)({ message: `A token with the display name "${displayName}" already exists. Delete the old token(s)?`, default: true, }); } if (shouldOverwrite) { for (const t of matchingTokens) { await (0, api_1.deleteDebugToken)(t.name); } } else { throw new error_1.FirebaseError("Registration canceled.", { exit: 1 }); } } const result = await (0, utils_1.promiseWithSpinner)(async () => await (0, api_1.createDebugToken)(projectNumber, appId, displayName, token), `Registering App Check debug token with Firebase for app ${clc.bold(appId)}`); (0, utils_1.logSuccess)(`Successfully registered App Check debug token: - Display Name: ${clc.bold(result.displayName)} - Resource Name: ${clc.cyan(result.name)}`); if (!options.json) { console.log(` - Token: ${clc.bold(clc.green(token))}\n`); } logger_1.logger.info(clc.yellow(clc.bold("Warning: ") + "This debug token is a secret and should not be shared or uploaded to source code.")); logger_1.logger.info(clc.yellow("TODO: Remember to delete this token once your testing has finished.")); return result; });