ts-packages 2.0.0 → 3.0.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/package.json +1 -1
- package/packages/cache/README.md +105 -9
- package/packages/cache/dist/cjs/adapters/memcache/MemcacheCache.d.ts +1 -54
- package/packages/cache/dist/cjs/adapters/memcache/MemcacheCache.js +75 -281
- package/packages/cache/dist/cjs/adapters/memory/MemoryCache.js +76 -22
- package/packages/cache/dist/cjs/adapters/redis/RedisCache.js +84 -26
- package/packages/cache/dist/cjs/core/BaseCache.js +13 -4
- package/packages/cache/dist/cjs/core/factory.js +26 -4
- package/packages/cache/dist/cjs/errors/CacheError.d.ts +10 -7
- package/packages/cache/dist/cjs/errors/CacheError.js +6 -11
- package/packages/cache/dist/cjs/errors/cacheErrorCodes.d.ts +22 -0
- package/packages/cache/dist/cjs/errors/cacheErrorCodes.js +24 -0
- package/packages/cache/dist/cjs/errors/index.js +3 -0
- package/packages/cache/dist/cjs/middleware/express/cacheMiddleware.js +8 -8
- package/packages/cache/dist/cjs/session/SessionStore.js +84 -28
- package/packages/cache/dist/esm/adapters/memcache/MemcacheCache.d.ts +1 -54
- package/packages/cache/dist/esm/adapters/memcache/MemcacheCache.js +75 -281
- package/packages/cache/dist/esm/adapters/memory/MemoryCache.js +76 -22
- package/packages/cache/dist/esm/adapters/redis/RedisCache.js +84 -26
- package/packages/cache/dist/esm/core/BaseCache.js +13 -4
- package/packages/cache/dist/esm/core/factory.js +26 -4
- package/packages/cache/dist/esm/errors/CacheError.d.ts +10 -7
- package/packages/cache/dist/esm/errors/CacheError.js +6 -11
- package/packages/cache/dist/esm/errors/cacheErrorCodes.d.ts +22 -0
- package/packages/cache/dist/esm/errors/cacheErrorCodes.js +21 -0
- package/packages/cache/dist/esm/errors/index.js +3 -0
- package/packages/cache/dist/esm/middleware/express/cacheMiddleware.js +8 -8
- package/packages/cache/dist/esm/session/SessionStore.js +84 -28
- package/packages/cache/dist/types/adapters/memcache/MemcacheCache.d.ts +1 -54
- package/packages/cache/dist/types/errors/CacheError.d.ts +10 -7
- package/packages/cache/dist/types/errors/cacheErrorCodes.d.ts +22 -0
- package/packages/cache/package.json +8 -6
- package/packages/cache/src/adapters/memcache/MemcacheCache.ts +79 -352
- package/packages/cache/src/adapters/memory/MemoryCache.ts +76 -77
- package/packages/cache/src/adapters/redis/RedisCache.ts +84 -86
- package/packages/cache/src/core/BaseCache.ts +13 -14
- package/packages/cache/src/core/factory.ts +27 -16
- package/packages/cache/src/errors/CacheError.ts +16 -17
- package/packages/cache/src/errors/cacheErrorCodes.ts +24 -0
- package/packages/cache/src/errors/index.ts +5 -0
- package/packages/cache/src/middleware/express/cacheMiddleware.ts +8 -8
- package/packages/cache/src/session/SessionStore.ts +84 -84
- package/packages/errors-utils/README.md +54 -57
- package/packages/errors-utils/dist/cjs/constants/errorCodes.d.ts +28 -23
- package/packages/errors-utils/dist/cjs/constants/errorCodes.js +57 -22
- package/packages/errors-utils/dist/cjs/constants/errorMessages.d.ts +8 -1
- package/packages/errors-utils/dist/cjs/constants/errorMessages.js +72 -29
- package/packages/errors-utils/dist/cjs/error/AppError.d.ts +2 -2
- package/packages/errors-utils/dist/cjs/error/AppError.js +2 -2
- package/packages/errors-utils/dist/cjs/error/ServiceUnavailableError.d.ts +4 -0
- package/packages/errors-utils/dist/cjs/error/ServiceUnavailableError.js +11 -0
- package/packages/errors-utils/dist/cjs/error/TokenExpiredError.d.ts +2 -2
- package/packages/errors-utils/dist/cjs/error/TokenExpiredError.js +2 -2
- package/packages/errors-utils/dist/cjs/error/TokenMalformedError.d.ts +2 -2
- package/packages/errors-utils/dist/cjs/error/TokenMalformedError.js +2 -2
- package/packages/errors-utils/dist/cjs/error/UnauthorizedError.d.ts +1 -2
- package/packages/errors-utils/dist/cjs/error/UnauthorizedError.js +2 -2
- package/packages/errors-utils/dist/cjs/error/index.d.ts +14 -0
- package/packages/errors-utils/dist/cjs/error/index.js +39 -0
- package/packages/errors-utils/dist/cjs/errorRegistry/errorRegistry.d.ts +19 -0
- package/packages/errors-utils/dist/cjs/errorRegistry/errorRegistry.js +63 -0
- package/packages/errors-utils/dist/cjs/errorRegistry/index.d.ts +3 -0
- package/packages/errors-utils/dist/cjs/errorRegistry/index.js +6 -0
- package/packages/errors-utils/dist/cjs/index.d.ts +3 -9
- package/packages/errors-utils/dist/cjs/index.js +12 -9
- package/packages/errors-utils/dist/cjs/middleware/express/{errorHandler.js → errorHandler.middleware.js} +4 -4
- package/packages/errors-utils/dist/cjs/middleware/express/index.d.ts +2 -2
- package/packages/errors-utils/dist/cjs/middleware/express/index.js +2 -2
- package/packages/errors-utils/dist/esm/constants/errorCodes.d.ts +28 -23
- package/packages/errors-utils/dist/esm/constants/errorCodes.js +57 -22
- package/packages/errors-utils/dist/esm/constants/errorMessages.d.ts +8 -1
- package/packages/errors-utils/dist/esm/constants/errorMessages.js +72 -29
- package/packages/errors-utils/dist/esm/error/AppError.d.ts +2 -2
- package/packages/errors-utils/dist/esm/error/AppError.js +2 -2
- package/packages/errors-utils/dist/esm/error/ServiceUnavailableError.d.ts +4 -0
- package/packages/errors-utils/dist/esm/error/ServiceUnavailableError.js +7 -0
- package/packages/errors-utils/dist/esm/error/TokenExpiredError.d.ts +2 -2
- package/packages/errors-utils/dist/esm/error/TokenExpiredError.js +2 -2
- package/packages/errors-utils/dist/esm/error/TokenMalformedError.d.ts +2 -2
- package/packages/errors-utils/dist/esm/error/TokenMalformedError.js +2 -2
- package/packages/errors-utils/dist/esm/error/UnauthorizedError.d.ts +1 -2
- package/packages/errors-utils/dist/esm/error/UnauthorizedError.js +2 -2
- package/packages/errors-utils/dist/esm/error/index.d.ts +14 -0
- package/packages/errors-utils/dist/esm/error/index.js +23 -0
- package/packages/errors-utils/dist/esm/errorRegistry/errorRegistry.d.ts +19 -0
- package/packages/errors-utils/dist/esm/errorRegistry/errorRegistry.js +59 -0
- package/packages/errors-utils/dist/esm/errorRegistry/index.d.ts +3 -0
- package/packages/errors-utils/dist/esm/errorRegistry/index.js +3 -0
- package/packages/errors-utils/dist/esm/index.d.ts +3 -9
- package/packages/errors-utils/dist/esm/index.js +12 -9
- package/packages/errors-utils/dist/esm/middleware/express/{errorHandler.js → errorHandler.middleware.js} +5 -5
- package/packages/errors-utils/dist/esm/middleware/express/index.d.ts +2 -2
- package/packages/errors-utils/dist/esm/middleware/express/index.js +2 -2
- package/packages/errors-utils/dist/types/constants/errorCodes.d.ts +28 -23
- package/packages/errors-utils/dist/types/constants/errorMessages.d.ts +8 -1
- package/packages/errors-utils/dist/types/error/AppError.d.ts +2 -2
- package/packages/errors-utils/dist/types/error/ServiceUnavailableError.d.ts +4 -0
- package/packages/errors-utils/dist/types/error/TokenExpiredError.d.ts +2 -2
- package/packages/errors-utils/dist/types/error/TokenMalformedError.d.ts +2 -2
- package/packages/errors-utils/dist/types/error/UnauthorizedError.d.ts +1 -2
- package/packages/errors-utils/dist/types/error/index.d.ts +14 -0
- package/packages/errors-utils/dist/types/errorRegistry/errorRegistry.d.ts +19 -0
- package/packages/errors-utils/dist/types/errorRegistry/index.d.ts +3 -0
- package/packages/errors-utils/dist/types/index.d.ts +3 -9
- package/packages/errors-utils/dist/types/middleware/express/index.d.ts +2 -2
- package/packages/errors-utils/package.json +4 -4
- package/packages/errors-utils/src/constants/errorCodes.ts +64 -23
- package/packages/errors-utils/src/constants/errorMessages.ts +91 -34
- package/packages/errors-utils/src/error/AppError.ts +3 -2
- package/packages/errors-utils/src/error/ServiceUnavailableError.ts +8 -0
- package/packages/errors-utils/src/error/TokenExpiredError.ts +2 -2
- package/packages/errors-utils/src/error/TokenMalformedError.ts +2 -2
- package/packages/errors-utils/src/error/UnauthorizedError.ts +8 -8
- package/packages/errors-utils/src/error/index.ts +26 -0
- package/packages/errors-utils/src/errorRegistry/errorRegistry.ts +74 -0
- package/packages/errors-utils/src/errorRegistry/index.ts +4 -0
- package/packages/errors-utils/src/index.ts +12 -10
- package/packages/errors-utils/src/middleware/express/{errorHandler.ts → errorHandler.middleware.ts} +5 -5
- package/packages/errors-utils/src/middleware/express/index.ts +2 -2
- package/packages/js-extensions/README.md +174 -425
- package/packages/js-extensions/dist/cjs/array/array-extensions.js +84 -50
- package/packages/js-extensions/dist/cjs/core/performance.d.ts +1 -0
- package/packages/js-extensions/dist/cjs/core/performance.js +6 -0
- package/packages/js-extensions/dist/cjs/core/version.d.ts +1 -0
- package/packages/js-extensions/dist/cjs/core/version.js +9 -0
- package/packages/js-extensions/dist/cjs/index.d.ts +1 -0
- package/packages/js-extensions/dist/cjs/index.js +1 -0
- package/packages/js-extensions/dist/cjs/number/number-extensions.js +85 -97
- package/packages/js-extensions/dist/cjs/object/object-extensions.js +102 -103
- package/packages/js-extensions/dist/cjs/string/string-extensions.js +66 -43
- package/packages/js-extensions/dist/cjs/types/global-augmentations.d.ts +1 -0
- package/packages/js-extensions/dist/cjs/utils/defineExtension.d.ts +1 -0
- package/packages/js-extensions/dist/cjs/utils/defineExtension.js +13 -0
- package/packages/js-extensions/dist/cjs/utils/index.d.ts +1 -0
- package/packages/js-extensions/dist/cjs/utils/index.js +1 -0
- package/packages/js-extensions/dist/esm/array/array-extensions.js +84 -50
- package/packages/js-extensions/dist/esm/core/performance.d.ts +1 -0
- package/packages/js-extensions/dist/esm/core/performance.js +5 -0
- package/packages/js-extensions/dist/esm/core/version.d.ts +1 -0
- package/packages/js-extensions/dist/esm/core/version.js +5 -0
- package/packages/js-extensions/dist/esm/index.d.ts +1 -0
- package/packages/js-extensions/dist/esm/index.js +1 -0
- package/packages/js-extensions/dist/esm/number/number-extensions.js +86 -98
- package/packages/js-extensions/dist/esm/object/object-extensions.js +102 -103
- package/packages/js-extensions/dist/esm/string/string-extensions.js +66 -43
- package/packages/js-extensions/dist/esm/types/global-augmentations.d.ts +1 -0
- package/packages/js-extensions/dist/esm/utils/defineExtension.d.ts +1 -0
- package/packages/js-extensions/dist/esm/utils/defineExtension.js +10 -0
- package/packages/js-extensions/dist/esm/utils/index.d.ts +1 -0
- package/packages/js-extensions/dist/esm/utils/index.js +1 -0
- package/packages/js-extensions/dist/types/core/performance.d.ts +1 -0
- package/packages/js-extensions/dist/types/core/version.d.ts +1 -0
- package/packages/js-extensions/dist/types/index.d.ts +1 -0
- package/packages/js-extensions/dist/types/types/global-augmentations.d.ts +1 -0
- package/packages/js-extensions/dist/types/utils/defineExtension.d.ts +1 -0
- package/packages/js-extensions/dist/types/utils/index.d.ts +1 -0
- package/packages/js-extensions/package.json +8 -4
- package/packages/js-extensions/src/array/array-extensions.ts +268 -150
- package/packages/js-extensions/src/core/performance.ts +11 -0
- package/packages/js-extensions/src/core/version.ts +7 -0
- package/packages/js-extensions/src/index.ts +2 -0
- package/packages/js-extensions/src/number/number-extensions.ts +90 -123
- package/packages/js-extensions/src/object/object-extensions.ts +102 -130
- package/packages/js-extensions/src/string/string-extensions.ts +80 -76
- package/packages/js-extensions/src/types/global-augmentations.ts +2 -1
- package/packages/js-extensions/src/utils/defineExtension.ts +14 -0
- package/packages/js-extensions/src/utils/index.ts +1 -0
- package/packages/response-utils/README.md +156 -198
- package/packages/response-utils/dist/cjs/core/BaseResponder.js +20 -4
- package/packages/response-utils/dist/cjs/index.d.ts +0 -1
- package/packages/response-utils/dist/cjs/index.js +1 -25
- package/packages/response-utils/dist/esm/core/BaseResponder.js +20 -4
- package/packages/response-utils/dist/esm/index.d.ts +0 -1
- package/packages/response-utils/dist/esm/index.js +0 -2
- package/packages/response-utils/dist/types/index.d.ts +0 -1
- package/packages/response-utils/package.json +1 -1
- package/packages/response-utils/src/core/BaseResponder.ts +25 -4
- package/packages/response-utils/src/index.ts +0 -3
- package/packages/response-utils/src/middleware/express/expressMiddleware.ts +1 -1
- package/packages/security/README.md +153 -355
- package/packages/security/dist/cjs/core/crypto/cryptoManager.js +34 -17
- package/packages/security/dist/cjs/core/jwt/decode.js +4 -1
- package/packages/security/dist/cjs/core/jwt/generateTokens.js +4 -1
- package/packages/security/dist/cjs/core/jwt/jwtManager.d.ts +19 -43
- package/packages/security/dist/cjs/core/jwt/jwtManager.js +84 -199
- package/packages/security/dist/cjs/core/jwt/parseDuration.js +3 -2
- package/packages/security/dist/cjs/core/jwt/signToken.js +2 -1
- package/packages/security/dist/cjs/core/jwt/validateToken.d.ts +10 -7
- package/packages/security/dist/cjs/core/jwt/validateToken.js +19 -10
- package/packages/security/dist/cjs/core/jwt/verify.d.ts +8 -9
- package/packages/security/dist/cjs/core/jwt/verify.js +59 -14
- package/packages/security/dist/cjs/core/password/hash.js +4 -4
- package/packages/security/dist/cjs/core/password/passwordManager.d.ts +1 -1
- package/packages/security/dist/cjs/core/password/passwordManager.js +36 -80
- package/packages/security/dist/cjs/core/password/strength.js +12 -6
- package/packages/security/dist/cjs/core/password/utils.d.ts +12 -0
- package/packages/security/dist/cjs/core/password/utils.js +16 -1
- package/packages/security/dist/cjs/core/password/verify.js +4 -4
- package/packages/security/dist/cjs/index.d.ts +2 -7
- package/packages/security/dist/esm/core/crypto/cryptoManager.js +34 -17
- package/packages/security/dist/esm/core/jwt/decode.js +4 -1
- package/packages/security/dist/esm/core/jwt/generateTokens.js +4 -1
- package/packages/security/dist/esm/core/jwt/jwtManager.d.ts +19 -43
- package/packages/security/dist/esm/core/jwt/jwtManager.js +85 -200
- package/packages/security/dist/esm/core/jwt/parseDuration.js +3 -2
- package/packages/security/dist/esm/core/jwt/signToken.js +2 -1
- package/packages/security/dist/esm/core/jwt/validateToken.d.ts +10 -7
- package/packages/security/dist/esm/core/jwt/validateToken.js +19 -10
- package/packages/security/dist/esm/core/jwt/verify.d.ts +8 -9
- package/packages/security/dist/esm/core/jwt/verify.js +58 -13
- package/packages/security/dist/esm/core/password/hash.js +4 -4
- package/packages/security/dist/esm/core/password/passwordManager.d.ts +1 -1
- package/packages/security/dist/esm/core/password/passwordManager.js +36 -80
- package/packages/security/dist/esm/core/password/strength.js +12 -6
- package/packages/security/dist/esm/core/password/utils.d.ts +12 -0
- package/packages/security/dist/esm/core/password/utils.js +16 -1
- package/packages/security/dist/esm/core/password/verify.js +4 -4
- package/packages/security/dist/esm/index.d.ts +2 -7
- package/packages/security/dist/types/core/jwt/jwtManager.d.ts +19 -43
- package/packages/security/dist/types/core/jwt/validateToken.d.ts +10 -7
- package/packages/security/dist/types/core/jwt/verify.d.ts +8 -9
- package/packages/security/dist/types/core/password/passwordManager.d.ts +1 -1
- package/packages/security/dist/types/core/password/utils.d.ts +12 -0
- package/packages/security/dist/types/index.d.ts +2 -7
- package/packages/security/package.json +3 -3
- package/packages/security/src/core/crypto/cryptoManager.ts +53 -21
- package/packages/security/src/core/jwt/decode.ts +4 -1
- package/packages/security/src/core/jwt/generateTokens.ts +4 -1
- package/packages/security/src/core/jwt/jwtManager.ts +105 -257
- package/packages/security/src/core/jwt/parseDuration.ts +4 -2
- package/packages/security/src/core/jwt/signToken.ts +2 -1
- package/packages/security/src/core/jwt/validateToken.ts +21 -13
- package/packages/security/src/core/jwt/verify.ts +82 -20
- package/packages/security/src/core/password/hash.ts +10 -4
- package/packages/security/src/core/password/passwordManager.ts +38 -108
- package/packages/security/src/core/password/strength.ts +13 -8
- package/packages/security/src/core/password/utils.ts +19 -3
- package/packages/security/src/core/password/verify.ts +6 -4
- package/packages/server-utils/README.md +1 -1
- package/packages/server-utils/dist/cjs/core/server.js +6 -6
- package/packages/server-utils/dist/cjs/middleware/auth.middleware.d.ts +1 -2
- package/packages/server-utils/dist/cjs/middleware/auth.middleware.js +17 -14
- package/packages/server-utils/dist/cjs/middleware/cache.middleware.js +2 -2
- package/packages/server-utils/dist/cjs/middleware/errorHandler.middleware.d.ts +1 -1
- package/packages/server-utils/dist/cjs/middleware/errorHandler.middleware.js +34 -17
- package/packages/server-utils/dist/cjs/middleware/session.middleware.js +8 -8
- package/packages/server-utils/dist/cjs/middleware/validation.middleware.js +2 -2
- package/packages/server-utils/dist/esm/core/server.js +6 -6
- package/packages/server-utils/dist/esm/middleware/auth.middleware.d.ts +1 -2
- package/packages/server-utils/dist/esm/middleware/auth.middleware.js +18 -15
- package/packages/server-utils/dist/esm/middleware/cache.middleware.js +2 -2
- package/packages/server-utils/dist/esm/middleware/errorHandler.middleware.d.ts +1 -1
- package/packages/server-utils/dist/esm/middleware/errorHandler.middleware.js +34 -17
- package/packages/server-utils/dist/esm/middleware/session.middleware.js +8 -8
- package/packages/server-utils/dist/esm/middleware/validation.middleware.js +2 -2
- package/packages/server-utils/dist/types/middleware/auth.middleware.d.ts +1 -2
- package/packages/server-utils/dist/types/middleware/errorHandler.middleware.d.ts +1 -1
- package/packages/server-utils/package.json +4 -4
- package/packages/server-utils/src/core/server.ts +6 -6
- package/packages/server-utils/src/middleware/auth.middleware.ts +40 -27
- package/packages/server-utils/src/middleware/cache.middleware.ts +2 -2
- package/packages/server-utils/src/middleware/errorHandler.middleware.ts +39 -24
- package/packages/server-utils/src/middleware/session.middleware.ts +11 -8
- package/packages/server-utils/src/middleware/validation.middleware.ts +2 -3
- package/packages/errors-utils/dist/cjs/error/RateLimitError.d.ts +0 -4
- package/packages/errors-utils/dist/cjs/error/RateLimitError.js +0 -11
- package/packages/errors-utils/dist/cjs/utils/mapAppErrorToResponder.d.ts +0 -3
- package/packages/errors-utils/dist/cjs/utils/mapAppErrorToResponder.js +0 -27
- package/packages/errors-utils/dist/esm/error/RateLimitError.d.ts +0 -4
- package/packages/errors-utils/dist/esm/error/RateLimitError.js +0 -7
- package/packages/errors-utils/dist/esm/utils/mapAppErrorToResponder.d.ts +0 -3
- package/packages/errors-utils/dist/esm/utils/mapAppErrorToResponder.js +0 -24
- package/packages/errors-utils/dist/types/error/RateLimitError.d.ts +0 -4
- package/packages/errors-utils/dist/types/utils/mapAppErrorToResponder.d.ts +0 -3
- package/packages/errors-utils/src/error/RateLimitError.ts +0 -8
- package/packages/errors-utils/src/utils/mapAppErrorToResponder.ts +0 -38
- package/packages/response-utils/src/legacy.ts +0 -30
- /package/packages/errors-utils/dist/cjs/middleware/express/{errorConverter.d.ts → errorConverter.middleware.d.ts} +0 -0
- /package/packages/errors-utils/dist/cjs/middleware/express/{errorConverter.js → errorConverter.middleware.js} +0 -0
- /package/packages/errors-utils/dist/cjs/middleware/express/{errorHandler.d.ts → errorHandler.middleware.d.ts} +0 -0
- /package/packages/errors-utils/dist/esm/middleware/express/{errorConverter.d.ts → errorConverter.middleware.d.ts} +0 -0
- /package/packages/errors-utils/dist/esm/middleware/express/{errorConverter.js → errorConverter.middleware.js} +0 -0
- /package/packages/errors-utils/dist/esm/middleware/express/{errorHandler.d.ts → errorHandler.middleware.d.ts} +0 -0
- /package/packages/errors-utils/dist/types/middleware/express/{errorConverter.d.ts → errorConverter.middleware.d.ts} +0 -0
- /package/packages/errors-utils/dist/types/middleware/express/{errorHandler.d.ts → errorHandler.middleware.d.ts} +0 -0
- /package/packages/errors-utils/src/middleware/express/{errorConverter.ts → errorConverter.middleware.ts} +0 -0
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.cryptoManager = exports.createCryptoManager = exports.CryptoManager = void 0;
|
|
4
|
+
const errors_utils_1 = require("@naman_deep_singh/errors-utils");
|
|
4
5
|
const index_1 = require("./index");
|
|
5
6
|
/**
|
|
6
7
|
* Default configuration
|
|
@@ -34,17 +35,27 @@ class CryptoManager {
|
|
|
34
35
|
* Encrypt data using the default or specified algorithm
|
|
35
36
|
*/
|
|
36
37
|
encrypt(plaintext, key, _options) {
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
38
|
+
try {
|
|
39
|
+
return (0, index_1.encrypt)(plaintext, key);
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
throw new errors_utils_1.CryptoIntegrityError({
|
|
43
|
+
reason: 'Encryption failed',
|
|
44
|
+
}, error instanceof Error ? error : undefined);
|
|
45
|
+
}
|
|
40
46
|
}
|
|
41
47
|
/**
|
|
42
48
|
* Decrypt data using the default or specified algorithm
|
|
43
49
|
*/
|
|
44
50
|
decrypt(encryptedData, key, _options) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
51
|
+
try {
|
|
52
|
+
return (0, index_1.decrypt)(encryptedData, key);
|
|
53
|
+
}
|
|
54
|
+
catch (error) {
|
|
55
|
+
throw new errors_utils_1.CryptoIntegrityError({
|
|
56
|
+
reason: 'Decryption failed',
|
|
57
|
+
}, error instanceof Error ? error : undefined);
|
|
58
|
+
}
|
|
48
59
|
}
|
|
49
60
|
/**
|
|
50
61
|
* Generate HMAC signature
|
|
@@ -74,9 +85,11 @@ class CryptoManager {
|
|
|
74
85
|
deriveKey(password, salt, iterations = 100000, keyLength = 32) {
|
|
75
86
|
return new Promise((resolve, reject) => {
|
|
76
87
|
const crypto = require('crypto');
|
|
77
|
-
crypto.pbkdf2(password, salt, iterations, keyLength, 'sha256', (
|
|
78
|
-
if (
|
|
79
|
-
reject(
|
|
88
|
+
crypto.pbkdf2(password, salt, iterations, keyLength, 'sha256', (error, derivedKey) => {
|
|
89
|
+
if (error) {
|
|
90
|
+
reject(new errors_utils_1.CryptoIntegrityError({
|
|
91
|
+
reason: 'Key derivation failed',
|
|
92
|
+
}, error instanceof Error ? error : undefined));
|
|
80
93
|
}
|
|
81
94
|
else {
|
|
82
95
|
resolve(derivedKey.toString('hex'));
|
|
@@ -146,15 +159,17 @@ class CryptoManager {
|
|
|
146
159
|
rsaSign(data, privateKey, algorithm = 'sha256') {
|
|
147
160
|
return new Promise((resolve, reject) => {
|
|
148
161
|
const crypto = require('crypto');
|
|
149
|
-
const sign = crypto.createSign(algorithm);
|
|
150
|
-
sign.update(data);
|
|
151
|
-
sign.end();
|
|
152
162
|
try {
|
|
163
|
+
const sign = crypto.createSign(algorithm);
|
|
164
|
+
sign.update(data);
|
|
165
|
+
sign.end();
|
|
153
166
|
const signature = sign.sign(privateKey, 'base64');
|
|
154
167
|
resolve(signature);
|
|
155
168
|
}
|
|
156
169
|
catch (error) {
|
|
157
|
-
reject(
|
|
170
|
+
reject(new errors_utils_1.CryptoIntegrityError({
|
|
171
|
+
reason: 'RSA signing failed',
|
|
172
|
+
}, error instanceof Error ? error : undefined));
|
|
158
173
|
}
|
|
159
174
|
});
|
|
160
175
|
}
|
|
@@ -164,15 +179,17 @@ class CryptoManager {
|
|
|
164
179
|
rsaVerify(data, signature, publicKey, algorithm = 'sha256') {
|
|
165
180
|
return new Promise((resolve, reject) => {
|
|
166
181
|
const crypto = require('crypto');
|
|
167
|
-
const verify = crypto.createVerify(algorithm);
|
|
168
|
-
verify.update(data);
|
|
169
|
-
verify.end();
|
|
170
182
|
try {
|
|
183
|
+
const verify = crypto.createVerify(algorithm);
|
|
184
|
+
verify.update(data);
|
|
185
|
+
verify.end();
|
|
171
186
|
const isValid = verify.verify(publicKey, signature, 'base64');
|
|
172
187
|
resolve(isValid);
|
|
173
188
|
}
|
|
174
189
|
catch (error) {
|
|
175
|
-
reject(
|
|
190
|
+
reject(new errors_utils_1.CryptoIntegrityError({
|
|
191
|
+
reason: 'RSA verification failed',
|
|
192
|
+
}, error instanceof Error ? error : undefined));
|
|
176
193
|
}
|
|
177
194
|
});
|
|
178
195
|
}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.decodeToken = decodeToken;
|
|
4
4
|
exports.decodeTokenStrict = decodeTokenStrict;
|
|
5
|
+
const errors_utils_1 = require("@naman_deep_singh/errors-utils");
|
|
5
6
|
// src/jwt/decodeToken.ts
|
|
6
7
|
const jsonwebtoken_1 = require("jsonwebtoken");
|
|
7
8
|
/**
|
|
@@ -19,7 +20,9 @@ function decodeToken(token) {
|
|
|
19
20
|
function decodeTokenStrict(token) {
|
|
20
21
|
const decoded = (0, jsonwebtoken_1.decode)(token);
|
|
21
22
|
if (!decoded || typeof decoded === 'string') {
|
|
22
|
-
throw new
|
|
23
|
+
throw new errors_utils_1.BadRequestError({
|
|
24
|
+
reason: 'Invalid JWT payload structure',
|
|
25
|
+
});
|
|
23
26
|
}
|
|
24
27
|
return decoded;
|
|
25
28
|
}
|
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.generateTokens = void 0;
|
|
4
4
|
exports.rotateRefreshToken = rotateRefreshToken;
|
|
5
|
+
const errors_utils_1 = require("@naman_deep_singh/errors-utils");
|
|
5
6
|
const signToken_1 = require("./signToken");
|
|
6
7
|
const verify_1 = require("./verify");
|
|
7
8
|
// Helper function to create branded tokens
|
|
@@ -24,7 +25,9 @@ exports.generateTokens = generateTokens;
|
|
|
24
25
|
function rotateRefreshToken(oldToken, secret) {
|
|
25
26
|
const decoded = (0, verify_1.verifyToken)(oldToken, secret);
|
|
26
27
|
if (typeof decoded === 'string') {
|
|
27
|
-
throw new
|
|
28
|
+
throw new errors_utils_1.TokenMalformedError({
|
|
29
|
+
reason: 'Invalid token payload — expected JWT payload object',
|
|
30
|
+
});
|
|
28
31
|
}
|
|
29
32
|
const payload = { ...decoded };
|
|
30
33
|
delete payload.iat;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type JwtPayload, type Secret } from 'jsonwebtoken';
|
|
2
|
-
import type { AccessToken, ITokenManager, JWTConfig, RefreshToken, TokenPair
|
|
2
|
+
import type { AccessToken, ITokenManager, JWTConfig, RefreshToken, TokenPair } from '../../interfaces/jwt.interface';
|
|
3
3
|
export declare class JWTManager implements ITokenManager {
|
|
4
4
|
private accessSecret;
|
|
5
5
|
private refreshSecret;
|
|
@@ -8,60 +8,36 @@ export declare class JWTManager implements ITokenManager {
|
|
|
8
8
|
private cache?;
|
|
9
9
|
private cacheTTL;
|
|
10
10
|
constructor(config: JWTConfig);
|
|
11
|
-
/**
|
|
12
|
-
* Generate both access and refresh tokens
|
|
13
|
-
*/
|
|
11
|
+
/** Generate both access and refresh tokens */
|
|
14
12
|
generateTokens(payload: Record<string, unknown>): Promise<TokenPair>;
|
|
15
|
-
/**
|
|
16
|
-
* Generate access token
|
|
17
|
-
*/
|
|
13
|
+
/** Generate access token */
|
|
18
14
|
generateAccessToken(payload: Record<string, unknown>): Promise<AccessToken>;
|
|
19
|
-
/**
|
|
20
|
-
* Generate refresh token
|
|
21
|
-
*/
|
|
15
|
+
/** Generate refresh token */
|
|
22
16
|
generateRefreshToken(payload: Record<string, unknown>): Promise<RefreshToken>;
|
|
23
|
-
/**
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
/**
|
|
28
|
-
* Verify refresh token
|
|
29
|
-
*/
|
|
30
|
-
verifyRefreshToken(token: string): Promise<JwtPayload | string>;
|
|
31
|
-
/**
|
|
32
|
-
* Decode token without verification
|
|
33
|
-
*/
|
|
17
|
+
/** Verify access token */
|
|
18
|
+
verifyAccessToken(token: string): Promise<JwtPayload>;
|
|
19
|
+
/** Verify refresh token */
|
|
20
|
+
verifyRefreshToken(token: string): Promise<JwtPayload>;
|
|
21
|
+
/** Decode token without verification */
|
|
34
22
|
decodeToken(token: string, complete?: boolean): JwtPayload | string | null;
|
|
35
|
-
/**
|
|
36
|
-
* Extract token from Authorization header
|
|
37
|
-
*/
|
|
23
|
+
/** Extract token from Authorization header */
|
|
38
24
|
extractTokenFromHeader(authHeader: string): string | null;
|
|
39
|
-
/**
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
validateToken(token: string, secret: Secret, _options?: TokenValidationOptions): boolean;
|
|
43
|
-
/**
|
|
44
|
-
* Rotate refresh token
|
|
45
|
-
*/
|
|
25
|
+
/** Validate token without throwing exceptions */
|
|
26
|
+
validateToken(token: string, secret: Secret): boolean;
|
|
27
|
+
/** Rotate refresh token */
|
|
46
28
|
rotateRefreshToken(oldToken: string): Promise<RefreshToken>;
|
|
47
|
-
/**
|
|
48
|
-
* Check if token is expired
|
|
49
|
-
*/
|
|
29
|
+
/** Check if token is expired */
|
|
50
30
|
isTokenExpired(token: string): boolean;
|
|
51
|
-
/**
|
|
52
|
-
* Get token expiration date
|
|
53
|
-
*/
|
|
31
|
+
/** Get token expiration date */
|
|
54
32
|
getTokenExpiration(token: string): Date | null;
|
|
55
|
-
/**
|
|
56
|
-
* Clear token cache
|
|
57
|
-
*/
|
|
33
|
+
/** Clear token cache */
|
|
58
34
|
clearCache(): void;
|
|
59
|
-
/**
|
|
60
|
-
* Get cache statistics
|
|
61
|
-
*/
|
|
35
|
+
/** Get cache statistics */
|
|
62
36
|
getCacheStats(): {
|
|
63
37
|
size: number;
|
|
64
38
|
maxSize: number;
|
|
65
39
|
} | null;
|
|
40
|
+
/** Private helper methods */
|
|
66
41
|
private validatePayload;
|
|
42
|
+
private verifyTokenWithCache;
|
|
67
43
|
}
|
|
@@ -15,35 +15,26 @@ class JWTManager {
|
|
|
15
15
|
this.refreshSecret = config.refreshSecret;
|
|
16
16
|
this.accessExpiry = config.accessExpiry || '15m';
|
|
17
17
|
this.refreshExpiry = config.refreshExpiry || '7d';
|
|
18
|
-
this.cacheTTL = 5 * 60 * 1000; // 5 minutes
|
|
18
|
+
this.cacheTTL = 5 * 60 * 1000; // 5 minutes
|
|
19
19
|
if (config.enableCaching) {
|
|
20
20
|
this.cache = new js_extensions_1.LRUCache(config.maxCacheSize || 100);
|
|
21
21
|
}
|
|
22
22
|
}
|
|
23
|
-
/**
|
|
24
|
-
* Generate both access and refresh tokens
|
|
25
|
-
*/
|
|
23
|
+
/** Generate both access and refresh tokens */
|
|
26
24
|
async generateTokens(payload) {
|
|
27
25
|
try {
|
|
28
26
|
this.validatePayload(payload);
|
|
29
27
|
const accessToken = await this.generateAccessToken(payload);
|
|
30
28
|
const refreshToken = await this.generateRefreshToken(payload);
|
|
31
|
-
return {
|
|
32
|
-
accessToken,
|
|
33
|
-
refreshToken,
|
|
34
|
-
};
|
|
29
|
+
return { accessToken, refreshToken };
|
|
35
30
|
}
|
|
36
31
|
catch (error) {
|
|
37
|
-
if (error instanceof errors_utils_1.BadRequestError ||
|
|
38
|
-
error instanceof errors_utils_1.ValidationError) {
|
|
32
|
+
if (error instanceof errors_utils_1.BadRequestError || error instanceof errors_utils_1.ValidationError)
|
|
39
33
|
throw error;
|
|
40
|
-
}
|
|
41
|
-
throw new errors_utils_1.BadRequestError('Failed to generate tokens');
|
|
34
|
+
throw new errors_utils_1.BadRequestError({ reason: 'Failed to generate tokens' }, error instanceof Error ? error : undefined);
|
|
42
35
|
}
|
|
43
36
|
}
|
|
44
|
-
/**
|
|
45
|
-
* Generate access token
|
|
46
|
-
*/
|
|
37
|
+
/** Generate access token */
|
|
47
38
|
async generateAccessToken(payload) {
|
|
48
39
|
try {
|
|
49
40
|
this.validatePayload(payload);
|
|
@@ -53,16 +44,12 @@ class JWTManager {
|
|
|
53
44
|
return token;
|
|
54
45
|
}
|
|
55
46
|
catch (error) {
|
|
56
|
-
if (error instanceof errors_utils_1.BadRequestError ||
|
|
57
|
-
error instanceof errors_utils_1.ValidationError) {
|
|
47
|
+
if (error instanceof errors_utils_1.BadRequestError || error instanceof errors_utils_1.ValidationError)
|
|
58
48
|
throw error;
|
|
59
|
-
}
|
|
60
|
-
throw new errors_utils_1.BadRequestError('Failed to generate access token');
|
|
49
|
+
throw new errors_utils_1.BadRequestError({ reason: 'Failed to generate access token' }, error instanceof Error ? error : undefined);
|
|
61
50
|
}
|
|
62
51
|
}
|
|
63
|
-
/**
|
|
64
|
-
* Generate refresh token
|
|
65
|
-
*/
|
|
52
|
+
/** Generate refresh token */
|
|
66
53
|
async generateRefreshToken(payload) {
|
|
67
54
|
try {
|
|
68
55
|
this.validatePayload(payload);
|
|
@@ -72,232 +59,130 @@ class JWTManager {
|
|
|
72
59
|
return token;
|
|
73
60
|
}
|
|
74
61
|
catch (error) {
|
|
75
|
-
if (error instanceof errors_utils_1.BadRequestError ||
|
|
76
|
-
error instanceof errors_utils_1.ValidationError) {
|
|
62
|
+
if (error instanceof errors_utils_1.BadRequestError || error instanceof errors_utils_1.ValidationError)
|
|
77
63
|
throw error;
|
|
78
|
-
}
|
|
79
|
-
throw new errors_utils_1.BadRequestError('Failed to generate refresh token');
|
|
64
|
+
throw new errors_utils_1.BadRequestError({ reason: 'Failed to generate refresh token' }, error instanceof Error ? error : undefined);
|
|
80
65
|
}
|
|
81
66
|
}
|
|
82
|
-
/**
|
|
83
|
-
* Verify access token
|
|
84
|
-
*/
|
|
67
|
+
/** Verify access token */
|
|
85
68
|
async verifyAccessToken(token) {
|
|
86
|
-
|
|
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
|
-
}
|
|
69
|
+
return this.verifyTokenWithCache(token, this.accessSecret, 'access');
|
|
123
70
|
}
|
|
124
|
-
/**
|
|
125
|
-
* Verify refresh token
|
|
126
|
-
*/
|
|
71
|
+
/** Verify refresh token */
|
|
127
72
|
async verifyRefreshToken(token) {
|
|
128
|
-
|
|
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, {
|
|
145
|
-
valid: true,
|
|
146
|
-
payload: decoded,
|
|
147
|
-
timestamp: Date.now(),
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
return decoded;
|
|
151
|
-
}
|
|
152
|
-
catch (error) {
|
|
153
|
-
if (error instanceof errors_utils_1.ValidationError ||
|
|
154
|
-
error instanceof errors_utils_1.UnauthorizedError) {
|
|
155
|
-
throw error;
|
|
156
|
-
}
|
|
157
|
-
if (error instanceof Error && error.name === 'TokenExpiredError') {
|
|
158
|
-
throw new errors_utils_1.UnauthorizedError('Refresh token has expired');
|
|
159
|
-
}
|
|
160
|
-
if (error instanceof Error && error.name === 'JsonWebTokenError') {
|
|
161
|
-
throw new errors_utils_1.UnauthorizedError('Refresh token is invalid');
|
|
162
|
-
}
|
|
163
|
-
throw new errors_utils_1.UnauthorizedError('Failed to verify refresh token');
|
|
164
|
-
}
|
|
73
|
+
return this.verifyTokenWithCache(token, this.refreshSecret, 'refresh');
|
|
165
74
|
}
|
|
166
|
-
/**
|
|
167
|
-
* Decode token without verification
|
|
168
|
-
*/
|
|
75
|
+
/** Decode token without verification */
|
|
169
76
|
decodeToken(token, complete = false) {
|
|
170
|
-
|
|
171
|
-
if (!token || typeof token !== 'string') {
|
|
172
|
-
throw new errors_utils_1.ValidationError('Token must be a non-empty string');
|
|
173
|
-
}
|
|
174
|
-
return jsonwebtoken_1.default.decode(token, { complete });
|
|
175
|
-
}
|
|
176
|
-
catch (error) {
|
|
177
|
-
if (error instanceof errors_utils_1.ValidationError) {
|
|
178
|
-
throw error;
|
|
179
|
-
}
|
|
77
|
+
if (!token || typeof token !== 'string')
|
|
180
78
|
return null;
|
|
181
|
-
}
|
|
79
|
+
return jsonwebtoken_1.default.decode(token, { complete });
|
|
182
80
|
}
|
|
183
|
-
/**
|
|
184
|
-
* Extract token from Authorization header
|
|
185
|
-
*/
|
|
81
|
+
/** Extract token from Authorization header */
|
|
186
82
|
extractTokenFromHeader(authHeader) {
|
|
187
|
-
|
|
188
|
-
if (!authHeader || typeof authHeader !== 'string') {
|
|
189
|
-
return null;
|
|
190
|
-
}
|
|
191
|
-
const parts = authHeader.split(' ');
|
|
192
|
-
if (parts.length !== 2 || parts[0] !== 'Bearer') {
|
|
193
|
-
return null;
|
|
194
|
-
}
|
|
195
|
-
return parts[1];
|
|
196
|
-
}
|
|
197
|
-
catch {
|
|
83
|
+
if (!authHeader || typeof authHeader !== 'string')
|
|
198
84
|
return null;
|
|
199
|
-
|
|
85
|
+
const parts = authHeader.split(' ');
|
|
86
|
+
if (parts.length !== 2 || parts[0] !== 'Bearer')
|
|
87
|
+
return null;
|
|
88
|
+
return parts[1];
|
|
200
89
|
}
|
|
201
|
-
/**
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
validateToken(token, secret, _options = {}) {
|
|
205
|
-
try {
|
|
206
|
-
if (!token || typeof token !== 'string') {
|
|
207
|
-
return false;
|
|
208
|
-
}
|
|
209
|
-
const result = (0, verify_1.safeVerifyToken)(token, secret);
|
|
210
|
-
return result.valid;
|
|
211
|
-
}
|
|
212
|
-
catch {
|
|
90
|
+
/** Validate token without throwing exceptions */
|
|
91
|
+
validateToken(token, secret) {
|
|
92
|
+
if (!token || typeof token !== 'string')
|
|
213
93
|
return false;
|
|
214
|
-
|
|
94
|
+
return (0, verify_1.safeVerifyToken)(token, secret).valid;
|
|
215
95
|
}
|
|
216
|
-
/**
|
|
217
|
-
* Rotate refresh token
|
|
218
|
-
*/
|
|
96
|
+
/** Rotate refresh token */
|
|
219
97
|
async rotateRefreshToken(oldToken) {
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
}
|
|
224
|
-
const decoded = await this.verifyRefreshToken(oldToken);
|
|
225
|
-
if (typeof decoded === 'string') {
|
|
226
|
-
throw new errors_utils_1.ValidationError('Invalid token payload — expected JWT payload object');
|
|
227
|
-
}
|
|
228
|
-
// Create new payload without issued/expired timestamps
|
|
229
|
-
const payload = { ...decoded };
|
|
230
|
-
delete payload.iat;
|
|
231
|
-
delete payload.exp;
|
|
232
|
-
// Generate new refresh token
|
|
233
|
-
const newToken = (0, signToken_1.signToken)(payload, this.refreshSecret, this.refreshExpiry);
|
|
234
|
-
return newToken;
|
|
235
|
-
}
|
|
236
|
-
catch (error) {
|
|
237
|
-
if (error instanceof errors_utils_1.ValidationError ||
|
|
238
|
-
error instanceof errors_utils_1.UnauthorizedError) {
|
|
239
|
-
throw error;
|
|
240
|
-
}
|
|
241
|
-
throw new errors_utils_1.BadRequestError('Failed to rotate refresh token');
|
|
98
|
+
if (!oldToken || typeof oldToken !== 'string') {
|
|
99
|
+
throw new errors_utils_1.ValidationError({
|
|
100
|
+
reason: 'Old refresh token must be a non-empty string',
|
|
101
|
+
});
|
|
242
102
|
}
|
|
103
|
+
const decoded = await this.verifyRefreshToken(oldToken);
|
|
104
|
+
const payload = { ...decoded };
|
|
105
|
+
delete payload.iat;
|
|
106
|
+
delete payload.exp;
|
|
107
|
+
const newToken = (0, signToken_1.signToken)(payload, this.refreshSecret, this.refreshExpiry);
|
|
108
|
+
return newToken;
|
|
243
109
|
}
|
|
244
|
-
/**
|
|
245
|
-
* Check if token is expired
|
|
246
|
-
*/
|
|
110
|
+
/** Check if token is expired */
|
|
247
111
|
isTokenExpired(token) {
|
|
248
112
|
try {
|
|
249
113
|
const decoded = this.decodeToken(token);
|
|
250
|
-
if (!decoded || !decoded.exp)
|
|
114
|
+
if (!decoded || !decoded.exp)
|
|
251
115
|
return true;
|
|
252
|
-
|
|
253
|
-
const currentTime = Math.floor(Date.now() / 1000);
|
|
254
|
-
return decoded.exp < currentTime;
|
|
116
|
+
return decoded.exp < Math.floor(Date.now() / 1000);
|
|
255
117
|
}
|
|
256
118
|
catch {
|
|
257
119
|
return true;
|
|
258
120
|
}
|
|
259
121
|
}
|
|
260
|
-
/**
|
|
261
|
-
* Get token expiration date
|
|
262
|
-
*/
|
|
122
|
+
/** Get token expiration date */
|
|
263
123
|
getTokenExpiration(token) {
|
|
264
124
|
try {
|
|
265
125
|
const decoded = this.decodeToken(token);
|
|
266
|
-
if (!decoded || !decoded.exp)
|
|
126
|
+
if (!decoded || !decoded.exp)
|
|
267
127
|
return null;
|
|
268
|
-
}
|
|
269
128
|
return new Date(decoded.exp * 1000);
|
|
270
129
|
}
|
|
271
130
|
catch {
|
|
272
131
|
return null;
|
|
273
132
|
}
|
|
274
133
|
}
|
|
275
|
-
/**
|
|
276
|
-
* Clear token cache
|
|
277
|
-
*/
|
|
134
|
+
/** Clear token cache */
|
|
278
135
|
clearCache() {
|
|
279
136
|
this.cache?.clear();
|
|
280
137
|
}
|
|
281
|
-
/**
|
|
282
|
-
* Get cache statistics
|
|
283
|
-
*/
|
|
138
|
+
/** Get cache statistics */
|
|
284
139
|
getCacheStats() {
|
|
285
140
|
if (!this.cache)
|
|
286
141
|
return null;
|
|
287
|
-
|
|
288
|
-
return {
|
|
289
|
-
size: -1, // Size not available from LRUCache
|
|
290
|
-
maxSize: this.cache.maxSize,
|
|
291
|
-
};
|
|
142
|
+
return { size: -1, maxSize: this.cache.maxSize };
|
|
292
143
|
}
|
|
293
|
-
|
|
144
|
+
/** Private helper methods */
|
|
294
145
|
validatePayload(payload) {
|
|
295
146
|
if (!payload || typeof payload !== 'object') {
|
|
296
|
-
throw new errors_utils_1.ValidationError(
|
|
147
|
+
throw new errors_utils_1.ValidationError({
|
|
148
|
+
reason: 'Payload must be a non-null object',
|
|
149
|
+
});
|
|
297
150
|
}
|
|
298
151
|
if (Object.keys(payload).length === 0) {
|
|
299
|
-
throw new errors_utils_1.ValidationError('Payload cannot be empty');
|
|
152
|
+
throw new errors_utils_1.ValidationError({ reason: 'Payload cannot be empty' });
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
async verifyTokenWithCache(token, secret, type) {
|
|
156
|
+
if (!token || typeof token !== 'string') {
|
|
157
|
+
throw new errors_utils_1.ValidationError({
|
|
158
|
+
reason: `${type} token must be a non-empty string`,
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
const cacheKey = `${type}_${token}`;
|
|
162
|
+
if (this.cache) {
|
|
163
|
+
const cached = this.cache.get(cacheKey);
|
|
164
|
+
if (cached && Date.now() - cached.timestamp <= this.cacheTTL) {
|
|
165
|
+
if (!cached.valid)
|
|
166
|
+
throw new errors_utils_1.UnauthorizedError({
|
|
167
|
+
reason: `${type} token is invalid or expired`,
|
|
168
|
+
});
|
|
169
|
+
return cached.payload;
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
const { valid, payload, error } = (0, verify_1.safeVerifyToken)(token, secret);
|
|
173
|
+
if (!valid || !payload || typeof payload === 'string') {
|
|
174
|
+
this.cache?.set(cacheKey, {
|
|
175
|
+
valid: false,
|
|
176
|
+
payload: {},
|
|
177
|
+
timestamp: Date.now(),
|
|
178
|
+
});
|
|
179
|
+
throw new errors_utils_1.UnauthorizedError({
|
|
180
|
+
reason: `${type} token is invalid or expired`,
|
|
181
|
+
cause: error,
|
|
182
|
+
});
|
|
300
183
|
}
|
|
184
|
+
this.cache?.set(cacheKey, { valid: true, payload, timestamp: Date.now() });
|
|
185
|
+
return payload;
|
|
301
186
|
}
|
|
302
187
|
}
|
|
303
188
|
exports.JWTManager = JWTManager;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.parseDuration = parseDuration;
|
|
4
|
+
const errors_utils_1 = require("@naman_deep_singh/errors-utils");
|
|
4
5
|
const TIME_UNITS = {
|
|
5
6
|
s: 1,
|
|
6
7
|
m: 60,
|
|
@@ -18,12 +19,12 @@ function parseDuration(input) {
|
|
|
18
19
|
const value = Number.parseInt(match[1], 10);
|
|
19
20
|
const unit = match[2].toLowerCase();
|
|
20
21
|
if (!TIME_UNITS[unit]) {
|
|
21
|
-
throw new
|
|
22
|
+
throw new errors_utils_1.ValidationError({ reason: `Invalid time unit: ${unit}` });
|
|
22
23
|
}
|
|
23
24
|
totalSeconds += value * TIME_UNITS[unit];
|
|
24
25
|
}
|
|
25
26
|
if (totalSeconds === 0) {
|
|
26
|
-
throw new
|
|
27
|
+
throw new errors_utils_1.ValidationError({ reason: `Invalid expiry format: "${input}"` });
|
|
27
28
|
}
|
|
28
29
|
return totalSeconds;
|
|
29
30
|
}
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.signToken = void 0;
|
|
4
|
+
const errors_utils_1 = require("@naman_deep_singh/errors-utils");
|
|
4
5
|
const jsonwebtoken_1 = require("jsonwebtoken");
|
|
5
6
|
const parseDuration_1 = require("./parseDuration");
|
|
6
7
|
function getExpiryTimestamp(seconds) {
|
|
@@ -9,7 +10,7 @@ function getExpiryTimestamp(seconds) {
|
|
|
9
10
|
const signToken = (payload, secret, expiresIn = '1h', options = {}) => {
|
|
10
11
|
const seconds = (0, parseDuration_1.parseDuration)(expiresIn);
|
|
11
12
|
if (!seconds || seconds < 10) {
|
|
12
|
-
throw new
|
|
13
|
+
throw new errors_utils_1.ValidationError({ reason: 'Token expiry too small' });
|
|
13
14
|
}
|
|
14
15
|
const tokenPayload = {
|
|
15
16
|
...payload,
|
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
import type { JwtPayload } from '
|
|
1
|
+
import type { JwtPayload } from 'jsonwebtoken';
|
|
2
2
|
export interface TokenRequirements {
|
|
3
3
|
requiredFields?: string[];
|
|
4
4
|
forbiddenFields?: string[];
|
|
5
5
|
validateTypes?: Record<string, 'string' | 'number' | 'boolean'>;
|
|
6
6
|
}
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
7
|
+
/**
|
|
8
|
+
* Validates a JWT payload according to the provided rules.
|
|
9
|
+
* Throws ValidationError if validation fails.
|
|
10
|
+
*/
|
|
11
|
+
export declare function validateTokenPayload(payload: Record<string, unknown>, rules?: TokenRequirements): void;
|
|
12
|
+
/**
|
|
13
|
+
* Checks if a JWT payload is expired.
|
|
14
|
+
* Returns true if expired or missing 'exp'.
|
|
15
|
+
*/
|
|
13
16
|
export declare function isTokenExpired(payload: JwtPayload): boolean;
|