mirror of
https://github.com/better-auth/better-auth.git
synced 2026-05-27 17:36:42 -05:00
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:
@@ -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 {
|
||||
|
||||
@@ -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, "+");
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user