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 16 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
58 changes: 57 additions & 1 deletion readme.md
Expand Up @@ -985,7 +985,24 @@ Type: `string`

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

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

Type: `string | Buffer | Array<string | Buffer | object>`

[PFX or PKCS12](https://en.wikipedia.org/wiki/PKCS_12) encoded private key and certificate chain. Using `options.https.pfx` is an alternative to providing `options.https.key` and `options.https.certificate` individually. A PFX is usually encrypted, and if it is, `options.https.passphrase` will be used to decrypt it.

Multiple PFX's can be be provided as an array of unencrypted buffers or an array of objects like:

```ts
{
buffer: string | Buffer,
passphrase?: string
}
```

This object form can only occur in an array. 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`, `https.passphrase`, and `https.pfx`

```js
// Single key with certificate
Expand Down Expand Up @@ -1032,6 +1049,45 @@ got('https://example.com', {
]
}
});

// Single encrypted PFX with passphrase
got('https://example.com', {
https: {
pfx: fs.readFileSync('./fake.pfx'),
passphrase: 'passphrase'
}
});

// Multiple encrypted PFX's with different passphrases
got('https://example.com', {
https: {
pfx: [
{
buffer: fs.readFileSync('./key1.pfx'),
passphrase: 'passphrase1'
},
{
buffer: fs.readFileSync('./key2.pfx'),
passphrase: 'passphrase2'
}
]
}
});

// Multiple encrypted PFX's with single passphrase
got('https://example.com', {
https: {
passphrase: 'passphrase',
pfx: [
{
buffer: fs.readFileSync('./key1.pfx')
},
{
buffer: fs.readFileSync('./key2.pfx')
}
]
}
});
```

##### https.rejectUnauthorized
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.buffer, is.array, is.undefined], options.https.pfx);
}

// `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