From dbba22131ce4787a44bcc2ef0af3a772e5320e3f Mon Sep 17 00:00:00 2001 From: lohxt1 Date: Thu, 27 Mar 2025 22:37:20 +0530 Subject: [PATCH] fix: proxy and certs not being used for oauth2 calls --- .../TimelineItem/Common/Time/index.js | 2 +- packages/bruno-electron/src/ipc/collection.js | 17 +++++------- .../bruno-electron/src/ipc/network/index.js | 27 +++++++------------ packages/bruno-electron/src/utils/oauth2.js | 26 ++++++++++-------- 4 files changed, 32 insertions(+), 40 deletions(-) diff --git a/packages/bruno-app/src/components/ResponsePane/Timeline/TimelineItem/Common/Time/index.js b/packages/bruno-app/src/components/ResponsePane/Timeline/TimelineItem/Common/Time/index.js index ab4cddce0..d4fd0ec07 100644 --- a/packages/bruno-app/src/components/ResponsePane/Timeline/TimelineItem/Common/Time/index.js +++ b/packages/bruno-app/src/components/ResponsePane/Timeline/TimelineItem/Common/Time/index.js @@ -27,7 +27,7 @@ export const RelativeTime = ({ timestamp }) => { useEffect(() => { const interval = setInterval(() => { setRelativeTime(getRelativeTime(new Date(timestamp))); - }, 60000); + }, 1000); return () => clearInterval(interval); }, [timestamp]); diff --git a/packages/bruno-electron/src/ipc/collection.js b/packages/bruno-electron/src/ipc/collection.js index 112d1c070..0aaf87d55 100644 --- a/packages/bruno-electron/src/ipc/collection.js +++ b/packages/bruno-electron/src/ipc/collection.js @@ -31,12 +31,11 @@ const { deleteCookiesForDomain, getDomainsWithCookies, addCookieForDomain, modif const EnvironmentSecretsStore = require('../store/env-secrets'); const CollectionSecurityStore = require('../store/collection-security'); const UiStateSnapshotStore = require('../store/ui-state-snapshot'); -const Oauth2Store = require('../store/oauth2'); const interpolateVars = require('./network/interpolate-vars'); const { getEnvVars, getTreePathFromCollectionToItem, mergeVars } = require('../utils/collection'); const { getProcessEnvVars } = require('../store/process-env'); const { getOAuth2TokenUsingAuthorizationCode, getOAuth2TokenUsingClientCredentials, getOAuth2TokenUsingPasswordCredentials, refreshOauth2Token } = require('../utils/oauth2'); -const { configureRequestWithCertsAndProxy } = require('./network'); +const { getCertsAndProxyConfig } = require('./network'); const { parseBruFileMeta, hydrateRequestWithUuid } = require('../utils/collection'); const environmentSecretsStore = new EnvironmentSecretsStore(); @@ -917,7 +916,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection } interpolateVars(requestCopy, envVars, runtimeVariables, processEnvVars); - const {newRequest} = await configureRequestWithCertsAndProxy({ + const certsAndProxyConfig = await getCertsAndProxyConfig({ collectionUid, request: requestCopy, envVars, @@ -925,21 +924,20 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection processEnvVars, collectionPath }); - requestCopy = newRequest const { oauth2: { grantType }} = requestCopy || {}; let credentials, url, credentialsId; switch (grantType) { case 'authorization_code': interpolateVars(requestCopy, envVars, runtimeVariables, processEnvVars); - ({ credentials, url, credentialsId, debugInfo } = await getOAuth2TokenUsingAuthorizationCode({ request: requestCopy, collectionUid, forceFetch: true })); + ({ credentials, url, credentialsId, debugInfo } = await getOAuth2TokenUsingAuthorizationCode({ request: requestCopy, collectionUid, forceFetch: true, certsAndProxyConfig })); break; case 'client_credentials': interpolateVars(requestCopy, envVars, runtimeVariables, processEnvVars); - ({ credentials, url, credentialsId, debugInfo } = await getOAuth2TokenUsingClientCredentials({ request: requestCopy, collectionUid, forceFetch: true })); + ({ credentials, url, credentialsId, debugInfo } = await getOAuth2TokenUsingClientCredentials({ request: requestCopy, collectionUid, forceFetch: true, certsAndProxyConfig })); break; case 'password': interpolateVars(requestCopy, envVars, runtimeVariables, processEnvVars); - ({ credentials, url, credentialsId, debugInfo } = await getOAuth2TokenUsingPasswordCredentials({ request: requestCopy, collectionUid, forceFetch: true })); + ({ credentials, url, credentialsId, debugInfo } = await getOAuth2TokenUsingPasswordCredentials({ request: requestCopy, collectionUid, forceFetch: true, certsAndProxyConfig })); break; } return { credentials, url, collectionUid, credentialsId, debugInfo }; @@ -1007,7 +1005,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection const envVars = getEnvVars(environment); const processEnvVars = getProcessEnvVars(collectionUid); interpolateVars(requestCopy, envVars, runtimeVariables, processEnvVars); - const {newRequest} = await configureRequestWithCertsAndProxy({ + const certsAndProxyConfig = await getCertsAndProxyConfig({ collectionUid, request: requestCopy, envVars, @@ -1015,8 +1013,7 @@ const registerRendererEventHandlers = (mainWindow, watcher, lastOpenedCollection processEnvVars, collectionPath }); - requestCopy = newRequest - let { credentials, url, credentialsId, debugInfo } = await refreshOauth2Token(requestCopy, collectionUid); + let { credentials, url, credentialsId, debugInfo } = await refreshOauth2Token({ requestCopy, collectionUid, certsAndProxyConfig }); return { credentials, url, collectionUid, credentialsId, debugInfo }; } } catch (error) { diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index 9a04cb75d..2fc32cba7 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -26,7 +26,6 @@ const { addCookieToJar, getDomainsWithCookies, getCookieStringForUrl } = require const { createFormData } = require('../../utils/form-data'); const { findItemInCollectionByPathname, sortFolder, getAllRequestsInFolderRecursively, getEnvVars } = require('../../utils/collection'); const { getOAuth2TokenUsingAuthorizationCode, getOAuth2TokenUsingClientCredentials, getOAuth2TokenUsingPasswordCredentials } = require('../../utils/oauth2'); -const { setupProxyAgents } = require('../../utils/proxy-util'); const { preferencesUtil } = require('../../store/preferences'); const { getProcessEnvVars } = require('../../store/process-env'); const { getBrunoConfig } = require('../../store/bruno-config'); @@ -53,7 +52,7 @@ const getJsSandboxRuntime = (collection) => { return securityConfig.jsSandboxMode === 'safe' ? 'quickjs' : 'vm2'; }; -const configureRequestWithCertsAndProxy = async ({ +const getCertsAndProxyConfig = async ({ collectionUid, request, envVars, @@ -150,16 +149,8 @@ const configureRequestWithCertsAndProxy = async ({ proxyConfig = preferencesUtil.getGlobalProxyConfig(); proxyMode = get(proxyConfig, 'mode', 'off'); } - - setupProxyAgents({ - requestConfig: request, - proxyMode, - proxyConfig, - httpsAgentRequestFields, - interpolationOptions - }); - - return {proxyMode, newRequest: request, proxyConfig, httpsAgentRequestFields, interpolationOptions}; + + return { proxyMode, proxyConfig, httpsAgentRequestFields, interpolationOptions }; } const configureRequest = async ( @@ -175,7 +166,7 @@ const configureRequest = async ( request.url = `http://${request.url}`; } - const {proxyMode, newRequest, proxyConfig, httpsAgentRequestFields, interpolationOptions} = await configureRequestWithCertsAndProxy({ + const certsAndProxyConfig = await getCertsAndProxyConfig({ collectionUid, request, envVars, @@ -184,7 +175,6 @@ const configureRequest = async ( collectionPath }); - request = newRequest let requestMaxRedirects = request.maxRedirects request.maxRedirects = 0 @@ -193,6 +183,7 @@ const configureRequest = async ( requestMaxRedirects = 5; // Default to 5 redirects } + let { proxyMode, proxyConfig, httpsAgentRequestFields, interpolationOptions } = certsAndProxyConfig; let axiosInstance = makeAxiosInstance({ proxyMode, proxyConfig, @@ -213,7 +204,7 @@ const configureRequest = async ( switch (grantType) { case 'authorization_code': interpolateVars(requestCopy, envVars, runtimeVariables, processEnvVars); - ({ credentials, url: oauth2Url, credentialsId, debugInfo } = await getOAuth2TokenUsingAuthorizationCode({ request: requestCopy, collectionUid })); + ({ credentials, url: oauth2Url, credentialsId, debugInfo } = await getOAuth2TokenUsingAuthorizationCode({ request: requestCopy, collectionUid, certsAndProxyConfig })); request.oauth2Credentials = { credentials, url: oauth2Url, collectionUid, credentialsId, debugInfo, folderUid: request.oauth2Credentials?.folderUid }; if (tokenPlacement == 'header') { request.headers['Authorization'] = `${tokenHeaderPrefix} ${credentials?.access_token}`; @@ -229,7 +220,7 @@ const configureRequest = async ( break; case 'client_credentials': interpolateVars(requestCopy, envVars, runtimeVariables, processEnvVars); - ({ credentials, url: oauth2Url, credentialsId, debugInfo } = await getOAuth2TokenUsingClientCredentials({ request: requestCopy, collectionUid })); + ({ credentials, url: oauth2Url, credentialsId, debugInfo } = await getOAuth2TokenUsingClientCredentials({ request: requestCopy, collectionUid, certsAndProxyConfig })); request.oauth2Credentials = { credentials, url: oauth2Url, collectionUid, credentialsId, debugInfo, folderUid: request.oauth2Credentials?.folderUid }; if (tokenPlacement == 'header') { request.headers['Authorization'] = `${tokenHeaderPrefix} ${credentials?.access_token}`; @@ -245,7 +236,7 @@ const configureRequest = async ( break; case 'password': interpolateVars(requestCopy, envVars, runtimeVariables, processEnvVars); - ({ credentials, url: oauth2Url, credentialsId, debugInfo } = await getOAuth2TokenUsingPasswordCredentials({ request: requestCopy, collectionUid })); + ({ credentials, url: oauth2Url, credentialsId, debugInfo } = await getOAuth2TokenUsingPasswordCredentials({ request: requestCopy, collectionUid, certsAndProxyConfig })); request.oauth2Credentials = { credentials, url: oauth2Url, collectionUid, credentialsId, debugInfo, folderUid: request.oauth2Credentials?.folderUid }; if (tokenPlacement == 'header') { request.headers['Authorization'] = `${tokenHeaderPrefix} ${credentials?.access_token}`; @@ -1320,4 +1311,4 @@ const registerNetworkIpc = (mainWindow) => { module.exports = registerNetworkIpc; module.exports.configureRequest = configureRequest; -module.exports.configureRequestWithCertsAndProxy = configureRequestWithCertsAndProxy; +module.exports.getCertsAndProxyConfig = getCertsAndProxyConfig; diff --git a/packages/bruno-electron/src/utils/oauth2.js b/packages/bruno-electron/src/utils/oauth2.js index ce7b32abd..882f39767 100644 --- a/packages/bruno-electron/src/utils/oauth2.js +++ b/packages/bruno-electron/src/utils/oauth2.js @@ -44,7 +44,7 @@ const isTokenExpired = (credentials) => { // AUTHORIZATION CODE -const getOAuth2TokenUsingAuthorizationCode = async ({ request, collectionUid, forceFetch = false }) => { +const getOAuth2TokenUsingAuthorizationCode = async ({ request, collectionUid, forceFetch = false, certsAndProxyConfig }) => { let codeVerifier = generateCodeVerifier(); let codeChallenge = generateCodeChallenge(codeVerifier); @@ -76,7 +76,7 @@ const getOAuth2TokenUsingAuthorizationCode = async ({ request, collectionUid, fo if (autoRefreshToken && storedCredentials.refresh_token) { // Try to refresh token try { - const refreshedCredentialsData = await refreshOauth2Token(requestCopy, collectionUid); + const refreshedCredentialsData = await refreshOauth2Token({ requestCopy, collectionUid, certsAndProxyConfig }); return { collectionUid, url, credentials: refreshedCredentialsData.credentials, credentialsId }; } catch (error) { // Refresh failed @@ -149,7 +149,8 @@ const getOAuth2TokenUsingAuthorizationCode = async ({ request, collectionUid, fo let axiosResponseInfo = null; try { - const axiosInstance = makeAxiosInstance(); + const { proxyMode, proxyConfig, httpsAgentRequestFields, interpolationOptions } = certsAndProxyConfig; + const axiosInstance = makeAxiosInstance({ proxyMode, proxyConfig, httpsAgentRequestFields, interpolationOptions }); // Interceptor to capture request data axiosInstance.interceptors.request.use((config) => { const requestData = typeof config?.data === 'string' ? config?.data : safeStringifyJSON(config?.data); @@ -282,7 +283,7 @@ const getOAuth2AuthorizationCode = (request, codeChallenge, collectionUid) => { // CLIENT CREDENTIALS -const getOAuth2TokenUsingClientCredentials = async ({ request, collectionUid, forceFetch = false }) => { +const getOAuth2TokenUsingClientCredentials = async ({ request, collectionUid, forceFetch = false, certsAndProxyConfig }) => { let requestCopy = cloneDeep(request); const oAuth = get(requestCopy, 'oauth2', {}); const { @@ -310,7 +311,7 @@ const getOAuth2TokenUsingClientCredentials = async ({ request, collectionUid, fo if (autoRefreshToken && storedCredentials.refresh_token) { // Try to refresh token try { - const refreshedCredentialsData = await refreshOauth2Token(requestCopy, collectionUid); + const refreshedCredentialsData = await refreshOauth2Token({ requestCopy, collectionUid, certsAndProxyConfig }); return { collectionUid, url, credentials: refreshedCredentialsData.credentials, credentialsId }; } catch (error) { clearOauth2Credentials({ collectionUid, url, credentialsId }); @@ -375,7 +376,8 @@ const getOAuth2TokenUsingClientCredentials = async ({ request, collectionUid, fo let debugInfo = { data: [] }; try { - const axiosInstance = makeAxiosInstance(); + const { proxyMode, proxyConfig, httpsAgentRequestFields, interpolationOptions } = certsAndProxyConfig; + const axiosInstance = makeAxiosInstance({ proxyMode, proxyConfig, httpsAgentRequestFields, interpolationOptions }); axiosInstance.interceptors.request.use((config) => { const requestData = typeof config?.data === 'string' ? config?.data : safeStringifyJSON(config?.data); axiosRequestInfo = { @@ -465,7 +467,7 @@ const getOAuth2TokenUsingClientCredentials = async ({ request, collectionUid, fo // PASSWORD CREDENTIALS -const getOAuth2TokenUsingPasswordCredentials = async ({ request, collectionUid, forceFetch = false }) => { +const getOAuth2TokenUsingPasswordCredentials = async ({ request, collectionUid, forceFetch = false, certsAndProxyConfig }) => { let requestCopy = cloneDeep(request); const oAuth = get(requestCopy, 'oauth2', {}); const { @@ -494,7 +496,7 @@ const getOAuth2TokenUsingPasswordCredentials = async ({ request, collectionUid, if (autoRefreshToken && storedCredentials.refresh_token) { // Try to refresh token try { - const refreshedCredentialsData = await refreshOauth2Token(requestCopy, collectionUid); + const refreshedCredentialsData = await refreshOauth2Token({ requestCopy, collectionUid, certsAndProxyConfig }); return { collectionUid, url, credentials: refreshedCredentialsData.credentials, credentialsId }; } catch (error) { clearOauth2Credentials({ collectionUid, url, credentialsId }); @@ -562,7 +564,8 @@ const getOAuth2TokenUsingPasswordCredentials = async ({ request, collectionUid, let debugInfo = { data: [] }; try { - const axiosInstance = makeAxiosInstance(); + const { proxyMode, proxyConfig, httpsAgentRequestFields, interpolationOptions } = certsAndProxyConfig; + const axiosInstance = makeAxiosInstance({ proxyMode, proxyConfig, httpsAgentRequestFields, interpolationOptions }); axiosInstance.interceptors.request.use((config) => { const requestData = typeof config?.data === 'string' ? config?.data : safeStringifyJSON(config?.data); axiosRequestInfo = { @@ -649,7 +652,7 @@ const getOAuth2TokenUsingPasswordCredentials = async ({ request, collectionUid, } }; -const refreshOauth2Token = async (requestCopy, collectionUid) => { +const refreshOauth2Token = async ({ requestCopy, collectionUid, certsAndProxyConfig }) => { const oAuth = get(requestCopy, 'oauth2', {}); const { clientId, clientSecret, credentialsId } = oAuth; const url = oAuth.refreshTokenUrl ? oAuth.refreshTokenUrl : oAuth.accessTokenUrl; @@ -680,7 +683,8 @@ const refreshOauth2Token = async (requestCopy, collectionUid) => { let axiosResponseInfo = null; let debugInfo = { data: [] }; - const axiosInstance = makeAxiosInstance(); + const { proxyMode, proxyConfig, httpsAgentRequestFields, interpolationOptions } = certsAndProxyConfig; + const axiosInstance = makeAxiosInstance({ proxyMode, proxyConfig, httpsAgentRequestFields, interpolationOptions }); axiosInstance.interceptors.request.use((config) => { const requestData = typeof config?.data === 'string' ? config?.data : safeStringifyJSON(config?.data); axiosRequestInfo = {