From b37d6a5b05eaed874f7fdcab0d3fc85e4a95df56 Mon Sep 17 00:00:00 2001 From: aludev Date: Mon, 31 Dec 2018 17:57:15 +0200 Subject: [PATCH] Fix readPkcs12 function types (#31612) --- types/pem/index.d.ts | 11 +++++++++-- types/pem/pem-tests.ts | 16 +++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/types/pem/index.d.ts b/types/pem/index.d.ts index c6689a273f..bdfa3e1196 100644 --- a/types/pem/index.d.ts +++ b/types/pem/index.d.ts @@ -2,6 +2,7 @@ // Project: https://github.com/andris9/pem // Definitions by: Anthony Trinh , Ruslan Arkhipau // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped +/// export interface ModuleConfiguration { /** @@ -135,6 +136,12 @@ export interface CertificateSubjectReadResult { emailAddress: string; } +export interface Pkcs12ReadResult { + key: string; + cert: string; + ca: string[]; +} + export type Callback = (error: any, result: T) => any; /** @@ -244,8 +251,8 @@ export function createPkcs12(key: string, certificate: string, password: string, * @param callback Callback function with an error object and {pkcs12} * @returns the result of the callback */ -export function readPkcs12(bufferOrPath: string, options: Pkcs12ReadOptions, callback: Callback<{ pkcs12: any }>): any; -export function readPkcs12(bufferOrPath: string, callback: Callback<{ pkcs12: any }>): any; +export function readPkcs12(bufferOrPath: Buffer | string, options: Pkcs12ReadOptions, callback: Callback): any; +export function readPkcs12(bufferOrPath: Buffer | string, callback: Callback): any; /** * Verifies the signing chain of the passed certificate diff --git a/types/pem/pem-tests.ts b/types/pem/pem-tests.ts index fe409b6560..6841b568d7 100644 --- a/types/pem/pem-tests.ts +++ b/types/pem/pem-tests.ts @@ -1,4 +1,5 @@ import * as pem from 'pem'; +import * as fs from 'fs'; const tests = { 'Create default sized dhparam key': (test: any) => { @@ -557,8 +558,21 @@ const tests = { test.ok(pkcs12.pkcs12); // test.ok(fs.readdirSync('./tmp').length === 0); + const pkcs12Buffer = new Buffer(pkcs12.pkcs12); - pem.readPkcs12(pkcs12.pkcs12, (error: any, keystore: any) => { + pem.readPkcs12(pkcs12Buffer, (error: any, keystore: pem.Pkcs12ReadResult) => { + test.ifError(error); + test.ok(keystore); + + test.equal(ca.certificate, keystore.ca[0]); + test.equal(cert.certificate, keystore.cert); + test.equal(cert.clientKey, keystore.key); + }); + + const pkcs12File: string = __dirname + '/test.pkcs12'; + fs.writeFileSync(pkcs12File, pkcs12Buffer); + + pem.readPkcs12(pkcs12File, (error: any, keystore: pem.Pkcs12ReadResult) => { test.ifError(error); test.ok(keystore);