From 74359ea74eb392018c104df59761e995e7817dcc Mon Sep 17 00:00:00 2001 From: Nivedin <53208152+nivedin@users.noreply.github.com> Date: Tue, 19 Dec 2023 17:04:24 +0530 Subject: [PATCH] fix: auth-header not inheriting properties (#3668) --- packages/hoppscotch-common/locales/en.json | 2 +- .../src/newstore/collections.ts | 22 ++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/packages/hoppscotch-common/locales/en.json b/packages/hoppscotch-common/locales/en.json index 0886baafd..8852e6741 100644 --- a/packages/hoppscotch-common/locales/en.json +++ b/packages/hoppscotch-common/locales/en.json @@ -121,7 +121,7 @@ "generate_token": "Generate Token", "graphql_headers": "Authorization Headers are sent as part of the payload to connection_init", "include_in_url": "Include in URL", - "inherited_from": "Inherited from {auth} from Parent Collection {collection} ", + "inherited_from": "Inherited {auth} from parent collection {collection} ", "learn": "Learn how", "oauth": { "redirect_auth_server_returned_error": "Auth Server returned an error state", diff --git a/packages/hoppscotch-common/src/newstore/collections.ts b/packages/hoppscotch-common/src/newstore/collections.ts index 7979855d0..a23042a7f 100644 --- a/packages/hoppscotch-common/src/newstore/collections.ts +++ b/packages/hoppscotch-common/src/newstore/collections.ts @@ -4,6 +4,7 @@ import { HoppRESTRequest, HoppCollection, makeCollection, + HoppGQLAuth, } from "@hoppscotch/data" import DispatchingStore, { defineDispatchers } from "./DispatchingStore" import { cloneDeep } from "lodash-es" @@ -11,6 +12,9 @@ import { resolveSaveContextOnRequestReorder } from "~/helpers/collection/request import { getService } from "~/modules/dioc" import { RESTTabService } from "~/services/tab/rest" import { HoppInheritedProperty } from "~/helpers/types/HoppInheritedProperties" +import { HoppRESTAuth } from "@hoppscotch/data" +import { HoppRESTHeaders } from "@hoppscotch/data" +import { HoppGQLHeader } from "~/helpers/graphql" const defaultRESTCollectionState = { state: [ @@ -63,6 +67,12 @@ export function navigateToFolderWithIndexPath( return target !== undefined ? target : null } +/** + * Used to obtain the inherited auth and headers for a given folder path, used for both REST and GraphQL + * @param folderPath the path of the folder to cascade the auth from + * @param type the type of collection + * @returns the inherited auth and headers for the given folder path + */ export function cascadeParentCollectionForHeaderAuth( folderPath: string | undefined, type: "rest" | "graphql" @@ -103,10 +113,16 @@ export function cascadeParentCollectionForHeaderAuth( return { auth, headers } } - const parentFolderAuth = parentFolder.auth - const parentFolderHeaders = parentFolder.headers + const parentFolderAuth = parentFolder.auth as HoppRESTAuth | HoppGQLAuth + const parentFolderHeaders = parentFolder.headers as + | HoppRESTHeaders + | HoppGQLHeader[] + // check if the parent folder has authType 'inherit' and if it is the root folder - if (parentFolderAuth?.authType === "inherit" && path.length === 1) { + if ( + parentFolderAuth?.authType === "inherit" && + [...path.slice(0, i + 1)].length === 1 + ) { auth = { parentID: [...path.slice(0, i + 1)].join("/"), parentName: parentFolder.name,