Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | import 'isomorphic-base64';
import {UserAccountInterface} from '../user';
import { HeadersInterface, RequestOptions } from '../types/RequestHeaders';
/**
* Returns the Authorization header as part of an Object.
*
* @export
* @param {Object} [server={}]
* @param {Object} [requestOptions]
* @param {string|function} [requestOptions.auth]
* @param {Object} [user]
* @param {function} [user.getAccessToken]
* @returns {Object} { Authorization }
*/
export default function getAuthorizationHeader(
{requestOptions}: RequestOptions = {},
user: UserAccountInterface = {}): HeadersInterface
{
const headers: HeadersInterface = {};
// Check for OHIF.user since this can also be run on the server
const accessToken = user && user.getAccessToken && user.getAccessToken();
// Auth for a specific server
if (requestOptions?.auth) {
if (typeof requestOptions.auth === 'function') {
// Custom Auth Header
headers.Authorization = requestOptions.auth(requestOptions);
} else {
// HTTP Basic Auth (user:password)
headers.Authorization = `Basic ${btoa(requestOptions.auth)}`;
}
} else Iif (accessToken) {
// Auth for the user's default
headers.Authorization = `Bearer ${accessToken}`;
}
return headers;
}
|