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
|
@@ -2,34 +2,43 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.validateTokenPayload = validateTokenPayload;
|
|
4
4
|
exports.isTokenExpired = isTokenExpired;
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
const errors_utils_1 = require("@naman_deep_singh/errors-utils");
|
|
6
|
+
/**
|
|
7
|
+
* Validates a JWT payload according to the provided rules.
|
|
8
|
+
* Throws ValidationError if validation fails.
|
|
9
|
+
*/
|
|
10
|
+
function validateTokenPayload(payload, rules = { requiredFields: ['exp', 'iat'] }) {
|
|
8
11
|
const { requiredFields = [], forbiddenFields = [], validateTypes = {}, } = rules;
|
|
9
12
|
// 1. Required fields
|
|
10
13
|
for (const field of requiredFields) {
|
|
11
14
|
if (!(field in payload)) {
|
|
12
|
-
|
|
15
|
+
throw new errors_utils_1.ValidationError({
|
|
16
|
+
reason: `Missing required field: ${field}`,
|
|
17
|
+
});
|
|
13
18
|
}
|
|
14
19
|
}
|
|
15
20
|
// 2. Forbidden fields
|
|
16
21
|
for (const field of forbiddenFields) {
|
|
17
22
|
if (field in payload) {
|
|
18
|
-
|
|
23
|
+
throw new errors_utils_1.ValidationError({
|
|
24
|
+
reason: `Forbidden field in token: ${field}`,
|
|
25
|
+
});
|
|
19
26
|
}
|
|
20
27
|
}
|
|
21
28
|
// 3. Type validation
|
|
22
29
|
for (const key in validateTypes) {
|
|
23
30
|
const expectedType = validateTypes[key];
|
|
24
31
|
if (key in payload && typeof payload[key] !== expectedType) {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
};
|
|
32
|
+
throw new errors_utils_1.ValidationError({
|
|
33
|
+
reason: `Invalid type for ${key}. Expected ${expectedType}, got ${typeof payload[key]}`,
|
|
34
|
+
});
|
|
29
35
|
}
|
|
30
36
|
}
|
|
31
|
-
return { valid: true };
|
|
32
37
|
}
|
|
38
|
+
/**
|
|
39
|
+
* Checks if a JWT payload is expired.
|
|
40
|
+
* Returns true if expired or missing 'exp'.
|
|
41
|
+
*/
|
|
33
42
|
function isTokenExpired(payload) {
|
|
34
43
|
if (!payload.exp)
|
|
35
44
|
return true;
|
|
@@ -1,19 +1,18 @@
|
|
|
1
|
-
import type
|
|
2
|
-
import { type JwtPayload, type Secret } from 'jsonwebtoken';
|
|
1
|
+
import { type JwtPayload, type Secret, type VerifyOptions } from 'jsonwebtoken';
|
|
3
2
|
import type { VerificationResult } from './types';
|
|
4
3
|
/**
|
|
5
|
-
* Verify token (throws if invalid or expired)
|
|
4
|
+
* Verify token (throws UnauthorizedError if invalid or expired)
|
|
6
5
|
*/
|
|
7
6
|
export declare const verifyToken: (token: string, secret: Secret) => string | JwtPayload;
|
|
8
7
|
/**
|
|
9
|
-
*
|
|
8
|
+
* Verify token with options
|
|
10
9
|
*/
|
|
11
|
-
export declare const
|
|
10
|
+
export declare const verifyTokenWithOptions: (token: string, secret: Secret, options?: VerifyOptions) => string | JwtPayload;
|
|
12
11
|
/**
|
|
13
|
-
*
|
|
12
|
+
* Safe verify — never throws, returns structured result with UnauthorizedError on failure
|
|
14
13
|
*/
|
|
15
|
-
export declare const
|
|
14
|
+
export declare const safeVerifyToken: (token: string, secret: Secret) => VerificationResult;
|
|
16
15
|
/**
|
|
17
|
-
* Safe verify with
|
|
16
|
+
* Safe verify with options — never throws, returns structured result with UnauthorizedError on failure
|
|
18
17
|
*/
|
|
19
|
-
export declare const safeVerifyTokenWithOptions: (token: string, secret: Secret, options?:
|
|
18
|
+
export declare const safeVerifyTokenWithOptions: (token: string, secret: Secret, options?: VerifyOptions) => VerificationResult;
|
|
@@ -1,16 +1,46 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.safeVerifyTokenWithOptions = exports.
|
|
3
|
+
exports.safeVerifyTokenWithOptions = exports.safeVerifyToken = exports.verifyTokenWithOptions = exports.verifyToken = void 0;
|
|
4
|
+
const errors_utils_1 = require("@naman_deep_singh/errors-utils");
|
|
4
5
|
const jsonwebtoken_1 = require("jsonwebtoken");
|
|
5
6
|
/**
|
|
6
|
-
* Verify token (throws if invalid or expired)
|
|
7
|
+
* Verify token (throws UnauthorizedError if invalid or expired)
|
|
7
8
|
*/
|
|
8
9
|
const verifyToken = (token, secret) => {
|
|
9
|
-
|
|
10
|
+
try {
|
|
11
|
+
return (0, jsonwebtoken_1.verify)(token, secret);
|
|
12
|
+
}
|
|
13
|
+
catch (error) {
|
|
14
|
+
if (error.name === 'TokenExpiredError') {
|
|
15
|
+
throw new errors_utils_1.UnauthorizedError({ reason: 'Token has expired' }, error);
|
|
16
|
+
}
|
|
17
|
+
if (error.name === 'JsonWebTokenError') {
|
|
18
|
+
throw new errors_utils_1.UnauthorizedError({ reason: 'Invalid token' }, error);
|
|
19
|
+
}
|
|
20
|
+
throw new errors_utils_1.UnauthorizedError({ reason: 'Failed to verify token' }, error);
|
|
21
|
+
}
|
|
10
22
|
};
|
|
11
23
|
exports.verifyToken = verifyToken;
|
|
12
24
|
/**
|
|
13
|
-
*
|
|
25
|
+
* Verify token with options
|
|
26
|
+
*/
|
|
27
|
+
const verifyTokenWithOptions = (token, secret, options = {}) => {
|
|
28
|
+
try {
|
|
29
|
+
return (0, jsonwebtoken_1.verify)(token, secret, options);
|
|
30
|
+
}
|
|
31
|
+
catch (error) {
|
|
32
|
+
if (error.name === 'TokenExpiredError') {
|
|
33
|
+
throw new errors_utils_1.UnauthorizedError({ reason: 'Token has expired' }, error);
|
|
34
|
+
}
|
|
35
|
+
if (error.name === 'JsonWebTokenError') {
|
|
36
|
+
throw new errors_utils_1.UnauthorizedError({ reason: 'Invalid token' }, error);
|
|
37
|
+
}
|
|
38
|
+
throw new errors_utils_1.UnauthorizedError({ reason: 'Failed to verify token' }, error);
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
exports.verifyTokenWithOptions = verifyTokenWithOptions;
|
|
42
|
+
/**
|
|
43
|
+
* Safe verify — never throws, returns structured result with UnauthorizedError on failure
|
|
14
44
|
*/
|
|
15
45
|
const safeVerifyToken = (token, secret) => {
|
|
16
46
|
try {
|
|
@@ -18,19 +48,22 @@ const safeVerifyToken = (token, secret) => {
|
|
|
18
48
|
return { valid: true, payload: decoded };
|
|
19
49
|
}
|
|
20
50
|
catch (error) {
|
|
21
|
-
|
|
51
|
+
let wrappedError;
|
|
52
|
+
if (error.name === 'TokenExpiredError') {
|
|
53
|
+
wrappedError = new errors_utils_1.UnauthorizedError({ reason: 'Token has expired' }, error);
|
|
54
|
+
}
|
|
55
|
+
else if (error.name === 'JsonWebTokenError') {
|
|
56
|
+
wrappedError = new errors_utils_1.UnauthorizedError({ reason: 'Invalid token' }, error);
|
|
57
|
+
}
|
|
58
|
+
else {
|
|
59
|
+
wrappedError = new errors_utils_1.UnauthorizedError({ reason: 'Failed to verify token' }, error);
|
|
60
|
+
}
|
|
61
|
+
return { valid: false, error: wrappedError };
|
|
22
62
|
}
|
|
23
63
|
};
|
|
24
64
|
exports.safeVerifyToken = safeVerifyToken;
|
|
25
65
|
/**
|
|
26
|
-
*
|
|
27
|
-
*/
|
|
28
|
-
const verifyTokenWithOptions = (token, secret, options = {}) => {
|
|
29
|
-
return (0, jsonwebtoken_1.verify)(token, secret, options);
|
|
30
|
-
};
|
|
31
|
-
exports.verifyTokenWithOptions = verifyTokenWithOptions;
|
|
32
|
-
/**
|
|
33
|
-
* Safe verify with validation options
|
|
66
|
+
* Safe verify with options — never throws, returns structured result with UnauthorizedError on failure
|
|
34
67
|
*/
|
|
35
68
|
const safeVerifyTokenWithOptions = (token, secret, options = {}) => {
|
|
36
69
|
try {
|
|
@@ -38,7 +71,19 @@ const safeVerifyTokenWithOptions = (token, secret, options = {}) => {
|
|
|
38
71
|
return { valid: true, payload: decoded };
|
|
39
72
|
}
|
|
40
73
|
catch (error) {
|
|
41
|
-
|
|
74
|
+
let wrappedError;
|
|
75
|
+
if (error.name === 'TokenExpiredError') {
|
|
76
|
+
wrappedError = new errors_utils_1.UnauthorizedError({ reason: 'Token has expired' }, error instanceof Error ? error : undefined);
|
|
77
|
+
}
|
|
78
|
+
else if (error.name === 'JsonWebTokenError') {
|
|
79
|
+
wrappedError = new errors_utils_1.UnauthorizedError({
|
|
80
|
+
reason: 'Invalid token',
|
|
81
|
+
}, error instanceof Error ? error : undefined);
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
wrappedError = new errors_utils_1.UnauthorizedError({ reason: 'Failed to verify token' }, error instanceof Error ? error : undefined);
|
|
85
|
+
}
|
|
86
|
+
return { valid: false, error: wrappedError };
|
|
42
87
|
}
|
|
43
88
|
};
|
|
44
89
|
exports.safeVerifyTokenWithOptions = safeVerifyTokenWithOptions;
|
|
@@ -18,8 +18,8 @@ const hashPassword = async (password, saltRounds = 10) => {
|
|
|
18
18
|
const salt = await bcryptjs_1.default.genSalt(saltRounds);
|
|
19
19
|
return bcryptjs_1.default.hash(password, salt);
|
|
20
20
|
}
|
|
21
|
-
catch (
|
|
22
|
-
throw new errors_utils_1.InternalServerError('Password hashing failed');
|
|
21
|
+
catch (error) {
|
|
22
|
+
throw new errors_utils_1.InternalServerError({ reason: 'Password hashing failed' }, error instanceof Error ? error : undefined);
|
|
23
23
|
}
|
|
24
24
|
};
|
|
25
25
|
exports.hashPassword = hashPassword;
|
|
@@ -35,8 +35,8 @@ const hashPasswordSync = (password, saltRounds = 10) => {
|
|
|
35
35
|
const salt = bcryptjs_1.default.genSaltSync(saltRounds);
|
|
36
36
|
return bcryptjs_1.default.hashSync(password, salt);
|
|
37
37
|
}
|
|
38
|
-
catch (
|
|
39
|
-
throw new errors_utils_1.InternalServerError('Password hashing failed');
|
|
38
|
+
catch (error) {
|
|
39
|
+
throw new errors_utils_1.InternalServerError({ reason: 'Password hashing failed' }, error instanceof Error ? error : undefined);
|
|
40
40
|
}
|
|
41
41
|
};
|
|
42
42
|
exports.hashPasswordSync = hashPasswordSync;
|
|
@@ -23,7 +23,7 @@ export declare class PasswordManager implements IPasswordManager {
|
|
|
23
23
|
*/
|
|
24
24
|
checkStrength(password: string): PasswordStrength;
|
|
25
25
|
/**
|
|
26
|
-
* Check if password hash needs upgrade (
|
|
26
|
+
* Check if password hash needs upgrade (saltRounds change)
|
|
27
27
|
*/
|
|
28
28
|
needsUpgrade(_hash: string, _currentConfig: PasswordConfig): boolean;
|
|
29
29
|
}
|
|
@@ -27,25 +27,21 @@ class PasswordManager {
|
|
|
27
27
|
async hash(password, salt) {
|
|
28
28
|
try {
|
|
29
29
|
(0, utils_1.ensureValidPassword)(password);
|
|
30
|
-
// Validate password meets basic requirements
|
|
31
30
|
this.validate(password);
|
|
32
31
|
const saltRounds = this.defaultConfig.saltRounds;
|
|
33
|
-
let
|
|
34
|
-
if (!
|
|
35
|
-
|
|
32
|
+
let finalSalt = salt;
|
|
33
|
+
if (!finalSalt) {
|
|
34
|
+
finalSalt = await bcryptjs_1.default.genSalt(saltRounds);
|
|
36
35
|
}
|
|
37
|
-
const hash = await bcryptjs_1.default.hash(password,
|
|
38
|
-
return {
|
|
39
|
-
hash,
|
|
40
|
-
salt: passwordSalt,
|
|
41
|
-
};
|
|
36
|
+
const hash = await bcryptjs_1.default.hash(password, finalSalt);
|
|
37
|
+
return { hash, salt: finalSalt };
|
|
42
38
|
}
|
|
43
39
|
catch (error) {
|
|
44
40
|
if (error instanceof errors_utils_1.BadRequestError ||
|
|
45
41
|
error instanceof errors_utils_1.ValidationError) {
|
|
46
42
|
throw error;
|
|
47
43
|
}
|
|
48
|
-
throw new errors_utils_1.BadRequestError('Failed to hash password');
|
|
44
|
+
throw new errors_utils_1.BadRequestError({ reason: 'Failed to hash password' }, error instanceof Error ? error : undefined);
|
|
49
45
|
}
|
|
50
46
|
}
|
|
51
47
|
/**
|
|
@@ -53,19 +49,12 @@ class PasswordManager {
|
|
|
53
49
|
*/
|
|
54
50
|
async verify(password, hash, salt) {
|
|
55
51
|
try {
|
|
56
|
-
if (!password || !hash || !salt)
|
|
52
|
+
if (!password || !hash || !salt)
|
|
57
53
|
return false;
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
const isValid = await bcryptjs_1.default.compare(password, hash);
|
|
61
|
-
// If invalid and different salt was used, try regenerating hash with new salt
|
|
62
|
-
if (!isValid && salt !== this.defaultConfig.saltRounds?.toString()) {
|
|
63
|
-
const newHash = await bcryptjs_1.default.hash(password, salt);
|
|
64
|
-
return newHash === hash;
|
|
65
|
-
}
|
|
66
|
-
return isValid;
|
|
54
|
+
// bcrypt compare works directly with hash
|
|
55
|
+
return await bcryptjs_1.default.compare(password, hash);
|
|
67
56
|
}
|
|
68
|
-
catch
|
|
57
|
+
catch {
|
|
69
58
|
return false;
|
|
70
59
|
}
|
|
71
60
|
}
|
|
@@ -75,7 +64,9 @@ class PasswordManager {
|
|
|
75
64
|
generate(length = 16, options = {}) {
|
|
76
65
|
const config = { ...this.defaultConfig, ...options };
|
|
77
66
|
if (length < config.minLength || length > config.maxLength) {
|
|
78
|
-
throw new errors_utils_1.ValidationError(
|
|
67
|
+
throw new errors_utils_1.ValidationError({
|
|
68
|
+
reason: `Password length must be between ${config.minLength} and ${config.maxLength}`,
|
|
69
|
+
});
|
|
79
70
|
}
|
|
80
71
|
let charset = 'abcdefghijklmnopqrstuvwxyz';
|
|
81
72
|
if (config.requireUppercase)
|
|
@@ -84,24 +75,20 @@ class PasswordManager {
|
|
|
84
75
|
charset += '0123456789';
|
|
85
76
|
if (config.requireSpecialChars)
|
|
86
77
|
charset += '!@#$%^&*()_+-=[]{}|;:,.<>?';
|
|
87
|
-
let password = '';
|
|
88
78
|
const randomBytes = crypto_1.default.randomBytes(length);
|
|
79
|
+
let password = '';
|
|
89
80
|
for (let i = 0; i < length; i++) {
|
|
90
81
|
password += charset[randomBytes[i] % charset.length];
|
|
91
82
|
}
|
|
92
|
-
// Ensure
|
|
93
|
-
if (config.requireUppercase && !/[A-Z]/.test(password))
|
|
83
|
+
// Ensure requirements
|
|
84
|
+
if (config.requireUppercase && !/[A-Z]/.test(password))
|
|
94
85
|
password = password.replace(/[a-z]/, 'A');
|
|
95
|
-
|
|
96
|
-
if (config.requireLowercase && !/[a-z]/.test(password)) {
|
|
86
|
+
if (config.requireLowercase && !/[a-z]/.test(password))
|
|
97
87
|
password = password.replace(/[A-Z]/, 'a');
|
|
98
|
-
|
|
99
|
-
if (config.requireNumbers && !/[0-9]/.test(password)) {
|
|
88
|
+
if (config.requireNumbers && !/[0-9]/.test(password))
|
|
100
89
|
password = password.replace(/[A-Za-z]/, '0');
|
|
101
|
-
|
|
102
|
-
if (config.requireSpecialChars && !/[^A-Za-z0-9]/.test(password)) {
|
|
90
|
+
if (config.requireSpecialChars && !/[^A-Za-z0-9]/.test(password))
|
|
103
91
|
password = password.replace(/[A-Za-z0-9]/, '!');
|
|
104
|
-
}
|
|
105
92
|
return password;
|
|
106
93
|
}
|
|
107
94
|
/**
|
|
@@ -110,44 +97,30 @@ class PasswordManager {
|
|
|
110
97
|
validate(password, config = {}) {
|
|
111
98
|
const finalConfig = { ...this.defaultConfig, ...config };
|
|
112
99
|
const errors = [];
|
|
113
|
-
|
|
114
|
-
if (!password || typeof password !== 'string') {
|
|
100
|
+
if (!password || typeof password !== 'string')
|
|
115
101
|
errors.push('Password must be a non-empty string');
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
if (password.length
|
|
119
|
-
errors.push(`Password must be at least ${finalConfig.minLength} characters long`);
|
|
120
|
-
}
|
|
121
|
-
if (password.length > finalConfig.maxLength) {
|
|
102
|
+
if (password.length < finalConfig.minLength)
|
|
103
|
+
errors.push(`Password must be at least ${finalConfig.minLength} characters`);
|
|
104
|
+
if (password.length > finalConfig.maxLength)
|
|
122
105
|
errors.push(`Password must not exceed ${finalConfig.maxLength} characters`);
|
|
123
|
-
|
|
124
|
-
// Complexity requirements
|
|
125
|
-
if (finalConfig.requireUppercase && !/[A-Z]/.test(password)) {
|
|
106
|
+
if (finalConfig.requireUppercase && !/[A-Z]/.test(password))
|
|
126
107
|
errors.push('Password must contain at least one uppercase letter');
|
|
127
|
-
|
|
128
|
-
if (finalConfig.requireLowercase && !/[a-z]/.test(password)) {
|
|
108
|
+
if (finalConfig.requireLowercase && !/[a-z]/.test(password))
|
|
129
109
|
errors.push('Password must contain at least one lowercase letter');
|
|
130
|
-
|
|
131
|
-
if (finalConfig.requireNumbers && !/[0-9]/.test(password)) {
|
|
110
|
+
if (finalConfig.requireNumbers && !/[0-9]/.test(password))
|
|
132
111
|
errors.push('Password must contain at least one number');
|
|
133
|
-
|
|
134
|
-
if (finalConfig.requireSpecialChars && !/[^A-Za-z0-9]/.test(password)) {
|
|
112
|
+
if (finalConfig.requireSpecialChars && !/[^A-Za-z0-9]/.test(password))
|
|
135
113
|
errors.push('Password must contain at least one special character');
|
|
136
|
-
}
|
|
137
|
-
// Custom rules
|
|
138
114
|
if (finalConfig.customRules) {
|
|
139
115
|
finalConfig.customRules.forEach((rule) => {
|
|
140
|
-
if (!rule.test(password))
|
|
116
|
+
if (!rule.test(password))
|
|
141
117
|
errors.push(rule.message);
|
|
142
|
-
}
|
|
143
118
|
});
|
|
144
119
|
}
|
|
145
|
-
const strength = this.checkStrength(password);
|
|
146
|
-
const isValid = errors.length === 0;
|
|
147
120
|
return {
|
|
148
|
-
isValid,
|
|
121
|
+
isValid: errors.length === 0,
|
|
149
122
|
errors,
|
|
150
|
-
strength,
|
|
123
|
+
strength: this.checkStrength(password),
|
|
151
124
|
};
|
|
152
125
|
}
|
|
153
126
|
/**
|
|
@@ -158,26 +131,20 @@ class PasswordManager {
|
|
|
158
131
|
let score = 0;
|
|
159
132
|
const feedback = [];
|
|
160
133
|
const suggestions = [];
|
|
161
|
-
/* ---------------- Entropy baseline ---------------- */
|
|
162
134
|
if (entropy < 28) {
|
|
163
135
|
feedback.push('Password is easy to guess');
|
|
164
136
|
suggestions.push('Use more unique characters and length');
|
|
165
137
|
}
|
|
166
|
-
else if (entropy < 36)
|
|
167
|
-
score
|
|
168
|
-
|
|
169
|
-
else if (entropy < 60) {
|
|
138
|
+
else if (entropy < 36)
|
|
139
|
+
score++;
|
|
140
|
+
else if (entropy < 60)
|
|
170
141
|
score += 2;
|
|
171
|
-
|
|
172
|
-
else {
|
|
142
|
+
else
|
|
173
143
|
score += 3;
|
|
174
|
-
}
|
|
175
|
-
/* ---------------- Length scoring ---------------- */
|
|
176
144
|
if (password.length >= 12)
|
|
177
145
|
score++;
|
|
178
146
|
if (password.length >= 16)
|
|
179
147
|
score++;
|
|
180
|
-
/* ---------------- Character variety ---------------- */
|
|
181
148
|
if (/[a-z]/.test(password))
|
|
182
149
|
score++;
|
|
183
150
|
if (/[A-Z]/.test(password))
|
|
@@ -186,7 +153,6 @@ class PasswordManager {
|
|
|
186
153
|
score++;
|
|
187
154
|
if (/[^A-Za-z0-9]/.test(password))
|
|
188
155
|
score++;
|
|
189
|
-
/* ---------------- Pattern deductions ---------------- */
|
|
190
156
|
if (/^[A-Za-z]+$/.test(password)) {
|
|
191
157
|
score--;
|
|
192
158
|
feedback.push('Consider adding numbers or symbols');
|
|
@@ -203,15 +169,12 @@ class PasswordManager {
|
|
|
203
169
|
score--;
|
|
204
170
|
feedback.push('Avoid sequential patterns');
|
|
205
171
|
}
|
|
206
|
-
/* ---------------- Common passwords ---------------- */
|
|
207
172
|
const commonPasswords = ['password', '123456', 'qwerty', 'admin', 'letmein'];
|
|
208
173
|
if (commonPasswords.some((common) => password.toLowerCase().includes(common))) {
|
|
209
174
|
score = 0;
|
|
210
175
|
feedback.push('Avoid common passwords');
|
|
211
176
|
}
|
|
212
|
-
/* ---------------- Clamp score ---------------- */
|
|
213
177
|
score = Math.max(0, Math.min(4, score));
|
|
214
|
-
/* ---------------- Strength label ---------------- */
|
|
215
178
|
let label;
|
|
216
179
|
switch (score) {
|
|
217
180
|
case 0:
|
|
@@ -237,19 +200,12 @@ class PasswordManager {
|
|
|
237
200
|
default:
|
|
238
201
|
label = 'very-weak';
|
|
239
202
|
}
|
|
240
|
-
return {
|
|
241
|
-
score,
|
|
242
|
-
label,
|
|
243
|
-
feedback,
|
|
244
|
-
suggestions,
|
|
245
|
-
};
|
|
203
|
+
return { score, label, feedback, suggestions };
|
|
246
204
|
}
|
|
247
205
|
/**
|
|
248
|
-
* Check if password hash needs upgrade (
|
|
206
|
+
* Check if password hash needs upgrade (saltRounds change)
|
|
249
207
|
*/
|
|
250
208
|
needsUpgrade(_hash, _currentConfig) {
|
|
251
|
-
// Simple heuristic: if the hash doesn't match current salt rounds pattern
|
|
252
|
-
// In practice, you'd need to store the salt rounds with the hash
|
|
253
209
|
return false;
|
|
254
210
|
}
|
|
255
211
|
}
|
|
@@ -4,18 +4,24 @@ exports.isPasswordStrong = void 0;
|
|
|
4
4
|
const errors_utils_1 = require("@naman_deep_singh/errors-utils");
|
|
5
5
|
const isPasswordStrong = (password, options = {}) => {
|
|
6
6
|
if (!password)
|
|
7
|
-
throw new errors_utils_1.BadRequestError('Invalid password provided');
|
|
7
|
+
throw new errors_utils_1.BadRequestError({ reason: 'Invalid password provided' });
|
|
8
8
|
const { minLength = 8, requireUppercase = true, requireLowercase = true, requireNumbers = true, requireSymbols = false, } = options;
|
|
9
9
|
if (password.length < minLength)
|
|
10
|
-
throw new errors_utils_1.ValidationError(
|
|
10
|
+
throw new errors_utils_1.ValidationError({
|
|
11
|
+
reason: `Password must be at least ${minLength} characters long`,
|
|
12
|
+
});
|
|
11
13
|
if (requireUppercase && !/[A-Z]/.test(password))
|
|
12
|
-
throw new errors_utils_1.ValidationError(
|
|
14
|
+
throw new errors_utils_1.ValidationError({
|
|
15
|
+
reason: 'Password must include uppercase letters',
|
|
16
|
+
});
|
|
13
17
|
if (requireLowercase && !/[a-z]/.test(password))
|
|
14
|
-
throw new errors_utils_1.ValidationError(
|
|
18
|
+
throw new errors_utils_1.ValidationError({
|
|
19
|
+
reason: 'Password must include lowercase letters',
|
|
20
|
+
});
|
|
15
21
|
if (requireNumbers && !/[0-9]/.test(password))
|
|
16
|
-
throw new errors_utils_1.ValidationError('Password must include numbers');
|
|
22
|
+
throw new errors_utils_1.ValidationError({ reason: 'Password must include numbers' });
|
|
17
23
|
if (requireSymbols && !/[^A-Za-z0-9]/.test(password))
|
|
18
|
-
throw new errors_utils_1.ValidationError('Password must include symbols');
|
|
24
|
+
throw new errors_utils_1.ValidationError({ reason: 'Password must include symbols' });
|
|
19
25
|
return true;
|
|
20
26
|
};
|
|
21
27
|
exports.isPasswordStrong = isPasswordStrong;
|
|
@@ -1,4 +1,16 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Ensure password is a valid non-empty string
|
|
3
|
+
*/
|
|
1
4
|
export declare function ensureValidPassword(password: string): void;
|
|
5
|
+
/**
|
|
6
|
+
* Timing-safe comparison between two strings
|
|
7
|
+
*/
|
|
2
8
|
export declare function safeCompare(a: string, b: string): boolean;
|
|
9
|
+
/**
|
|
10
|
+
* Estimate password entropy based on character pool
|
|
11
|
+
*/
|
|
3
12
|
export declare function estimatePasswordEntropy(password: string): number;
|
|
13
|
+
/**
|
|
14
|
+
* Normalize password string to a consistent form
|
|
15
|
+
*/
|
|
4
16
|
export declare function normalizePassword(password: string): string;
|
|
@@ -9,11 +9,17 @@ exports.estimatePasswordEntropy = estimatePasswordEntropy;
|
|
|
9
9
|
exports.normalizePassword = normalizePassword;
|
|
10
10
|
const crypto_1 = __importDefault(require("crypto"));
|
|
11
11
|
const errors_utils_1 = require("@naman_deep_singh/errors-utils");
|
|
12
|
+
/**
|
|
13
|
+
* Ensure password is a valid non-empty string
|
|
14
|
+
*/
|
|
12
15
|
function ensureValidPassword(password) {
|
|
13
16
|
if (!password || typeof password !== 'string') {
|
|
14
|
-
throw new errors_utils_1.BadRequestError('Invalid password provided');
|
|
17
|
+
throw new errors_utils_1.BadRequestError({ reason: 'Invalid password provided' });
|
|
15
18
|
}
|
|
16
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* Timing-safe comparison between two strings
|
|
22
|
+
*/
|
|
17
23
|
function safeCompare(a, b) {
|
|
18
24
|
const bufA = Buffer.from(a);
|
|
19
25
|
const bufB = Buffer.from(b);
|
|
@@ -21,6 +27,9 @@ function safeCompare(a, b) {
|
|
|
21
27
|
return false;
|
|
22
28
|
return crypto_1.default.timingSafeEqual(bufA, bufB);
|
|
23
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Estimate password entropy based on character pool
|
|
32
|
+
*/
|
|
24
33
|
function estimatePasswordEntropy(password) {
|
|
25
34
|
let pool = 0;
|
|
26
35
|
if (/[a-z]/.test(password))
|
|
@@ -31,8 +40,14 @@ function estimatePasswordEntropy(password) {
|
|
|
31
40
|
pool += 10;
|
|
32
41
|
if (/[^A-Za-z0-9]/.test(password))
|
|
33
42
|
pool += 32;
|
|
43
|
+
// If no characters matched, fallback to 1 to avoid log2(0)
|
|
44
|
+
if (pool === 0)
|
|
45
|
+
pool = 1;
|
|
34
46
|
return password.length * Math.log2(pool);
|
|
35
47
|
}
|
|
48
|
+
/**
|
|
49
|
+
* Normalize password string to a consistent form
|
|
50
|
+
*/
|
|
36
51
|
function normalizePassword(password) {
|
|
37
52
|
return password.normalize('NFKC');
|
|
38
53
|
}
|
|
@@ -15,11 +15,11 @@ const verifyPassword = async (password, hash) => {
|
|
|
15
15
|
try {
|
|
16
16
|
const result = await bcryptjs_1.default.compare(password, hash);
|
|
17
17
|
if (!result)
|
|
18
|
-
throw new errors_utils_1.UnauthorizedError('Password verification failed');
|
|
18
|
+
throw new errors_utils_1.UnauthorizedError({ reason: 'Password verification failed' });
|
|
19
19
|
return result;
|
|
20
20
|
}
|
|
21
21
|
catch {
|
|
22
|
-
throw new errors_utils_1.UnauthorizedError('Password verification failed');
|
|
22
|
+
throw new errors_utils_1.UnauthorizedError({ reason: 'Password verification failed' });
|
|
23
23
|
}
|
|
24
24
|
};
|
|
25
25
|
exports.verifyPassword = verifyPassword;
|
|
@@ -33,11 +33,11 @@ const verifyPasswordSync = (password, hash) => {
|
|
|
33
33
|
try {
|
|
34
34
|
const result = bcryptjs_1.default.compareSync(password, hash);
|
|
35
35
|
if (!result)
|
|
36
|
-
throw new errors_utils_1.UnauthorizedError('Password verification failed');
|
|
36
|
+
throw new errors_utils_1.UnauthorizedError({ reason: 'Password verification failed' });
|
|
37
37
|
return result;
|
|
38
38
|
}
|
|
39
39
|
catch (_error) {
|
|
40
|
-
throw new errors_utils_1.UnauthorizedError('Password verification failed');
|
|
40
|
+
throw new errors_utils_1.UnauthorizedError({ reason: 'Password verification failed' });
|
|
41
41
|
}
|
|
42
42
|
};
|
|
43
43
|
exports.verifyPasswordSync = verifyPasswordSync;
|
|
@@ -21,16 +21,11 @@ declare const _default: {
|
|
|
21
21
|
generateTokens: (payload: Record<string, unknown>, accessSecret: import("node_modules/@types/jsonwebtoken").Secret, refreshSecret: import("node_modules/@types/jsonwebtoken").Secret, accessExpiry?: string | number, refreshExpiry?: string | number) => JWTUtils.TokenPair;
|
|
22
22
|
parseDuration(input: string | number): number;
|
|
23
23
|
signToken: (payload: Record<string, unknown>, secret: import("node_modules/@types/jsonwebtoken").Secret, expiresIn?: string | number, options?: import("node_modules/@types/jsonwebtoken").SignOptions) => string;
|
|
24
|
-
validateTokenPayload(payload: Record<string, unknown>, rules?: JWTUtils.TokenRequirements):
|
|
25
|
-
valid: true;
|
|
26
|
-
} | {
|
|
27
|
-
valid: false;
|
|
28
|
-
error: string;
|
|
29
|
-
};
|
|
24
|
+
validateTokenPayload(payload: Record<string, unknown>, rules?: JWTUtils.TokenRequirements): void;
|
|
30
25
|
isTokenExpired(payload: import("node_modules/@types/jsonwebtoken").JwtPayload): boolean;
|
|
31
26
|
verifyToken: (token: string, secret: import("node_modules/@types/jsonwebtoken").Secret) => string | import("node_modules/@types/jsonwebtoken").JwtPayload;
|
|
32
|
-
safeVerifyToken: (token: string, secret: import("node_modules/@types/jsonwebtoken").Secret) => JWTUtils.VerificationResult;
|
|
33
27
|
verifyTokenWithOptions: (token: string, secret: import("node_modules/@types/jsonwebtoken").Secret, options?: import("node_modules/@types/jsonwebtoken").VerifyOptions) => string | import("node_modules/@types/jsonwebtoken").JwtPayload;
|
|
28
|
+
safeVerifyToken: (token: string, secret: import("node_modules/@types/jsonwebtoken").Secret) => JWTUtils.VerificationResult;
|
|
34
29
|
safeVerifyTokenWithOptions: (token: string, secret: import("node_modules/@types/jsonwebtoken").Secret, options?: import("node_modules/@types/jsonwebtoken").VerifyOptions) => JWTUtils.VerificationResult;
|
|
35
30
|
hashPasswordWithPepper(password: string, pepper: string): Promise<string>;
|
|
36
31
|
hashPasswordWithPepperSync(password: string, pepper: string): string;
|