2017-03-23 22:10:42 +00:00
|
|
|
import {AUTH_BASIC, AUTH_OAUTH_2} from '../common/constants';
|
|
|
|
import {getBasicAuthHeader} from '../common/misc';
|
|
|
|
import getOAuth2Token from './o-auth-2/get-token';
|
|
|
|
|
|
|
|
export async function getAuthHeader (requestId, authentication) {
|
|
|
|
if (authentication.disabled) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (authentication.type === AUTH_BASIC) {
|
|
|
|
const {username, password} = authentication;
|
|
|
|
return getBasicAuthHeader(username, password);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (authentication.type === AUTH_OAUTH_2) {
|
|
|
|
const oAuth2Token = await getOAuth2Token(requestId, authentication);
|
|
|
|
if (oAuth2Token) {
|
2017-04-07 18:10:15 +00:00
|
|
|
const token = oAuth2Token.accessToken;
|
2017-03-23 22:10:42 +00:00
|
|
|
return _buildBearerHeader(token);
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
function _buildBearerHeader (accessToken) {
|
|
|
|
if (!accessToken) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const name = 'Authorization';
|
|
|
|
const value = `Bearer ${accessToken}`;
|
|
|
|
|
|
|
|
return {name, value};
|
|
|
|
}
|