Skip to content

Commit b07212c

Browse files
feat(common): add advanced configuration for auth and token request parameters (hoppscotch#5253)
Co-authored-by: jamesgeorge007 <[email protected]>
1 parent 8f5eed5 commit b07212c

File tree

24 files changed

+1426
-113
lines changed

24 files changed

+1426
-113
lines changed

packages/hoppscotch-cli/src/__tests__/unit/fixtures/workspace-access.mock.ts

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -485,17 +485,17 @@ export const WORKSPACE_MULTIPLE_CHILD_COLLECTIONS_WITH_AUTH_HEADERS_MOCK: Worksp
485485
export const TRANSFORMED_MULTIPLE_CHILD_COLLECTIONS_WITH_AUTH_HEADERS_MOCK: HoppCollection[] =
486486
[
487487
{
488-
v: 8,
488+
v: 9,
489489
id: "clx1f86hv000010f8szcfya0t",
490490
name: "Multiple child collections with authorization & headers set at each level",
491491
folders: [
492492
{
493-
v: 8,
493+
v: 9,
494494
id: "clx1fjgah000110f8a5bs68gd",
495495
name: "folder-1",
496496
folders: [
497497
{
498-
v: 8,
498+
v: 9,
499499
id: "clx1fjwmm000410f8l1gkkr1a",
500500
name: "folder-11",
501501
folders: [],
@@ -537,7 +537,7 @@ export const TRANSFORMED_MULTIPLE_CHILD_COLLECTIONS_WITH_AUTH_HEADERS_MOCK: Hopp
537537
],
538538
},
539539
{
540-
v: 8,
540+
v: 9,
541541
id: "clx1fjyxm000510f8pv90dt43",
542542
name: "folder-12",
543543
folders: [],
@@ -595,7 +595,7 @@ export const TRANSFORMED_MULTIPLE_CHILD_COLLECTIONS_WITH_AUTH_HEADERS_MOCK: Hopp
595595
],
596596
},
597597
{
598-
v: 8,
598+
v: 9,
599599
id: "clx1fk1cv000610f88kc3aupy",
600600
name: "folder-13",
601601
folders: [],
@@ -707,12 +707,12 @@ export const TRANSFORMED_MULTIPLE_CHILD_COLLECTIONS_WITH_AUTH_HEADERS_MOCK: Hopp
707707
],
708708
},
709709
{
710-
v: 8,
710+
v: 9,
711711
id: "clx1fjk9o000210f8j0573pls",
712712
name: "folder-2",
713713
folders: [
714714
{
715-
v: 8,
715+
v: 9,
716716
id: "clx1fk516000710f87sfpw6bo",
717717
name: "folder-21",
718718
folders: [],
@@ -752,7 +752,7 @@ export const TRANSFORMED_MULTIPLE_CHILD_COLLECTIONS_WITH_AUTH_HEADERS_MOCK: Hopp
752752
],
753753
},
754754
{
755-
v: 8,
755+
v: 9,
756756
id: "clx1fk72t000810f8gfwkpi5y",
757757
name: "folder-22",
758758
folders: [],
@@ -810,7 +810,7 @@ export const TRANSFORMED_MULTIPLE_CHILD_COLLECTIONS_WITH_AUTH_HEADERS_MOCK: Hopp
810810
],
811811
},
812812
{
813-
v: 8,
813+
v: 9,
814814
id: "clx1fk95g000910f8bunhaoo8",
815815
name: "folder-23",
816816
folders: [],
@@ -915,12 +915,12 @@ export const TRANSFORMED_MULTIPLE_CHILD_COLLECTIONS_WITH_AUTH_HEADERS_MOCK: Hopp
915915
],
916916
},
917917
{
918-
v: 8,
918+
v: 9,
919919
id: "clx1fjmlq000310f86o4d3w2o",
920920
name: "folder-3",
921921
folders: [
922922
{
923-
v: 8,
923+
v: 9,
924924
id: "clx1iwq0p003e10f8u8zg0p85",
925925
name: "folder-31",
926926
folders: [],
@@ -960,7 +960,7 @@ export const TRANSFORMED_MULTIPLE_CHILD_COLLECTIONS_WITH_AUTH_HEADERS_MOCK: Hopp
960960
],
961961
},
962962
{
963-
v: 8,
963+
v: 9,
964964
id: "clx1izut7003m10f894ip59zg",
965965
name: "folder-32",
966966
folders: [],
@@ -1018,7 +1018,7 @@ export const TRANSFORMED_MULTIPLE_CHILD_COLLECTIONS_WITH_AUTH_HEADERS_MOCK: Hopp
10181018
],
10191019
},
10201020
{
1021-
v: 8,
1021+
v: 9,
10221022
id: "clx1j2ka9003q10f8cdbzpgpg",
10231023
name: "folder-33",
10241024
folders: [],

packages/hoppscotch-cli/src/utils/workspace-access.ts

Lines changed: 70 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,71 @@ const normalizeEnvironmentVariable = (variable: HoppEnvPair): HoppEnvPair => {
7676
};
7777
};
7878

79+
/**
80+
* Transforms the given `HoppRESTAuth` object to ensure it conforms to the latest
81+
* OAuth 2.0 authentication structure. Depending on the `grantType` within the
82+
* `grantTypeInfo` property, this function adds or initializes specific fields
83+
* such as `clientAuthentication`, `authRequestParams`, `tokenRequestParams`,
84+
* and `refreshRequestParams` to maintain compatibility with updated schema
85+
* requirements.
86+
*
87+
* - For "CLIENT_CREDENTIALS" grant type, sets `clientAuthentication` to "IN_BODY"
88+
* and initializes `tokenRequestParams` and `refreshRequestParams` as empty arrays.
89+
* - For "AUTHORIZATION_CODE" grant type, initializes `authRequestParams`,
90+
* `tokenRequestParams`, and `refreshRequestParams` as empty arrays.
91+
* - For "PASSWORD" grant type, initializes `tokenRequestParams` and
92+
* `refreshRequestParams` as empty arrays.
93+
* - For "IMPLICIT" grant type, initializes `authRequestParams` and
94+
* `refreshRequestParams` as empty arrays.
95+
*
96+
* If the `authType` is not "oauth-2", the original `auth` object is returned unchanged.
97+
*
98+
* @param {HoppRESTAuth} auth - The authentication object to transform.
99+
* @returns {HoppRESTAuth} The transformed authentication object with updated grant type information.
100+
*/
101+
const transformAuth = (auth: HoppRESTAuth): HoppRESTAuth => {
102+
if (auth.authType === "oauth-2") {
103+
const oldGrantTypeInfo = auth.grantTypeInfo;
104+
let newGrantTypeInfo = oldGrantTypeInfo;
105+
106+
// Add clientAuthentication for CLIENT_CREDENTIALS
107+
if (oldGrantTypeInfo.grantType === "CLIENT_CREDENTIALS") {
108+
newGrantTypeInfo = {
109+
...oldGrantTypeInfo,
110+
clientAuthentication: "IN_BODY",
111+
tokenRequestParams: [],
112+
refreshRequestParams: [],
113+
};
114+
} else if (oldGrantTypeInfo.grantType === "AUTHORIZATION_CODE") {
115+
newGrantTypeInfo = {
116+
...oldGrantTypeInfo,
117+
authRequestParams: [],
118+
tokenRequestParams: [],
119+
refreshRequestParams: [],
120+
};
121+
} else if (oldGrantTypeInfo.grantType === "PASSWORD") {
122+
newGrantTypeInfo = {
123+
...oldGrantTypeInfo,
124+
tokenRequestParams: [],
125+
refreshRequestParams: [],
126+
};
127+
} else if (oldGrantTypeInfo.grantType === "IMPLICIT") {
128+
newGrantTypeInfo = {
129+
...oldGrantTypeInfo,
130+
authRequestParams: [],
131+
refreshRequestParams: [],
132+
};
133+
}
134+
135+
return {
136+
...auth,
137+
grantTypeInfo: newGrantTypeInfo,
138+
};
139+
}
140+
141+
return auth;
142+
};
143+
79144
/**
80145
* Transforms workspace environment data to the `HoppEnvironment` format.
81146
*
@@ -115,21 +180,9 @@ export const transformWorkspaceCollections = (
115180
const { auth = { authType: "inherit", authActive: true }, headers = [] } =
116181
parsedData;
117182

118-
const migratedAuth: HoppRESTAuth =
119-
auth.authType === "oauth-2"
120-
? {
121-
...auth,
122-
grantTypeInfo:
123-
auth.grantTypeInfo.grantType === "CLIENT_CREDENTIALS"
124-
? {
125-
...auth.grantTypeInfo,
126-
clientAuthentication: "IN_BODY",
127-
}
128-
: auth.grantTypeInfo,
129-
}
130-
: auth;
131-
132-
const migratedHeaders = headers.map((header) =>
183+
const transformedAuth = transformAuth(auth);
184+
185+
const transformedHeaders = headers.map((header) =>
133186
header.description ? header : { ...header, description: "" }
134187
);
135188

@@ -142,8 +195,8 @@ export const transformWorkspaceCollections = (
142195
name: title,
143196
folders: transformWorkspaceCollections(folders),
144197
requests: transformWorkspaceRequests(requests),
145-
auth: migratedAuth,
146-
headers: migratedHeaders,
198+
auth: transformedAuth,
199+
headers: transformedHeaders,
147200
};
148201
});
149202
};

packages/hoppscotch-common/locales/en.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,11 @@
241241
"label_send_as": "Client Authentication",
242242
"label_send_in_body": "Send Credentials in Body",
243243
"label_send_as_basic_auth": "Send Credentials as Basic Auth",
244-
"enter_value": "Enter value"
244+
"enter_value": "Enter value",
245+
"auth_request": "Auth Request",
246+
"token_request": "Token Request",
247+
"refresh_request": "Refresh Request",
248+
"send_in": "Send In"
245249
},
246250
"pass_key_by": "Pass by",
247251
"pass_by_query_params_label": "Query Parameters",

packages/hoppscotch-common/src/components.d.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,9 @@ declare module 'vue' {
211211
IconLucideArrowUpRight: typeof import('~icons/lucide/arrow-up-right')['default']
212212
IconLucideBrush: typeof import('~icons/lucide/brush')['default']
213213
IconLucideCheckCircle: typeof import('~icons/lucide/check-circle')['default']
214+
IconLucideChevronDown: typeof import('~icons/lucide/chevron-down')['default']
214215
IconLucideChevronRight: typeof import('~icons/lucide/chevron-right')['default']
216+
IconLucideChevronUp: typeof import('~icons/lucide/chevron-up')['default']
215217
IconLucideCircleCheck: typeof import('~icons/lucide/circle-check')['default']
216218
IconLucideGlobe: typeof import('~icons/lucide/globe')['default']
217219
IconLucideHelpCircle: typeof import('~icons/lucide/help-circle')['default']
@@ -221,7 +223,6 @@ declare module 'vue' {
221223
IconLucideListEnd: typeof import('~icons/lucide/list-end')['default']
222224
IconLucideMinus: typeof import('~icons/lucide/minus')['default']
223225
IconLucidePlusCircle: typeof import('~icons/lucide/plus-circle')['default']
224-
IconLucideRss: typeof import('~icons/lucide/rss')['default']
225226
IconLucideSearch: typeof import('~icons/lucide/search')['default']
226227
IconLucideTriangleAlert: typeof import('~icons/lucide/triangle-alert')['default']
227228
IconLucideUsers: typeof import('~icons/lucide/users')['default']
@@ -254,6 +255,7 @@ declare module 'vue' {
254255
LensesRenderersVideoLensRenderer: typeof import('./components/lenses/renderers/VideoLensRenderer.vue')['default']
255256
LensesRenderersXMLLensRenderer: typeof import('./components/lenses/renderers/XMLLensRenderer.vue')['default']
256257
LensesResponseBodyRenderer: typeof import('./components/lenses/ResponseBodyRenderer.vue')['default']
258+
MonacoScriptEditor: typeof import('./components/MonacoScriptEditor.vue')['default']
257259
ProfileUserDelete: typeof import('./components/profile/UserDelete.vue')['default']
258260
RealtimeCommunication: typeof import('./components/realtime/Communication.vue')['default']
259261
RealtimeConnectionConfig: typeof import('./components/realtime/ConnectionConfig.vue')['default']

packages/hoppscotch-common/src/components/http/KeyValue.vue

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
:model-value="name"
2424
:placeholder="t('count.key')"
2525
:auto-complete-source="keyAutoCompleteSource"
26-
:auto-complete-env="true"
26+
:auto-complete-env="autoCompleteEnv"
2727
:envs="envs"
2828
:inspection-results="inspectionKeyResult"
2929
@update:model-value="emit('update:name', $event)"
@@ -41,7 +41,7 @@
4141
:class="{ 'opacity-50': !entityActive }"
4242
:model-value="value"
4343
:placeholder="t('count.value')"
44-
:auto-complete-env="true"
44+
:auto-complete-env="autoCompleteEnv"
4545
:envs="envs"
4646
:inspection-results="inspectionValueResult"
4747
@update:model-value="emit('update:value', $event)"
@@ -56,7 +56,10 @@
5656
"
5757
/>
5858

59+
<slot name="after-value"></slot>
60+
5961
<input
62+
v-if="showDescription"
6063
:value="description"
6164
:placeholder="t('count.description')"
6265
class="flex flex-1 px-4 bg-transparent"
@@ -133,20 +136,33 @@ type Entity = {
133136
134137
const t = useI18n()
135138
136-
defineProps<{
137-
total: number
138-
index: number
139-
entityId: number
140-
isActive: boolean
141-
entityActive: boolean
142-
name: string
143-
value: string
144-
inspectionKeyResult?: InspectorResult[]
145-
inspectionValueResult?: InspectorResult[]
146-
description?: string
147-
envs?: AggregateEnvironment[]
148-
keyAutoCompleteSource?: string[]
149-
}>()
139+
withDefaults(
140+
defineProps<{
141+
showDescription?: boolean
142+
total: number
143+
index: number
144+
entityId: number
145+
isActive: boolean
146+
entityActive: boolean
147+
name: string
148+
value: string
149+
inspectionKeyResult?: InspectorResult[]
150+
inspectionValueResult?: InspectorResult[]
151+
description?: string
152+
envs?: AggregateEnvironment[]
153+
autoCompleteEnv?: boolean
154+
keyAutoCompleteSource?: string[]
155+
}>(),
156+
{
157+
showDescription: true,
158+
description: "",
159+
inspectionKeyResult: () => [],
160+
inspectionValueResult: () => [],
161+
envs: () => [],
162+
autoCompleteEnv: true,
163+
keyAutoCompleteSource: () => [],
164+
}
165+
)
150166
151167
const emit = defineEmits<{
152168
(e: "update:name", value: string): void

0 commit comments

Comments
 (0)