feat: Allow a server requestOptions.auth to be a function that returns the Authorization header. (#928)

This commit is contained in:
Jody Zeitler 2019-10-06 20:07:44 -05:00 committed by Danny Brown
parent 74ce89b22b
commit 0064a4b1cd
2 changed files with 16 additions and 19 deletions

View File

@ -9,7 +9,7 @@ import user from '../user';
* @export * @export
* @param {Object} [server={}] * @param {Object} [server={}]
* @param {Object} [server.requestOptions] * @param {Object} [server.requestOptions]
* @param {string} [server.requestOptions.auth] * @param {string|function} [server.requestOptions.auth]
* @returns {Object} { Authorization } * @returns {Object} { Authorization }
*/ */
export default function getAuthorizationHeader({ requestOptions } = {}) { export default function getAuthorizationHeader({ requestOptions } = {}) {
@ -19,8 +19,13 @@ export default function getAuthorizationHeader({ requestOptions } = {}) {
const accessToken = user && user.getAccessToken && user.getAccessToken(); const accessToken = user && user.getAccessToken && user.getAccessToken();
if (requestOptions && requestOptions.auth) { if (requestOptions && requestOptions.auth) {
if (typeof requestOptions.auth === 'function') {
// Custom Auth Header
headers.Authorization = requestOptions.auth(requestOptions);
} else {
// HTTP Basic Auth (user:password) // HTTP Basic Auth (user:password)
headers.Authorization = `Basic ${btoa(requestOptions.auth)}`; headers.Authorization = `Basic ${btoa(requestOptions.auth)}`;
}
} else if (accessToken) { } else if (accessToken) {
headers.Authorization = `Bearer ${accessToken}`; headers.Authorization = `Bearer ${accessToken}`;
} }

View File

@ -7,10 +7,7 @@ describe('getAuthorizationHeader', () => {
it('should return a HTTP Basic Auth when server contains requestOptions.auth', () => { it('should return a HTTP Basic Auth when server contains requestOptions.auth', () => {
const validServer = { const validServer = {
requestOptions: { requestOptions: {
auth: { auth: 'dummy_user:dummy_password',
user: 'dummy_user',
password: 'dummy_password',
},
}, },
}; };
@ -26,9 +23,7 @@ describe('getAuthorizationHeader', () => {
it('should return a HTTP Basic Auth when server contains requestOptions.auth even though there is no password', () => { it('should return a HTTP Basic Auth when server contains requestOptions.auth even though there is no password', () => {
const validServerWithoutPassword = { const validServerWithoutPassword = {
requestOptions: { requestOptions: {
auth: { auth: 'dummy_user',
user: 'dummy_user',
},
}, },
}; };
@ -43,22 +38,19 @@ describe('getAuthorizationHeader', () => {
expect(authentication).toEqual(expectedAuthorizationHeader); expect(authentication).toEqual(expectedAuthorizationHeader);
}); });
it('should return a HTTP Basic Auth when server contains requestOptions.auth even though there is no username', () => { it('should return a HTTP Basic Auth when server contains requestOptions.auth custom function', () => {
const validServerWithoutPassword = { const validServerCustomAuth = {
requestOptions: { requestOptions: {
auth: { auth: options => `Basic ${options.token}`,
user: 'dummy_user', token: 'ZHVtbXlfdXNlcjpkdW1teV9wYXNzd29yZA==',
},
}, },
}; };
const expectedAuthorizationHeader = { const expectedAuthorizationHeader = {
Authorization: `Basic ${btoa( Authorization: `Basic ${validServerCustomAuth.requestOptions.token}`,
validServerWithoutPassword.requestOptions.auth
)}`,
}; };
const authentication = getAuthorizationHeader(validServerWithoutPassword); const authentication = getAuthorizationHeader(validServerCustomAuth);
expect(authentication).toEqual(expectedAuthorizationHeader); expect(authentication).toEqual(expectedAuthorizationHeader);
}); });