feat(#354): Use collection level auth if available for introspection request

This commit is contained in:
Jonathan Gruber 2023-10-10 11:25:13 +02:00
parent ff3321d643
commit 12f8ae37a6
3 changed files with 45 additions and 51 deletions

View File

@ -493,7 +493,8 @@ const registerNetworkIpc = (mainWindow) => {
ipcMain.handle('fetch-gql-schema', async (event, endpoint, environment, request, collection) => { ipcMain.handle('fetch-gql-schema', async (event, endpoint, environment, request, collection) => {
try { try {
const envVars = getEnvVars(environment); 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 preferences = getPreferences();
const sslVerification = get(preferences, 'request.sslVerification', true); const sslVerification = get(preferences, 'request.sslVerification', true);
@ -711,14 +712,14 @@ const registerNetworkIpc = (mainWindow) => {
if (socksEnabled) { if (socksEnabled) {
const socksProxyAgent = new SocksProxyAgent(proxyUri); const socksProxyAgent = new SocksProxyAgent(proxyUri);
request.httpsAgent = socksProxyAgent; request.httpsAgent = socksProxyAgent;
request.httpAgent = socksProxyAgent; request.httpAgent = socksProxyAgent;
} else { } else {
request.httpsAgent = new HttpsProxyAgent(proxyUri, { request.httpsAgent = new HttpsProxyAgent(proxyUri, {
rejectUnauthorized: sslVerification rejectUnauthorized: sslVerification
}); });
request.httpAgent = new HttpProxyAgent(proxyUri); request.httpAgent = new HttpProxyAgent(proxyUri);
} }
} else if (!sslVerification) { } else if (!sslVerification) {

View File

@ -1,15 +1,14 @@
const Handlebars = require('handlebars'); const Handlebars = require('handlebars');
const { getIntrospectionQuery } = require('graphql'); 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) { if (endpoint && endpoint.length) {
endpoint = Handlebars.compile(endpoint, { noEscape: true })(envVars); endpoint = Handlebars.compile(endpoint, { noEscape: true })(envVars);
} }
const introspectionQuery = getIntrospectionQuery();
const queryParams = { const queryParams = {
query: introspectionQuery query: getIntrospectionQuery()
}; };
let axiosRequest = { let axiosRequest = {
@ -23,20 +22,7 @@ const prepareGqlIntrospectionRequest = (endpoint, envVars, request) => {
data: JSON.stringify(queryParams) data: JSON.stringify(queryParams)
}; };
if (request.auth) { return setAuthHeaders(axiosRequest, request, collectionRoot);
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 mapHeaders = (headers) => { const mapHeaders = (headers) => {

View File

@ -1,6 +1,41 @@
const { get, each, filter } = require('lodash'); const { get, each, filter } = require('lodash');
const decomment = require('decomment'); 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 prepareRequest = (request, collectionRoot) => {
const headers = {}; const headers = {};
let contentTypeDefined = false; let contentTypeDefined = false;
@ -30,36 +65,7 @@ const prepareRequest = (request, collectionRoot) => {
headers: headers headers: headers
}; };
// Authentication axiosRequest = setAuthHeaders(axiosRequest, request, collectionRoot);
// 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')}`;
}
}
if (request.body.mode === 'json') { if (request.body.mode === 'json') {
if (!contentTypeDefined) { if (!contentTypeDefined) {
@ -125,3 +131,4 @@ const prepareRequest = (request, collectionRoot) => {
}; };
module.exports = prepareRequest; module.exports = prepareRequest;
module.exports.setAuthHeaders = setAuthHeaders;