Create getBasicAuthHeader util

This commit is contained in:
Christos Nasikas 2024-05-10 17:53:34 +03:00
parent f7a2caf38d
commit fad6bde6af
2 changed files with 33 additions and 0 deletions

View file

@ -0,0 +1,16 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
import { 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')}`,
});
});
});

View file

@ -0,0 +1,17 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License
* 2.0; you may not use this file except in compliance with the Elastic License
* 2.0.
*/
interface GetBasicAuthHeaderArgs {
username: string;
password: string;
}
export const getBasicAuthHeader = ({ username, password }: GetBasicAuthHeaderArgs) => {
const header = `Basic ${Buffer.from(`${username}:${password}`).toString('base64')}`;
return { Authorization: header };
};