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

feat: add parameter to ProxyAgent options, which is type of authentication #1705

Merged
merged 5 commits into from Oct 17, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
22 changes: 22 additions & 0 deletions docs/api/ProxyAgent.md
Expand Up @@ -17,6 +17,8 @@ Returns: `ProxyAgent`
Extends: [`AgentOptions`](Agent.md#parameter-agentoptions)

* **uri** `string` (required) - It can be passed either by a string or a object containing `uri` as string.
* **token** `string` (optional) - It can be passed by a string of token for authentication.
* **auth** `string` (**deprecated**) - Use token.

Examples:

Expand Down Expand Up @@ -74,6 +76,26 @@ for await (const data of body) {
}
```

#### Example - Basic Proxy Request with authentication

```js
import { setGlobalDispatcher, request, ProxyAgent } from 'undici';

const proxyAgent = new ProxyAgent({
uri: 'my.proxy.server',
token: 'Bearer xxxx'
});
setGlobalDispatcher(proxyAgent);

const { statusCode, body } = await request('http://localhost:3000/foo');

console.log('response received', statusCode); // response received 200

for await (const data of body) {
console.log('data', data.toString('utf8')); // data foo
}
```

### `ProxyAgent.close()`

Closes the proxy agent and waits for registered pools and clients to also close before resolving.
Expand Down
3 changes: 3 additions & 0 deletions lib/proxy-agent.js
Expand Up @@ -55,8 +55,11 @@ class ProxyAgent extends DispatcherBase {
this[kProxyTls] = opts.proxyTls
this[kProxyHeaders] = {}

// @deprecated in favour of opts.token
if (opts.auth) {
this[kProxyHeaders]['proxy-authorization'] = `Basic ${opts.auth}`
} else if (opts.token) {
this[kProxyHeaders]['proxy-authorization'] = opts.token
ronag marked this conversation as resolved.
Show resolved Hide resolved
}

const resolvedUrl = new URL(opts.uri)
Expand Down
44 changes: 44 additions & 0 deletions test/proxy-agent.js
Expand Up @@ -142,6 +142,50 @@ test('use proxy-agent with auth', async (t) => {
proxyAgent.close()
})

test('use proxy-agent with token', async (t) => {
t.plan(7)
const server = await buildServer()
const proxy = await buildProxy()

const serverUrl = `http://localhost:${server.address().port}`
const proxyUrl = `http://localhost:${proxy.address().port}`
const proxyAgent = new ProxyAgent({
token: `Bearer ${Buffer.from('user:pass').toString('base64')}`,
uri: proxyUrl
})
const parsedOrigin = new URL(serverUrl)

proxy.authenticate = function (req, fn) {
t.pass('authentication should be called')
fn(null, req.headers['proxy-authorization'] === `Bearer ${Buffer.from('user:pass').toString('base64')}`)
}
proxy.on('connect', () => {
t.pass('proxy should be called')
})

server.on('request', (req, res) => {
t.equal(req.url, '/hello?foo=bar')
t.equal(req.headers.host, parsedOrigin.host, 'should not use proxyUrl as host')
res.setHeader('content-type', 'application/json')
res.end(JSON.stringify({ hello: 'world' }))
})

const {
statusCode,
headers,
body
} = await request(serverUrl + '/hello?foo=bar', { dispatcher: proxyAgent })
const json = await body.json()

t.equal(statusCode, 200)
t.same(json, { hello: 'world' })
t.equal(headers.connection, 'keep-alive', 'should remain the connection open')

server.close()
proxy.close()
proxyAgent.close()
})

test('sending proxy-authorization in request headers should throw', async (t) => {
t.plan(3)
const server = await buildServer()
Expand Down
4 changes: 4 additions & 0 deletions types/proxy-agent.d.ts
Expand Up @@ -14,7 +14,11 @@ declare class ProxyAgent extends Dispatcher {
declare namespace ProxyAgent {
export interface Options extends Agent.Options {
uri: string;
/**
* @deprecated use opts.token
*/
auth?: string;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
auth?: string;
/**
* @deprecated use opts.token
*/
auth?: string;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the lint too

token?: string;
requestTls?: TlsOptions & { servername?: string };
proxyTls?: TlsOptions & { servername?: string };
}
Expand Down