insomnia/app/network/authentication.js
Gregory Schier 62719f3201 More 5.0 Fixes and Stuff (#134)
* Fix OAuth token, new request content-type, and more

* better error messaging around invalid Curl opts

* Make XFERINFOFUNCTION an optional option

* Fix content-type change prompt and double quotes in prettify

* Don't send Expect header on file upload

* Remove dead code

* Base64 tag

* Fix HTML entities in URL clicks

* Fix curl paste
2017-04-07 11:10:15 -07:00

38 lines
911 B
JavaScript

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) {
const token = oAuth2Token.accessToken;
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};
}