mirror of
https://github.com/n8n-io/n8n.git
synced 2024-12-23 11:44:06 -08:00
⚡ Fix OAuth-Token refresh
This commit is contained in:
parent
15e92ca494
commit
928bf4dc68
|
@ -66,6 +66,12 @@ export class CredentialsHelper extends ICredentialsHelper {
|
|||
async updateCredentials(name: string, type: string, data: ICredentialDataDecryptedObject): Promise<void> {
|
||||
const credentials = await this.getCredentials(name, type);
|
||||
|
||||
if (Db.collections!.Credentials === null) {
|
||||
// The first time executeWorkflow gets called the Database has
|
||||
// to get initialized first
|
||||
await Db.init();
|
||||
}
|
||||
|
||||
credentials.setData(data, this.encryptionKey);
|
||||
const newCredentialsData = credentials.getDataToSave() as ICredentialsDb;
|
||||
|
||||
|
@ -75,7 +81,12 @@ export class CredentialsHelper extends ICredentialsHelper {
|
|||
// TODO: also add user automatically depending on who is logged in, if anybody is logged in
|
||||
|
||||
// Save the credentials in DB
|
||||
await Db.collections.Credentials!.save(newCredentialsData);
|
||||
const findQuery = {
|
||||
name,
|
||||
type,
|
||||
};
|
||||
|
||||
await Db.collections.Credentials!.update(findQuery, newCredentialsData);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -969,7 +969,7 @@ class App {
|
|||
return ResponseHelper.sendErrorResponse(res, errorResponse);
|
||||
}
|
||||
|
||||
savedCredentialsData.oauthTokenData = JSON.stringify(oauthToken.data);
|
||||
savedCredentialsData.oauthTokenData = oauthToken.data;
|
||||
_.unset(savedCredentialsData, 'csrfSecret');
|
||||
|
||||
credentials.setData(savedCredentialsData, encryptionKey);
|
||||
|
|
|
@ -36,8 +36,8 @@ import {
|
|||
} from 'n8n-workflow';
|
||||
|
||||
import * as clientOAuth2 from 'client-oauth2';
|
||||
import { get, unset } from 'lodash';
|
||||
import * as express from "express";
|
||||
import { get } from 'lodash';
|
||||
import * as express from 'express';
|
||||
import * as path from 'path';
|
||||
import { OptionsWithUri } from 'request';
|
||||
import * as requestPromise from 'request-promise-native';
|
||||
|
@ -126,8 +126,13 @@ export function requestOAuth(this: IAllExecuteFunctions, credentialsType: string
|
|||
throw new Error('OAuth credentials not connected!');
|
||||
}
|
||||
|
||||
const oAuthClient = new clientOAuth2({});
|
||||
const oauthTokenData = JSON.parse(credentials.oauthTokenData as string);
|
||||
const oAuthClient = new clientOAuth2({
|
||||
clientId: credentials.clientId as string,
|
||||
clientSecret: credentials.clientSecret as string,
|
||||
accessTokenUri: credentials.accessTokenUrl as string,
|
||||
});
|
||||
|
||||
const oauthTokenData = credentials.oauthTokenData as clientOAuth2.Data;
|
||||
const token = oAuthClient.createToken(oauthTokenData);
|
||||
|
||||
// Signs the request by adding authorization headers or query parameters depending
|
||||
|
@ -142,9 +147,7 @@ export function requestOAuth(this: IAllExecuteFunctions, credentialsType: string
|
|||
// Token is probably not valid anymore. So try refresh it.
|
||||
const newToken = await token.refresh();
|
||||
|
||||
const newCredentialsData = newToken.data;
|
||||
unset(newCredentialsData, 'csrfSecret');
|
||||
unset(newCredentialsData, 'oauthTokenData');
|
||||
credentials.oauthTokenData = newToken.data;
|
||||
|
||||
// Find the name of the credentials
|
||||
if (!node.credentials || !node.credentials[credentialsType]) {
|
||||
|
@ -153,7 +156,7 @@ export function requestOAuth(this: IAllExecuteFunctions, credentialsType: string
|
|||
const name = node.credentials[credentialsType];
|
||||
|
||||
// Save the refreshed token
|
||||
await additionalData.credentialsHelper.updateCredentials(name, credentialsType, newCredentialsData);
|
||||
await additionalData.credentialsHelper.updateCredentials(name, credentialsType, credentials);
|
||||
|
||||
// Make the request again with the new token
|
||||
const newRequestOptions = newToken.sign(requestOptions as clientOAuth2.RequestObject);
|
||||
|
|
Loading…
Reference in a new issue