mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-25 04:34:06 -08: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:
parent
e7aaa9425a
commit
33c67f45ba
|
@ -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,85 +1178,95 @@ export async function requestOAuth2(
|
|||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
return this.helpers.request(newRequestOptions).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,
|
||||
client_secret: credentials.clientSecret,
|
||||
};
|
||||
tokenRefreshOptions.body = body;
|
||||
// Override authorization property so the credentials are not included in it
|
||||
tokenRefreshOptions.headers = {
|
||||
Authorization: '',
|
||||
};
|
||||
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;
|
||||
}
|
||||
|
||||
Logger.debug(
|
||||
`OAuth2 token for "${credentialsType}" used by node "${node.name}" expired. Should revalidate.`,
|
||||
);
|
||||
|
||||
let newToken;
|
||||
|
||||
// if it's OAuth2 with client credentials grant type, get a new token
|
||||
// instead of refreshing it.
|
||||
if (OAuth2GrantType.clientCredentials === credentials.grantType) {
|
||||
newToken = await getClientCredentialsToken(token.client, credentials);
|
||||
} else {
|
||||
newToken = await token.refresh(tokenRefreshOptions);
|
||||
}
|
||||
|
||||
Logger.debug(
|
||||
`OAuth2 token for "${credentialsType}" used by node "${node.name}" has been renewed.`,
|
||||
);
|
||||
|
||||
credentials.oauthTokenData = newToken.data;
|
||||
|
||||
// Find the credentials
|
||||
if (!node.credentials?.[credentialsType]) {
|
||||
throw new Error(
|
||||
`The node "${node.name}" does not have credentials of type "${credentialsType}"!`,
|
||||
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,
|
||||
client_secret: credentials.clientSecret,
|
||||
};
|
||||
tokenRefreshOptions.body = body;
|
||||
// Override authorization property so the credentials are not included in it
|
||||
tokenRefreshOptions.headers = {
|
||||
Authorization: '',
|
||||
};
|
||||
}
|
||||
Logger.debug(
|
||||
`OAuth2 token for "${credentialsType}" used by node "${node.name}" expired. Should revalidate.`,
|
||||
);
|
||||
}
|
||||
const nodeCredentials = node.credentials[credentialsType];
|
||||
|
||||
// Save the refreshed token
|
||||
await additionalData.credentialsHelper.updateCredentials(
|
||||
nodeCredentials,
|
||||
credentialsType,
|
||||
credentials as unknown as ICredentialDataDecryptedObject,
|
||||
);
|
||||
let newToken;
|
||||
|
||||
Logger.debug(
|
||||
`OAuth2 token for "${credentialsType}" used by node "${node.name}" has been saved to database successfully.`,
|
||||
);
|
||||
// if it's OAuth2 with client credentials grant type, get a new token
|
||||
// instead of refreshing it.
|
||||
if (OAuth2GrantType.clientCredentials === credentials.grantType) {
|
||||
newToken = await getClientCredentialsToken(token.client, credentials);
|
||||
} else {
|
||||
newToken = await token.refresh(tokenRefreshOptions);
|
||||
}
|
||||
Logger.debug(
|
||||
`OAuth2 token for "${credentialsType}" used by node "${node.name}" has been renewed.`,
|
||||
);
|
||||
|
||||
// Make the request again with the new token
|
||||
const newRequestOptions = newToken.sign(requestOptions as clientOAuth2.RequestObject);
|
||||
newRequestOptions.headers = newRequestOptions.headers ?? {};
|
||||
credentials.oauthTokenData = newToken.data;
|
||||
|
||||
if (oAuth2Options?.keyToIncludeInAccessTokenHeader) {
|
||||
Object.assign(newRequestOptions.headers, {
|
||||
[oAuth2Options.keyToIncludeInAccessTokenHeader]: token.accessToken,
|
||||
});
|
||||
// Find the credentials
|
||||
if (!node.credentials?.[credentialsType]) {
|
||||
throw new Error(
|
||||
`The node "${node.name}" does not have credentials of type "${credentialsType}"!`,
|
||||
);
|
||||
}
|
||||
const nodeCredentials = node.credentials[credentialsType];
|
||||
|
||||
// Save the refreshed token
|
||||
await additionalData.credentialsHelper.updateCredentials(
|
||||
nodeCredentials,
|
||||
credentialsType,
|
||||
credentials as unknown as ICredentialDataDecryptedObject,
|
||||
);
|
||||
|
||||
Logger.debug(
|
||||
`OAuth2 token for "${credentialsType}" used by node "${node.name}" has been saved to database successfully.`,
|
||||
);
|
||||
|
||||
// Make the request again with the new token
|
||||
const newRequestOptions = newToken.sign(requestOptions as clientOAuth2.RequestObject);
|
||||
newRequestOptions.headers = newRequestOptions.headers ?? {};
|
||||
|
||||
if (oAuth2Options?.keyToIncludeInAccessTokenHeader) {
|
||||
Object.assign(newRequestOptions.headers, {
|
||||
[oAuth2Options.keyToIncludeInAccessTokenHeader]: token.accessToken,
|
||||
});
|
||||
}
|
||||
|
||||
return this.helpers.request(newRequestOptions);
|
||||
}
|
||||
|
||||
return this.helpers.request(newRequestOptions);
|
||||
}
|
||||
|
||||
// Unknown error so simply throw it
|
||||
throw error;
|
||||
});
|
||||
// Unknown error so simply throw it
|
||||
throw error;
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Reference in a new issue