fix(HTTP Request Node): Refresh token properly on never fail option (#5861)

* Add handle for simple request options and refresh token

* Remove console.logs

* Add safe check for full response
This commit is contained in:
agobrech 2023-04-05 15:27:04 +02:00 committed by GitHub
parent e7aaa9425a
commit 33c67f45ba
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1117,7 +1117,6 @@ export async function requestOAuth2(
[oAuth2Options.keyToIncludeInAccessTokenHeader]: token.accessToken,
});
}
if (isN8nRequest) {
return this.helpers.httpRequest(newRequestOptions).catch(async (error: AxiosError) => {
if (error.response?.status === 401) {
@ -1179,18 +1178,30 @@ export async function requestOAuth2(
throw error;
});
}
return this.helpers.request(newRequestOptions).catch(async (error: IResponseError) => {
return this.helpers
.request(newRequestOptions)
.then((response) => {
const requestOptions = newRequestOptions as any;
if (
requestOptions.resolveWithFullResponse === true &&
requestOptions.simple === false &&
response.statusCode ===
(oAuth2Options?.tokenExpiredStatusCode === undefined
? 401
: oAuth2Options?.tokenExpiredStatusCode)
) {
throw response;
}
return response;
})
.catch(async (error: IResponseError) => {
const statusCodeReturned =
oAuth2Options?.tokenExpiredStatusCode === undefined
? 401
: oAuth2Options?.tokenExpiredStatusCode;
if (error.statusCode === statusCodeReturned) {
// Token is probably not valid anymore. So try refresh it.
const tokenRefreshOptions: IDataObject = {};
if (oAuth2Options?.includeCredentialsOnRefreshOnBody) {
const body: IDataObject = {
client_id: credentials.clientId,
@ -1202,7 +1213,6 @@ export async function requestOAuth2(
Authorization: '',
};
}
Logger.debug(
`OAuth2 token for "${credentialsType}" used by node "${node.name}" expired. Should revalidate.`,
);
@ -1216,7 +1226,6 @@ export async function requestOAuth2(
} else {
newToken = await token.refresh(tokenRefreshOptions);
}
Logger.debug(
`OAuth2 token for "${credentialsType}" used by node "${node.name}" has been renewed.`,
);