Skip to content

Commit f246eac

Browse files
committed
feat: updates
1 parent 5d1988e commit f246eac

File tree

5 files changed

+140
-140
lines changed

5 files changed

+140
-140
lines changed

lib/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export * from "./main";
2+
export * from "./types";

lib/main.ts

+91-114
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { subtle, getRandomValues } from "uncrypto";
2-
import { AuthUrlOptions, PKCEChallenge, IssuerRouteTypes } from "./types";
1+
// import { getRandomValues,subtle } from "uncrypto";
2+
import { IssuerRouteTypes, LoginOptions } from "./types";
33

44
/**
55
*
@@ -16,142 +16,119 @@ export const base64UrlEncode = (str: string): string => {
1616
.replace(/=+$/, "");
1717
};
1818

19+
//function to remove trailing slash
20+
export const sanitizeRedirect = (url: string): string => {
21+
return url.replace(/\/$/, "");
22+
};
23+
1924
/**
2025
*
2126
* @param options
2227
* @param type
2328
* @returns URL to redirect to
2429
*/
2530
export const generateAuthUrl = (
26-
options: AuthUrlOptions,
31+
domain: string,
2732
type: IssuerRouteTypes = IssuerRouteTypes.login,
33+
options: LoginOptions,
2834
): URL => {
29-
const authUrl = new URL(options.issuerURL + options.issuerRoutes[type]);
35+
console.log("generateAuthUrl", options);
36+
37+
const authUrl = new URL(`${domain}/oauth2/auth`);
3038

3139
const searchParams: Record<string, string> = {
32-
redirect_uri: generateCallbackUrl(
33-
options.redirectURL,
34-
options.redirectRoutes.callback,
35-
),
40+
redirect_uri: sanitizeRedirect(options.callbackURL),
3641
client_id: options.clientID,
37-
response_type: options.responseType,
38-
scope: options.scope,
39-
code_challenge: options.code_challenge,
40-
code_challenge_method: options.codeChallengeMethod,
42+
response_type: options.responseType || "code",
43+
scope: options.scope.join(" "),
4144
state: options.state,
42-
audience: options.audience,
43-
start_page: type === IssuerRouteTypes.register ? "registration" : "",
45+
start_page: type,
4446
};
4547

46-
for (const [key, value] of Object.entries(options)) {
47-
if (key === "kindeAuth" || searchParams[key]) continue;
48-
if (value !== null && value !== undefined) {
49-
searchParams[key] = value;
50-
}
48+
if (options.codeChallenge) {
49+
searchParams["code_challenge"] = options.codeChallenge;
50+
searchParams["code_challenge_method"] = "S256";
51+
}
52+
53+
if (options.codeChallengeMethod) {
54+
searchParams["code_challenge_method"] = options.codeChallengeMethod;
55+
}
56+
57+
if (options.audience) {
58+
searchParams["audience"] = options.audience;
5159
}
5260

5361
authUrl.search = new URLSearchParams(searchParams).toString();
5462
return authUrl;
5563
};
5664

57-
/**
58-
*
59-
* @param base Base domain URL
60-
* @param path Path to append to the base URL
61-
* @returns
62-
*/
63-
const generateCallbackUrl = (base: string, path: string): string => {
64-
const siteUrl = base.endsWith("/") ? base.slice(0, -1) : base;
65-
const callbackPath = path.startsWith("/") ? path.substring(1) : path;
66-
return `${siteUrl}/${callbackPath}`;
67-
};
68-
69-
/**
70-
*
71-
* @param code_verifier Verifier to generate challenge from
72-
* @returns URL safe base64 encoded string
73-
*/
74-
export async function pkceChallengeFromVerifier(
75-
code_verifier: string,
76-
): Promise<string> {
77-
const hashed = await sha256(code_verifier);
78-
const hashedString = Array.from(new Uint8Array(hashed))
79-
.map((byte) => String.fromCharCode(byte))
80-
.join("");
81-
return base64UrlEncode(hashedString);
82-
}
83-
84-
/**
85-
* Creates a random string of provided length.
86-
* @param {number} length
87-
* @returns {string} required secret
88-
*/
89-
export const generateRandomString = (length: number = 28): string => {
90-
const bytesNeeded = Math.ceil(length / 2);
91-
const array = new Uint32Array(bytesNeeded);
92-
getRandomValues(array);
93-
let result = Array.from(array, (dec) =>
94-
("0" + dec.toString(16)).slice(-2),
95-
).join("");
96-
if (length % 2 !== 0) {
97-
// If the requested length is odd, remove the last character to adjust the length
98-
result = result.slice(0, -1);
99-
}
100-
return result;
101-
};
65+
// /**
66+
// * Creates a random string of provided length.
67+
// * @param {number} length
68+
// * @returns {string} required secret
69+
// */
70+
// export const generateRandomString = (length: number = 28): string => {
71+
// const bytesNeeded = Math.ceil(length / 2);
72+
// const array = new Uint32Array(bytesNeeded);
73+
// getRandomValues(array);
74+
// let result = Array.from(array, (dec) =>
75+
// ("0" + dec.toString(16)).slice(-2),
76+
// ).join("");
77+
// if (length % 2 !== 0) {
78+
// // If the requested length is odd, remove the last character to adjust the length
79+
// result = result.slice(0, -1);
80+
// }
81+
// return result;
82+
// };
10283

103-
/**
104-
* Sanitizes the redirect URL
105-
* @param param0 {baseUrl: string, url: string}
106-
* @returns URL
107-
*/
108-
export const sanitizeRedirect = ({
109-
baseUrl,
110-
url,
111-
}: {
112-
baseUrl: string;
113-
url: string;
114-
}): string => {
115-
if (url.startsWith("/")) {
116-
return `${baseUrl}${url}`;
117-
} else if (new URL(url).origin === baseUrl) {
118-
return url;
119-
}
84+
// //////
12085

121-
return baseUrl;
122-
};
86+
// /**
87+
// *
88+
// * @param code_verifier Verifier to generate challenge from
89+
// * @returns URL safe base64 encoded string
90+
// */
91+
// export async function pkceChallengeFromVerifier(
92+
// code_verifier: string,
93+
// ): Promise<string> {
94+
// const hashed = await sha256(code_verifier);
95+
// const hashedString = Array.from(new Uint8Array(hashed))
96+
// .map((byte) => String.fromCharCode(byte))
97+
// .join("");
98+
// return base64UrlEncode(hashedString);
99+
// }
123100

124-
/**
125-
* setups up PKCE challenge
126-
* @returns
127-
*/
128-
export const setupChallenge = () => {
129-
return { state: generateRandomString(), ...pkceChallenge() };
130-
};
101+
// /**
102+
// * setups up PKCE challenge
103+
// * @returns
104+
// */
105+
// export const setupChallenge = () => {
106+
// return { state: generateRandomString(), ...pkceChallenge() };
107+
// };
131108

132-
/**
133-
* Calculate the SHA256 hash of the input text.
134-
* @param plain the text to hash
135-
* @returns a promise that resolves to an ArrayBuffer
136-
*/
137-
export const sha256 = (plain: string) => {
138-
const encoder = new TextEncoder();
139-
const data = encoder.encode(plain);
140-
return subtle.digest("SHA-256", data);
141-
};
109+
// /**
110+
// * Calculate the SHA256 hash of the input text.
111+
// * @param plain the text to hash
112+
// * @returns a promise that resolves to an ArrayBuffer
113+
// */
114+
// export const sha256 = (plain: string) => {
115+
// const encoder = new TextEncoder();
116+
// const data = encoder.encode(plain);
117+
// return subtle.digest("SHA-256", data);
118+
// };
142119

143-
export async function generateChallenge(code_verifier: string) {
144-
return (await sha256(code_verifier)).toString();
145-
}
120+
// export async function generateChallenge(code_verifier: string) {
121+
// return (await sha256(code_verifier)).toString();
122+
// }
146123

147-
/**
148-
*
149-
* @returns
150-
*/
151-
export const pkceChallenge = async (): Promise<PKCEChallenge> => {
152-
const codeVerifier = generateRandomString();
153-
return {
154-
codeVerifier,
155-
codeChallenge: await generateChallenge(codeVerifier),
156-
};
157-
};
124+
// /**
125+
// *
126+
// * @returns
127+
// */
128+
// export const pkceChallenge = async (): Promise<PKCEChallenge> => {
129+
// const codeVerifier = generateRandomString();
130+
// return {
131+
// codeVerifier,
132+
// codeChallenge: await generateChallenge(codeVerifier),
133+
// };
134+
// };

lib/types.ts

+38-17
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,47 @@
1-
export interface AuthUrlOptions {
2-
code_challenge: string;
3-
state: string;
4-
clientID: string;
5-
issuerURL: string;
1+
export enum Scopes {
2+
email = "email",
3+
profile = "profile",
4+
openid = "openid",
5+
offline_access = "offline_access",
6+
}
7+
8+
export type LoginMethodParams = Pick<
9+
LoginOptions,
10+
| "audience"
11+
| "scope"
12+
| "isCreateOrg"
13+
| "prompt"
14+
| "lang"
15+
| "loginHint"
16+
| "orgCode"
17+
| "orgName"
18+
| "connectionId"
19+
| "redirectURL"
20+
>;
21+
22+
export type LoginOptions = {
23+
audience?: string;
24+
clientId: string;
25+
codeChallenge?: string;
26+
codeChallengeMethod?: string;
27+
connectionId?: string;
28+
isCreateOrg: string;
29+
lang: string;
30+
loginHint: string;
31+
orgCode: string;
32+
orgName: string;
33+
prompt: string;
634
redirectURL: string;
7-
redirectRoutes: {
8-
callback: string;
9-
logout: string;
10-
};
1135
responseType: string;
12-
scope: string;
13-
codeChallengeMethod: string;
14-
audience: string;
15-
issuerRoutes: {
16-
[key in IssuerRouteTypes]: string;
17-
};
18-
}
36+
scope: Scopes[];
37+
state: string;
38+
callbackURL: string;
39+
};
1940

2041
export enum IssuerRouteTypes {
2142
logout = "logout",
2243
login = "login",
23-
register = "register",
44+
register = "registration",
2445
token = "token",
2546
profile = "profile",
2647
}

package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
"publishConfig": {
99
"access": "public"
1010
},
11-
"version": "0.1.0",
11+
"version": "0.1.0-2",
1212
"scripts": {
1313
"dev": "vite",
1414
"build": "tsc && vite build",
@@ -20,7 +20,7 @@
2020
},
2121
"module": "dist/js-utils.js",
2222
"main": "dist/js-utils.cjs",
23-
"types": "dist/main.d.ts",
23+
"types": "dist/index.d.ts",
2424
"devDependencies": {
2525
"@eslint/js": "^9.2.0",
2626
"@types/node": "^20.12.7",
@@ -35,7 +35,7 @@
3535
"vitest": "^1.5.2"
3636
},
3737
"dependencies": {
38-
"jose": "^5.2.4",
39-
"uncrypto": "^0.1.3"
40-
}
38+
"jose": "^5.2.4"
39+
},
40+
"packageManager": "[email protected]+sha256.2df78e65d433d7693b9d3fbdaf431b2d96bb4f96a2ffecd51a50efe16e50a6a8"
4141
}

vite.config.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,12 @@ export default defineConfig({
66
build: {
77
copyPublicDir: false,
88
lib: {
9-
entry: resolve(__dirname, "lib/main.ts"),
9+
entry: resolve(__dirname, "lib/index.ts"),
1010
formats: ["es", "cjs"],
11-
name: "@kinde/jwt-validator",
12-
fileName: "jwt-validator",
11+
name: "@kinde/js-utils",
12+
fileName: "js-utils",
1313
},
14-
target: "modules",
14+
target: "esnext",
1515
outDir: "../dist",
1616
emptyOutDir: true,
1717
},

0 commit comments

Comments
 (0)