2017-11-06 20:44:55 +00:00
|
|
|
// @flow
|
2017-11-07 18:14:08 +00:00
|
|
|
import {AUTH_ASAP, AUTH_BASIC, AUTH_BEARER, AUTH_HAWK, AUTH_OAUTH_1, AUTH_OAUTH_2} from '../common/constants';
|
2017-03-23 22:10:42 +00:00
|
|
|
import getOAuth2Token from './o-auth-2/get-token';
|
2017-11-06 19:26:31 +00:00
|
|
|
import getOAuth1Token from './o-auth-1/get-token';
|
2017-08-21 17:43:12 +00:00
|
|
|
import * as Hawk from 'hawk';
|
2017-11-07 18:14:08 +00:00
|
|
|
import jwtAuthentication from 'jwt-authentication';
|
2017-11-06 20:44:55 +00:00
|
|
|
import type {RequestAuthentication} from '../models/request';
|
2017-11-26 20:45:40 +00:00
|
|
|
import {getBasicAuthHeader} from './basic-auth/get-header';
|
|
|
|
import {getBearerAuthHeader} from './bearer-auth/get-header';
|
2017-03-23 22:10:42 +00:00
|
|
|
|
2017-11-06 20:44:55 +00:00
|
|
|
type Header = {
|
|
|
|
name: string,
|
|
|
|
value: string
|
|
|
|
};
|
|
|
|
|
|
|
|
export async function getAuthHeader (
|
|
|
|
requestId: string,
|
|
|
|
url: string,
|
|
|
|
method: string,
|
|
|
|
authentication: RequestAuthentication
|
|
|
|
): Promise<Header | null> {
|
2017-03-23 22:10:42 +00:00
|
|
|
if (authentication.disabled) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (authentication.type === AUTH_BASIC) {
|
|
|
|
const {username, password} = authentication;
|
|
|
|
return getBasicAuthHeader(username, password);
|
|
|
|
}
|
|
|
|
|
2017-06-01 13:18:42 +00:00
|
|
|
if (authentication.type === AUTH_BEARER) {
|
|
|
|
const {token} = authentication;
|
|
|
|
return getBearerAuthHeader(token);
|
|
|
|
}
|
|
|
|
|
2017-03-23 22:10:42 +00:00
|
|
|
if (authentication.type === AUTH_OAUTH_2) {
|
2018-03-29 02:32:09 +00:00
|
|
|
// 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.
|
|
|
|
const tokenId = requestId.match(/\.graphql$/)
|
|
|
|
? requestId.replace(/\.graphql$/, '')
|
|
|
|
: requestId;
|
|
|
|
|
|
|
|
const oAuth2Token = await getOAuth2Token(tokenId, authentication);
|
2017-03-23 22:10:42 +00:00
|
|
|
if (oAuth2Token) {
|
2017-04-07 18:10:15 +00:00
|
|
|
const token = oAuth2Token.accessToken;
|
2017-08-10 19:34:33 +00:00
|
|
|
return _buildBearerHeader(token, authentication.tokenPrefix);
|
2017-03-23 22:10:42 +00:00
|
|
|
} 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
|
|
|
|
};
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-21 17:43:12 +00:00
|
|
|
if (authentication.type === AUTH_HAWK) {
|
|
|
|
const {id, key, algorithm} = authentication;
|
2017-11-26 20:45:40 +00:00
|
|
|
const header = Hawk.client.header(url, method, {credentials: {id, key, algorithm}});
|
2017-08-21 17:43:12 +00:00
|
|
|
return {
|
|
|
|
name: 'Authorization',
|
|
|
|
value: header.field
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2017-11-07 18:14:08 +00:00
|
|
|
if (authentication.type === AUTH_ASAP) {
|
2018-03-28 23:27:21 +00:00
|
|
|
const {issuer, subject, audience, keyId, additionalClaims, privateKey} = authentication;
|
2017-11-07 18:19:16 +00:00
|
|
|
|
2017-11-07 18:14:08 +00:00
|
|
|
const generator = jwtAuthentication.client.create();
|
2018-03-28 23:27:21 +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') {
|
|
|
|
throw new Error(`additional-claims must be an object recieved: '${typeof parsedAdditionalClaims}' instead`);
|
|
|
|
}
|
|
|
|
|
|
|
|
claims = Object.assign(parsedAdditionalClaims, claims);
|
|
|
|
}
|
|
|
|
|
2017-11-07 18:14:08 +00:00
|
|
|
const options = {
|
|
|
|
privateKey,
|
|
|
|
kid: keyId
|
|
|
|
};
|
|
|
|
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
generator.generateAuthorizationHeader(claims, options, (error, headerValue) => {
|
|
|
|
if (error) {
|
|
|
|
reject(error);
|
|
|
|
} else {
|
|
|
|
resolve({
|
|
|
|
name: 'Authorization',
|
|
|
|
value: headerValue
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-03-23 22:10:42 +00:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-08-10 19:34:33 +00:00
|
|
|
function _buildBearerHeader (accessToken, prefix) {
|
2017-03-23 22:10:42 +00:00
|
|
|
if (!accessToken) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const name = 'Authorization';
|
2017-08-10 19:34:33 +00:00
|
|
|
const value = `${prefix || 'Bearer'} ${accessToken}`;
|
2017-03-23 22:10:42 +00:00
|
|
|
|
|
|
|
return {name, value};
|
|
|
|
}
|