Skip to content

Commit 795cc82

Browse files
authored
fix: preserve PKCE and client secret in postman collection imports (hoppscotch#5480)
1 parent aa15837 commit 795cc82

File tree

3 files changed

+54
-6
lines changed

3 files changed

+54
-6
lines changed

packages/hoppscotch-common/src/composables/oauth2/useOAuth2GrantTypes.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,6 +178,9 @@ export const useOAuth2GrantTypes = (
178178
id: "plain" | "S256"
179179
label: string
180180
} | null> = refWithCallbackOnChange(
181+
// If the collection was imported before `codeVerifierMethod` existed,
182+
// default to 'plain' when PKCE is enabled so the UI and validation
183+
// remain consistent.
181184
auth.value.grantTypeInfo.codeVerifierMethod
182185
? {
183186
id: auth.value.grantTypeInfo.codeVerifierMethod,
@@ -186,7 +189,12 @@ export const useOAuth2GrantTypes = (
186189
? "Plain"
187190
: "SHA-256",
188191
}
189-
: null,
192+
: auth.value.grantTypeInfo.isPKCE
193+
? {
194+
id: "plain",
195+
label: "Plain",
196+
}
197+
: null,
190198
(value) => {
191199
if (!("codeVerifierMethod" in auth.value.grantTypeInfo) || !value) {
192200
return
@@ -249,7 +257,12 @@ export const useOAuth2GrantTypes = (
249257
clientSecret: clientSecret.value,
250258
scopes: scopes.value,
251259
isPKCE: isPKCE.value,
252-
codeVerifierMethod: codeChallenge.value?.id,
260+
// Ensure older collections without `codeVerifierMethod` get a default
261+
// so schema validation does not fail. Default to 'plain' when PKCE
262+
// is enabled.
263+
codeVerifierMethod:
264+
codeChallenge.value?.id ??
265+
(isPKCE.value ? ("plain" as const) : undefined),
253266
authRequestParams: preparedAuthRequestParams.value,
254267
tokenRequestParams: preparedTokenRequestParams.value,
255268
refreshRequestParams: preparedRefreshRequestParams.value,

packages/hoppscotch-common/src/helpers/import-export/import/postman.ts

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,6 +303,31 @@ const getHoppReqAuth = (
303303
const token = replacePMVarTemplating(
304304
getVariableValue(auth.oauth2, "accessToken") ?? ""
305305
)
306+
const clientSecret = replacePMVarTemplating(
307+
getVariableValue(auth.oauth2, "clientSecret") ?? ""
308+
)
309+
310+
// Check for PKCE settings
311+
const usePkce = getVariableValue(auth.oauth2, "usePkce")
312+
const isPKCE = usePkce === "true"
313+
314+
// Get challenge algorithm, default to S256 if PKCE is enabled but no algorithm specified
315+
const challengeAlgorithm = getVariableValue(
316+
auth.oauth2,
317+
"challengeAlgorithm"
318+
)
319+
let codeVerifierMethod: "plain" | "S256" | undefined
320+
321+
if (isPKCE) {
322+
// Postman uses "SHA-256" or "plain" - normalize to our format
323+
// Default to S256 for any value other than "plain"
324+
if (challengeAlgorithm === "plain") {
325+
codeVerifierMethod = "plain"
326+
} else {
327+
// Covers "S256", "SHA-256", undefined, and any other value
328+
codeVerifierMethod = "S256"
329+
}
330+
}
306331

307332
return {
308333
authType: "oauth-2",
@@ -314,8 +339,9 @@ const getHoppReqAuth = (
314339
scopes: scope,
315340
token: token,
316341
tokenEndpoint: accessTokenURL,
317-
clientSecret: "",
318-
isPKCE: false,
342+
clientSecret: clientSecret,
343+
isPKCE: isPKCE,
344+
...(codeVerifierMethod ? { codeVerifierMethod } : {}),
319345
authRequestParams: [],
320346
tokenRequestParams: [],
321347
refreshRequestParams: [],

packages/hoppscotch-common/src/services/oauth/flows/authCode.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,11 +91,19 @@ const initAuthCodeOauthFlow = async ({
9191
let codeVerifier: string | undefined
9292
let codeChallenge: string | undefined
9393

94+
// Ensure backward compatibility for collections that were imported before
95+
// `codeVerifierMethod` was added. If PKCE is enabled but the method is
96+
// missing, default to 'plain' as requested by the user.
97+
const codeVerifierMethodNormalized =
98+
isPKCE && !codeVerifierMethod ? ("plain" as const) : codeVerifierMethod
99+
94100
if (isPKCE) {
95101
codeVerifier = generateCodeVerifier()
102+
// codeVerifierMethodNormalized might be undefined only if isPKCE is false,
103+
// but here we guard with isPKCE so it's safe to pass a value.
96104
codeChallenge = await generateCodeChallenge(
97105
codeVerifier,
98-
codeVerifierMethod
106+
codeVerifierMethodNormalized
99107
)
100108
}
101109

@@ -137,7 +145,8 @@ const initAuthCodeOauthFlow = async ({
137145
clientSecret,
138146
clientID,
139147
isPKCE,
140-
codeVerifierMethod,
148+
// Persist the normalized method so subsequent redirect handling has a value
149+
codeVerifierMethod: codeVerifierMethodNormalized,
141150
scopes,
142151
authRequestParams,
143152
refreshRequestParams,

0 commit comments

Comments
 (0)