From cb60b993cb75185671820a878cc2ac1c3274bcad Mon Sep 17 00:00:00 2001 From: Xiao Yijun Date: Fri, 12 Aug 2022 11:59:14 +0800 Subject: [PATCH] chore(core): update inaccurate content in `readPrivateKeys` method (#1768) --- packages/core/src/env-set/oidc.ts | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/packages/core/src/env-set/oidc.ts b/packages/core/src/env-set/oidc.ts index 6b92a927c..3ddbaedb0 100644 --- a/packages/core/src/env-set/oidc.ts +++ b/packages/core/src/env-set/oidc.ts @@ -12,7 +12,7 @@ import { appendDotEnv } from './dot-env'; import { allYes, noInquiry } from './parameters'; import { getEnvAsStringArray } from './utils'; -const defaultLogtoOidcPrivateKey = './oidc-private-key.pem'; +const defaultLogtoOidcPrivateKeyPath = './oidc-private-key.pem'; const listFormatter = new Intl.ListFormat('en', { style: 'long', type: 'conjunction' }); @@ -36,10 +36,15 @@ export const readPrivateKeys = async (): Promise => { const privateKeyPaths = getEnvAsStringArray('OIDC_PRIVATE_KEY_PATHS'); - // If no private key path is found, ask the user to generate a new one. + /** + * If neither `OIDC_PRIVATE_KEYS` nor `OIDC_PRIVATE_KEY_PATHS` is provided: + * + * 1. Try to read the private key from `defaultLogtoOidcPrivateKeyPath` + * 2. If the `defaultLogtoOidcPrivateKeyPath` doesn't exist, then ask user to generate a new key. + */ if (privateKeyPaths.length === 0) { try { - return [readFileSync(defaultLogtoOidcPrivateKey, 'utf8')]; + return [readFileSync(defaultLogtoOidcPrivateKeyPath, 'utf8')]; } catch (error: unknown) { if (noInquiry) { throw error; @@ -49,7 +54,7 @@ export const readPrivateKeys = async (): Promise => { const answer = await inquirer.prompt({ type: 'confirm', name: 'confirm', - message: `No private key found in env \`OIDC_PRIVATE_KEYS\` nor \`${defaultLogtoOidcPrivateKey}\`, would you like to generate a new one?`, + message: `No private key found in env \`OIDC_PRIVATE_KEYS\` nor \`${defaultLogtoOidcPrivateKeyPath}\`, would you like to generate a new one?`, }); if (!answer.confirm) { @@ -68,18 +73,18 @@ export const readPrivateKeys = async (): Promise => { format: 'pem', }, }); - writeFileSync(defaultLogtoOidcPrivateKey, privateKey); + writeFileSync(defaultLogtoOidcPrivateKeyPath, privateKey); return [privateKey]; } } - const notExistPrivateKeys = privateKeyPaths.filter((path): boolean => !existsSync(path)); + const nonExistentPrivateKeys = privateKeyPaths.filter((path): boolean => !existsSync(path)); - if (notExistPrivateKeys.length > 0) { + if (nonExistentPrivateKeys.length > 0) { throw new Error( `Private keys ${listFormatter.format( - notExistPrivateKeys + nonExistentPrivateKeys )} configured in env \`OIDC_PRIVATE_KEY_PATHS\` not found.` ); }