diff --git a/packages/bruno-electron/src/ipc/network/index.js b/packages/bruno-electron/src/ipc/network/index.js index f95340229..b08344b1f 100644 --- a/packages/bruno-electron/src/ipc/network/index.js +++ b/packages/bruno-electron/src/ipc/network/index.js @@ -493,7 +493,8 @@ const registerNetworkIpc = (mainWindow) => { ipcMain.handle('fetch-gql-schema', async (event, endpoint, environment, request, collection) => { try { const envVars = getEnvVars(environment); - const preparedRequest = prepareGqlIntrospectionRequest(endpoint, envVars, request); + const collectionRoot = get(collection, 'root', {}); + const preparedRequest = prepareGqlIntrospectionRequest(endpoint, envVars, request, collectionRoot); const preferences = getPreferences(); const sslVerification = get(preferences, 'request.sslVerification', true); @@ -711,14 +712,14 @@ const registerNetworkIpc = (mainWindow) => { if (socksEnabled) { const socksProxyAgent = new SocksProxyAgent(proxyUri); - + request.httpsAgent = socksProxyAgent; request.httpAgent = socksProxyAgent; } else { request.httpsAgent = new HttpsProxyAgent(proxyUri, { rejectUnauthorized: sslVerification }); - + request.httpAgent = new HttpProxyAgent(proxyUri); } } else if (!sslVerification) { diff --git a/packages/bruno-electron/src/ipc/network/prepare-gql-introspection-request.js b/packages/bruno-electron/src/ipc/network/prepare-gql-introspection-request.js index 4a1e41c88..a448d9106 100644 --- a/packages/bruno-electron/src/ipc/network/prepare-gql-introspection-request.js +++ b/packages/bruno-electron/src/ipc/network/prepare-gql-introspection-request.js @@ -1,15 +1,14 @@ const Handlebars = require('handlebars'); const { getIntrospectionQuery } = require('graphql'); -const { get } = require('lodash'); +const { setAuthHeaders } = require('./prepare-request'); -const prepareGqlIntrospectionRequest = (endpoint, envVars, request) => { +const prepareGqlIntrospectionRequest = (endpoint, envVars, request, collectionRoot) => { if (endpoint && endpoint.length) { endpoint = Handlebars.compile(endpoint, { noEscape: true })(envVars); } - const introspectionQuery = getIntrospectionQuery(); const queryParams = { - query: introspectionQuery + query: getIntrospectionQuery() }; let axiosRequest = { @@ -23,20 +22,7 @@ const prepareGqlIntrospectionRequest = (endpoint, envVars, request) => { data: JSON.stringify(queryParams) }; - if (request.auth) { - if (request.auth.mode === 'basic') { - axiosRequest.auth = { - username: get(request, 'auth.basic.username'), - password: get(request, 'auth.basic.password') - }; - } - - if (request.auth.mode === 'bearer') { - axiosRequest.headers.authorization = `Bearer ${get(request, 'auth.bearer.token')}`; - } - } - - return axiosRequest; + return setAuthHeaders(axiosRequest, request, collectionRoot); }; const mapHeaders = (headers) => { diff --git a/packages/bruno-electron/src/ipc/network/prepare-request.js b/packages/bruno-electron/src/ipc/network/prepare-request.js index 42f4b39f8..06f84e75d 100644 --- a/packages/bruno-electron/src/ipc/network/prepare-request.js +++ b/packages/bruno-electron/src/ipc/network/prepare-request.js @@ -1,6 +1,41 @@ const { get, each, filter } = require('lodash'); const decomment = require('decomment'); +// Authentication +// A request can override the collection auth with another auth +// But it cannot override the collection auth with no auth +// We will provide support for disabling the auth via scripting in the future +const setAuthHeaders = (axiosRequest, request, collectionRoot) => { + const collectionAuth = get(collectionRoot, 'request.auth'); + if (collectionAuth) { + if (collectionAuth.mode === 'basic') { + axiosRequest.auth = { + username: get(collectionAuth, 'basic.username'), + password: get(collectionAuth, 'basic.password') + }; + } + + if (collectionAuth.mode === 'bearer') { + axiosRequest.headers['authorization'] = `Bearer ${get(collectionAuth, 'bearer.token')}`; + } + } + + if (request.auth) { + if (request.auth.mode === 'basic') { + axiosRequest.auth = { + username: get(request, 'auth.basic.username'), + password: get(request, 'auth.basic.password') + }; + } + + if (request.auth.mode === 'bearer') { + axiosRequest.headers['authorization'] = `Bearer ${get(request, 'auth.bearer.token')}`; + } + } + + return axiosRequest; +}; + const prepareRequest = (request, collectionRoot) => { const headers = {}; let contentTypeDefined = false; @@ -30,36 +65,7 @@ const prepareRequest = (request, collectionRoot) => { headers: headers }; - // Authentication - // A request can override the collection auth with another auth - // But it cannot override the collection auth with no auth - // We will provide support for disabling the auth via scripting in the future - const collectionAuth = get(collectionRoot, 'request.auth'); - if (collectionAuth) { - if (collectionAuth.mode === 'basic') { - axiosRequest.auth = { - username: get(collectionAuth, 'basic.username'), - password: get(collectionAuth, 'basic.password') - }; - } - - if (collectionAuth.mode === 'bearer') { - axiosRequest.headers['authorization'] = `Bearer ${get(collectionAuth, 'bearer.token')}`; - } - } - - if (request.auth) { - if (request.auth.mode === 'basic') { - axiosRequest.auth = { - username: get(request, 'auth.basic.username'), - password: get(request, 'auth.basic.password') - }; - } - - if (request.auth.mode === 'bearer') { - axiosRequest.headers['authorization'] = `Bearer ${get(request, 'auth.bearer.token')}`; - } - } + axiosRequest = setAuthHeaders(axiosRequest, request, collectionRoot); if (request.body.mode === 'json') { if (!contentTypeDefined) { @@ -125,3 +131,4 @@ const prepareRequest = (request, collectionRoot) => { }; module.exports = prepareRequest; +module.exports.setAuthHeaders = setAuthHeaders;