@aws-amplify/amplify-category-notifications
Version:
amplify-cli notifications plugin
89 lines • 4.17 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.run = void 0;
const fs_extra_1 = __importDefault(require("fs-extra"));
const os_1 = __importDefault(require("os"));
const path_1 = __importDefault(require("path"));
const child_process_1 = require("child_process");
const amplify_cli_core_1 = require("@aws-amplify/amplify-cli-core");
const run = (info) => {
const { P12FilePath, P12FilePassword } = info;
if (!fs_extra_1.default.existsSync(P12FilePath)) {
throw new amplify_cli_core_1.AmplifyError('InputValidationError', {
message: `The p12 file does not exist: ${P12FilePath}`,
resolution: 'Verify the file path is correct and the file exists',
});
}
const pemFileContent = getPemFileContent(P12FilePath, P12FilePassword);
const Certificate = getCertificate(pemFileContent);
let PrivateKey = getPrivateKey(pemFileContent);
if (!PrivateKey) {
PrivateKey = getRSAPrivateKey(pemFileContent);
}
if (!PrivateKey) {
PrivateKey = getEncryptedPrivateKey(pemFileContent);
}
if (!Certificate) {
throw new amplify_cli_core_1.AmplifyError('OpenSslCertificateError', {
message: 'OpenSSL can not extract the Certificate from the p12 file',
resolution: 'Check the p12 file and password and try again',
});
}
if (!PrivateKey) {
throw new amplify_cli_core_1.AmplifyError('OpenSslCertificateError', {
message: 'OpenSSL can not extract the Private Key from the p12 file',
resolution: 'Check the p12 file and password and try again',
});
}
return {
Certificate,
PrivateKey,
};
};
exports.run = run;
const getPemFileContent = (filePath, filePassword) => {
const outputFilePath = path_1.default.join(os_1.default.tmpdir(), `amplify-${Date.now()}.pem`);
try {
const result = (0, child_process_1.spawnSync)('openssl', ['pkcs12', '-in', filePath, '-out', outputFilePath, '-nodes', '-passin', 'stdin'], {
input: filePassword,
encoding: 'utf8',
});
if (result.error) {
throw new amplify_cli_core_1.AmplifyError('OpenSslCertificateError', {
message: `OpenSSL command failed: ${result.error.message}`,
resolution: 'Ensure OpenSSL is installed and accessible in your PATH',
});
}
if (result.status !== 0) {
throw new amplify_cli_core_1.AmplifyError('OpenSslCertificateError', {
message: `OpenSSL failed to process the p12 file: ${result.stderr || 'Unknown error'}`,
resolution: 'Check the p12 file and password and try again',
});
}
return fs_extra_1.default.readFileSync(outputFilePath, 'utf8');
}
finally {
if (fs_extra_1.default.existsSync(outputFilePath)) {
fs_extra_1.default.removeSync(outputFilePath);
}
}
};
const extractPemBlock = (pemFileContent, beginMark, endMark) => {
const rawIndex = pemFileContent.indexOf(beginMark);
if (rawIndex === -1)
return undefined;
const beginIndex = rawIndex + beginMark.length;
const endIndex = pemFileContent.indexOf(endMark, beginIndex);
if (endIndex === -1)
return undefined;
const content = pemFileContent.slice(beginIndex, endIndex).replace(/\s/g, '');
return beginMark + os_1.default.EOL + content + os_1.default.EOL + endMark;
};
const getCertificate = (pem) => extractPemBlock(pem, '-----BEGIN CERTIFICATE-----', '-----END CERTIFICATE-----');
const getPrivateKey = (pem) => extractPemBlock(pem, '-----BEGIN PRIVATE KEY-----', '-----END PRIVATE KEY-----');
const getRSAPrivateKey = (pem) => extractPemBlock(pem, '-----BEGIN RSA PRIVATE KEY-----', '-----END RSA PRIVATE KEY-----');
const getEncryptedPrivateKey = (pem) => extractPemBlock(pem, '-----BEGIN ENCRYPTED PRIVATE KEY-----', '-----END ENCRYPTED PRIVATE KEY-----');
//# sourceMappingURL=apns-cert-p12decoder.js.map