Skip to content

Open-Source API Development Ecosystem • https://hoppscotch.io • Offline, On-Prem & Cloud • Web, Desktop & CLI • Open-Source Alternative to Postman, Insomnia

fix(common): oauth2 basic header encoding (#4927)

Christoffer Hjalmarsson0c361faeabfb
diff --git a/packages/hoppscotch-common/src/services/oauth/flows/clientCredentials.ts b/packages/hoppscotch-common/src/services/oauth/flows/clientCredentials.ts
index 19fa0bca2..cb0941567 100644
--- a/packages/hoppscotch-common/src/services/oauth/flows/clientCredentials.ts
+++ b/packages/hoppscotch-common/src/services/oauth/flows/clientCredentials.ts
@@ -177,7 +177,10 @@ const getPayloadForViaBasicAuthHeader = (
): RelayRequest => {
const { clientID, clientSecret, scopes, authEndpoint } = payload
- const basicAuthToken = btoa(`${clientID}:${clientSecret}`)
+ // RFC 6749 Section 2.3.1 states that the client ID and secret should be URL encoded.
+ const encodedClientID = encodeBasicAuthComponent(clientID)
+ const encodedClientSecret = encodeBasicAuthComponent(clientSecret || "")
+ const basicAuthToken = btoa(`${encodedClientID}:${encodedClientSecret}`)
return {
id: Date.now(),
@@ -218,3 +221,9 @@ const getPayloadForViaBody = (
}),
}
}
+
+const encodeBasicAuthComponent = (component: string): string => {
+ // application/x-www-form-urlencoded expects spaces to be encoded as '+', but
+ // encodeURIComponent encodes them as '%20'.
+ return encodeURIComponent(component).replace(/%20/g, "+")
+}