From a48d7e879f0b94ecab2e3fcd9b02144b78b2e404 Mon Sep 17 00:00:00 2001 From: nivedin Date: Tue, 28 Nov 2023 23:26:03 +0530 Subject: [PATCH] refactor: inherited auth and header for req runner --- .../src/helpers/RequestRunner.ts | 28 ++++++++++++---- .../src/helpers/collection/collection.ts | 25 ++++++++++++++ .../src/helpers/utils/EffectiveURL.ts | 33 +++++++++++-------- 3 files changed, 65 insertions(+), 21 deletions(-) diff --git a/packages/hoppscotch-common/src/helpers/RequestRunner.ts b/packages/hoppscotch-common/src/helpers/RequestRunner.ts index bd4c910b5..843728e12 100644 --- a/packages/hoppscotch-common/src/helpers/RequestRunner.ts +++ b/packages/hoppscotch-common/src/helpers/RequestRunner.ts @@ -95,13 +95,27 @@ export function runRESTRequest$( return E.left("script_fail" as const) } - const effectiveRequest = getEffectiveRESTRequest( - tab.value.document.request, - { - name: "Env", - variables: combineEnvVariables(envs.right), - } - ) + const requestAuth = + tab.value.document.request.auth.authType === "inherit" && + tab.value.document.request.auth.authActive + ? tab.value.document.inheritedProperties?.auth + : tab.value.document.request.auth + + const requestHeaders = [ + ...(tab.value.document.inheritedProperties?.headers ?? []), + ...tab.value.document.request.headers, + ] + + const finalRequest = { + ...tab.value.document.request, + auth: requestAuth, + headers: requestHeaders, + } + + const effectiveRequest = getEffectiveRESTRequest(finalRequest, { + name: "Env", + variables: combineEnvVariables(envs.right), + }) const [stream, cancelRun] = createRESTNetworkRequestStream(effectiveRequest) cancelFunc = cancelRun diff --git a/packages/hoppscotch-common/src/helpers/collection/collection.ts b/packages/hoppscotch-common/src/helpers/collection/collection.ts index a0f14df14..245b9203d 100644 --- a/packages/hoppscotch-common/src/helpers/collection/collection.ts +++ b/packages/hoppscotch-common/src/helpers/collection/collection.ts @@ -5,6 +5,7 @@ import { runGQLQuery } from "../backend/GQLClient" import * as E from "fp-ts/Either" import { getService } from "~/modules/dioc" import { RESTTabService } from "~/services/tab/rest" +import { HoppInheritedProperty } from "../types/HoppInheritedProperties" /** * Resolve save context on reorder @@ -108,6 +109,30 @@ export function updateSaveContextForAffectedRequests( } } +export function updateInheritedPropertiesForAffectedRequests( + path: string, + inheritedProperties: HoppInheritedProperty +) { + const tabService = getService(RESTTabService) + const tabs = tabService.getTabsRefTo((tab) => { + return ( + tab.document.saveContext?.originLocation === "user-collection" && + tab.document.saveContext.folderPath.startsWith(path) + ) + }) + + const filteredTabs = tabs.filter((tab) => { + return ( + tab.value.document.inheritedProperties && + tab.value.document.inheritedProperties.parentId === path + ) + }) + + for (const tab of filteredTabs) { + tab.value.document.inheritedProperties = inheritedProperties + } +} + function resetSaveContextForAffectedRequests(folderPath: string) { const tabService = getService(RESTTabService) const tabs = tabService.getTabsRefTo((tab) => { diff --git a/packages/hoppscotch-common/src/helpers/utils/EffectiveURL.ts b/packages/hoppscotch-common/src/helpers/utils/EffectiveURL.ts index 047d6684c..37419d253 100644 --- a/packages/hoppscotch-common/src/helpers/utils/EffectiveURL.ts +++ b/packages/hoppscotch-common/src/helpers/utils/EffectiveURL.ts @@ -42,22 +42,27 @@ export interface EffectiveHoppRESTRequest extends HoppRESTRequest { * @param envVars Currently active environment variables * @returns The list of headers */ -const getComputedAuthHeaders = ( - req: HoppRESTRequest, - envVars: Environment["variables"] +export const getComputedAuthHeaders = ( + envVars: Environment["variables"], + req?: HoppRESTRequest, + auth?: HoppRESTRequest["auth"] ) => { + const request = auth ? { auth: auth ?? { authActive: false } } : req + // If Authorization header is also being user-defined, that takes priority - if (req.headers.find((h) => h.key.toLowerCase() === "authorization")) + if (req && req.headers.find((h) => h.key.toLowerCase() === "authorization")) return [] - if (!req.auth.authActive) return [] + if (!request) return [] + + if (!request.auth.authActive) return [] const headers: HoppRESTHeader[] = [] // TODO: Support a better b64 implementation than btoa ? - if (req.auth.authType === "basic") { - const username = parseTemplateString(req.auth.username, envVars) - const password = parseTemplateString(req.auth.password, envVars) + if (request.auth.authType === "basic") { + const username = parseTemplateString(request.auth.username, envVars) + const password = parseTemplateString(request.auth.password, envVars) headers.push({ active: true, @@ -65,16 +70,16 @@ const getComputedAuthHeaders = ( value: `Basic ${btoa(`${username}:${password}`)}`, }) } else if ( - req.auth.authType === "bearer" || - req.auth.authType === "oauth-2" + request.auth.authType === "bearer" || + request.auth.authType === "oauth-2" ) { headers.push({ active: true, key: "Authorization", - value: `Bearer ${parseTemplateString(req.auth.token, envVars)}`, + value: `Bearer ${parseTemplateString(request.auth.token, envVars)}`, }) - } else if (req.auth.authType === "api-key") { - const { key, value, addTo } = req.auth + } else if (request.auth.authType === "api-key") { + const { key, value, addTo } = request.auth if (addTo === "Headers") { headers.push({ @@ -132,7 +137,7 @@ export const getComputedHeaders = ( req: HoppRESTRequest, envVars: Environment["variables"] ): ComputedHeader[] => [ - ...getComputedAuthHeaders(req, envVars).map((header) => ({ + ...getComputedAuthHeaders(envVars, req).map((header) => ({ source: "auth" as const, header, })),