2018-06-25 17:42:50 +00:00
|
|
|
import { setDefaultProtocol } from 'insomnia-url';
|
2021-07-22 23:04:56 +00:00
|
|
|
|
2017-11-10 10:50:39 +00:00
|
|
|
import * as models from '../../models/index';
|
2018-06-25 17:42:50 +00:00
|
|
|
import { getBasicAuthHeader } from '../basic-auth/get-header';
|
2021-07-22 23:04:56 +00:00
|
|
|
import * as network from '../network';
|
|
|
|
import * as c from './constants';
|
|
|
|
import { responseToObject } from './misc';
|
2017-03-23 22:10:42 +00:00
|
|
|
|
2018-06-25 17:42:50 +00:00
|
|
|
export default async function(
|
2017-11-10 10:50:39 +00:00
|
|
|
requestId: string,
|
|
|
|
accessTokenUrl: string,
|
|
|
|
credentialsInBody: boolean,
|
|
|
|
clientId: string,
|
|
|
|
clientSecret: string,
|
|
|
|
username: string,
|
|
|
|
password: string,
|
2021-05-12 06:35:00 +00:00
|
|
|
scope = '',
|
|
|
|
audience = '',
|
|
|
|
): Promise<Record<string, any>> {
|
2017-03-23 22:10:42 +00:00
|
|
|
const params = [
|
2021-05-12 06:35:00 +00:00
|
|
|
{
|
|
|
|
name: c.P_GRANT_TYPE,
|
|
|
|
value: c.GRANT_TYPE_PASSWORD,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: c.P_USERNAME,
|
|
|
|
value: username,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: c.P_PASSWORD,
|
|
|
|
value: password,
|
|
|
|
},
|
2017-03-23 22:10:42 +00:00
|
|
|
];
|
|
|
|
// Add optional params
|
2021-05-12 06:35:00 +00:00
|
|
|
scope &&
|
|
|
|
params.push({
|
|
|
|
name: c.P_SCOPE,
|
|
|
|
value: scope,
|
|
|
|
});
|
|
|
|
audience &&
|
|
|
|
params.push({
|
|
|
|
name: c.P_AUDIENCE,
|
|
|
|
value: audience,
|
|
|
|
});
|
2017-11-10 10:50:39 +00:00
|
|
|
const headers = [
|
2021-05-12 06:35:00 +00:00
|
|
|
{
|
|
|
|
name: 'Content-Type',
|
|
|
|
value: 'application/x-www-form-urlencoded',
|
|
|
|
},
|
2018-06-25 17:42:50 +00:00
|
|
|
{
|
|
|
|
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) {
|
2021-05-12 06:35:00 +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
|
|
|
}
|
|
|
|
|
2017-05-25 18:31:36 +00:00
|
|
|
const url = setDefaultProtocol(accessTokenUrl);
|
2018-07-01 20:18:18 +00:00
|
|
|
const responsePatch = await network.sendWithSettings(requestId, {
|
2017-11-10 10:50:39 +00:00
|
|
|
url,
|
|
|
|
headers,
|
|
|
|
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);
|
2021-05-12 06:35:00 +00:00
|
|
|
// @ts-expect-error -- TSCONVERSION
|
2017-11-23 21:57:08 +00:00
|
|
|
const bodyBuffer = models.response.getBodyBuffer(response);
|
2021-05-12 06:35:00 +00:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-05-12 06:35:00 +00:00
|
|
|
// @ts-expect-error -- TSCONVERSION
|
2017-11-20 16:07:36 +00:00
|
|
|
const statusCode = response.statusCode || 0;
|
2021-05-12 06:35:00 +00:00
|
|
|
|
2017-11-20 16:07:36 +00:00
|
|
|
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-05-25 18:31:36 +00:00
|
|
|
}
|
|
|
|
|
2017-11-10 10:50:39 +00:00
|
|
|
const results = responseToObject(bodyBuffer.toString(), [
|
2017-03-23 22:10:42 +00:00
|
|
|
c.P_ACCESS_TOKEN,
|
2021-04-05 17:03:34 +00:00
|
|
|
c.P_ID_TOKEN,
|
2017-03-23 22:10:42 +00:00
|
|
|
c.P_TOKEN_TYPE,
|
|
|
|
c.P_EXPIRES_IN,
|
|
|
|
c.P_REFRESH_TOKEN,
|
|
|
|
c.P_SCOPE,
|
2019-09-17 18:59:59 +00:00
|
|
|
c.P_AUDIENCE,
|
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;
|
2017-03-23 22:10:42 +00:00
|
|
|
return results;
|
|
|
|
}
|