insomnia/packages/insomnia-app/app/network/authentication.js

139 lines
3.7 KiB
JavaScript
Raw Normal View History

2017-11-06 20:44:55 +00:00
// @flow
2018-06-25 17:42:50 +00:00
import {
AUTH_ASAP,
AUTH_BASIC,
AUTH_BEARER,
AUTH_HAWK,
AUTH_OAUTH_1,
AUTH_OAUTH_2,
2018-06-25 17:42:50 +00:00
} from '../common/constants';
import getOAuth2Token from './o-auth-2/get-token';
2017-11-06 19:26:31 +00:00
import getOAuth1Token from './o-auth-1/get-token';
import * as Hawk from 'hawk';
import jwtAuthentication from 'jwt-authentication';
2018-06-25 17:42:50 +00:00
import type { RequestAuthentication } from '../models/request';
import { getBasicAuthHeader } from './basic-auth/get-header';
import { getBearerAuthHeader } from './bearer-auth/get-header';
2017-11-06 20:44:55 +00:00
type Header = {
name: string,
value: string,
2017-11-06 20:44:55 +00:00
};
2018-06-25 17:42:50 +00:00
export async function getAuthHeader(
2017-11-06 20:44:55 +00:00
requestId: string,
url: string,
method: string,
authentication: RequestAuthentication,
2017-11-06 20:44:55 +00:00
): Promise<Header | null> {
if (authentication.disabled) {
return null;
}
if (authentication.type === AUTH_BASIC) {
2018-06-25 17:42:50 +00:00
const { username, password } = authentication;
return getBasicAuthHeader(username, password);
}
if (authentication.type === AUTH_BEARER) {
2018-06-25 17:42:50 +00:00
const { token, prefix } = authentication;
return getBearerAuthHeader(token, prefix);
}
if (authentication.type === AUTH_OAUTH_2) {
// HACK: GraphQL requests use a child request to fetch the schema with an
// ID of "{{request_id}}.graphql". Here we are removing the .graphql suffix and
// pretending we are fetching a token for the original request. This makes sure
// the same tokens are used for schema fetching. See issue #835 on GitHub.
2018-10-17 16:42:33 +00:00
const tokenId = requestId.match(/\.graphql$/) ? requestId.replace(/\.graphql$/, '') : requestId;
const oAuth2Token = await getOAuth2Token(tokenId, authentication);
if (oAuth2Token) {
const token = oAuth2Token.accessToken;
return _buildBearerHeader(token, authentication.tokenPrefix);
} else {
return null;
}
}
2017-11-06 19:26:31 +00:00
if (authentication.type === AUTH_OAUTH_1) {
const oAuth1Token = await getOAuth1Token(url, method, authentication);
if (oAuth1Token) {
return {
name: 'Authorization',
value: oAuth1Token.Authorization,
2017-11-06 19:26:31 +00:00
};
} else {
return null;
}
}
if (authentication.type === AUTH_HAWK) {
2018-11-18 02:04:03 +00:00
const { id, key, algorithm, ext } = authentication;
2018-06-25 17:42:50 +00:00
const header = Hawk.client.header(url, method, {
2018-11-18 02:04:03 +00:00
credentials: { id, key, algorithm },
ext: ext,
2018-06-25 17:42:50 +00:00
});
return {
name: 'Authorization',
value: header.field,
};
}
if (authentication.type === AUTH_ASAP) {
2018-10-17 16:42:33 +00:00
const { issuer, subject, audience, keyId, additionalClaims, privateKey } = authentication;
2017-11-07 18:19:16 +00:00
const generator = jwtAuthentication.client.create();
2018-06-25 17:42:50 +00:00
let claims = { iss: issuer, sub: subject, aud: audience };
let parsedAdditionalClaims;
try {
parsedAdditionalClaims = JSON.parse(additionalClaims || '{}');
} catch (err) {
throw new Error(`Unable to parse additional-claims: ${err}`);
}
if (parsedAdditionalClaims) {
if (typeof parsedAdditionalClaims !== 'object') {
2018-06-25 17:42:50 +00:00
throw new Error(
`additional-claims must be an object received: '${typeof parsedAdditionalClaims}' instead`,
2018-06-25 17:42:50 +00:00
);
}
claims = Object.assign(parsedAdditionalClaims, claims);
}
const options = {
privateKey,
kid: keyId,
};
return new Promise((resolve, reject) => {
2018-10-17 16:42:33 +00:00
generator.generateAuthorizationHeader(claims, options, (error, headerValue) => {
if (error) {
reject(error);
} else {
resolve({
name: 'Authorization',
value: headerValue,
2018-10-17 16:42:33 +00:00
});
}
2018-10-17 16:42:33 +00:00
});
});
}
return null;
}
2018-06-25 17:42:50 +00:00
function _buildBearerHeader(accessToken, prefix) {
if (!accessToken) {
return null;
}
const name = 'Authorization';
const value = `${prefix || 'Bearer'} ${accessToken}`;
2018-06-25 17:42:50 +00:00
return { name, value };
}