@naman_deep_singh/security 1.1.0 → 1.3.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/README.md +358 -175
- package/dist/cjs/core/crypto/cryptoManager.d.ts +111 -0
- package/dist/cjs/core/crypto/cryptoManager.js +185 -0
- package/dist/cjs/core/crypto/index.d.ts +5 -4
- package/dist/cjs/core/crypto/index.js +12 -4
- package/dist/cjs/core/jwt/extractToken.d.ts +2 -2
- package/dist/cjs/core/jwt/extractToken.js +12 -7
- package/dist/cjs/core/jwt/generateTokens.d.ts +3 -6
- package/dist/cjs/core/jwt/generateTokens.js +10 -3
- package/dist/cjs/core/jwt/index.d.ts +1 -0
- package/dist/cjs/core/jwt/index.js +1 -0
- package/dist/cjs/core/jwt/jwtManager.d.ts +66 -0
- package/dist/cjs/core/jwt/jwtManager.js +319 -0
- package/dist/cjs/core/jwt/signToken.d.ts +1 -1
- package/dist/cjs/core/jwt/types.d.ts +22 -0
- package/dist/cjs/core/jwt/types.js +2 -0
- package/dist/cjs/core/jwt/validateToken.d.ts +1 -1
- package/dist/cjs/core/jwt/verify.d.ts +12 -7
- package/dist/cjs/core/jwt/verify.js +23 -3
- package/dist/cjs/core/password/passwordManager.d.ts +29 -0
- package/dist/cjs/core/password/passwordManager.js +242 -0
- package/dist/cjs/index.d.ts +11 -9
- package/dist/cjs/interfaces/jwt.interface.d.ts +47 -0
- package/dist/cjs/interfaces/jwt.interface.js +2 -0
- package/dist/cjs/interfaces/password.interface.d.ts +60 -0
- package/dist/cjs/interfaces/password.interface.js +2 -0
- package/dist/esm/core/crypto/cryptoManager.d.ts +111 -0
- package/dist/esm/core/crypto/cryptoManager.js +180 -0
- package/dist/esm/core/crypto/index.d.ts +5 -4
- package/dist/esm/core/crypto/index.js +5 -4
- package/dist/esm/core/jwt/extractToken.d.ts +2 -2
- package/dist/esm/core/jwt/extractToken.js +12 -7
- package/dist/esm/core/jwt/generateTokens.d.ts +3 -6
- package/dist/esm/core/jwt/generateTokens.js +10 -3
- package/dist/esm/core/jwt/index.d.ts +1 -0
- package/dist/esm/core/jwt/index.js +1 -0
- package/dist/esm/core/jwt/jwtManager.d.ts +66 -0
- package/dist/esm/core/jwt/jwtManager.js +312 -0
- package/dist/esm/core/jwt/signToken.d.ts +1 -1
- package/dist/esm/core/jwt/types.d.ts +22 -0
- package/dist/esm/core/jwt/types.js +1 -0
- package/dist/esm/core/jwt/validateToken.d.ts +1 -1
- package/dist/esm/core/jwt/verify.d.ts +12 -7
- package/dist/esm/core/jwt/verify.js +20 -2
- package/dist/esm/core/password/passwordManager.d.ts +29 -0
- package/dist/esm/core/password/passwordManager.js +235 -0
- package/dist/esm/index.d.ts +11 -9
- package/dist/esm/interfaces/jwt.interface.d.ts +47 -0
- package/dist/esm/interfaces/jwt.interface.js +1 -0
- package/dist/esm/interfaces/password.interface.d.ts +60 -0
- package/dist/esm/interfaces/password.interface.js +1 -0
- package/dist/types/core/crypto/cryptoManager.d.ts +111 -0
- package/dist/types/core/crypto/index.d.ts +5 -4
- package/dist/types/core/jwt/extractToken.d.ts +2 -2
- package/dist/types/core/jwt/generateTokens.d.ts +3 -6
- package/dist/types/core/jwt/index.d.ts +1 -0
- package/dist/types/core/jwt/jwtManager.d.ts +66 -0
- package/dist/types/core/jwt/signToken.d.ts +1 -1
- package/dist/types/core/jwt/types.d.ts +22 -0
- package/dist/types/core/jwt/validateToken.d.ts +1 -1
- package/dist/types/core/jwt/verify.d.ts +12 -7
- package/dist/types/core/password/passwordManager.d.ts +29 -0
- package/dist/types/index.d.ts +11 -9
- package/dist/types/interfaces/jwt.interface.d.ts +47 -0
- package/dist/types/interfaces/password.interface.d.ts +60 -0
- package/package.json +1 -1
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Configuration options for CryptoManager
|
|
3
|
+
*/
|
|
4
|
+
export interface CryptoManagerConfig {
|
|
5
|
+
defaultAlgorithm?: string;
|
|
6
|
+
defaultEncoding?: BufferEncoding;
|
|
7
|
+
hmacAlgorithm?: string;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* CryptoManager - Class-based wrapper for all cryptographic operations
|
|
11
|
+
* Provides a consistent interface for encryption, decryption, HMAC generation, and secure random generation
|
|
12
|
+
*/
|
|
13
|
+
export declare class CryptoManager {
|
|
14
|
+
private config;
|
|
15
|
+
constructor(config?: CryptoManagerConfig);
|
|
16
|
+
/**
|
|
17
|
+
* Update configuration
|
|
18
|
+
*/
|
|
19
|
+
updateConfig(config: Partial<CryptoManagerConfig>): void;
|
|
20
|
+
/**
|
|
21
|
+
* Get current configuration
|
|
22
|
+
*/
|
|
23
|
+
getConfig(): Required<CryptoManagerConfig>;
|
|
24
|
+
/**
|
|
25
|
+
* Encrypt data using the default or specified algorithm
|
|
26
|
+
*/
|
|
27
|
+
encrypt(plaintext: string, key: string, options?: {
|
|
28
|
+
algorithm?: string;
|
|
29
|
+
encoding?: BufferEncoding;
|
|
30
|
+
iv?: string;
|
|
31
|
+
}): string;
|
|
32
|
+
/**
|
|
33
|
+
* Decrypt data using the default or specified algorithm
|
|
34
|
+
*/
|
|
35
|
+
decrypt(encryptedData: string, key: string, options?: {
|
|
36
|
+
algorithm?: string;
|
|
37
|
+
encoding?: BufferEncoding;
|
|
38
|
+
iv?: string;
|
|
39
|
+
}): string;
|
|
40
|
+
/**
|
|
41
|
+
* Generate HMAC signature
|
|
42
|
+
*/
|
|
43
|
+
generateHmac(data: string, secret: string, options?: {
|
|
44
|
+
algorithm?: string;
|
|
45
|
+
encoding?: BufferEncoding;
|
|
46
|
+
}): string;
|
|
47
|
+
/**
|
|
48
|
+
* Generate cryptographically secure random bytes
|
|
49
|
+
*/
|
|
50
|
+
generateSecureRandom(length: number, encoding?: BufferEncoding): string;
|
|
51
|
+
/**
|
|
52
|
+
* Verify HMAC signature
|
|
53
|
+
*/
|
|
54
|
+
verifyHmac(data: string, secret: string, signature: string, options?: {
|
|
55
|
+
algorithm?: string;
|
|
56
|
+
encoding?: BufferEncoding;
|
|
57
|
+
}): boolean;
|
|
58
|
+
/**
|
|
59
|
+
* Create a key derivation function using PBKDF2
|
|
60
|
+
*/
|
|
61
|
+
deriveKey(password: string, salt: string, iterations?: number, keyLength?: number): Promise<string>;
|
|
62
|
+
/**
|
|
63
|
+
* Hash data using SHA-256
|
|
64
|
+
*/
|
|
65
|
+
sha256(data: string, encoding?: BufferEncoding): string;
|
|
66
|
+
/**
|
|
67
|
+
* Hash data using SHA-512
|
|
68
|
+
*/
|
|
69
|
+
sha512(data: string, encoding?: BufferEncoding): string;
|
|
70
|
+
/**
|
|
71
|
+
* Generate a secure key pair for asymmetric encryption
|
|
72
|
+
*/
|
|
73
|
+
generateKeyPair(options?: {
|
|
74
|
+
modulusLength?: number;
|
|
75
|
+
publicKeyEncoding?: {
|
|
76
|
+
type: string;
|
|
77
|
+
format: string;
|
|
78
|
+
};
|
|
79
|
+
privateKeyEncoding?: {
|
|
80
|
+
type: string;
|
|
81
|
+
format: string;
|
|
82
|
+
};
|
|
83
|
+
}): Promise<{
|
|
84
|
+
publicKey: string;
|
|
85
|
+
privateKey: string;
|
|
86
|
+
}>;
|
|
87
|
+
/**
|
|
88
|
+
* Encrypt data using RSA public key
|
|
89
|
+
*/
|
|
90
|
+
rsaEncrypt(data: string, publicKey: string): Promise<string>;
|
|
91
|
+
/**
|
|
92
|
+
* Decrypt data using RSA private key
|
|
93
|
+
*/
|
|
94
|
+
rsaDecrypt(encryptedData: string, privateKey: string): Promise<string>;
|
|
95
|
+
/**
|
|
96
|
+
* Create digital signature using RSA private key
|
|
97
|
+
*/
|
|
98
|
+
rsaSign(data: string, privateKey: string, algorithm?: string): Promise<string>;
|
|
99
|
+
/**
|
|
100
|
+
* Verify digital signature using RSA public key
|
|
101
|
+
*/
|
|
102
|
+
rsaVerify(data: string, signature: string, publicKey: string, algorithm?: string): Promise<boolean>;
|
|
103
|
+
}
|
|
104
|
+
/**
|
|
105
|
+
* Create a CryptoManager instance with default configuration
|
|
106
|
+
*/
|
|
107
|
+
export declare const createCryptoManager: (config?: CryptoManagerConfig) => CryptoManager;
|
|
108
|
+
/**
|
|
109
|
+
* Default CryptoManager instance
|
|
110
|
+
*/
|
|
111
|
+
export declare const cryptoManager: CryptoManager;
|
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.cryptoManager = exports.createCryptoManager = exports.CryptoManager = void 0;
|
|
4
|
+
const index_1 = require("./index");
|
|
5
|
+
/**
|
|
6
|
+
* Default configuration
|
|
7
|
+
*/
|
|
8
|
+
const DEFAULT_CONFIG = {
|
|
9
|
+
defaultAlgorithm: 'aes-256-gcm',
|
|
10
|
+
defaultEncoding: 'utf8',
|
|
11
|
+
hmacAlgorithm: 'sha256'
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* CryptoManager - Class-based wrapper for all cryptographic operations
|
|
15
|
+
* Provides a consistent interface for encryption, decryption, HMAC generation, and secure random generation
|
|
16
|
+
*/
|
|
17
|
+
class CryptoManager {
|
|
18
|
+
constructor(config = {}) {
|
|
19
|
+
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Update configuration
|
|
23
|
+
*/
|
|
24
|
+
updateConfig(config) {
|
|
25
|
+
this.config = { ...this.config, ...config };
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Get current configuration
|
|
29
|
+
*/
|
|
30
|
+
getConfig() {
|
|
31
|
+
return { ...this.config };
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Encrypt data using the default or specified algorithm
|
|
35
|
+
*/
|
|
36
|
+
encrypt(plaintext, key, options) {
|
|
37
|
+
// For now, use the basic encrypt function
|
|
38
|
+
// TODO: Enhance to support different algorithms and options
|
|
39
|
+
return (0, index_1.encrypt)(plaintext, key);
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* Decrypt data using the default or specified algorithm
|
|
43
|
+
*/
|
|
44
|
+
decrypt(encryptedData, key, options) {
|
|
45
|
+
// For now, use the basic decrypt function
|
|
46
|
+
// TODO: Enhance to support different algorithms and options
|
|
47
|
+
return (0, index_1.decrypt)(encryptedData, key);
|
|
48
|
+
}
|
|
49
|
+
/**
|
|
50
|
+
* Generate HMAC signature
|
|
51
|
+
*/
|
|
52
|
+
generateHmac(data, secret, options) {
|
|
53
|
+
// Use the basic HMAC sign function for now
|
|
54
|
+
// TODO: Add support for different algorithms
|
|
55
|
+
return (0, index_1.hmacSign)(data, secret);
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Generate cryptographically secure random bytes
|
|
59
|
+
*/
|
|
60
|
+
generateSecureRandom(length, encoding = 'hex') {
|
|
61
|
+
// Use the basic random token function
|
|
62
|
+
return (0, index_1.randomToken)(length);
|
|
63
|
+
}
|
|
64
|
+
/**
|
|
65
|
+
* Verify HMAC signature
|
|
66
|
+
*/
|
|
67
|
+
verifyHmac(data, secret, signature, options) {
|
|
68
|
+
// Use the basic HMAC verify function
|
|
69
|
+
return (0, index_1.hmacVerify)(data, secret, signature);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Create a key derivation function using PBKDF2
|
|
73
|
+
*/
|
|
74
|
+
deriveKey(password, salt, iterations = 100000, keyLength = 32) {
|
|
75
|
+
return new Promise((resolve, reject) => {
|
|
76
|
+
const crypto = require('crypto');
|
|
77
|
+
crypto.pbkdf2(password, salt, iterations, keyLength, 'sha256', (err, derivedKey) => {
|
|
78
|
+
if (err) {
|
|
79
|
+
reject(err);
|
|
80
|
+
}
|
|
81
|
+
else {
|
|
82
|
+
resolve(derivedKey.toString('hex'));
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
/**
|
|
88
|
+
* Hash data using SHA-256
|
|
89
|
+
*/
|
|
90
|
+
sha256(data, encoding = 'hex') {
|
|
91
|
+
const crypto = require('crypto');
|
|
92
|
+
return crypto.createHash('sha256').update(data).digest(encoding);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* Hash data using SHA-512
|
|
96
|
+
*/
|
|
97
|
+
sha512(data, encoding = 'hex') {
|
|
98
|
+
const crypto = require('crypto');
|
|
99
|
+
return crypto.createHash('sha512').update(data).digest(encoding);
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Generate a secure key pair for asymmetric encryption
|
|
103
|
+
*/
|
|
104
|
+
generateKeyPair(options) {
|
|
105
|
+
return new Promise((resolve, reject) => {
|
|
106
|
+
const crypto = require('crypto');
|
|
107
|
+
const keyPair = crypto.generateKeyPairSync('rsa', {
|
|
108
|
+
modulusLength: options?.modulusLength || 2048,
|
|
109
|
+
publicKeyEncoding: options?.publicKeyEncoding || { type: 'spki', format: 'pem' },
|
|
110
|
+
privateKeyEncoding: options?.privateKeyEncoding || { type: 'pkcs8', format: 'pem' }
|
|
111
|
+
});
|
|
112
|
+
resolve(keyPair);
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Encrypt data using RSA public key
|
|
117
|
+
*/
|
|
118
|
+
rsaEncrypt(data, publicKey) {
|
|
119
|
+
return new Promise((resolve, reject) => {
|
|
120
|
+
const crypto = require('crypto');
|
|
121
|
+
const buffer = Buffer.from(data, 'utf8');
|
|
122
|
+
const encrypted = crypto.publicEncrypt(publicKey, buffer);
|
|
123
|
+
resolve(encrypted.toString('base64'));
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
/**
|
|
127
|
+
* Decrypt data using RSA private key
|
|
128
|
+
*/
|
|
129
|
+
rsaDecrypt(encryptedData, privateKey) {
|
|
130
|
+
return new Promise((resolve, reject) => {
|
|
131
|
+
const crypto = require('crypto');
|
|
132
|
+
const buffer = Buffer.from(encryptedData, 'base64');
|
|
133
|
+
const decrypted = crypto.privateDecrypt(privateKey, buffer);
|
|
134
|
+
resolve(decrypted.toString('utf8'));
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Create digital signature using RSA private key
|
|
139
|
+
*/
|
|
140
|
+
rsaSign(data, privateKey, algorithm = 'sha256') {
|
|
141
|
+
return new Promise((resolve, reject) => {
|
|
142
|
+
const crypto = require('crypto');
|
|
143
|
+
const sign = crypto.createSign(algorithm);
|
|
144
|
+
sign.update(data);
|
|
145
|
+
sign.end();
|
|
146
|
+
try {
|
|
147
|
+
const signature = sign.sign(privateKey, 'base64');
|
|
148
|
+
resolve(signature);
|
|
149
|
+
}
|
|
150
|
+
catch (error) {
|
|
151
|
+
reject(error);
|
|
152
|
+
}
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
/**
|
|
156
|
+
* Verify digital signature using RSA public key
|
|
157
|
+
*/
|
|
158
|
+
rsaVerify(data, signature, publicKey, algorithm = 'sha256') {
|
|
159
|
+
return new Promise((resolve, reject) => {
|
|
160
|
+
const crypto = require('crypto');
|
|
161
|
+
const verify = crypto.createVerify(algorithm);
|
|
162
|
+
verify.update(data);
|
|
163
|
+
verify.end();
|
|
164
|
+
try {
|
|
165
|
+
const isValid = verify.verify(publicKey, signature, 'base64');
|
|
166
|
+
resolve(isValid);
|
|
167
|
+
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
reject(error);
|
|
170
|
+
}
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
exports.CryptoManager = CryptoManager;
|
|
175
|
+
/**
|
|
176
|
+
* Create a CryptoManager instance with default configuration
|
|
177
|
+
*/
|
|
178
|
+
const createCryptoManager = (config) => {
|
|
179
|
+
return new CryptoManager(config);
|
|
180
|
+
};
|
|
181
|
+
exports.createCryptoManager = createCryptoManager;
|
|
182
|
+
/**
|
|
183
|
+
* Default CryptoManager instance
|
|
184
|
+
*/
|
|
185
|
+
exports.cryptoManager = new CryptoManager();
|
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
export
|
|
2
|
-
export
|
|
3
|
-
export
|
|
4
|
-
export
|
|
1
|
+
export { decrypt } from "./decrypt";
|
|
2
|
+
export { encrypt } from "./encrypt";
|
|
3
|
+
export { hmacSign, hmacVerify } from "./hmac";
|
|
4
|
+
export { randomToken, generateStrongPassword } from "./random";
|
|
5
|
+
export * from "./cryptoManager";
|
|
@@ -14,7 +14,15 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
14
14
|
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
15
|
};
|
|
16
16
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
17
|
+
exports.generateStrongPassword = exports.randomToken = exports.hmacVerify = exports.hmacSign = exports.encrypt = exports.decrypt = void 0;
|
|
18
|
+
var decrypt_1 = require("./decrypt");
|
|
19
|
+
Object.defineProperty(exports, "decrypt", { enumerable: true, get: function () { return decrypt_1.decrypt; } });
|
|
20
|
+
var encrypt_1 = require("./encrypt");
|
|
21
|
+
Object.defineProperty(exports, "encrypt", { enumerable: true, get: function () { return encrypt_1.encrypt; } });
|
|
22
|
+
var hmac_1 = require("./hmac");
|
|
23
|
+
Object.defineProperty(exports, "hmacSign", { enumerable: true, get: function () { return hmac_1.hmacSign; } });
|
|
24
|
+
Object.defineProperty(exports, "hmacVerify", { enumerable: true, get: function () { return hmac_1.hmacVerify; } });
|
|
25
|
+
var random_1 = require("./random");
|
|
26
|
+
Object.defineProperty(exports, "randomToken", { enumerable: true, get: function () { return random_1.randomToken; } });
|
|
27
|
+
Object.defineProperty(exports, "generateStrongPassword", { enumerable: true, get: function () { return random_1.generateStrongPassword; } });
|
|
28
|
+
__exportStar(require("./cryptoManager"), exports);
|
|
@@ -2,8 +2,8 @@ export interface TokenSources {
|
|
|
2
2
|
header?: string | undefined | null;
|
|
3
3
|
cookies?: Record<string, string> | undefined;
|
|
4
4
|
query?: Record<string, string | undefined> | undefined;
|
|
5
|
-
body?: Record<string,
|
|
6
|
-
wsMessage?: string | Record<string,
|
|
5
|
+
body?: Record<string, unknown> | undefined;
|
|
6
|
+
wsMessage?: string | Record<string, unknown> | undefined;
|
|
7
7
|
}
|
|
8
8
|
/**
|
|
9
9
|
* Universal token extractor
|
|
@@ -23,7 +23,7 @@ function extractToken(sources) {
|
|
|
23
23
|
if (query?.token)
|
|
24
24
|
return query.token;
|
|
25
25
|
// 4. Body: { token: "" }
|
|
26
|
-
if (body?.token)
|
|
26
|
+
if (body?.token && typeof body.token === 'string')
|
|
27
27
|
return body.token;
|
|
28
28
|
// 5. WebSocket message extraction (NEW)
|
|
29
29
|
if (wsMessage) {
|
|
@@ -33,12 +33,17 @@ function extractToken(sources) {
|
|
|
33
33
|
if (typeof wsMessage === "string") {
|
|
34
34
|
msg = JSON.parse(wsMessage);
|
|
35
35
|
}
|
|
36
|
-
//
|
|
37
|
-
if (typeof msg
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
36
|
+
// Ensure msg is an object before property access
|
|
37
|
+
if (typeof msg === 'object' && msg !== null) {
|
|
38
|
+
const m = msg;
|
|
39
|
+
if (typeof m['token'] === 'string')
|
|
40
|
+
return m['token'];
|
|
41
|
+
const auth = m['auth'];
|
|
42
|
+
if (typeof auth === 'object' && auth !== null) {
|
|
43
|
+
const a = auth;
|
|
44
|
+
if (typeof a['token'] === 'string')
|
|
45
|
+
return a['token'];
|
|
46
|
+
}
|
|
42
47
|
}
|
|
43
48
|
}
|
|
44
49
|
catch {
|
|
@@ -1,7 +1,4 @@
|
|
|
1
1
|
import { Secret } from "jsonwebtoken";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
}
|
|
6
|
-
export declare const generateTokens: (payload: object, accessSecret: Secret, refreshSecret: Secret, accessExpiry?: string | number, refreshExpiry?: string | number) => TokenPair;
|
|
7
|
-
export declare function rotateRefreshToken(oldToken: string, secret: Secret): string;
|
|
2
|
+
import { RefreshToken, TokenPair } from "./types";
|
|
3
|
+
export declare const generateTokens: (payload: Record<string, unknown>, accessSecret: Secret, refreshSecret: Secret, accessExpiry?: string | number, refreshExpiry?: string | number) => TokenPair;
|
|
4
|
+
export declare function rotateRefreshToken(oldToken: string, secret: Secret): RefreshToken;
|
|
@@ -4,10 +4,16 @@ exports.generateTokens = void 0;
|
|
|
4
4
|
exports.rotateRefreshToken = rotateRefreshToken;
|
|
5
5
|
const signToken_1 = require("./signToken");
|
|
6
6
|
const verify_1 = require("./verify");
|
|
7
|
+
// Helper function to create branded tokens
|
|
8
|
+
const createBrandedToken = (token, _brand) => {
|
|
9
|
+
return token;
|
|
10
|
+
};
|
|
7
11
|
const generateTokens = (payload, accessSecret, refreshSecret, accessExpiry = "15m", refreshExpiry = "7d") => {
|
|
12
|
+
const accessToken = (0, signToken_1.signToken)(payload, accessSecret, accessExpiry, { algorithm: "HS256" });
|
|
13
|
+
const refreshToken = (0, signToken_1.signToken)(payload, refreshSecret, refreshExpiry, { algorithm: "HS256" });
|
|
8
14
|
return {
|
|
9
|
-
accessToken:
|
|
10
|
-
refreshToken:
|
|
15
|
+
accessToken: accessToken,
|
|
16
|
+
refreshToken: refreshToken,
|
|
11
17
|
};
|
|
12
18
|
};
|
|
13
19
|
exports.generateTokens = generateTokens;
|
|
@@ -19,5 +25,6 @@ function rotateRefreshToken(oldToken, secret) {
|
|
|
19
25
|
const payload = { ...decoded };
|
|
20
26
|
delete payload.iat;
|
|
21
27
|
delete payload.exp;
|
|
22
|
-
|
|
28
|
+
const newToken = (0, signToken_1.signToken)(payload, secret, "7d");
|
|
29
|
+
return newToken;
|
|
23
30
|
}
|
|
@@ -19,5 +19,6 @@ __exportStar(require("./extractToken"), exports);
|
|
|
19
19
|
__exportStar(require("./generateTokens"), exports);
|
|
20
20
|
__exportStar(require("./parseDuration"), exports);
|
|
21
21
|
__exportStar(require("./signToken"), exports);
|
|
22
|
+
__exportStar(require("./types"), exports);
|
|
22
23
|
__exportStar(require("./validateToken"), exports);
|
|
23
24
|
__exportStar(require("./verify"), exports);
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { JwtPayload, Secret } from "jsonwebtoken";
|
|
2
|
+
import { ITokenManager, TokenPair, AccessToken, RefreshToken, JWTConfig, TokenValidationOptions } from "../../interfaces/jwt.interface";
|
|
3
|
+
export declare class JWTManager implements ITokenManager {
|
|
4
|
+
private accessSecret;
|
|
5
|
+
private refreshSecret;
|
|
6
|
+
private accessExpiry;
|
|
7
|
+
private refreshExpiry;
|
|
8
|
+
private cache?;
|
|
9
|
+
constructor(config: JWTConfig);
|
|
10
|
+
/**
|
|
11
|
+
* Generate both access and refresh tokens
|
|
12
|
+
*/
|
|
13
|
+
generateTokens(payload: Record<string, unknown>): Promise<TokenPair>;
|
|
14
|
+
/**
|
|
15
|
+
* Generate access token
|
|
16
|
+
*/
|
|
17
|
+
generateAccessToken(payload: Record<string, unknown>): Promise<AccessToken>;
|
|
18
|
+
/**
|
|
19
|
+
* Generate refresh token
|
|
20
|
+
*/
|
|
21
|
+
generateRefreshToken(payload: Record<string, unknown>): Promise<RefreshToken>;
|
|
22
|
+
/**
|
|
23
|
+
* Verify access token
|
|
24
|
+
*/
|
|
25
|
+
verifyAccessToken(token: string): Promise<JwtPayload | string>;
|
|
26
|
+
/**
|
|
27
|
+
* Verify refresh token
|
|
28
|
+
*/
|
|
29
|
+
verifyRefreshToken(token: string): Promise<JwtPayload | string>;
|
|
30
|
+
/**
|
|
31
|
+
* Decode token without verification
|
|
32
|
+
*/
|
|
33
|
+
decodeToken(token: string, complete?: boolean): JwtPayload | string | null;
|
|
34
|
+
/**
|
|
35
|
+
* Extract token from Authorization header
|
|
36
|
+
*/
|
|
37
|
+
extractTokenFromHeader(authHeader: string): string | null;
|
|
38
|
+
/**
|
|
39
|
+
* Validate token without throwing exceptions
|
|
40
|
+
*/
|
|
41
|
+
validateToken(token: string, secret: Secret, options?: TokenValidationOptions): boolean;
|
|
42
|
+
/**
|
|
43
|
+
* Rotate refresh token
|
|
44
|
+
*/
|
|
45
|
+
rotateRefreshToken(oldToken: string): Promise<RefreshToken>;
|
|
46
|
+
/**
|
|
47
|
+
* Check if token is expired
|
|
48
|
+
*/
|
|
49
|
+
isTokenExpired(token: string): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Get token expiration date
|
|
52
|
+
*/
|
|
53
|
+
getTokenExpiration(token: string): Date | null;
|
|
54
|
+
/**
|
|
55
|
+
* Clear token cache
|
|
56
|
+
*/
|
|
57
|
+
clearCache(): void;
|
|
58
|
+
/**
|
|
59
|
+
* Get cache statistics
|
|
60
|
+
*/
|
|
61
|
+
getCacheStats(): {
|
|
62
|
+
size: number;
|
|
63
|
+
maxSize: number;
|
|
64
|
+
} | null;
|
|
65
|
+
private validatePayload;
|
|
66
|
+
}
|