Add support for bearer auth (#1488)

This commit is contained in:
Tomas Della Vedova
2021-07-13 09:39:10 +02:00
committed by delvedor
parent 3d4323043d
commit 187c229ba7
6 changed files with 65 additions and 3 deletions

View File

@ -67,6 +67,13 @@ auth: {
apiKey: 'base64EncodedKey' apiKey: 'base64EncodedKey'
} }
---- ----
Bearer authentication, useful for https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-service-token.html[service account tokens]. Be aware that it does not handle automatic token refresh:
[source,js]
----
auth: {
bearer: 'token'
}
----
|`maxRetries` |`maxRetries`
@ -248,4 +255,4 @@ const client = new Client({
|`boolean`, `'proto'`, `'constructor'` - By the default the client will protect you against prototype poisoning attacks. Read https://web.archive.org/web/20200319091159/https://hueniverse.com/square-brackets-are-the-enemy-ff5b9fd8a3e8?gi=184a27ee2a08[this article] to learn more. If needed you can disable prototype poisoning protection entirely or one of the two checks. Read the `secure-json-parse` https://github.com/fastify/secure-json-parse[documentation] to learn more. + |`boolean`, `'proto'`, `'constructor'` - By the default the client will protect you against prototype poisoning attacks. Read https://web.archive.org/web/20200319091159/https://hueniverse.com/square-brackets-are-the-enemy-ff5b9fd8a3e8?gi=184a27ee2a08[this article] to learn more. If needed you can disable prototype poisoning protection entirely or one of the two checks. Read the `secure-json-parse` https://github.com/fastify/secure-json-parse[documentation] to learn more. +
_Default:_ `false` _Default:_ `false`
|=== |===

View File

@ -93,6 +93,26 @@ const client = new Client({
}) })
---- ----
[discrete]
[[auth-bearer]]
==== Bearer authentication
You can provide your credentials by passing the `bearer` token
parameter via the `auth` option.
Useful for https://www.elastic.co/guide/en/elasticsearch/reference/current/security-api-create-service-token.html[service account tokens].
Be aware that it does not handle automatic token refresh.
[source,js]
----
const { Client } = require('@elastic/elasticsearch')
const client = new Client({
node: 'https://localhost:9200',
auth: {
bearer: 'token'
}
})
----
[discrete] [discrete]
[[auth-basic]] [[auth-basic]]

5
index.d.ts vendored
View File

@ -43,7 +43,8 @@ import {
CloudConnectionPool, CloudConnectionPool,
ResurrectEvent, ResurrectEvent,
BasicAuth, BasicAuth,
ApiKeyAuth ApiKeyAuth,
BearerAuth
} from './lib/pool'; } from './lib/pool';
import Serializer from './lib/Serializer'; import Serializer from './lib/Serializer';
import Helpers from './lib/Helpers'; import Helpers from './lib/Helpers';
@ -106,7 +107,7 @@ interface ClientOptions {
opaqueIdPrefix?: string; opaqueIdPrefix?: string;
generateRequestId?: generateRequestIdFn; generateRequestId?: generateRequestIdFn;
name?: string | symbol; name?: string | symbol;
auth?: BasicAuth | ApiKeyAuth; auth?: BasicAuth | ApiKeyAuth | BearerAuth;
context?: Context; context?: Context;
proxy?: string | URL; proxy?: string | URL;
enableMetaHeader?: boolean; enableMetaHeader?: boolean;

View File

@ -331,6 +331,8 @@ function prepareHeaders (headers = {}, auth) {
} else { } else {
headers.authorization = `ApiKey ${auth.apiKey}` headers.authorization = `ApiKey ${auth.apiKey}`
} }
} else if (auth.bearer) {
headers.authorization = `Bearer ${auth.bearer}`
} else if (auth.username && auth.password) { } else if (auth.username && auth.password) {
headers.authorization = 'Basic ' + Buffer.from(`${auth.username}:${auth.password}`).toString('base64') headers.authorization = 'Basic ' + Buffer.from(`${auth.username}:${auth.password}`).toString('base64')
} }

5
lib/pool/index.d.ts vendored
View File

@ -61,6 +61,10 @@ interface BasicAuth {
password: string; password: string;
} }
interface BearerAuth {
bearer: string
}
interface resurrectOptions { interface resurrectOptions {
now?: number; now?: number;
requestId: string; requestId: string;
@ -204,6 +208,7 @@ export {
getConnectionOptions, getConnectionOptions,
ApiKeyAuth, ApiKeyAuth,
BasicAuth, BasicAuth,
BearerAuth,
internals, internals,
resurrectOptions, resurrectOptions,
ResurrectEvent, ResurrectEvent,

View File

@ -1421,3 +1421,30 @@ test('Disable prototype poisoning protection', t => {
t.error(err) t.error(err)
}) })
}) })
test('Bearer auth', t => {
t.plan(3)
function handler (req, res) {
t.match(req.headers, {
authorization: 'Bearer Zm9vOmJhcg=='
})
res.setHeader('Content-Type', 'application/json;utf=8')
res.end(JSON.stringify({ hello: 'world' }))
}
buildServer(handler, ({ port }, server) => {
const client = new Client({
node: `http://localhost:${port}`,
auth: {
bearer: 'Zm9vOmJhcg=='
}
})
client.info((err, { body }) => {
t.error(err)
t.same(body, { hello: 'world' })
server.stop()
})
})
})