0
Fork 0
mirror of https://github.com/logto-io/logto.git synced 2025-01-20 21:32:31 -05:00

chore(core): update inaccurate content in readPrivateKeys method (#1768)

This commit is contained in:
Xiao Yijun 2022-08-12 11:59:14 +08:00 committed by GitHub
parent 1f4d35ad42
commit cb60b993cb
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -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<string[]> => {
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<string[]> => {
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<string[]> => {
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.`
);
}