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