feat: Allow a server requestOptions.auth to be a function that returns the Authorization header. (#928)
This commit is contained in:
parent
74ce89b22b
commit
0064a4b1cd
@ -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) {
|
||||||
// HTTP Basic Auth (user:password)
|
if (typeof requestOptions.auth === 'function') {
|
||||||
headers.Authorization = `Basic ${btoa(requestOptions.auth)}`;
|
// Custom Auth Header
|
||||||
|
headers.Authorization = requestOptions.auth(requestOptions);
|
||||||
|
} else {
|
||||||
|
// HTTP Basic Auth (user:password)
|
||||||
|
headers.Authorization = `Basic ${btoa(requestOptions.auth)}`;
|
||||||
|
}
|
||||||
} else if (accessToken) {
|
} else if (accessToken) {
|
||||||
headers.Authorization = `Bearer ${accessToken}`;
|
headers.Authorization = `Bearer ${accessToken}`;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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);
|
||||||
});
|
});
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user