insomnia/app/network/authentication.js
Gregory Schier 3f5e7e2e14 First-Party OAuth 2.0 Support (#120)
* Proof of concept authorize call

* Authorize and Refresh endpoints done

* OAuth2 editor started

* Some small fixes

* Set OAuth headers on request

* Started on some OAuth tests

* Updated network logic with new OAuth API

* OAuth forms and refactor flows

* Fix grant type handling

* Moved auth handling out of render pipeline

* Fixed legacy auth header

* Fix vertical center

* Prompt user on auth type change

* Refresh tokens working (I think) and better UI

* Catch same type auth change

* POC refresh token and small refactor

* Better token handling

* LOading state to token refresh

* Show o-auth-2 errors

* Some minor updates
2017-03-23 15:10:42 -07:00

38 lines
939 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.refreshToken || 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};
}