@payello-module/jwt 1.20240419.1629 → 1.20240419.1635
Sign up to get free protection for your applications and to get access to all the features.
- package/package.json +1 -1
- package/dist/JWT.d.ts +0 -32
- package/dist/JWT.js +0 -107
- package/dist/JwtError.d.ts +0 -4
- package/dist/JwtError.js +0 -6
- package/dist/JwtExtract.d.ts +0 -6
- package/dist/JwtExtract.js +0 -1
- package/dist/JwtExtractOpts.d.ts +0 -3
- package/dist/JwtExtractOpts.js +0 -1
- package/dist/JwtHeader.d.ts +0 -4
- package/dist/JwtHeader.js +0 -1
- package/dist/JwtSignOpts.d.ts +0 -5
- package/dist/JwtSignOpts.js +0 -1
- package/dist/example.d.ts +0 -1
- package/dist/example.js +0 -13
- package/dist/index.d.ts +0 -7
- package/dist/index.js +0 -7
package/package.json
CHANGED
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
|
-
}
|
package/dist/JwtError.d.ts
DELETED
package/dist/JwtError.js
DELETED
package/dist/JwtExtract.d.ts
DELETED
package/dist/JwtExtract.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export {};
|
package/dist/JwtExtractOpts.d.ts
DELETED
package/dist/JwtExtractOpts.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export {};
|
package/dist/JwtHeader.d.ts
DELETED
package/dist/JwtHeader.js
DELETED
@@ -1 +0,0 @@
|
|
1
|
-
export {};
|
package/dist/JwtSignOpts.d.ts
DELETED
package/dist/JwtSignOpts.js
DELETED
@@ -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