@payello-module/jwt 1.20240419.1640 → 1.20240419.2346

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,4 +1,5 @@
1
1
  export declare class JwtError extends Error {
2
2
  type: "@payello/module-jwt#JwtError";
3
+ name: string;
3
4
  constructor(message: string, options?: ErrorOptions);
4
5
  }
package/dist/JwtError.js CHANGED
@@ -1,5 +1,6 @@
1
1
  export class JwtError extends Error {
2
2
  type = "@payello/module-jwt#JwtError";
3
+ name = "JwtError";
3
4
  constructor(message, options) {
4
5
  super(message, options);
5
6
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@payello-module/jwt",
3
- "version": "1.20240419.1640",
3
+ "version": "1.20240419.2346",
4
4
  "author": "Payello <devsupport@payello.com> (https://payello.com/)",
5
5
  "displayName": "@payello-module/jwt",
6
6
  "description": "JSON Web Token Module",
@@ -18,12 +18,5 @@
18
18
  "files": [
19
19
  "dist/*"
20
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
- }
21
+ "license": "UNLICENSED"
29
22
  }
package/readme.md CHANGED
@@ -1,14 +1,31 @@
1
1
  # JWT Module
2
2
 
3
- This is a TypeScript library for working with JSON Web Tokens (JWT). It provides easy-to-use asynchronous methods to sign, extract, and verify JWTs using HMAC SHA256 and HMAC SHA512 hashing algorithms.
4
-
5
- This module relies on the `@payello-module/encryption` package for encryption operations.
3
+ This is a TypeScript library for working with JSON Web Tokens (JWT). It provides easy-to-use asynchronous methods to sign, extract, and verify JWTs using a variety of hashing algorithms.
6
4
 
7
5
  ## Features
8
6
 
7
+ - **Generate key pairs:** Create keys for all supported algorithms.
9
8
  - **Sign JWTs:** Create signed JWTs with custom payloads and options.
10
- - **Extract JWTs:** Extract the payload, header, and signature from a JWT.
11
- - **Verify JWTs:** Verify the authenticity of a JWT against a secret key.
9
+ - **Extract JWTs:** Extract the header, payload, and signature from a JWT.
10
+ - **Verify JWT Signature:** Verify the signature of a JWT against a verify key.
11
+
12
+ ### Supported algorithms
13
+ This package supports all algorithms defined in [RFC 7518 (JSON Web Algorithms (JWA))](https://datatracker.ietf.org/doc/html/rfc7518).
14
+
15
+ | Algorithm | Description |
16
+ |-----------|-------------|
17
+ | `HS256` | HMAC using SHA-256 |
18
+ | `HS384` | HMAC using SHA-384 |
19
+ | `HS512` | HMAC using SHA-512 |
20
+ | `RS256` | RSASSA-PKCS1-v1_5 using SHA-256 |
21
+ | `RS384` | RSASSA-PKCS1-v1_5 using SHA-384 |
22
+ | `RS512` | RSASSA-PKCS1-v1_5 using SHA-512 |
23
+ | `ES256` | ECDSA using P-256 and SHA-256 |
24
+ | `ES384` | ECDSA using P-384 and SHA-384 |
25
+ | `ES512` | ECDSA using P-521 and SHA-512 |
26
+ | `PS256` | RSASSA-PSS using SHA-256 and MGF1 with SHA-256 |
27
+ | `PS384` | RSASSA-PSS using SHA-384 and MGF1 with SHA-384 |
28
+ | `PS512` | RSASSA-PSS using SHA-512 and MGF1 with SHA-512 |
12
29
 
13
30
  ## Installation
14
31
 
@@ -22,19 +39,33 @@ yarn add @payello-module/jwt
22
39
 
23
40
  ## Usage
24
41
 
42
+ ### Generating Key Pairs
43
+
44
+ To generate a key pair for a specific algorithm, you can use the `generateKeys` method:
45
+
46
+ ```typescript
47
+ import { JWT } from '@payello-module/jwt';
48
+
49
+ const alg = 'RS256'; // or any other supported algorithm
50
+
51
+ JWT.generateKeys(alg)
52
+ .then(keyPair => {
53
+ console.log('Sign Key (Private Key):', keyPair.sign.base64);
54
+ console.log('Verify Key (Public Key):', keyPair.verify.base64);
55
+ })
56
+ .catch(error => console.error(error));
57
+ ```
58
+
25
59
  ### Signing a JWT
26
60
 
27
61
  ```typescript
28
62
  import { JWT } from '@payello-module/jwt';
29
63
 
30
64
  const payload = { /* Your JWT payload here */ };
31
- const options = {
32
- privKey: 'your_private_key',
33
- pubKey: 'your_public_key',
34
- algorithm: 'HS512' // or 'HS256'
35
- };
65
+ const alg = 'HS512'; // or any other supported algorithm
66
+ const key = 'your_signing_key';
36
67
 
37
- JWT.sign(payload, options)
68
+ JWT.sign(payload, alg, key)
38
69
  .then(token => console.log(token))
39
70
  .catch(error => console.error(error));
40
71
  ```
@@ -59,29 +90,41 @@ JWT.extract(token)
59
90
  import { JWT } from '@payello-module/jwt';
60
91
 
61
92
  const token = 'your.jwt.token';
62
- const getSecretKey = async (issuer) => {
63
- // Logic to retrieve the secret key for a given issuer
64
- return 'secret_key';
93
+ const getVerifyKey = async (header, payload) => {
94
+ // Logic to retrieve the verification key for the given header and payload
95
+ return 'verify_key';
65
96
  };
66
97
 
67
- JWT.verify(token, { getSecretKey })
68
- .then(isVerified => console.log(isVerified))
98
+ JWT.verifySignature(token, getVerifyKey)
99
+ .then(({ verified, extracted }) => {
100
+ if (verified) {
101
+ console.log('JWT is verified');
102
+ console.log(extracted);
103
+ } else {
104
+ console.log('JWT verification failed');
105
+ }
106
+ })
69
107
  .catch(error => console.error(error));
70
108
  ```
71
109
 
72
110
  ## API Reference
73
111
 
74
- #### `JWT.sign(payload: any, options: JwtSignOpts): Promise<string>`
112
+ #### `JWT.generateKeys(alg?: JWTAlgorithm): Promise<JWTKeyPair>`
113
+
114
+ Generates a new key pair for the given algorithm. If no algorithm is provided, it defaults to "HS256" (HMAC with SHA-256).
115
+
116
+ #### `JWT.sign(payload: JWTPayload, alg: JWTAlgorithm, key: string | BufferSource): Promise<string>`
75
117
 
76
118
  Signs the provided payload and returns a JWT string.
77
119
 
78
- #### `JWT.extract(input: string, opts: JwtExtractOpts): Promise<JwtExtract>`
120
+ #### `JWT.extract(input: string, opts?: JwtExtractOpts): Promise<JwtExtract>`
79
121
 
80
122
  Extracts and returns the header, payload, and signature from a JWT string.
81
123
 
82
- #### `JWT.verify(input: string, opts: { getSecretKey(issuer: string): Promise<string> }): Promise<boolean>`
124
+ #### `JWT.verifySignature(token: string, getVerifyKey: (header: JwtHeader, payload: JWTPayload) => Promise<BufferSource | string> | BufferSource | string, throwErrors?: boolean): Promise<{ verified: boolean, extracted: JwtExtract | null }>`
125
+
126
+ Verifies a JWT string by checking the signature using the provided verification key. If `throwErrors` is set to `true`, it will throw a `JwtError` if the token is not valid.
83
127
 
84
- Verifies a JWT string against a secret key retrieved by the `getSecretKey` function.
85
128
 
86
129
  ## Contributing
87
130