@payello-module/jwt 0.1.0 → 0.1.1

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.
@@ -1,13 +1,6 @@
1
- import { HmacSha512, HmacSha256 } from '@payello-module/encryption';
2
-
3
- class JwtError extends Error {
4
- type = "@payello/module-jwt#JwtError";
5
- constructor(message, options) {
6
- super(message, options);
7
- }
8
- }
9
-
10
- class JWT {
1
+ import { HmacSha256, HmacSha512 } from "@payello-module/encryption";
2
+ import { JwtError } from "./JwtError";
3
+ export class JWT {
11
4
  static async sign(payload, options = { privKey: '', pubKey: '', algorithm: 'HS512' }) {
12
5
  const _header = {
13
6
  typ: 'JWT',
@@ -80,5 +73,3 @@ class JWT {
80
73
  return verify;
81
74
  }
82
75
  }
83
-
84
- export { JWT, JwtError };
@@ -0,0 +1,6 @@
1
+ export class JwtError extends Error {
2
+ type = "@payello/module-jwt#JwtError";
3
+ constructor(message, options) {
4
+ super(message, options);
5
+ }
6
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,13 @@
1
+ import { JWT } from "./JWT";
2
+ async function example() {
3
+ const payload = {
4
+ exp: Math.floor(Date.now().valueOf() / 1000) + 300 // Expire in 300 seconds
5
+ };
6
+ const opts = {
7
+ privKey: "79c4e267e63845a986e669388fce66e9", // Private/Secret Key
8
+ pubKey: "f266a28e-5e9a-4fe3-90a8-2e8b2ef0f62d", // Public Key (Issuer ID)
9
+ algorithm: "HS256" // Possible values: HS256 or HS512
10
+ };
11
+ const jwt = await JWT.sign(payload, opts);
12
+ console.log(jwt);
13
+ }
package/dist/index.js CHANGED
@@ -1,87 +1,7 @@
1
- 'use strict';
2
-
3
- var encryption = require('@payello-module/encryption');
4
-
5
- class JwtError extends Error {
6
- type = "@payello/module-jwt#JwtError";
7
- constructor(message, options) {
8
- super(message, options);
9
- }
10
- }
11
-
12
- class JWT {
13
- static async sign(payload, options = { privKey: '', pubKey: '', algorithm: 'HS512' }) {
14
- const _header = {
15
- typ: 'JWT',
16
- alg: options.algorithm
17
- };
18
- const _payload = {
19
- jti: `jti_${Date.now().valueOf()}`,
20
- iat: Math.floor(Date.now().valueOf() / 1000),
21
- iss: options.pubKey,
22
- ...payload
23
- };
24
- const _body = btoa(JSON.stringify(_header)) + "." + btoa(JSON.stringify(_payload));
25
- let signature = "";
26
- switch (options.algorithm) {
27
- case 'HS256':
28
- signature = await encryption.HmacSha256.encrypt(_body, options.privKey);
29
- break;
30
- case 'HS512':
31
- signature = await encryption.HmacSha512.encrypt(_body, options.privKey);
32
- break;
33
- }
34
- return _body + "." + signature;
35
- }
36
- static async extract(input, opts = {}) {
37
- const bits = input.split(".");
38
- if (bits.length !== 3) {
39
- throw new JwtError(`Invalid number of parts in JWT string. Expected 3 but got ${bits.length}`);
40
- }
41
- const header = JSON.parse(bits[0]);
42
- if (!header || !header.typ || header.typ !== "JWT") {
43
- throw new JwtError("Header invalid or type is not JWT");
44
- }
45
- const payload = JSON.parse(bits[1]);
46
- if (!payload || !payload.jti) {
47
- throw new JwtError("Payload invalid or missing jti value");
48
- }
49
- const requiredProps = opts.requiredProps || ["jti", "iss", "iat"];
50
- for (const prop in requiredProps) {
51
- if (!payload[prop]) {
52
- throw new JwtError(`Payload missing ${prop} value`);
53
- }
54
- }
55
- return {
56
- header: header,
57
- payload: payload,
58
- signature: bits[2]
59
- };
60
- }
61
- static async verify(input, opts) {
62
- const extracted = await this.extract(input);
63
- const secretKey = await opts.getSecretKey(extracted.payload.jti);
64
- if (!secretKey) {
65
- throw new JwtError(`Public key not found`);
66
- }
67
- let verify = false;
68
- const data = `${btoa(JSON.stringify(extracted.header))}.${btoa(JSON.stringify(extracted.payload))}`;
69
- switch (extracted.header.alg) {
70
- case 'HS256':
71
- verify = await encryption.HmacSha256.verify(data, extracted.signature, secretKey);
72
- break;
73
- case 'HS512':
74
- verify = await encryption.HmacSha512.verify(data, extracted.signature, secretKey);
75
- break;
76
- default:
77
- throw new JwtError(`Unsupported algorithm`);
78
- }
79
- if (!verify) {
80
- throw new JwtError(`Signature not verified`);
81
- }
82
- return verify;
83
- }
84
- }
85
-
86
- exports.JWT = JWT;
87
- exports.JwtError = JwtError;
1
+ export * from './JWT';
2
+ export * from './JwtError';
3
+ export * from './JwtExtract';
4
+ export * from './JwtExtractOpts';
5
+ export * from './JwtHeader';
6
+ export * from './JwtSignOpts';
7
+ export * from './example';
package/package.json CHANGED
@@ -1,41 +1,28 @@
1
- {
2
- "name": "@payello-module/jwt",
3
- "description": "JSON Web Token Module",
4
- "author": "Payello (https://payello.com/)",
5
- "version": "0.1.0",
6
- "license": "UNLICENSED",
7
- "type": "module",
8
- "exports": {
9
- ".": {
10
- "import": "./dist/index.js"
11
- }
12
- },
13
- "typesVersions": {
14
- "*": {
15
- "*": [
16
- "./dist/*"
17
- ]
18
- }
19
- },
20
- "files": [
21
- "dist/*"
22
- ],
23
- "devDependencies": {
24
- "@rollup/plugin-commonjs": "^25.0.7",
25
- "create-ts-index": "^1.14.0",
26
- "rollup": "^4.1.4",
27
- "rollup-plugin-delete": "^2.0.0",
28
- "rollup-plugin-typescript2": "^0.36.0",
29
- "tslib": "^2.6.2",
30
- "typescript": "^5.2.2"
31
- },
32
- "scripts": {
33
- "build": "cti create ./src -b -w & rollup -c",
34
- "patch": "npm update & npm version patch -f & npm run build & npm publish -access=public",
35
- "minor": "npm update & npm version minor -f & npm run build & npm publish -access=public",
36
- "major": "npm update & npm version major -f & npm run build & npm publish -access=public"
37
- },
38
- "dependencies": {
39
- "@payello-module/encryption": "^0.1.1"
40
- }
41
- }
1
+ {
2
+ "name": "@payello-module/jwt",
3
+ "version": "0.1.1",
4
+ "author": "Payello <devsupport@payello.com> (https://payello.com/)",
5
+ "displayName": "@payello-module/jwt",
6
+ "description": "JSON Web Token Module",
7
+ "main": "dist/index.js",
8
+ "types": "dist/index.d.ts",
9
+ "scripts": {
10
+ "build": "tsc --build --clean && tsc",
11
+ "watch": "tsc --watch",
12
+ "prepare": "npm run build"
13
+ },
14
+ "devDependencies": {
15
+ "typescript": "^5.3.3"
16
+ },
17
+ "files": [
18
+ "dist/*"
19
+ ],
20
+ "license": "UNLICENSED",
21
+ "repository": {
22
+ "type": "git",
23
+ "url": "https://git.fuse.hk/payello/dev/payello-module/jwt"
24
+ },
25
+ "dependencies": {
26
+ "@payello-module/encryption": "^0.1.2"
27
+ }
28
+ }
package/readme.md CHANGED
@@ -1,28 +1,28 @@
1
- # JWT Module
2
- JWT Module is used to generate JSON Web Tokens.
3
-
4
- ## Usage
5
- First install the module to your project.
6
- ```
7
- npm i @payello-module/jwt
8
- ```
9
-
10
- Then you can use the JWT Module as below:
11
- ```ts
12
- import { JWT } from "@payello-module/jwt";
13
-
14
- const payload = {
15
- exp: Math.floor(Date.now().valueOf() / 1000) + 300 // Expire in 300 seconds
16
- };
17
-
18
- const opts = {
19
- privKey: "79c4e267e63845a986e669388fce66e9", // Secret Key
20
- pubKey: "f266a28e-5e9a-4fe3-90a8-2e8b2ef0f62d", // Issuer ID
21
- algorithm: "HS256" // Possible values: HS256 or HS512
22
- };
23
-
24
- const jwt = await JWT.sign(payload, opts);
25
-
26
- console.log(jwt);
27
-
1
+ # JWT Module
2
+ JWT Module is used to generate JSON Web Tokens.
3
+
4
+ ## Usage
5
+ First install the module to your project.
6
+ ```
7
+ npm i @payello-module/jwt
8
+ ```
9
+
10
+ Then you can use the JWT Module as below:
11
+ ```ts
12
+ import { JWT } from "@payello-module/jwt";
13
+
14
+ const payload = {
15
+ exp: Math.floor(Date.now().valueOf() / 1000) + 300 // Expire in 300 seconds
16
+ };
17
+
18
+ const opts = {
19
+ privKey: "79c4e267e63845a986e669388fce66e9", // Secret Key
20
+ pubKey: "f266a28e-5e9a-4fe3-90a8-2e8b2ef0f62d", // Issuer ID
21
+ algorithm: "HS256" // Possible values: HS256 or HS512
22
+ };
23
+
24
+ const jwt = await JWT.sign(payload, opts);
25
+
26
+ console.log(jwt);
27
+
28
28
  ```
package/dist/index.mjs DELETED
@@ -1,85 +0,0 @@
1
- import { HmacSha512, HmacSha256 } from '@payello-module/encryption';
2
-
3
- class JwtError extends Error {
4
- type = "@payello/module-jwt#JwtError";
5
- constructor(message, options) {
6
- super(message, options);
7
- }
8
- }
9
-
10
- class JWT {
11
- static async sign(payload, options = { privKey: '', pubKey: '', algorithm: 'HS512' }) {
12
- const _header = {
13
- typ: 'JWT',
14
- alg: options.algorithm
15
- };
16
- const _payload = {
17
- jti: `jti_${Date.now().valueOf()}`,
18
- iat: Math.floor(Date.now().valueOf() / 1000),
19
- iss: options.pubKey,
20
- ...payload
21
- };
22
- const _body = btoa(JSON.stringify(_header)) + "." + btoa(JSON.stringify(_payload));
23
- let signature = "";
24
- switch (options.algorithm) {
25
- case 'HS256':
26
- signature = await HmacSha256.encrypt(_body, options.privKey);
27
- break;
28
- case 'HS512':
29
- signature = await HmacSha512.encrypt(_body, options.privKey);
30
- break;
31
- }
32
- return _body + "." + signature;
33
- }
34
- static async extract(input, opts = {}) {
35
- const bits = input.split(".");
36
- if (bits.length !== 3) {
37
- throw new JwtError(`Invalid number of parts in JWT string. Expected 3 but got ${bits.length}`);
38
- }
39
- const header = JSON.parse(bits[0]);
40
- if (!header || !header.typ || header.typ !== "JWT") {
41
- throw new JwtError("Header invalid or type is not JWT");
42
- }
43
- const payload = JSON.parse(bits[1]);
44
- if (!payload || !payload.jti) {
45
- throw new JwtError("Payload invalid or missing jti value");
46
- }
47
- const requiredProps = opts.requiredProps || ["jti", "iss", "iat"];
48
- for (const prop in requiredProps) {
49
- if (!payload[prop]) {
50
- throw new JwtError(`Payload missing ${prop} value`);
51
- }
52
- }
53
- return {
54
- header: header,
55
- payload: payload,
56
- signature: bits[2]
57
- };
58
- }
59
- static async verify(input, opts) {
60
- const extracted = await this.extract(input);
61
- const secretKey = await opts.getSecretKey(extracted.payload.jti);
62
- if (!secretKey) {
63
- throw new JwtError(`Public key not found`);
64
- }
65
- let verify = false;
66
- const data = `${btoa(JSON.stringify(extracted.header))}.${btoa(JSON.stringify(extracted.payload))}`;
67
- switch (extracted.header.alg) {
68
- case 'HS256':
69
- verify = await HmacSha256.verify(data, extracted.signature, secretKey);
70
- break;
71
- case 'HS512':
72
- verify = await HmacSha512.verify(data, extracted.signature, secretKey);
73
- break;
74
- default:
75
- throw new JwtError(`Unsupported algorithm`);
76
- }
77
- if (!verify) {
78
- throw new JwtError(`Signature not verified`);
79
- }
80
- return verify;
81
- }
82
- }
83
-
84
- export { JWT, JwtError };
85
- //# sourceMappingURL=index.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"index.mjs","sources":["../src/JwtError.ts","../src/JWT.ts"],"sourcesContent":["\r\n\r\nexport class JwtError extends Error {\r\n type: \"@payello/module-jwt#JwtError\" = \"@payello/module-jwt#JwtError\";\r\n\r\n constructor(message: string, options?: ErrorOptions) {\r\n super(message, options)\r\n }\r\n}\r\n","import { HmacSha256, HmacSha512 } from \"@payello-module/encryption\"\r\nimport { JwtHeader } from \"./JwtHeader\"\r\nimport { JwtSignOpts } from \"./JwtSignOpts\"\r\nimport { JwtError } from \"./JwtError\"\r\nimport { JwtExtract } from \"./JwtExtract\"\r\nimport { JwtExtractOpts } from \"./JwtExtractOpts\"\r\n\r\nexport class JWT {\r\n public static async sign(payload: any, options: JwtSignOpts = { privKey: '', pubKey: '', algorithm: 'HS512' }): Promise<string> {\r\n const _header: JwtHeader = {\r\n typ: 'JWT',\r\n alg: options.algorithm\r\n }\r\n\r\n const _payload = {\r\n jti: `jti_${Date.now().valueOf()}`,\r\n iat: Math.floor(Date.now().valueOf() / 1000),\r\n iss: options.pubKey,\r\n ...payload\r\n }\r\n\r\n const _body = btoa(JSON.stringify(_header)) + \".\" + btoa(JSON.stringify(_payload))\r\n\r\n let signature = \"\"\r\n switch (options.algorithm) {\r\n case 'HS256':\r\n signature = await HmacSha256.encrypt(_body, options.privKey)\r\n break\r\n\r\n case 'HS512':\r\n signature = await HmacSha512.encrypt(_body, options.privKey)\r\n break\r\n }\r\n\r\n return _body + \".\" + signature\r\n }\r\n\r\n public static async extract(input: string, opts: JwtExtractOpts = {}): Promise<JwtExtract> {\r\n const bits: string[] = input.split(\".\")\r\n if(bits.length !== 3) {\r\n throw new JwtError(`Invalid number of parts in JWT string. Expected 3 but got ${bits.length}`)\r\n }\r\n\r\n const header = JSON.parse(bits[0])\r\n\r\n if(!header || !header.typ || header.typ !== \"JWT\") {\r\n throw new JwtError(\"Header invalid or type is not JWT\")\r\n }\r\n\r\n const payload = JSON.parse(bits[1])\r\n if(!payload || !payload.jti) {\r\n throw new JwtError(\"Payload invalid or missing jti value\")\r\n }\r\n\r\n const requiredProps = opts.requiredProps || [\"jti\", \"iss\", \"iat\"];\r\n for(const prop in requiredProps) {\r\n if(!payload[prop]) {\r\n throw new JwtError(`Payload missing ${prop} value`)\r\n }\r\n }\r\n\r\n return {\r\n header: header,\r\n payload: payload,\r\n signature: bits[2]\r\n }\r\n }\r\n\r\n static async verify(input: string, opts: { getSecretKey(issuer: string): Promise<string> }) {\r\n const extracted = await this.extract(input);\r\n const secretKey = await opts.getSecretKey(extracted.payload.jti);\r\n\r\n if(!secretKey) {\r\n throw new JwtError(`Public key not found`)\r\n }\r\n\r\n let verify = false\r\n const data = `${btoa(JSON.stringify(extracted.header))}.${btoa(JSON.stringify(extracted.payload))}`\r\n\r\n switch(extracted.header.alg) {\r\n case 'HS256':\r\n verify = await HmacSha256.verify(data, extracted.signature, secretKey)\r\n break;\r\n\r\n case 'HS512':\r\n verify = await HmacSha512.verify(data, extracted.signature, secretKey)\r\n break;\r\n\r\n default:\r\n throw new JwtError(`Unsupported algorithm`)\r\n }\r\n\r\n if(!verify) {\r\n throw new JwtError(`Signature not verified`)\r\n }\r\n\r\n return verify\r\n }\r\n}\r\n\r\n"],"names":[],"mappings":";;AAEM,MAAO,QAAS,SAAQ,KAAK,CAAA;IAC/B,IAAI,GAAmC,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C,KAAa,EAAE,IAAuD,EAAA;QACtF,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,OAAO,CAAC,KAAK,CAAC,CAAC;AAC5C,QAAA,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,YAAY,CAAC,SAAS,CAAC,OAAO,CAAC,GAAG,CAAC,CAAC;QAEjE,IAAG,CAAC,SAAS,EAAE;AACX,YAAA,MAAM,IAAI,QAAQ,CAAC,CAAA,oBAAA,CAAsB,CAAC,CAAA;AAC7C,SAAA;QAED,IAAI,MAAM,GAAG,KAAK,CAAA;QAClB,MAAM,IAAI,GAAG,CAAA,EAAG,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,MAAM,CAAC,CAAC,CAAA,CAAA,EAAI,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC,CAAA,CAAE,CAAA;AAEnG,QAAA,QAAO,SAAS,CAAC,MAAM,CAAC,GAAG;AACvB,YAAA,KAAK,OAAO;AACR,gBAAA,MAAM,GAAG,MAAM,UAAU,CAAC,MAAM,CAAC,IAAI,EAAE,SAAS,CAAC,SAAS,EAAE,SAAS,CAAC,CAAA;gBACtE,MAAM;AAEV,YAAA,KAAK,OAAO;AACR,gBAAA,MAAM,GAAG,MAAM,UAAU,CAAC,MAAM,CAAC,IAAI,EAAE,SAAS,CAAC,SAAS,EAAE,SAAS,CAAC,CAAA;gBACtE,MAAM;AAEV,YAAA;AACI,gBAAA,MAAM,IAAI,QAAQ,CAAC,CAAA,qBAAA,CAAuB,CAAC,CAAA;AAClD,SAAA;QAED,IAAG,CAAC,MAAM,EAAE;AACR,YAAA,MAAM,IAAI,QAAQ,CAAC,CAAA,sBAAA,CAAwB,CAAC,CAAA;AAC/C,SAAA;AAED,QAAA,OAAO,MAAM,CAAA;KAChB;AACJ;;;;"}
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes