mirror of
https://github.com/elastic/kibana.git
synced 2025-04-23 17:28:26 -04:00
Create combineHeadersWithBasicAuthHeader util
This commit is contained in:
parent
94753a7342
commit
b10d103bd9
5 changed files with 87 additions and 22 deletions
|
@ -18,7 +18,7 @@ import { Logger } from '@kbn/core/server';
|
|||
import { getCustomAgents } from './get_custom_agents';
|
||||
import { ActionsConfigurationUtilities } from '../actions_config';
|
||||
import { SSLSettings } from '../types';
|
||||
import { getBasicAuthHeader } from './get_basic_auth_header';
|
||||
import { combineHeadersWithBasicAuthHeader } from './get_basic_auth_header';
|
||||
|
||||
export const request = async <T = unknown>({
|
||||
axios,
|
||||
|
@ -58,13 +58,11 @@ export const request = async <T = unknown>({
|
|||
|
||||
const { auth, ...restConfig } = config;
|
||||
|
||||
const headersWithBasicAuth =
|
||||
auth != null
|
||||
? {
|
||||
...getBasicAuthHeader({ username: auth.username, password: auth.password }),
|
||||
...headers,
|
||||
}
|
||||
: headers;
|
||||
const headersWithBasicAuth = combineHeadersWithBasicAuthHeader({
|
||||
username: auth?.username,
|
||||
password: auth?.password,
|
||||
headers,
|
||||
});
|
||||
|
||||
return await axios(url, {
|
||||
...restConfig,
|
||||
|
|
|
@ -5,12 +5,65 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
import { getBasicAuthHeader } from './get_basic_auth_header';
|
||||
import { combineHeadersWithBasicAuthHeader, getBasicAuthHeader } from './get_basic_auth_header';
|
||||
|
||||
describe('getBasicAuthHeader', () => {
|
||||
it('constructs the basic auth header correctly', () => {
|
||||
expect(getBasicAuthHeader({ username: 'test', password: 'foo' })).toEqual({
|
||||
Authorization: `Basic ${Buffer.from('test:foo').toString('base64')}`,
|
||||
describe('get_basic_auth_header', () => {
|
||||
describe('getBasicAuthHeader', () => {
|
||||
it('constructs the basic auth header correctly', () => {
|
||||
expect(getBasicAuthHeader({ username: 'test', password: 'foo' })).toEqual({
|
||||
Authorization: `Basic ${Buffer.from('test:foo').toString('base64')}`,
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe('combineHeadersWithBasicAuthHeader', () => {
|
||||
it('constructs the basic auth header correctly', () => {
|
||||
expect(combineHeadersWithBasicAuthHeader({ username: 'test', password: 'foo' })).toEqual({
|
||||
Authorization: `Basic ${Buffer.from('test:foo').toString('base64')}`,
|
||||
});
|
||||
});
|
||||
|
||||
it('adds extra headers correctly', () => {
|
||||
expect(
|
||||
combineHeadersWithBasicAuthHeader({
|
||||
username: 'test',
|
||||
password: 'foo',
|
||||
headers: { 'X-token': 'foo' },
|
||||
})
|
||||
).toEqual({
|
||||
Authorization: `Basic ${Buffer.from('test:foo').toString('base64')}`,
|
||||
'X-token': 'foo',
|
||||
});
|
||||
});
|
||||
|
||||
it('overrides the auth header if provided', () => {
|
||||
expect(
|
||||
combineHeadersWithBasicAuthHeader({
|
||||
username: 'test',
|
||||
password: 'foo',
|
||||
headers: { Authorization: 'Bearer my_token' },
|
||||
})
|
||||
).toEqual({
|
||||
Authorization: 'Bearer my_token',
|
||||
});
|
||||
});
|
||||
|
||||
it('returns only the headers if auth is undefined', () => {
|
||||
expect(
|
||||
combineHeadersWithBasicAuthHeader({
|
||||
headers: { 'X-token': 'foo' },
|
||||
})
|
||||
).toEqual({
|
||||
'X-token': 'foo',
|
||||
});
|
||||
});
|
||||
|
||||
it('returns undefined with no arguments', () => {
|
||||
expect(combineHeadersWithBasicAuthHeader()).toEqual(undefined);
|
||||
});
|
||||
|
||||
it('returns undefined when headers are null', () => {
|
||||
expect(combineHeadersWithBasicAuthHeader({ headers: null })).toEqual(undefined);
|
||||
});
|
||||
});
|
||||
});
|
||||
|
|
|
@ -5,13 +5,29 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
import type { AxiosHeaderValue } from 'axios';
|
||||
|
||||
interface GetBasicAuthHeaderArgs {
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
||||
|
||||
type CombineHeadersWithBasicAuthHeader = Partial<GetBasicAuthHeaderArgs> & {
|
||||
headers?: Record<string, AxiosHeaderValue> | null;
|
||||
};
|
||||
|
||||
export const getBasicAuthHeader = ({ username, password }: GetBasicAuthHeaderArgs) => {
|
||||
const header = `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`;
|
||||
|
||||
return { Authorization: header };
|
||||
};
|
||||
|
||||
export const combineHeadersWithBasicAuthHeader = ({
|
||||
username,
|
||||
password,
|
||||
headers,
|
||||
}: CombineHeadersWithBasicAuthHeader = {}) => {
|
||||
return username != null && password != null
|
||||
? { ...getBasicAuthHeader({ username, password }), ...headers }
|
||||
: headers ?? undefined;
|
||||
};
|
||||
|
|
|
@ -38,4 +38,4 @@ export {
|
|||
export { validateEmptyStrings } from './validate_empty_strings';
|
||||
export { parseDate } from './parse_date';
|
||||
export type { RelatedSavedObjects } from './related_saved_objects';
|
||||
export { getBasicAuthHeader } from './get_basic_auth_header';
|
||||
export { getBasicAuthHeader, combineHeadersWithBasicAuthHeader } from './get_basic_auth_header';
|
||||
|
|
|
@ -30,7 +30,7 @@ import { SubAction, SubActionRequestParams } from './types';
|
|||
import { ServiceParams } from './types';
|
||||
import * as i18n from './translations';
|
||||
import { request } from '../lib/axios_utils';
|
||||
import { getBasicAuthHeader } from '../lib';
|
||||
import { combineHeadersWithBasicAuthHeader } from '../lib/get_basic_auth_header';
|
||||
|
||||
const isObject = (value: unknown): value is Record<string, unknown> => {
|
||||
return isPlainObject(value);
|
||||
|
@ -93,13 +93,11 @@ export abstract class SubActionConnector<Config, Secrets> {
|
|||
auth?: AxiosBasicCredentials,
|
||||
headers?: AxiosRequestHeaders
|
||||
): Record<string, AxiosHeaderValue> {
|
||||
const headersWithBasicAuth =
|
||||
auth != null
|
||||
? {
|
||||
...getBasicAuthHeader({ username: auth.username, password: auth.password }),
|
||||
...headers,
|
||||
}
|
||||
: headers;
|
||||
const headersWithBasicAuth = combineHeadersWithBasicAuthHeader({
|
||||
username: auth?.username,
|
||||
password: auth?.password,
|
||||
headers,
|
||||
});
|
||||
|
||||
return { 'Content-Type': 'application/json', ...headersWithBasicAuth };
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue