Skip to content

Commit

Permalink
remove apikey and bearer auth (#111)
Browse files Browse the repository at this point in the history
* remove apikey and bearer auth

Issue Resolved:
#110

Signed-off-by: Anan Zhuang <[email protected]>

* change auth to username for child

Signed-off-by: Anan Zhuang <[email protected]>
  • Loading branch information
ananzh authored Aug 6, 2021
1 parent 9e64c1b commit 705df17
Show file tree
Hide file tree
Showing 8 changed files with 9 additions and 333 deletions.
6 changes: 2 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ import {
BaseConnectionPool,
CloudConnectionPool,
ResurrectEvent,
BasicAuth,
ApiKeyAuth,
BearerAuth
BasicAuth
} from './lib/pool';
import Serializer from './lib/Serializer';
import Helpers from './lib/Helpers';
Expand Down Expand Up @@ -117,7 +115,7 @@ interface ClientOptions {
opaqueIdPrefix?: string;
generateRequestId?: generateRequestIdFn;
name?: string | symbol;
auth?: BasicAuth | ApiKeyAuth | BearerAuth;
auth?: BasicAuth;
context?: Context;
proxy?: string | URL;
enableMetaHeader?: boolean;
Expand Down
4 changes: 2 additions & 2 deletions lib/Connection.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
import { URL } from 'url';
import { inspect, InspectOptions } from 'util'
import { Readable as ReadableStream } from 'stream';
import { ApiKeyAuth, BasicAuth } from './pool'
import { BasicAuth } from './pool'
import * as http from 'http'
import * as https from 'https'
import * as hpagent from 'hpagent'
Expand All @@ -49,7 +49,7 @@ export interface ConnectionOptions {
agent?: AgentOptions | agentFn;
status?: string;
roles?: ConnectionRoles;
auth?: BasicAuth | ApiKeyAuth;
auth?: BasicAuth;
proxy?: string | URL;
}

Expand Down
10 changes: 1 addition & 9 deletions lib/Connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -334,15 +334,7 @@ function resolve (host, path) {
function prepareHeaders (headers = {}, auth) {
if (auth != null && headers.authorization == null) {
/* istanbul ignore else */
if (auth.apiKey) {
if (typeof auth.apiKey === 'object') {
headers.authorization = 'ApiKey ' + Buffer.from(`${auth.apiKey.id}:${auth.apiKey.api_key}`).toString('base64')
} else {
headers.authorization = `ApiKey ${auth.apiKey}`
}
} else if (auth.bearer) {
headers.authorization = `Bearer ${auth.bearer}`
} else if (auth.username && auth.password) {
if (auth.username && auth.password) {
headers.authorization = 'Basic ' + Buffer.from(`${auth.username}:${auth.password}`).toString('base64')
}
}
Expand Down
19 changes: 2 additions & 17 deletions lib/pool/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ interface BaseConnectionPoolOptions {
ssl?: SecureContextOptions;
agent?: AgentOptions;
proxy?: string | URL;
auth?: BasicAuth | ApiKeyAuth;
auth?: BasicAuth;
emit: (event: string | symbol, ...args: any[]) => boolean;
Connection: typeof Connection;
}
Expand All @@ -58,24 +58,11 @@ interface getConnectionOptions {
now?: number;
}

interface ApiKeyAuth {
apiKey:
| string
| {
id: string;
api_key: string;
}
}

interface BasicAuth {
username: string;
password: string;
}

interface BearerAuth {
bearer: string
}

interface resurrectOptions {
now?: number;
requestId: string;
Expand All @@ -100,7 +87,7 @@ declare class BaseConnectionPool {
_ssl: SecureContextOptions | null;
_agent: AgentOptions | null;
_proxy: string | URL;
auth: BasicAuth | ApiKeyAuth;
auth: BasicAuth;
Connection: typeof Connection;
constructor(opts?: BaseConnectionPoolOptions);
/**
Expand Down Expand Up @@ -217,9 +204,7 @@ export {
// Interfaces
ConnectionPoolOptions,
getConnectionOptions,
ApiKeyAuth,
BasicAuth,
BearerAuth,
internals,
resurrectOptions,
ResurrectEvent,
Expand Down
21 changes: 0 additions & 21 deletions test/types/client-options.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -368,27 +368,6 @@ expectType<Client>(
})
)

expectType<Client>(
new Client({
node: 'http://localhost:9200',
auth: {
apiKey: 'abcd'
}
})
)

expectType<Client>(
new Client({
node: 'http://localhost:9200',
auth: {
apiKey: {
api_key: 'foo',
id: 'bar'
}
}
})
)

expectError<errors.ConfigurationError>(
// @ts-expect-error
new Client({
Expand Down
9 changes: 2 additions & 7 deletions test/unit/child.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -296,13 +296,8 @@ test('Should create a child client (name check)', t => {
test('Should create a child client (auth check)', t => {
t.plan(4)

let count = 0
function handler (req, res) {
if (count++ === 0) {
t.match(req.headers, { authorization: 'Basic Zm9vOmJhcg==' })
} else {
t.match(req.headers, { authorization: 'ApiKey foobar' })
}
t.match(req.headers, { authorization: 'Basic Zm9vOmJhcg==' })
res.setHeader('Content-Type', 'application/json;utf=8')
res.end(JSON.stringify({ hello: 'world' }))
}
Expand All @@ -317,7 +312,7 @@ test('Should create a child client (auth check)', t => {
})
const child = client.child({
auth: {
apiKey: 'foobar'
username: 'foobar'
}
})

Expand Down
Loading

0 comments on commit 705df17

Please sign in to comment.