@payello-module/jwt 0.2.0 → 1.20240419.1635

Sign up to get free protection for your applications and to get access to all the features.
package/package.json CHANGED
@@ -1,29 +1,29 @@
1
1
  {
2
- "name": "@payello-module/jwt",
3
- "version": "0.2.0",
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
- "payellodev": "^0.20240413.1813",
16
- "typescript": "^5.3.3"
17
- },
18
- "files": [
19
- "dist/*"
20
- ],
21
- "license": "UNLICENSED",
22
- "repository": {
23
- "type": "git",
24
- "url": "https://git.fuse.hk/payello/dev/payello-module/jwt"
25
- },
26
- "dependencies": {
27
- "@payello-module/encryption": "^0.1.3"
28
- }
29
- }
2
+ "name": "@payello-module/jwt",
3
+ "version": "1.20240419.1635",
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
+ "payellodev": "^0.20240413.1813",
16
+ "typescript": "^5.3.3"
17
+ },
18
+ "files": [
19
+ "dist/*"
20
+ ],
21
+ "license": "UNLICENSED",
22
+ "repository": {
23
+ "type": "git",
24
+ "url": "https://git.fuse.hk/payello/dev/payello-module/jwt"
25
+ },
26
+ "dependencies": {
27
+ "@payello-module/encryption": "^0.1.3"
28
+ }
29
+ }
package/dist/JWT.d.ts DELETED
@@ -1,32 +0,0 @@
1
- import { JwtSignOpts } from "./JwtSignOpts";
2
- import { JwtExtract } from "./JwtExtract";
3
- import { JwtExtractOpts } from "./JwtExtractOpts";
4
- /**
5
- * Class representing JSON Web Tokens (JWT) functionalities, including signing, extracting components,
6
- * and verifying the signature of the token.
7
- */
8
- export declare class JWT {
9
- /**
10
- * Signs the given payload and returns a JWT string.
11
- * @param payload - The payload of the JWT which is the content that you want to protect.
12
- * @param options - The options for signing the JWT, including the private key, public key, and algorithm.
13
- * @returns A promise that resolves to the signed JWT string.
14
- */
15
- static sign(payload: any, options?: JwtSignOpts): Promise<string>;
16
- /**
17
- * Extracts and returns the header, payload, and signature components from a JWT string.
18
- * @param input - The JWT string to be parsed.
19
- * @param opts - Optional parameters, including the requirements for the presence of required properties in the payload.
20
- * @returns A promise that resolves to an object containing the separated components of the JWT (header, payload, signature).
21
- */
22
- static extract(input: string, opts?: JwtExtractOpts): Promise<JwtExtract>;
23
- /**
24
- * Verifies the given JWT string using the secret key fetched by the given issuer.
25
- * @param input - The JWT string to be verified.
26
- * @param opts - An object containing a function getSecretKey to retrieve the secret key based on the issuer.
27
- * @returns A promise that resolves to a boolean indicating whether the JWT has been verified successfully or not.
28
- */
29
- static verify(input: string, opts: {
30
- getSecretKey(issuer: string): Promise<string>;
31
- }): Promise<true>;
32
- }
package/dist/JWT.js DELETED
@@ -1,107 +0,0 @@
1
- import { HmacSha256, HmacSha512 } from "@payello-module/encryption";
2
- import { JwtError } from "./JwtError";
3
- /**
4
- * Class representing JSON Web Tokens (JWT) functionalities, including signing, extracting components,
5
- * and verifying the signature of the token.
6
- */
7
- export class JWT {
8
- /**
9
- * Signs the given payload and returns a JWT string.
10
- * @param payload - The payload of the JWT which is the content that you want to protect.
11
- * @param options - The options for signing the JWT, including the private key, public key, and algorithm.
12
- * @returns A promise that resolves to the signed JWT string.
13
- */
14
- static async sign(payload, options = { privKey: '', pubKey: '', algorithm: 'HS512' }) {
15
- const _header = {
16
- typ: 'JWT',
17
- alg: options.algorithm
18
- };
19
- // Create payload with unique identifier, issued-at time, and incorporate the public key if provided.
20
- const _payload = {
21
- jti: `jti_${Date.now().valueOf()}`,
22
- iat: Math.floor(Date.now().valueOf() / 1000),
23
- iss: options.pubKey,
24
- ...payload
25
- };
26
- const _body = btoa(JSON.stringify(_header)) + "." + btoa(JSON.stringify(_payload));
27
- let signature = "";
28
- // Create signature based on selected algorithm.
29
- switch (options.algorithm) {
30
- case 'HS256':
31
- signature = await HmacSha256.encrypt(_body, options.privKey);
32
- break;
33
- case 'HS512':
34
- signature = await HmacSha512.encrypt(_body, options.privKey);
35
- break;
36
- }
37
- // Returns the final JWT token as a concatenation of header, payload, and signature
38
- return _body + "." + signature;
39
- }
40
- /**
41
- * Extracts and returns the header, payload, and signature components from a JWT string.
42
- * @param input - The JWT string to be parsed.
43
- * @param opts - Optional parameters, including the requirements for the presence of required properties in the payload.
44
- * @returns A promise that resolves to an object containing the separated components of the JWT (header, payload, signature).
45
- */
46
- static async extract(input, opts = {}) {
47
- const bits = input.split(".");
48
- // Ensures that the JWT string has three parts: header, payload, and signature.
49
- if (bits.length !== 3) {
50
- throw new JwtError(`Invalid number of parts in JWT string. Expected 3 but got ${bits.length}`);
51
- }
52
- const header = JSON.parse(bits[0]);
53
- if (!header || !header.typ || header.typ !== "JWT") {
54
- throw new JwtError("Header invalid or type is not JWT");
55
- }
56
- const payload = JSON.parse(bits[1]);
57
- if (!payload || !payload.jti) {
58
- throw new JwtError("Payload invalid or missing jti value");
59
- }
60
- // Validates the present of required properties in the payload.
61
- const requiredProps = opts.requiredProps || ["jti", "iss", "iat"];
62
- for (const prop in requiredProps) {
63
- if (!payload[prop]) {
64
- throw new JwtError(`Payload missing ${prop} value`);
65
- }
66
- }
67
- // Returns an object containing the extracted components of the JWT.
68
- return {
69
- header: header,
70
- payload: payload,
71
- signature: bits[2]
72
- };
73
- }
74
- /**
75
- * Verifies the given JWT string using the secret key fetched by the given issuer.
76
- * @param input - The JWT string to be verified.
77
- * @param opts - An object containing a function getSecretKey to retrieve the secret key based on the issuer.
78
- * @returns A promise that resolves to a boolean indicating whether the JWT has been verified successfully or not.
79
- */
80
- static async verify(input, opts) {
81
- const extracted = await this.extract(input);
82
- // Fetches the secret key based on the issuer in the payload.
83
- const secretKey = await opts.getSecretKey(extracted.payload.jti);
84
- if (!secretKey) {
85
- throw new JwtError(`Public key not found`);
86
- }
87
- let verify = false;
88
- // Preparation of the data to verify the signature.
89
- const data = `${btoa(JSON.stringify(extracted.header))}.${btoa(JSON.stringify(extracted.payload))}`;
90
- // Verification of the signature based on the algorithm specified in the header.
91
- switch (extracted.header.alg) {
92
- case 'HS256':
93
- verify = await HmacSha256.verify(data, extracted.signature, secretKey);
94
- break;
95
- case 'HS512':
96
- verify = await HmacSha512.verify(data, extracted.signature, secretKey);
97
- break;
98
- default:
99
- throw new JwtError(`Unsupported algorithm`);
100
- }
101
- if (!verify) {
102
- throw new JwtError(`Signature not verified`);
103
- }
104
- // Returns the result of the verification process.
105
- return verify;
106
- }
107
- }
@@ -1,4 +0,0 @@
1
- export declare class JwtError extends Error {
2
- type: "@payello/module-jwt#JwtError";
3
- constructor(message: string, options?: ErrorOptions);
4
- }
package/dist/JwtError.js DELETED
@@ -1,6 +0,0 @@
1
- export class JwtError extends Error {
2
- type = "@payello/module-jwt#JwtError";
3
- constructor(message, options) {
4
- super(message, options);
5
- }
6
- }
@@ -1,6 +0,0 @@
1
- import { JwtHeader } from "./JwtHeader";
2
- export interface JwtExtract {
3
- header: JwtHeader;
4
- payload: any;
5
- signature: string;
6
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,3 +0,0 @@
1
- export interface JwtExtractOpts {
2
- requiredProps?: string[];
3
- }
@@ -1 +0,0 @@
1
- export {};
@@ -1,4 +0,0 @@
1
- export interface JwtHeader {
2
- typ: 'JWT';
3
- alg: 'HS256' | 'HS512';
4
- }
package/dist/JwtHeader.js DELETED
@@ -1 +0,0 @@
1
- export {};
@@ -1,5 +0,0 @@
1
- export interface JwtSignOpts {
2
- privKey: string;
3
- pubKey: string;
4
- algorithm: "HS256" | "HS512";
5
- }
@@ -1 +0,0 @@
1
- export {};
package/dist/example.d.ts DELETED
@@ -1 +0,0 @@
1
- export {};
package/dist/example.js DELETED
@@ -1,13 +0,0 @@
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.d.ts DELETED
@@ -1,7 +0,0 @@
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/dist/index.js DELETED
@@ -1,7 +0,0 @@
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';