mirror of
https://github.com/elastic/kibana.git
synced 2025-07-04 22:35:59 -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 { getCustomAgents } from './get_custom_agents';
|
||||||
import { ActionsConfigurationUtilities } from '../actions_config';
|
import { ActionsConfigurationUtilities } from '../actions_config';
|
||||||
import { SSLSettings } from '../types';
|
import { SSLSettings } from '../types';
|
||||||
import { getBasicAuthHeader } from './get_basic_auth_header';
|
import { combineHeadersWithBasicAuthHeader } from './get_basic_auth_header';
|
||||||
|
|
||||||
export const request = async <T = unknown>({
|
export const request = async <T = unknown>({
|
||||||
axios,
|
axios,
|
||||||
|
@ -58,13 +58,11 @@ export const request = async <T = unknown>({
|
||||||
|
|
||||||
const { auth, ...restConfig } = config;
|
const { auth, ...restConfig } = config;
|
||||||
|
|
||||||
const headersWithBasicAuth =
|
const headersWithBasicAuth = combineHeadersWithBasicAuthHeader({
|
||||||
auth != null
|
username: auth?.username,
|
||||||
? {
|
password: auth?.password,
|
||||||
...getBasicAuthHeader({ username: auth.username, password: auth.password }),
|
headers,
|
||||||
...headers,
|
});
|
||||||
}
|
|
||||||
: headers;
|
|
||||||
|
|
||||||
return await axios(url, {
|
return await axios(url, {
|
||||||
...restConfig,
|
...restConfig,
|
||||||
|
|
|
@ -5,12 +5,65 @@
|
||||||
* 2.0.
|
* 2.0.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { getBasicAuthHeader } from './get_basic_auth_header';
|
import { combineHeadersWithBasicAuthHeader, getBasicAuthHeader } from './get_basic_auth_header';
|
||||||
|
|
||||||
describe('getBasicAuthHeader', () => {
|
describe('get_basic_auth_header', () => {
|
||||||
it('constructs the basic auth header correctly', () => {
|
describe('getBasicAuthHeader', () => {
|
||||||
expect(getBasicAuthHeader({ username: 'test', password: 'foo' })).toEqual({
|
it('constructs the basic auth header correctly', () => {
|
||||||
Authorization: `Basic ${Buffer.from('test:foo').toString('base64')}`,
|
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.
|
* 2.0.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
import type { AxiosHeaderValue } from 'axios';
|
||||||
|
|
||||||
interface GetBasicAuthHeaderArgs {
|
interface GetBasicAuthHeaderArgs {
|
||||||
username: string;
|
username: string;
|
||||||
password: string;
|
password: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type CombineHeadersWithBasicAuthHeader = Partial<GetBasicAuthHeaderArgs> & {
|
||||||
|
headers?: Record<string, AxiosHeaderValue> | null;
|
||||||
|
};
|
||||||
|
|
||||||
export const getBasicAuthHeader = ({ username, password }: GetBasicAuthHeaderArgs) => {
|
export const getBasicAuthHeader = ({ username, password }: GetBasicAuthHeaderArgs) => {
|
||||||
const header = `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`;
|
const header = `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`;
|
||||||
|
|
||||||
return { Authorization: header };
|
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 { validateEmptyStrings } from './validate_empty_strings';
|
||||||
export { parseDate } from './parse_date';
|
export { parseDate } from './parse_date';
|
||||||
export type { RelatedSavedObjects } from './related_saved_objects';
|
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 { ServiceParams } from './types';
|
||||||
import * as i18n from './translations';
|
import * as i18n from './translations';
|
||||||
import { request } from '../lib/axios_utils';
|
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> => {
|
const isObject = (value: unknown): value is Record<string, unknown> => {
|
||||||
return isPlainObject(value);
|
return isPlainObject(value);
|
||||||
|
@ -93,13 +93,11 @@ export abstract class SubActionConnector<Config, Secrets> {
|
||||||
auth?: AxiosBasicCredentials,
|
auth?: AxiosBasicCredentials,
|
||||||
headers?: AxiosRequestHeaders
|
headers?: AxiosRequestHeaders
|
||||||
): Record<string, AxiosHeaderValue> {
|
): Record<string, AxiosHeaderValue> {
|
||||||
const headersWithBasicAuth =
|
const headersWithBasicAuth = combineHeadersWithBasicAuthHeader({
|
||||||
auth != null
|
username: auth?.username,
|
||||||
? {
|
password: auth?.password,
|
||||||
...getBasicAuthHeader({ username: auth.username, password: auth.password }),
|
headers,
|
||||||
...headers,
|
});
|
||||||
}
|
|
||||||
: headers;
|
|
||||||
|
|
||||||
return { 'Content-Type': 'application/json', ...headersWithBasicAuth };
|
return { 'Content-Type': 'application/json', ...headersWithBasicAuth };
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue