Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pfx HTTPS option #1364

Merged
merged 25 commits into from Sep 13, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions readme.md
Expand Up @@ -985,6 +985,18 @@ Type: `string`

The passphrase to decrypt the `options.https.key` (if different keys have different passphrases refer to `options.https.key` documentation).

##### https.pfx

Type: `string | Buffer | (string | Buffer | object)[]`
markdboyd marked this conversation as resolved.
Show resolved Hide resolved

PFX or PKCS12 encoded private key and certificate chain. `pfx` is an alternative to providing `options.https.key` and `options.https.certificate` individually. PFX is usually encrypted, if it is, `options.https.passphrase` will be used to decrypt it.
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
markdboyd marked this conversation as resolved.
Show resolved Hide resolved

Multiple PFX can be be provided as an array of unencrypted buffers or an array of objects like:
markdboyd marked this conversation as resolved.
Show resolved Hide resolved

`{buf: <string|buffer>[, passphrase: <string>]}`
markdboyd marked this conversation as resolved.
Show resolved Hide resolved

If the provided buffers are encrypted, `object.passphrase` can be used to decrypt them. If `object.passphrase` is not provided, `options.https.passphrase` will be used for decryption.

##### Examples for `https.key`, `https.certificate` and `https.passphrase`

```js
Expand Down
10 changes: 10 additions & 0 deletions source/core/index.ts
Expand Up @@ -187,6 +187,7 @@ export interface HTTPSOptions {
key?: SecureContextOptions['key'];
certificate?: SecureContextOptions['cert'];
passphrase?: SecureContextOptions['passphrase'];
pfx?: SecureContextOptions['pfx'];
}

interface NormalizedPlainOptions extends PlainOptions {
Expand Down Expand Up @@ -684,6 +685,7 @@ export default class Request extends Duplex implements RequestEvents<Request> {
assert.any([is.string, is.object, is.array, is.undefined], options.https.key);
assert.any([is.string, is.object, is.array, is.undefined], options.https.certificate);
assert.any([is.string, is.undefined], options.https.passphrase);
assert.any([is.string, is.array, is.undefined], options.https.pfx);
sindresorhus marked this conversation as resolved.
Show resolved Hide resolved
}

// `options.method`
Expand Down Expand Up @@ -975,6 +977,10 @@ export default class Request extends Duplex implements RequestEvents<Request> {
deprecationWarning('"options.passphrase" was never documented, please use "options.https.passphrase"');
}

if ('pfx' in options) {
deprecationWarning('"options.pfx" was never documented, please use "options.https.pfx"');
}

// Other options
if ('followRedirects' in options) {
throw new TypeError('The `followRedirects` option does not exist. Use `followRedirect` instead.');
Expand Down Expand Up @@ -1527,6 +1533,10 @@ export default class Request extends Duplex implements RequestEvents<Request> {
if (options.https.passphrase) {
requestOptions.passphrase = options.https.passphrase;
}

if (options.https.pfx) {
requestOptions.pfx = options.https.pfx;
}
}

try {
Expand Down
19 changes: 19 additions & 0 deletions test/https.ts
Expand Up @@ -159,6 +159,25 @@ test.serial('non-deprecated `rejectUnauthorized` option', withServer, async (t,
t.pass();
});

test.serial('non-deprecated `pfx` option', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.end('ok');
});

(async () => {
const warning = await pEvent(process, 'warning');
t.not(warning.name, 'DeprecationWarning');
})();

await got.secure({
https: {
pfx: 'fake-pfx'
}
});

t.pass();
});

test.serial('no double deprecated warning', withServer, async (t, server, got) => {
server.get('/', (_request, response) => {
response.end('ok');
Expand Down