Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: OAuth 2.0 Grant Type Authorization: "invalid_client" error / URL Encode of Client ID #2129

Merged
merged 1 commit into from
Jul 26, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions packages/bruno-electron/src/ipc/network/oauth2-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,26 +51,26 @@ const getOAuth2AuthorizationCode = (request, codeChallenge, collectionUid) => {
const { oauth2 } = request;
const { callbackUrl, clientId, authorizationUrl, scope, state, pkce } = oauth2;

let oauth2QueryParams =
(authorizationUrl.indexOf('?') > -1 ? '&' : '?') + `client_id=${clientId}&response_type=code`;
const authorizationUrlWithQueryParams = new URL(authorizationUrl);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the URL class to construct the request URL looks a lot cleaner than the previous string concatenation with some nested ternary operator in the middle. 👍

authorizationUrlWithQueryParams.searchParams.append('response_type', 'code');
authorizationUrlWithQueryParams.searchParams.append('client_id', clientId);
if (callbackUrl) {
oauth2QueryParams += `&redirect_uri=${callbackUrl}`;
authorizationUrlWithQueryParams.searchParams.append('redirect_uri', callbackUrl);
}
if (scope) {
oauth2QueryParams += `&scope=${scope}`;
authorizationUrlWithQueryParams.searchParams.append('scope', scope);
}
if (pkce) {
oauth2QueryParams += `&code_challenge=${codeChallenge}&code_challenge_method=S256`;
authorizationUrlWithQueryParams.searchParams.append('code_challenge', codeChallenge);
authorizationUrlWithQueryParams.searchParams.append('code_challenge_method', 'S256');
}
if (state) {
oauth2QueryParams += `&state=${state}`;
authorizationUrlWithQueryParams.searchParams.append('state', state);
}

const authorizationUrlWithQueryParams = authorizationUrl + oauth2QueryParams;
try {
const oauth2Store = new Oauth2Store();
const { authorizationCode } = await authorizeUserInWindow({
authorizeUrl: authorizationUrlWithQueryParams,
authorizeUrl: authorizationUrlWithQueryParams.toString(),
callbackUrl,
session: oauth2Store.getSessionIdOfCollection(collectionUid)
});
Expand Down
Loading