fix(oauth): encode clientId and clientSecret in authorization header (#2120)

* fix(oauth2): encode clientId and clientSecret in authorization header

* fix(oauth2): refactor to use encodeOAuthParameter for clientId and clientSecret

* chore: lint

---------

Co-authored-by: Bereket Engida <bekacru@gmail.com>
This commit is contained in:
Xinyao
2025-04-05 13:02:26 +08:00
committed by GitHub
parent a686070437
commit ffa24f74a5
3 changed files with 11 additions and 2 deletions

View File

@@ -1,6 +1,7 @@
import { betterFetch } from "@better-fetch/fetch";
import type { OAuth2Tokens } from "./types";
import type { ProviderOptions } from "./types";
import { encodeOAuthParameter } from "./utils";
export async function refreshAccessToken({
refreshToken,
@@ -27,7 +28,9 @@ export async function refreshAccessToken({
body.set("refresh_token", refreshToken);
if (authentication === "basic") {
const encodedCredentials = btoa(
`${options.clientId}:${options.clientSecret}`,
`${encodeOAuthParameter(options.clientId)}:${encodeOAuthParameter(
options.clientSecret,
)}`,
);
headers["authorization"] = `Basic ${encodedCredentials}`;
} else {

View File

@@ -26,3 +26,6 @@ export function getOAuth2Tokens(data: Record<string, any>): OAuth2Tokens {
idToken: data.id_token,
};
}
export const encodeOAuthParameter = (value: string) =>
encodeURIComponent(value).replace(/%20/g, "+");

View File

@@ -2,6 +2,7 @@ import { betterFetch } from "@better-fetch/fetch";
import type { ProviderOptions } from "./types";
import { getOAuth2Tokens } from "./utils";
import { jwtVerify } from "jose";
import { encodeOAuthParameter } from "./utils";
export async function validateAuthorizationCode({
code,
@@ -34,7 +35,9 @@ export async function validateAuthorizationCode({
body.set("redirect_uri", options.redirectURI || redirectURI);
if (authentication === "basic") {
const encodedCredentials = btoa(
`${options.clientId}:${options.clientSecret}`,
`${encodeOAuthParameter(options.clientId)}:${encodeOAuthParameter(
options.clientSecret,
)}`,
);
headers["authorization"] = `Basic ${encodedCredentials}`;
} else {