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 { sendWithSettings } from '../network';
|
|
|
|
import * as c from './constants';
|
|
|
|
import { responseToObject } from './misc';
|
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,
|
|
|
|
accessTokenUrl: string,
|
|
|
|
credentialsInBody: boolean,
|
|
|
|
clientId: string,
|
|
|
|
clientSecret: string,
|
|
|
|
refreshToken: string,
|
2018-12-12 17:36:11 +00:00
|
|
|
scope: string,
|
2021-08-09 22:30:32 +00:00
|
|
|
origin: string,
|
2021-05-12 06:35:00 +00:00
|
|
|
): 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_REFRESH,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: c.P_REFRESH_TOKEN,
|
|
|
|
value: refreshToken,
|
|
|
|
},
|
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,
|
|
|
|
});
|
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
|
|
|
}
|
|
|
|
|
2021-08-09 22:30:32 +00:00
|
|
|
if (origin) {
|
|
|
|
headers.push({ name: 'Origin', value: origin });
|
|
|
|
}
|
|
|
|
|
2017-11-10 10:50:39 +00:00
|
|
|
const url = setDefaultProtocol(accessTokenUrl);
|
2017-11-21 17:49:17 +00:00
|
|
|
const response = 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
|
|
|
});
|
2017-11-20 16:07:36 +00:00
|
|
|
const statusCode = response.statusCode || 0;
|
2019-10-31 15:58:58 +00:00
|
|
|
const bodyBuffer = models.response.getBodyBuffer(response);
|
2019-01-21 16:38:44 +00:00
|
|
|
|
|
|
|
if (statusCode === 401) {
|
|
|
|
// If the refresh token was rejected due an unauthorized request, we will
|
|
|
|
// return a null access_token to trigger an authentication request to fetch
|
|
|
|
// brand new refresh and access tokens.
|
|
|
|
return responseToObject(null, [c.P_ACCESS_TOKEN]);
|
|
|
|
} else if (statusCode < 200 || statusCode >= 300) {
|
2019-10-31 15:58:58 +00:00
|
|
|
if (bodyBuffer && statusCode === 400) {
|
|
|
|
const response = responseToObject(bodyBuffer.toString(), [c.P_ERROR, c.P_ERROR_DESCRIPTION]);
|
|
|
|
|
|
|
|
// If the refresh token was rejected due an oauth2 invalid_grant error, we will
|
|
|
|
// return a null access_token to trigger an authentication request to fetch
|
|
|
|
// brand new refresh and access tokens.
|
|
|
|
if (response[c.P_ERROR] === 'invalid_grant') {
|
|
|
|
return responseToObject(null, [c.P_ACCESS_TOKEN]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-17 16:42:33 +00:00
|
|
|
throw new Error(`[oauth2] Failed to refresh token url=${url} status=${statusCode}`);
|
2017-11-20 16:07:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!bodyBuffer) {
|
|
|
|
throw new Error(`[oauth2] No body returned from ${url}`);
|
2017-11-10 10:50:39 +00:00
|
|
|
}
|
2017-07-05 21:08:57 +00:00
|
|
|
|
2019-05-10 01:44:22 +00:00
|
|
|
return responseToObject(
|
|
|
|
bodyBuffer.toString(),
|
|
|
|
[
|
|
|
|
c.P_ACCESS_TOKEN,
|
2021-04-05 17:03:34 +00:00
|
|
|
c.P_ID_TOKEN,
|
2019-05-10 01:44:22 +00:00
|
|
|
c.P_REFRESH_TOKEN,
|
|
|
|
c.P_EXPIRES_IN,
|
|
|
|
c.P_TOKEN_TYPE,
|
|
|
|
c.P_SCOPE,
|
|
|
|
c.P_ERROR,
|
|
|
|
c.P_ERROR_URI,
|
|
|
|
c.P_ERROR_DESCRIPTION,
|
|
|
|
],
|
|
|
|
{
|
|
|
|
// Refresh token is optional, so we'll default it to the existing value
|
|
|
|
[c.P_REFRESH_TOKEN]: refreshToken,
|
|
|
|
},
|
|
|
|
);
|
2017-03-23 22:10:42 +00:00
|
|
|
}
|