mirror of
https://github.com/Abdulazizzn/n8n-enterprise-unlocked.git
synced 2025-12-17 18:12:04 +00:00
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:
@@ -1117,7 +1117,6 @@ export async function requestOAuth2(
|
|||||||
[oAuth2Options.keyToIncludeInAccessTokenHeader]: token.accessToken,
|
[oAuth2Options.keyToIncludeInAccessTokenHeader]: token.accessToken,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isN8nRequest) {
|
if (isN8nRequest) {
|
||||||
return this.helpers.httpRequest(newRequestOptions).catch(async (error: AxiosError) => {
|
return this.helpers.httpRequest(newRequestOptions).catch(async (error: AxiosError) => {
|
||||||
if (error.response?.status === 401) {
|
if (error.response?.status === 401) {
|
||||||
@@ -1179,18 +1178,30 @@ export async function requestOAuth2(
|
|||||||
throw error;
|
throw error;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
return this.helpers
|
||||||
return this.helpers.request(newRequestOptions).catch(async (error: IResponseError) => {
|
.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 =
|
const statusCodeReturned =
|
||||||
oAuth2Options?.tokenExpiredStatusCode === undefined
|
oAuth2Options?.tokenExpiredStatusCode === undefined
|
||||||
? 401
|
? 401
|
||||||
: oAuth2Options?.tokenExpiredStatusCode;
|
: oAuth2Options?.tokenExpiredStatusCode;
|
||||||
|
|
||||||
if (error.statusCode === statusCodeReturned) {
|
if (error.statusCode === statusCodeReturned) {
|
||||||
// Token is probably not valid anymore. So try refresh it.
|
// Token is probably not valid anymore. So try refresh it.
|
||||||
|
|
||||||
const tokenRefreshOptions: IDataObject = {};
|
const tokenRefreshOptions: IDataObject = {};
|
||||||
|
|
||||||
if (oAuth2Options?.includeCredentialsOnRefreshOnBody) {
|
if (oAuth2Options?.includeCredentialsOnRefreshOnBody) {
|
||||||
const body: IDataObject = {
|
const body: IDataObject = {
|
||||||
client_id: credentials.clientId,
|
client_id: credentials.clientId,
|
||||||
@@ -1202,7 +1213,6 @@ export async function requestOAuth2(
|
|||||||
Authorization: '',
|
Authorization: '',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.debug(
|
Logger.debug(
|
||||||
`OAuth2 token for "${credentialsType}" used by node "${node.name}" expired. Should revalidate.`,
|
`OAuth2 token for "${credentialsType}" used by node "${node.name}" expired. Should revalidate.`,
|
||||||
);
|
);
|
||||||
@@ -1216,7 +1226,6 @@ export async function requestOAuth2(
|
|||||||
} else {
|
} else {
|
||||||
newToken = await token.refresh(tokenRefreshOptions);
|
newToken = await token.refresh(tokenRefreshOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
Logger.debug(
|
Logger.debug(
|
||||||
`OAuth2 token for "${credentialsType}" used by node "${node.name}" has been renewed.`,
|
`OAuth2 token for "${credentialsType}" used by node "${node.name}" has been renewed.`,
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user