From 9f47200e7bceda16a235d13520cb32e5b76ba5df Mon Sep 17 00:00:00 2001 From: lohit Date: Tue, 7 Oct 2025 22:40:51 +0530 Subject: [PATCH] fix(bru-1939): fix OAuth2 credentials not persisting across requests in the same collection run. (#5730) --- .../bruno-electron/src/ipc/network/index.js | 9 +++++- packages/bruno-electron/src/utils/oauth2.js | 31 +++++++++++++++++-- 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index 26e2e2863..05d3ce748 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -24,7 +24,7 @@ const { chooseFileToSave, writeBinaryFile, writeFile } = require('../../utils/fi const { addCookieToJar, getDomainsWithCookies, getCookieStringForUrl } = require('../../utils/cookies'); const { createFormData } = require('../../utils/form-data'); const { findItemInCollectionByPathname, sortFolder, getAllRequestsInFolderRecursively, getEnvVars, getTreePathFromCollectionToItem, mergeVars, sortByNameThenSequence } = require('../../utils/collection'); -const { getOAuth2TokenUsingAuthorizationCode, getOAuth2TokenUsingClientCredentials, getOAuth2TokenUsingPasswordCredentials, getOAuth2TokenUsingImplicitGrant } = require('../../utils/oauth2'); +const { getOAuth2TokenUsingAuthorizationCode, getOAuth2TokenUsingClientCredentials, getOAuth2TokenUsingPasswordCredentials, getOAuth2TokenUsingImplicitGrant, updateCollectionOauth2Credentials } = require('../../utils/oauth2'); const { preferencesUtil } = require('../../store/preferences'); const { getProcessEnvVars } = require('../../store/process-env'); const { getBrunoConfig } = require('../../store/bruno-config'); @@ -1126,6 +1126,13 @@ const registerNetworkIpc = (mainWindow) => { ...(request?.oauth2Credentials?.folderUid ? { folderUid: request.oauth2Credentials.folderUid } : { itemUid: item.uid }), debugInfo: request?.oauth2Credentials?.debugInfo, }); + + collection.oauth2Credentials = updateCollectionOauth2Credentials({ + itemUid: item.uid, + collectionUid, + collectionOauth2Credentials: collection.oauth2Credentials, + requestOauth2Credentials: request.oauth2Credentials + }); } timeStart = Date.now(); diff --git a/packages/bruno-electron/src/utils/oauth2.js b/packages/bruno-electron/src/utils/oauth2.js index 61d3d80c3..4c24c7f22 100644 --- a/packages/bruno-electron/src/utils/oauth2.js +++ b/packages/bruno-electron/src/utils/oauth2.js @@ -1,4 +1,4 @@ -const { get, cloneDeep } = require('lodash'); +const { get, cloneDeep, filter } = require('lodash'); const crypto = require('crypto'); const { authorizeUserInWindow } = require('../ipc/network/authorize-user-in-window'); const Oauth2Store = require('../store/oauth2'); @@ -894,6 +894,30 @@ const getOAuth2TokenUsingImplicitGrant = async ({ request, collectionUid, forceF } }; +const updateCollectionOauth2Credentials = ({ collectionUid, itemUid, collectionOauth2Credentials = [], requestOauth2Credentials = {} }) => { + const { url, credentialsId, folderUid, credentials, debugInfo } = requestOauth2Credentials; + + // Remove existing credentials for the same combination + const filteredOauth2Credentials = filter(cloneDeep(collectionOauth2Credentials), + (creds) => + !(creds.url === url + && creds.collectionUid === collectionUid + && creds.credentialsId === credentialsId)); + + // Add the new credential with folderUid and itemUid + filteredOauth2Credentials.push({ + collectionUid, + folderUid: folderUid, + itemUid: folderUid ? null : itemUid, + url, + credentials, + credentialsId, + debugInfo + }); + + return filteredOauth2Credentials; +}; + module.exports = { persistOauth2Credentials, clearOauth2Credentials, @@ -904,5 +928,6 @@ module.exports = { getOAuth2TokenUsingImplicitGrant, refreshOauth2Token, generateCodeVerifier, - generateCodeChallenge -}; \ No newline at end of file + generateCodeChallenge, + updateCollectionOauth2Credentials +};