Skip to content

Commit c7b873e

Browse files
committed
feat: updates
1 parent ec2e6b6 commit c7b873e

6 files changed

+47
-16
lines changed

lib/utils/base64UrlEncode.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@
33
* @param str String to encode
44
* @returns encoded string
55
*/
6-
export const base64UrlEncode = (str: string): string => {
6+
export const base64UrlEncode = (str: string | ArrayBuffer): string => {
7+
if (str instanceof ArrayBuffer) {
8+
const numberArray = Array.from<number>(new Uint8Array(str));
9+
return btoa(String.fromCharCode.apply(null, numberArray))
10+
.replace(/\+/g, '-')
11+
.replace(/\//g, '_')
12+
.replace(/=+$/, '');
13+
}
14+
715
const encoder = new TextEncoder();
816
const uintArray = encoder.encode(str);
917
const charArray = Array.from(uintArray);
1018
return btoa(String.fromCharCode.apply(null, charArray))
1119
.replace(/\+/g, "-")
1220
.replace(/\//g, "_")
1321
.replace(/=+$/, "");
22+
1423
};

lib/utils/exchangeAuthCode.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,13 @@ export const exchangeAuthCode = async ({
7171

7272
const headers: {
7373
"Content-type": string;
74-
"Cache-Control": string;
75-
Pragma: string;
74+
// "Cache-Control": string;
75+
// Pragma: string;
7676
"Kinde-SDK"?: string;
7777
} = {
7878
"Content-type": "application/x-www-form-urlencoded; charset=UTF-8",
79-
"Cache-Control": "no-store",
80-
Pragma: "no-cache",
79+
// "Cache-Control": "no-store",
80+
// Pragma: "no-cache",
8181
};
8282

8383
if (frameworkSettings.framework) {
@@ -88,8 +88,8 @@ export const exchangeAuthCode = async ({
8888
const response = await fetch(`${domain}/oauth2/token`, {
8989
method: "POST",
9090
// ...(isUseCookie && {credentials: 'include'}),
91-
credentials: "include",
92-
headers,
91+
// credentials: "include",
92+
headers: new Headers(headers),
9393
body: new URLSearchParams({
9494
client_id: clientId,
9595
code,

lib/utils/generateAuthUrl.ts

+7-6
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export const generateAuthUrl = async (
1313
domain: string,
1414
type: IssuerRouteTypes = IssuerRouteTypes.login,
1515
options: LoginOptions,
16-
): Promise<{ url: URL; state: string; nonce: string }> => {
16+
): Promise<{ url: URL; state: string; nonce: string; codeChallenge: string }> => {
1717
const authUrl = new URL(`${domain}/oauth2/auth`);
1818
const activeStorage = getActiveStorage();
1919
const searchParams: Record<string, string> = {
@@ -59,18 +59,19 @@ export const generateAuthUrl = async (
5959
url: authUrl,
6060
state: searchParams["state"],
6161
nonce: searchParams["nonce"],
62+
codeChallenge: searchParams["code_challenge"],
6263
};
6364
};
6465

65-
async function generatePKCEPair(): Promise<{
66+
export async function generatePKCEPair(): Promise<{
6667
codeVerifier: string;
6768
codeChallenge: string;
6869
}> {
69-
const codeVerifier = generateRandomString(43);
70+
const codeVerifier = generateRandomString(52);
7071
const data = new TextEncoder().encode(codeVerifier);
7172
const hashed = await crypto.subtle.digest("SHA-256", data);
72-
const hashArray = Array.from(new Uint8Array(hashed));
73-
const hashString = hashArray.map((b) => String.fromCharCode(b)).join("");
74-
const codeChallenge = base64UrlEncode(hashString);
73+
// const hashArray = Array.from(new Uint8Array(hashed));
74+
// const hashString = hashArray.map((b) => String.fromCharCode(b)).join("");
75+
const codeChallenge = base64UrlEncode(hashed);
7576
return { codeVerifier, codeChallenge };
7677
}

lib/utils/token/isAuthenticated.test.ts

+10
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,14 @@ describe("isAuthenticated", () => {
9191
mockError,
9292
);
9393
});
94+
95+
it("should return false if token is missing exp", async () => {
96+
vi.spyOn(tokenUtils, "getDecodedToken").mockResolvedValue({
97+
// Missing 'exp' field
98+
});
99+
100+
const result = await isAuthenticated();
101+
102+
expect(result).toBe(false);
103+
});
94104
});

lib/utils/token/isAuthenticated.ts

+5
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ export const isAuthenticated = async (
2828
const token = await getDecodedToken<JWTDecoded>("accessToken");
2929
if (!token) return false;
3030

31+
if (!token.exp) {
32+
console.error("Token does not have an expiry");
33+
return false;
34+
}
35+
3136
const isExpired = token.exp < Math.floor(Date.now() / 1000);
3237

3338
if (isExpired && props?.useRefreshToken) {

lib/utils/token/refreshToken.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import { sanatizeURL } from "..";
77
* @returns { Promise<boolean> }
88
*/
99
export const refreshToken = async (
10-
domain?: string,
11-
clientId?: string,
10+
domain: string,
11+
clientId: string,
1212
): Promise<boolean> => {
1313
try {
1414
if (!domain) {
@@ -23,9 +23,15 @@ export const refreshToken = async (
2323

2424
const storage = await getActiveStorage();
2525

26+
if (!storage) {
27+
console.error("No active storage found");
28+
return false;
29+
}
30+
2631
const refreshTokenValue = await storage.getSessionItem(
2732
StorageKeys.refreshToken,
28-
);
33+
) as string;
34+
2935

3036
if (!refreshTokenValue) {
3137
console.error("No refresh token found");

0 commit comments

Comments
 (0)