@naman_deep_singh/security 1.2.0 → 1.3.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.
- package/README.md +355 -176
- package/dist/cjs/core/crypto/cryptoManager.d.ts +111 -0
- package/dist/cjs/core/crypto/cryptoManager.js +191 -0
- package/dist/cjs/core/crypto/decrypt.js +6 -6
- package/dist/cjs/core/crypto/encrypt.js +4 -4
- package/dist/cjs/core/crypto/hmac.js +1 -1
- package/dist/cjs/core/crypto/index.d.ts +5 -4
- package/dist/cjs/core/crypto/index.js +12 -4
- package/dist/cjs/core/crypto/random.js +2 -2
- package/dist/cjs/core/jwt/decode.d.ts +1 -1
- package/dist/cjs/core/jwt/decode.js +2 -2
- package/dist/cjs/core/jwt/extractToken.js +7 -7
- package/dist/cjs/core/jwt/generateTokens.d.ts +2 -2
- package/dist/cjs/core/jwt/generateTokens.js +10 -6
- package/dist/cjs/core/jwt/index.d.ts +8 -8
- package/dist/cjs/core/jwt/jwtManager.d.ts +67 -0
- package/dist/cjs/core/jwt/jwtManager.js +299 -0
- package/dist/cjs/core/jwt/parseDuration.js +3 -3
- package/dist/cjs/core/jwt/signToken.d.ts +1 -1
- package/dist/cjs/core/jwt/signToken.js +7 -7
- package/dist/cjs/core/jwt/types.d.ts +1 -1
- package/dist/cjs/core/jwt/validateToken.d.ts +2 -2
- package/dist/cjs/core/jwt/validateToken.js +3 -3
- package/dist/cjs/core/jwt/verify.d.ts +3 -2
- package/dist/cjs/core/password/hash.js +1 -1
- package/dist/cjs/core/password/index.d.ts +3 -3
- package/dist/cjs/core/password/passwordManager.d.ts +29 -0
- package/dist/cjs/core/password/passwordManager.js +243 -0
- package/dist/cjs/core/password/strength.d.ts +1 -1
- package/dist/cjs/core/password/strength.js +4 -4
- package/dist/cjs/core/password/utils.js +2 -2
- package/dist/cjs/core/password/verify.js +1 -1
- package/dist/cjs/index.d.ts +9 -5
- package/dist/cjs/index.js +2 -2
- 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 +186 -0
- package/dist/esm/core/crypto/decrypt.js +7 -7
- package/dist/esm/core/crypto/encrypt.js +5 -5
- package/dist/esm/core/crypto/hmac.js +2 -2
- package/dist/esm/core/crypto/index.d.ts +5 -4
- package/dist/esm/core/crypto/index.js +5 -4
- package/dist/esm/core/crypto/random.js +3 -3
- package/dist/esm/core/jwt/decode.d.ts +1 -1
- package/dist/esm/core/jwt/decode.js +3 -3
- package/dist/esm/core/jwt/extractToken.js +7 -7
- package/dist/esm/core/jwt/generateTokens.d.ts +2 -2
- package/dist/esm/core/jwt/generateTokens.js +12 -8
- package/dist/esm/core/jwt/index.d.ts +8 -8
- package/dist/esm/core/jwt/index.js +8 -8
- package/dist/esm/core/jwt/jwtManager.d.ts +67 -0
- package/dist/esm/core/jwt/jwtManager.js +292 -0
- package/dist/esm/core/jwt/parseDuration.js +3 -3
- package/dist/esm/core/jwt/signToken.d.ts +1 -1
- package/dist/esm/core/jwt/signToken.js +9 -9
- package/dist/esm/core/jwt/types.d.ts +1 -1
- package/dist/esm/core/jwt/validateToken.d.ts +2 -2
- package/dist/esm/core/jwt/validateToken.js +3 -3
- package/dist/esm/core/jwt/verify.d.ts +3 -2
- package/dist/esm/core/jwt/verify.js +1 -1
- package/dist/esm/core/password/hash.js +3 -3
- package/dist/esm/core/password/index.d.ts +3 -3
- package/dist/esm/core/password/index.js +3 -3
- package/dist/esm/core/password/passwordManager.d.ts +29 -0
- package/dist/esm/core/password/passwordManager.js +236 -0
- package/dist/esm/core/password/strength.d.ts +1 -1
- package/dist/esm/core/password/strength.js +5 -5
- package/dist/esm/core/password/utils.js +4 -4
- package/dist/esm/core/password/verify.js +2 -2
- package/dist/esm/index.d.ts +9 -5
- package/dist/esm/index.js +7 -7
- 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/decode.d.ts +1 -1
- package/dist/types/core/jwt/generateTokens.d.ts +2 -2
- package/dist/types/core/jwt/index.d.ts +8 -8
- package/dist/types/core/jwt/jwtManager.d.ts +67 -0
- package/dist/types/core/jwt/signToken.d.ts +1 -1
- package/dist/types/core/jwt/types.d.ts +1 -1
- package/dist/types/core/jwt/validateToken.d.ts +2 -2
- package/dist/types/core/jwt/verify.d.ts +3 -2
- package/dist/types/core/password/index.d.ts +3 -3
- package/dist/types/core/password/passwordManager.d.ts +29 -0
- package/dist/types/core/password/strength.d.ts +1 -1
- package/dist/types/index.d.ts +9 -5
- package/dist/types/interfaces/jwt.interface.d.ts +47 -0
- package/dist/types/interfaces/password.interface.d.ts +60 -0
- package/package.json +4 -3
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.JWTManager = void 0;
|
|
7
|
+
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
8
|
+
const signToken_1 = require("./signToken");
|
|
9
|
+
const verify_1 = require("./verify");
|
|
10
|
+
const errors_utils_1 = require("@naman_deep_singh/errors-utils");
|
|
11
|
+
const js_extensions_1 = require("@naman_deep_singh/js-extensions");
|
|
12
|
+
class JWTManager {
|
|
13
|
+
constructor(config) {
|
|
14
|
+
this.accessSecret = config.accessSecret;
|
|
15
|
+
this.refreshSecret = config.refreshSecret;
|
|
16
|
+
this.accessExpiry = config.accessExpiry || '15m';
|
|
17
|
+
this.refreshExpiry = config.refreshExpiry || '7d';
|
|
18
|
+
this.cacheTTL = 5 * 60 * 1000; // 5 minutes default TTL
|
|
19
|
+
if (config.enableCaching) {
|
|
20
|
+
this.cache = new js_extensions_1.LRUCache(config.maxCacheSize || 100);
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Generate both access and refresh tokens
|
|
25
|
+
*/
|
|
26
|
+
async generateTokens(payload) {
|
|
27
|
+
try {
|
|
28
|
+
this.validatePayload(payload);
|
|
29
|
+
const accessToken = await this.generateAccessToken(payload);
|
|
30
|
+
const refreshToken = await this.generateRefreshToken(payload);
|
|
31
|
+
return {
|
|
32
|
+
accessToken,
|
|
33
|
+
refreshToken,
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
if (error instanceof errors_utils_1.BadRequestError ||
|
|
38
|
+
error instanceof errors_utils_1.ValidationError) {
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
throw new errors_utils_1.BadRequestError('Failed to generate tokens');
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Generate access token
|
|
46
|
+
*/
|
|
47
|
+
async generateAccessToken(payload) {
|
|
48
|
+
try {
|
|
49
|
+
this.validatePayload(payload);
|
|
50
|
+
const token = (0, signToken_1.signToken)(payload, this.accessSecret, this.accessExpiry, {
|
|
51
|
+
algorithm: 'HS256',
|
|
52
|
+
});
|
|
53
|
+
return token;
|
|
54
|
+
}
|
|
55
|
+
catch (error) {
|
|
56
|
+
if (error instanceof errors_utils_1.BadRequestError ||
|
|
57
|
+
error instanceof errors_utils_1.ValidationError) {
|
|
58
|
+
throw error;
|
|
59
|
+
}
|
|
60
|
+
throw new errors_utils_1.BadRequestError('Failed to generate access token');
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Generate refresh token
|
|
65
|
+
*/
|
|
66
|
+
async generateRefreshToken(payload) {
|
|
67
|
+
try {
|
|
68
|
+
this.validatePayload(payload);
|
|
69
|
+
const token = (0, signToken_1.signToken)(payload, this.refreshSecret, this.refreshExpiry, {
|
|
70
|
+
algorithm: 'HS256',
|
|
71
|
+
});
|
|
72
|
+
return token;
|
|
73
|
+
}
|
|
74
|
+
catch (error) {
|
|
75
|
+
if (error instanceof errors_utils_1.BadRequestError ||
|
|
76
|
+
error instanceof errors_utils_1.ValidationError) {
|
|
77
|
+
throw error;
|
|
78
|
+
}
|
|
79
|
+
throw new errors_utils_1.BadRequestError('Failed to generate refresh token');
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* Verify access token
|
|
84
|
+
*/
|
|
85
|
+
async verifyAccessToken(token) {
|
|
86
|
+
try {
|
|
87
|
+
if (!token || typeof token !== 'string') {
|
|
88
|
+
throw new errors_utils_1.ValidationError('Access token must be a non-empty string');
|
|
89
|
+
}
|
|
90
|
+
const cacheKey = `access_${token}`;
|
|
91
|
+
if (this.cache) {
|
|
92
|
+
const cached = this.cache.get(cacheKey);
|
|
93
|
+
if (cached && Date.now() - cached.timestamp <= this.cacheTTL) {
|
|
94
|
+
if (!cached.valid) {
|
|
95
|
+
throw new errors_utils_1.UnauthorizedError('Access token is invalid or expired');
|
|
96
|
+
}
|
|
97
|
+
return cached.payload;
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
const decoded = (0, verify_1.verifyToken)(token, this.accessSecret);
|
|
101
|
+
if (this.cache) {
|
|
102
|
+
this.cache.set(cacheKey, {
|
|
103
|
+
valid: true,
|
|
104
|
+
payload: decoded,
|
|
105
|
+
timestamp: Date.now(),
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
return decoded;
|
|
109
|
+
}
|
|
110
|
+
catch (error) {
|
|
111
|
+
if (error instanceof errors_utils_1.ValidationError ||
|
|
112
|
+
error instanceof errors_utils_1.UnauthorizedError) {
|
|
113
|
+
throw error;
|
|
114
|
+
}
|
|
115
|
+
if (error instanceof Error && error.name === 'TokenExpiredError') {
|
|
116
|
+
throw new errors_utils_1.UnauthorizedError('Access token has expired');
|
|
117
|
+
}
|
|
118
|
+
if (error instanceof Error && error.name === 'JsonWebTokenError') {
|
|
119
|
+
throw new errors_utils_1.UnauthorizedError('Access token is invalid');
|
|
120
|
+
}
|
|
121
|
+
throw new errors_utils_1.UnauthorizedError('Failed to verify access token');
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/**
|
|
125
|
+
* Verify refresh token
|
|
126
|
+
*/
|
|
127
|
+
async verifyRefreshToken(token) {
|
|
128
|
+
try {
|
|
129
|
+
if (!token || typeof token !== 'string') {
|
|
130
|
+
throw new errors_utils_1.ValidationError('Refresh token must be a non-empty string');
|
|
131
|
+
}
|
|
132
|
+
const cacheKey = `refresh_${token}`;
|
|
133
|
+
if (this.cache) {
|
|
134
|
+
const cached = this.cache.get(cacheKey);
|
|
135
|
+
if (cached) {
|
|
136
|
+
if (!cached.valid) {
|
|
137
|
+
throw new errors_utils_1.UnauthorizedError('Refresh token is invalid or expired');
|
|
138
|
+
}
|
|
139
|
+
return cached.payload;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
const decoded = (0, verify_1.verifyToken)(token, this.refreshSecret);
|
|
143
|
+
if (this.cache) {
|
|
144
|
+
this.cache.set(cacheKey, { valid: true, payload: decoded, timestamp: Date.now() });
|
|
145
|
+
}
|
|
146
|
+
return decoded;
|
|
147
|
+
}
|
|
148
|
+
catch (error) {
|
|
149
|
+
if (error instanceof errors_utils_1.ValidationError ||
|
|
150
|
+
error instanceof errors_utils_1.UnauthorizedError) {
|
|
151
|
+
throw error;
|
|
152
|
+
}
|
|
153
|
+
if (error instanceof Error && error.name === 'TokenExpiredError') {
|
|
154
|
+
throw new errors_utils_1.UnauthorizedError('Refresh token has expired');
|
|
155
|
+
}
|
|
156
|
+
if (error instanceof Error && error.name === 'JsonWebTokenError') {
|
|
157
|
+
throw new errors_utils_1.UnauthorizedError('Refresh token is invalid');
|
|
158
|
+
}
|
|
159
|
+
throw new errors_utils_1.UnauthorizedError('Failed to verify refresh token');
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Decode token without verification
|
|
164
|
+
*/
|
|
165
|
+
decodeToken(token, complete = false) {
|
|
166
|
+
try {
|
|
167
|
+
if (!token || typeof token !== 'string') {
|
|
168
|
+
throw new errors_utils_1.ValidationError('Token must be a non-empty string');
|
|
169
|
+
}
|
|
170
|
+
return jsonwebtoken_1.default.decode(token, { complete });
|
|
171
|
+
}
|
|
172
|
+
catch (error) {
|
|
173
|
+
if (error instanceof errors_utils_1.ValidationError) {
|
|
174
|
+
throw error;
|
|
175
|
+
}
|
|
176
|
+
return null;
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
/**
|
|
180
|
+
* Extract token from Authorization header
|
|
181
|
+
*/
|
|
182
|
+
extractTokenFromHeader(authHeader) {
|
|
183
|
+
try {
|
|
184
|
+
if (!authHeader || typeof authHeader !== 'string') {
|
|
185
|
+
return null;
|
|
186
|
+
}
|
|
187
|
+
const parts = authHeader.split(' ');
|
|
188
|
+
if (parts.length !== 2 || parts[0] !== 'Bearer') {
|
|
189
|
+
return null;
|
|
190
|
+
}
|
|
191
|
+
return parts[1];
|
|
192
|
+
}
|
|
193
|
+
catch {
|
|
194
|
+
return null;
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Validate token without throwing exceptions
|
|
199
|
+
*/
|
|
200
|
+
validateToken(token, secret, options = {}) {
|
|
201
|
+
try {
|
|
202
|
+
if (!token || typeof token !== 'string') {
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
const result = (0, verify_1.safeVerifyToken)(token, secret);
|
|
206
|
+
return result.valid;
|
|
207
|
+
}
|
|
208
|
+
catch {
|
|
209
|
+
return false;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Rotate refresh token
|
|
214
|
+
*/
|
|
215
|
+
async rotateRefreshToken(oldToken) {
|
|
216
|
+
try {
|
|
217
|
+
if (!oldToken || typeof oldToken !== 'string') {
|
|
218
|
+
throw new errors_utils_1.ValidationError('Old refresh token must be a non-empty string');
|
|
219
|
+
}
|
|
220
|
+
const decoded = await this.verifyRefreshToken(oldToken);
|
|
221
|
+
if (typeof decoded === 'string') {
|
|
222
|
+
throw new errors_utils_1.ValidationError('Invalid token payload — expected JWT payload object');
|
|
223
|
+
}
|
|
224
|
+
// Create new payload without issued/expired timestamps
|
|
225
|
+
const payload = { ...decoded };
|
|
226
|
+
delete payload.iat;
|
|
227
|
+
delete payload.exp;
|
|
228
|
+
// Generate new refresh token
|
|
229
|
+
const newToken = (0, signToken_1.signToken)(payload, this.refreshSecret, this.refreshExpiry);
|
|
230
|
+
return newToken;
|
|
231
|
+
}
|
|
232
|
+
catch (error) {
|
|
233
|
+
if (error instanceof errors_utils_1.ValidationError ||
|
|
234
|
+
error instanceof errors_utils_1.UnauthorizedError) {
|
|
235
|
+
throw error;
|
|
236
|
+
}
|
|
237
|
+
throw new errors_utils_1.BadRequestError('Failed to rotate refresh token');
|
|
238
|
+
}
|
|
239
|
+
}
|
|
240
|
+
/**
|
|
241
|
+
* Check if token is expired
|
|
242
|
+
*/
|
|
243
|
+
isTokenExpired(token) {
|
|
244
|
+
try {
|
|
245
|
+
const decoded = this.decodeToken(token);
|
|
246
|
+
if (!decoded || !decoded.exp) {
|
|
247
|
+
return true;
|
|
248
|
+
}
|
|
249
|
+
const currentTime = Math.floor(Date.now() / 1000);
|
|
250
|
+
return decoded.exp < currentTime;
|
|
251
|
+
}
|
|
252
|
+
catch {
|
|
253
|
+
return true;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Get token expiration date
|
|
258
|
+
*/
|
|
259
|
+
getTokenExpiration(token) {
|
|
260
|
+
try {
|
|
261
|
+
const decoded = this.decodeToken(token);
|
|
262
|
+
if (!decoded || !decoded.exp) {
|
|
263
|
+
return null;
|
|
264
|
+
}
|
|
265
|
+
return new Date(decoded.exp * 1000);
|
|
266
|
+
}
|
|
267
|
+
catch {
|
|
268
|
+
return null;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
/**
|
|
272
|
+
* Clear token cache
|
|
273
|
+
*/
|
|
274
|
+
clearCache() {
|
|
275
|
+
this.cache?.clear();
|
|
276
|
+
}
|
|
277
|
+
/**
|
|
278
|
+
* Get cache statistics
|
|
279
|
+
*/
|
|
280
|
+
getCacheStats() {
|
|
281
|
+
if (!this.cache)
|
|
282
|
+
return null;
|
|
283
|
+
// Note: LRUCache doesn't expose internal size, so we return maxSize only
|
|
284
|
+
return {
|
|
285
|
+
size: -1, // Size not available from LRUCache
|
|
286
|
+
maxSize: this.cache.maxSize,
|
|
287
|
+
};
|
|
288
|
+
}
|
|
289
|
+
// Private helper methods
|
|
290
|
+
validatePayload(payload) {
|
|
291
|
+
if (!payload || typeof payload !== 'object') {
|
|
292
|
+
throw new errors_utils_1.ValidationError('Payload must be a non-null object');
|
|
293
|
+
}
|
|
294
|
+
if (Object.keys(payload).length === 0) {
|
|
295
|
+
throw new errors_utils_1.ValidationError('Payload cannot be empty');
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
exports.JWTManager = JWTManager;
|
|
@@ -6,16 +6,16 @@ const TIME_UNITS = {
|
|
|
6
6
|
m: 60,
|
|
7
7
|
h: 3600,
|
|
8
8
|
d: 86400,
|
|
9
|
-
w: 604800
|
|
9
|
+
w: 604800,
|
|
10
10
|
};
|
|
11
11
|
function parseDuration(input) {
|
|
12
|
-
if (typeof input ===
|
|
12
|
+
if (typeof input === 'number')
|
|
13
13
|
return input;
|
|
14
14
|
const regex = /(\d+)\s*(s|m|h|d|w)/gi;
|
|
15
15
|
let totalSeconds = 0;
|
|
16
16
|
let match;
|
|
17
17
|
while ((match = regex.exec(input)) !== null) {
|
|
18
|
-
const value = parseInt(match[1], 10);
|
|
18
|
+
const value = Number.parseInt(match[1], 10);
|
|
19
19
|
const unit = match[2].toLowerCase();
|
|
20
20
|
if (!TIME_UNITS[unit]) {
|
|
21
21
|
throw new Error(`Invalid time unit: ${unit}`);
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Secret, SignOptions } from
|
|
1
|
+
import { type Secret, type SignOptions } from 'jsonwebtoken';
|
|
2
2
|
export declare const signToken: (payload: Record<string, unknown>, secret: Secret, expiresIn?: string | number, options?: SignOptions) => string;
|
|
@@ -6,21 +6,21 @@ const parseDuration_1 = require("./parseDuration");
|
|
|
6
6
|
function getExpiryTimestamp(seconds) {
|
|
7
7
|
return Math.floor(Date.now() / 1000) + seconds;
|
|
8
8
|
}
|
|
9
|
-
const signToken = (payload, secret, expiresIn =
|
|
9
|
+
const signToken = (payload, secret, expiresIn = '1h', options = {}) => {
|
|
10
10
|
const seconds = (0, parseDuration_1.parseDuration)(expiresIn);
|
|
11
11
|
if (!seconds || seconds < 10) {
|
|
12
|
-
throw new Error(
|
|
12
|
+
throw new Error('Token expiry too small');
|
|
13
13
|
}
|
|
14
14
|
const tokenPayload = {
|
|
15
|
-
...payload
|
|
15
|
+
...payload,
|
|
16
16
|
};
|
|
17
|
-
if (!(
|
|
17
|
+
if (!('exp' in payload))
|
|
18
18
|
tokenPayload.exp = getExpiryTimestamp(seconds);
|
|
19
|
-
if (!(
|
|
19
|
+
if (!('iat' in payload))
|
|
20
20
|
tokenPayload.iat = Math.floor(Date.now() / 1000);
|
|
21
21
|
return (0, jsonwebtoken_1.sign)(tokenPayload, secret, {
|
|
22
|
-
algorithm:
|
|
23
|
-
...options
|
|
22
|
+
algorithm: 'HS256',
|
|
23
|
+
...options,
|
|
24
24
|
});
|
|
25
25
|
};
|
|
26
26
|
exports.signToken = signToken;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { JwtPayload } from
|
|
1
|
+
import type { JwtPayload } from 'node_modules/@types/jsonwebtoken';
|
|
2
2
|
export interface TokenRequirements {
|
|
3
3
|
requiredFields?: string[];
|
|
4
4
|
forbiddenFields?: string[];
|
|
5
|
-
validateTypes?: Record<string,
|
|
5
|
+
validateTypes?: Record<string, 'string' | 'number' | 'boolean'>;
|
|
6
6
|
}
|
|
7
7
|
export declare function validateTokenPayload(payload: Record<string, unknown>, rules?: TokenRequirements): {
|
|
8
8
|
valid: true;
|
|
@@ -3,9 +3,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.validateTokenPayload = validateTokenPayload;
|
|
4
4
|
exports.isTokenExpired = isTokenExpired;
|
|
5
5
|
function validateTokenPayload(payload, rules = {
|
|
6
|
-
requiredFields: [
|
|
6
|
+
requiredFields: ['exp', 'iat'],
|
|
7
7
|
}) {
|
|
8
|
-
const { requiredFields = [], forbiddenFields = [], validateTypes = {} } = rules;
|
|
8
|
+
const { requiredFields = [], forbiddenFields = [], validateTypes = {}, } = rules;
|
|
9
9
|
// 1. Required fields
|
|
10
10
|
for (const field of requiredFields) {
|
|
11
11
|
if (!(field in payload)) {
|
|
@@ -24,7 +24,7 @@ function validateTokenPayload(payload, rules = {
|
|
|
24
24
|
if (key in payload && typeof payload[key] !== expectedType) {
|
|
25
25
|
return {
|
|
26
26
|
valid: false,
|
|
27
|
-
error: `Invalid type for ${key}. Expected ${expectedType}
|
|
27
|
+
error: `Invalid type for ${key}. Expected ${expectedType}.`,
|
|
28
28
|
};
|
|
29
29
|
}
|
|
30
30
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import jwt
|
|
2
|
-
import {
|
|
1
|
+
import type jwt from 'jsonwebtoken';
|
|
2
|
+
import { type JwtPayload, type Secret } from 'jsonwebtoken';
|
|
3
|
+
import type { VerificationResult } from './types';
|
|
3
4
|
/**
|
|
4
5
|
* Verify token (throws if invalid or expired)
|
|
5
6
|
*/
|
|
@@ -6,9 +6,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
6
6
|
exports.hashPasswordSync = exports.hashPassword = void 0;
|
|
7
7
|
exports.hashPasswordWithPepper = hashPasswordWithPepper;
|
|
8
8
|
exports.hashPasswordWithPepperSync = hashPasswordWithPepperSync;
|
|
9
|
+
const errors_utils_1 = require("@naman_deep_singh/errors-utils");
|
|
9
10
|
const bcryptjs_1 = __importDefault(require("bcryptjs"));
|
|
10
11
|
const utils_1 = require("./utils");
|
|
11
|
-
const errors_utils_1 = require("@naman_deep_singh/errors-utils");
|
|
12
12
|
/**
|
|
13
13
|
* Hash a password asynchronously using bcrypt.
|
|
14
14
|
*/
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
1
|
+
export * from './hash';
|
|
2
|
+
export * from './strength';
|
|
3
|
+
export * from './verify';
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import type { HashedPassword, IPasswordManager, PasswordConfig, PasswordStrength, PasswordValidationResult } from '../../interfaces/password.interface';
|
|
2
|
+
export declare class PasswordManager implements IPasswordManager {
|
|
3
|
+
private defaultConfig;
|
|
4
|
+
constructor(config?: PasswordConfig);
|
|
5
|
+
/**
|
|
6
|
+
* Hash a password asynchronously using bcrypt
|
|
7
|
+
*/
|
|
8
|
+
hash(password: string, salt?: string): Promise<HashedPassword>;
|
|
9
|
+
/**
|
|
10
|
+
* Verify password against hash and salt
|
|
11
|
+
*/
|
|
12
|
+
verify(password: string, hash: string, salt: string): Promise<boolean>;
|
|
13
|
+
/**
|
|
14
|
+
* Generate a random password
|
|
15
|
+
*/
|
|
16
|
+
generate(length?: number, options?: PasswordConfig): string;
|
|
17
|
+
/**
|
|
18
|
+
* Validate password against configuration
|
|
19
|
+
*/
|
|
20
|
+
validate(password: string, config?: PasswordConfig): PasswordValidationResult;
|
|
21
|
+
/**
|
|
22
|
+
* Check password strength
|
|
23
|
+
*/
|
|
24
|
+
checkStrength(password: string): PasswordStrength;
|
|
25
|
+
/**
|
|
26
|
+
* Check if password hash needs upgrade (different salt rounds)
|
|
27
|
+
*/
|
|
28
|
+
needsUpgrade(hash: string, currentConfig: PasswordConfig): boolean;
|
|
29
|
+
}
|