nodejs-pkce 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +13 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +48 -0
- package/dist/index.js.map +1 -0
- package/package.json +47 -0
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export type PkceCodeChallengeMethodType = "plain" | "S256";
|
|
2
|
+
export type PkcePairType = {
|
|
3
|
+
codeVerifier: string;
|
|
4
|
+
codeChallenge: string;
|
|
5
|
+
codeChallengeMethod: PkceCodeChallengeMethodType;
|
|
6
|
+
};
|
|
7
|
+
export declare function generateRandomString(length?: number): Promise<string>;
|
|
8
|
+
export declare function getCryptoInstance(): Promise<Crypto>;
|
|
9
|
+
export declare function generatePkcePair(length?: number, method?: PkceCodeChallengeMethodType): Promise<PkcePairType>;
|
|
10
|
+
export declare function generatePkceCodeVerifier(length?: number): Promise<string>;
|
|
11
|
+
export declare function generatePkceCodeChallenge(codeVerifier: string, method: PkceCodeChallengeMethodType): Promise<string>;
|
|
12
|
+
export declare function verifyPkceCode(codeVerifier: string, codeChallenge: string, method?: PkceCodeChallengeMethodType): Promise<boolean>;
|
|
13
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAEA,MAAM,MAAM,2BAA2B,GAAG,OAAO,GAAG,MAAM,CAAC;AAE3D,MAAM,MAAM,YAAY,GAAG;IACzB,YAAY,EAAE,MAAM,CAAC;IACrB,aAAa,EAAE,MAAM,CAAC;IACtB,mBAAmB,EAAE,2BAA2B,CAAC;CAClD,CAAC;AAEF,wBAAsB,oBAAoB,CACxC,MAAM,GAAE,MAAW,GAClB,OAAO,CAAC,MAAM,CAAC,CAajB;AAED,wBAAsB,iBAAiB,IAAI,OAAO,CAAC,MAAM,CAAC,CAMzD;AAED,wBAAsB,gBAAgB,CACpC,MAAM,GAAE,MAAW,EACnB,MAAM,GAAE,2BAAoC,GAC3C,OAAO,CAAC,YAAY,CAAC,CAKvB;AAED,wBAAsB,wBAAwB,CAC5C,MAAM,GAAE,MAAW,GAClB,OAAO,CAAC,MAAM,CAAC,CAKjB;AAED,wBAAsB,yBAAyB,CAC7C,YAAY,EAAE,MAAM,EACpB,MAAM,EAAE,2BAA2B,GAClC,OAAO,CAAC,MAAM,CAAC,CAmBjB;AAED,wBAAsB,cAAc,CAClC,YAAY,EAAE,MAAM,EACpB,aAAa,EAAE,MAAM,EACrB,MAAM,GAAE,2BAAoC,GAC3C,OAAO,CAAC,OAAO,CAAC,CAIlB"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { webcrypto } from "node:crypto";
|
|
2
|
+
export async function generateRandomString(length = 43) {
|
|
3
|
+
const crypto = await getCryptoInstance();
|
|
4
|
+
const characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-._~";
|
|
5
|
+
let result = "";
|
|
6
|
+
const randomValues = crypto.getRandomValues(new Uint8Array(length));
|
|
7
|
+
for (const value of randomValues) {
|
|
8
|
+
result += characters[value % characters.length];
|
|
9
|
+
}
|
|
10
|
+
return result;
|
|
11
|
+
}
|
|
12
|
+
export async function getCryptoInstance() {
|
|
13
|
+
if (typeof globalThis.crypto !== "undefined") {
|
|
14
|
+
return globalThis.crypto;
|
|
15
|
+
}
|
|
16
|
+
return webcrypto;
|
|
17
|
+
}
|
|
18
|
+
export async function generatePkcePair(length = 43, method = "S256") {
|
|
19
|
+
const codeVerifier = await generatePkceCodeVerifier(length);
|
|
20
|
+
const codeChallenge = await generatePkceCodeChallenge(codeVerifier, method);
|
|
21
|
+
return { codeVerifier, codeChallenge, codeChallengeMethod: method };
|
|
22
|
+
}
|
|
23
|
+
export async function generatePkceCodeVerifier(length = 43) {
|
|
24
|
+
if (length < 43 || length > 128) {
|
|
25
|
+
throw new Error("PKCE code_verifier length must be between 43 and 128");
|
|
26
|
+
}
|
|
27
|
+
return await generateRandomString(length);
|
|
28
|
+
}
|
|
29
|
+
export async function generatePkceCodeChallenge(codeVerifier, method) {
|
|
30
|
+
if (method === "plain") {
|
|
31
|
+
return codeVerifier;
|
|
32
|
+
}
|
|
33
|
+
if (method !== "S256") {
|
|
34
|
+
throw new Error(`PKCE Only support plain or S256 method !`);
|
|
35
|
+
}
|
|
36
|
+
const crypto = await getCryptoInstance();
|
|
37
|
+
const buffer = await crypto.subtle.digest("SHA-256", new TextEncoder().encode(codeVerifier));
|
|
38
|
+
const bytes = buffer instanceof Uint8Array ? buffer : new Uint8Array(buffer);
|
|
39
|
+
return btoa(String.fromCharCode(...bytes))
|
|
40
|
+
.replace(/\+/g, "-")
|
|
41
|
+
.replace(/\//g, "_")
|
|
42
|
+
.replace(/=/g, "");
|
|
43
|
+
}
|
|
44
|
+
export async function verifyPkceCode(codeVerifier, codeChallenge, method = "S256") {
|
|
45
|
+
const compare = await generatePkceCodeChallenge(codeVerifier, method);
|
|
46
|
+
return codeChallenge === compare;
|
|
47
|
+
}
|
|
48
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,SAAS,EAAE,MAAM,aAAa,CAAC;AAUxC,MAAM,CAAC,KAAK,UAAU,oBAAoB,CACxC,SAAiB,EAAE;IAEnB,MAAM,MAAM,GAAG,MAAM,iBAAiB,EAAE,CAAC;IACzC,MAAM,UAAU,GACd,oEAAoE,CAAC;IAEvE,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,MAAM,YAAY,GAAG,MAAM,CAAC,eAAe,CAAC,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC;IAEpE,KAAK,MAAM,KAAK,IAAI,YAAY,EAAE,CAAC;QACj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AAI,WAAW,EAAE,CAAC,MAAM,CAAC,YAAY,CAAC,CACvC,CAAC;IACF,MAAM,KAAK,GAAG,MAAM,YAAY,UAAU,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,IAAI,UAAU,CAAC,MAAM,CAAC,CAAC;IAE7E,OAAO,IAAI,CAAC,MAAM,CAAC,YAAY,CAAC,GAAG,KAAK,CAAC,CAAC;SACvC,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC;SACnB,OAAO,CAAC,KAAK,EAAE,GAAG,CAAC;SACnB,OAAO,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;AACvB,CAAC;AAED,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,YAAoB,EACpB,aAAqB,EACrB,SAAsC,MAAM;IAE5C,MAAM,OAAO,GAAG,MAAM,yBAAyB,CAAC,YAAY,EAAE,MAAM,CAAC,CAAC;IAEtE,OAAO,aAAa,KAAK,OAAO,CAAC;AACnC,CAAC"}
|
package/package.json
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "nodejs-pkce",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "PKCE Code Challenge Generator for Node.js Application",
|
|
5
|
+
"keywords": [
|
|
6
|
+
"pkce",
|
|
7
|
+
"oauth2",
|
|
8
|
+
"oidc",
|
|
9
|
+
"nodejs"
|
|
10
|
+
],
|
|
11
|
+
"homepage": "https://github.com/dheanka10px/nodejs-pkce#readme",
|
|
12
|
+
"bugs": {
|
|
13
|
+
"url": "https://github.com/dheanka10px/nodejs-pkce/issues"
|
|
14
|
+
},
|
|
15
|
+
"repository": {
|
|
16
|
+
"type": "git",
|
|
17
|
+
"url": "git+https://github.com/dheanka10px/nodejs-pkce.git"
|
|
18
|
+
},
|
|
19
|
+
"license": "MIT",
|
|
20
|
+
"author": "dheanka10px",
|
|
21
|
+
"type": "module",
|
|
22
|
+
"main": "dist/index.js",
|
|
23
|
+
"scripts": {
|
|
24
|
+
"build": "tsc",
|
|
25
|
+
"watch": "tsc --watch",
|
|
26
|
+
"format": "npx prettier ./src --write",
|
|
27
|
+
"test": "echo \"Error: no test specified\" && exit 1",
|
|
28
|
+
"lint": "npx eslint ./src",
|
|
29
|
+
"prepublishOnly": "npm run build"
|
|
30
|
+
},
|
|
31
|
+
"exports": {
|
|
32
|
+
".": "./dist/index.js"
|
|
33
|
+
},
|
|
34
|
+
"devDependencies": {
|
|
35
|
+
"@eslint/js": "^10.0.1",
|
|
36
|
+
"@types/node": "^25.3.5",
|
|
37
|
+
"eslint": "^10.0.3",
|
|
38
|
+
"eslint-config-xo": "^0.50.0",
|
|
39
|
+
"globals": "^17.4.0",
|
|
40
|
+
"prettier": "3.8.1",
|
|
41
|
+
"typescript": "^5.9.3",
|
|
42
|
+
"typescript-eslint": "^8.56.1"
|
|
43
|
+
},
|
|
44
|
+
"files": [
|
|
45
|
+
"dist"
|
|
46
|
+
]
|
|
47
|
+
}
|