2017-11-10 10:50:39 +00:00
|
|
|
// @flow
|
2018-06-25 17:42:50 +00:00
|
|
|
import { parse as urlParse } from 'url';
|
2017-03-23 22:10:42 +00:00
|
|
|
import * as c from './constants';
|
2018-10-17 16:42:33 +00:00
|
|
|
import { buildQueryStringFromParams, joinUrlAndQueryString } from 'insomnia-url';
|
2018-06-25 17:42:50 +00:00
|
|
|
import { authorizeUserInWindow, responseToObject } from './misc';
|
|
|
|
import { escapeRegex } from '../../common/misc';
|
2017-11-10 10:50:39 +00:00
|
|
|
import * as models from '../../models/index';
|
2018-06-25 17:42:50 +00:00
|
|
|
import { sendWithSettings } from '../network';
|
|
|
|
import { getBasicAuthHeader } from '../basic-auth/get-header';
|
2017-11-10 10:50:39 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export default async function(
|
2017-11-10 10:50:39 +00:00
|
|
|
requestId: string,
|
|
|
|
authorizeUrl: string,
|
|
|
|
accessTokenUrl: string,
|
|
|
|
credentialsInBody: boolean,
|
|
|
|
clientId: string,
|
|
|
|
clientSecret: string,
|
|
|
|
redirectUri: string = '',
|
|
|
|
scope: string = '',
|
2018-12-12 17:36:11 +00:00
|
|
|
state: string = '',
|
2019-11-22 17:48:34 +00:00
|
|
|
audience: string = '',
|
|
|
|
resource: string = '',
|
2017-11-10 10:50:39 +00:00
|
|
|
): Promise<Object> {
|
2017-03-28 22:45:23 +00:00
|
|
|
if (!authorizeUrl) {
|
|
|
|
throw new Error('Invalid authorization URL');
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!accessTokenUrl) {
|
|
|
|
throw new Error('Invalid access token URL');
|
|
|
|
}
|
|
|
|
|
2019-11-22 17:48:34 +00:00
|
|
|
const authorizeResults = await _authorize(
|
|
|
|
authorizeUrl,
|
|
|
|
clientId,
|
|
|
|
redirectUri,
|
|
|
|
scope,
|
|
|
|
state,
|
|
|
|
audience,
|
|
|
|
resource,
|
|
|
|
);
|
2017-03-23 22:10:42 +00:00
|
|
|
|
2018-01-17 06:04:47 +00:00
|
|
|
// Handle the error
|
|
|
|
if (authorizeResults[c.P_ERROR]) {
|
|
|
|
const code = authorizeResults[c.P_ERROR];
|
|
|
|
const msg = authorizeResults[c.P_ERROR_DESCRIPTION];
|
|
|
|
const uri = authorizeResults[c.P_ERROR_URI];
|
|
|
|
throw new Error(`OAuth 2.0 Error ${code}\n\n${msg}\n\n${uri}`);
|
|
|
|
}
|
2017-03-23 22:10:42 +00:00
|
|
|
|
2018-01-17 06:04:47 +00:00
|
|
|
return _getToken(
|
2017-11-10 10:50:39 +00:00
|
|
|
requestId,
|
2017-03-23 22:10:42 +00:00
|
|
|
accessTokenUrl,
|
|
|
|
credentialsInBody,
|
|
|
|
clientId,
|
|
|
|
clientSecret,
|
|
|
|
authorizeResults[c.P_CODE],
|
|
|
|
redirectUri,
|
2018-12-12 17:36:11 +00:00
|
|
|
state,
|
2019-11-22 17:48:34 +00:00
|
|
|
audience,
|
|
|
|
resource,
|
2017-03-23 22:10:42 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-22 17:48:34 +00:00
|
|
|
async function _authorize(
|
|
|
|
url,
|
|
|
|
clientId,
|
|
|
|
redirectUri = '',
|
|
|
|
scope = '',
|
|
|
|
state = '',
|
|
|
|
audience = '',
|
|
|
|
resource = '',
|
|
|
|
) {
|
2017-03-23 22:10:42 +00:00
|
|
|
const params = [
|
2018-06-25 17:42:50 +00:00
|
|
|
{ name: c.P_RESPONSE_TYPE, value: c.RESPONSE_TYPE_CODE },
|
2018-12-12 17:36:11 +00:00
|
|
|
{ name: c.P_CLIENT_ID, value: clientId },
|
2017-03-23 22:10:42 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
// Add optional params
|
2018-06-25 17:42:50 +00:00
|
|
|
redirectUri && params.push({ name: c.P_REDIRECT_URI, value: redirectUri });
|
|
|
|
scope && params.push({ name: c.P_SCOPE, value: scope });
|
|
|
|
state && params.push({ name: c.P_STATE, value: state });
|
2019-11-22 17:48:34 +00:00
|
|
|
audience && params.push({ name: c.P_AUDIENCE, value: audience });
|
|
|
|
resource && params.push({ name: c.P_RESOURCE, value: resource });
|
2017-03-23 22:10:42 +00:00
|
|
|
|
|
|
|
// Add query params to URL
|
2017-11-26 20:45:40 +00:00
|
|
|
const qs = buildQueryStringFromParams(params);
|
|
|
|
const finalUrl = joinUrlAndQueryString(url, qs);
|
2018-01-17 04:20:45 +00:00
|
|
|
const successRegex = new RegExp(`${escapeRegex(redirectUri)}.*(code=)`, 'i');
|
|
|
|
const failureRegex = new RegExp(`${escapeRegex(redirectUri)}.*(error=)`, 'i');
|
2017-03-23 22:10:42 +00:00
|
|
|
|
2018-10-17 16:42:33 +00:00
|
|
|
const redirectedTo = await authorizeUserInWindow(finalUrl, successRegex, failureRegex);
|
2017-03-23 22:10:42 +00:00
|
|
|
|
2017-11-22 00:07:28 +00:00
|
|
|
console.log('[oauth2] Detected redirect ' + redirectedTo);
|
2017-11-03 16:25:12 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
const { query } = urlParse(redirectedTo);
|
2017-03-23 22:10:42 +00:00
|
|
|
return responseToObject(query, [
|
|
|
|
c.P_CODE,
|
|
|
|
c.P_STATE,
|
|
|
|
c.P_ERROR,
|
|
|
|
c.P_ERROR_DESCRIPTION,
|
2018-12-12 17:36:11 +00:00
|
|
|
c.P_ERROR_URI,
|
2017-03-23 22:10:42 +00:00
|
|
|
]);
|
|
|
|
}
|
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
async function _getToken(
|
2017-11-10 10:50:39 +00:00
|
|
|
requestId: string,
|
|
|
|
url: string,
|
|
|
|
credentialsInBody: boolean,
|
|
|
|
clientId: string,
|
|
|
|
clientSecret: string,
|
|
|
|
code: string,
|
|
|
|
redirectUri: string = '',
|
2018-12-12 17:36:11 +00:00
|
|
|
state: string = '',
|
2019-11-22 17:48:34 +00:00
|
|
|
audience: string = '',
|
|
|
|
resource: string = '',
|
2017-11-10 10:50:39 +00:00
|
|
|
): Promise<Object> {
|
2017-03-23 22:10:42 +00:00
|
|
|
const params = [
|
2018-06-25 17:42:50 +00:00
|
|
|
{ name: c.P_GRANT_TYPE, value: c.GRANT_TYPE_AUTHORIZATION_CODE },
|
2018-12-12 17:36:11 +00:00
|
|
|
{ name: c.P_CODE, value: code },
|
2017-03-23 22:10:42 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
// Add optional params
|
2018-06-25 17:42:50 +00:00
|
|
|
redirectUri && params.push({ name: c.P_REDIRECT_URI, value: redirectUri });
|
|
|
|
state && params.push({ name: c.P_STATE, value: state });
|
2019-11-22 17:48:34 +00:00
|
|
|
audience && params.push({ name: c.P_AUDIENCE, value: audience });
|
|
|
|
resource && params.push({ name: c.P_RESOURCE, value: resource });
|
2017-03-23 22:10:42 +00:00
|
|
|
|
2017-11-10 10:50:39 +00:00
|
|
|
const headers = [
|
2018-06-25 17:42:50 +00:00
|
|
|
{ name: 'Content-Type', value: 'application/x-www-form-urlencoded' },
|
|
|
|
{
|
|
|
|
name: 'Accept',
|
2018-12-12 17:36:11 +00:00
|
|
|
value: 'application/x-www-form-urlencoded, application/json',
|
|
|
|
},
|
2017-11-10 10:50:39 +00:00
|
|
|
];
|
2017-03-23 22:10:42 +00:00
|
|
|
|
|
|
|
if (credentialsInBody) {
|
2018-06-25 17:42:50 +00:00
|
|
|
params.push({ name: c.P_CLIENT_ID, value: clientId });
|
|
|
|
params.push({ name: c.P_CLIENT_SECRET, value: clientSecret });
|
2017-03-23 22:10:42 +00:00
|
|
|
} else {
|
2017-11-10 10:50:39 +00:00
|
|
|
headers.push(getBasicAuthHeader(clientId, clientSecret));
|
2017-03-23 22:10:42 +00:00
|
|
|
}
|
|
|
|
|
2018-07-01 20:18:18 +00:00
|
|
|
const responsePatch = await sendWithSettings(requestId, {
|
2017-11-10 10:50:39 +00:00
|
|
|
headers,
|
|
|
|
url,
|
2017-03-23 22:10:42 +00:00
|
|
|
method: 'POST',
|
2018-12-12 17:36:11 +00:00
|
|
|
body: models.request.newBodyFormUrlEncoded(params),
|
2017-11-10 10:50:39 +00:00
|
|
|
});
|
|
|
|
|
2018-07-01 20:18:18 +00:00
|
|
|
const response = await models.response.create(responsePatch);
|
|
|
|
|
2017-11-23 21:57:08 +00:00
|
|
|
const bodyBuffer = models.response.getBodyBuffer(response);
|
2017-11-20 16:07:36 +00:00
|
|
|
if (!bodyBuffer) {
|
2018-07-01 20:18:18 +00:00
|
|
|
return {
|
|
|
|
[c.X_ERROR]: `No body returned from ${url}`,
|
2018-12-12 17:36:11 +00:00
|
|
|
[c.X_RESPONSE_ID]: response._id,
|
2018-07-01 20:18:18 +00:00
|
|
|
};
|
2017-11-20 16:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const statusCode = response.statusCode || 0;
|
|
|
|
if (statusCode < 200 || statusCode >= 300) {
|
2018-07-01 20:18:18 +00:00
|
|
|
return {
|
|
|
|
[c.X_ERROR]: `Failed to fetch token url=${url} status=${statusCode}`,
|
2018-12-12 17:36:11 +00:00
|
|
|
[c.X_RESPONSE_ID]: response._id,
|
2018-07-01 20:18:18 +00:00
|
|
|
};
|
2017-11-10 10:50:39 +00:00
|
|
|
}
|
2017-03-23 22:10:42 +00:00
|
|
|
|
2018-07-01 20:18:18 +00:00
|
|
|
const results = responseToObject(bodyBuffer.toString('utf8'), [
|
2017-03-23 22:10:42 +00:00
|
|
|
c.P_ACCESS_TOKEN,
|
|
|
|
c.P_REFRESH_TOKEN,
|
|
|
|
c.P_EXPIRES_IN,
|
|
|
|
c.P_TOKEN_TYPE,
|
|
|
|
c.P_SCOPE,
|
2019-11-22 17:48:34 +00:00
|
|
|
c.P_AUDIENCE,
|
|
|
|
c.P_RESOURCE,
|
2017-03-23 22:10:42 +00:00
|
|
|
c.P_ERROR,
|
|
|
|
c.P_ERROR_URI,
|
2018-12-12 17:36:11 +00:00
|
|
|
c.P_ERROR_DESCRIPTION,
|
2017-03-23 22:10:42 +00:00
|
|
|
]);
|
2018-07-01 20:18:18 +00:00
|
|
|
|
|
|
|
results[c.X_RESPONSE_ID] = response._id;
|
|
|
|
|
|
|
|
return results;
|
2017-03-23 22:10:42 +00:00
|
|
|
}
|