@naman_deep_singh/security 1.0.3 → 1.1.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.
Files changed (106) hide show
  1. package/README.md +329 -24
  2. package/dist/cjs/core/crypto/decrypt.d.ts +1 -0
  3. package/dist/cjs/core/crypto/decrypt.js +21 -0
  4. package/dist/cjs/core/crypto/encrypt.d.ts +1 -0
  5. package/dist/cjs/core/crypto/encrypt.js +16 -0
  6. package/dist/cjs/core/crypto/hmac.d.ts +8 -0
  7. package/dist/cjs/core/crypto/hmac.js +24 -0
  8. package/dist/cjs/core/crypto/index.d.ts +4 -0
  9. package/dist/cjs/core/crypto/index.js +20 -0
  10. package/dist/cjs/core/crypto/random.d.ts +8 -0
  11. package/dist/cjs/core/crypto/random.js +21 -0
  12. package/dist/cjs/core/jwt/decode.d.ts +12 -0
  13. package/dist/cjs/core/jwt/decode.js +25 -0
  14. package/dist/cjs/core/jwt/extractToken.d.ts +11 -0
  15. package/dist/cjs/core/jwt/extractToken.js +49 -0
  16. package/dist/cjs/core/jwt/generateTokens.d.ts +7 -0
  17. package/dist/cjs/core/jwt/generateTokens.js +23 -0
  18. package/dist/cjs/core/jwt/index.d.ts +7 -0
  19. package/dist/cjs/core/jwt/index.js +23 -0
  20. package/dist/cjs/core/jwt/parseDuration.d.ts +1 -0
  21. package/dist/cjs/core/jwt/parseDuration.js +29 -0
  22. package/dist/cjs/core/jwt/signToken.d.ts +2 -0
  23. package/dist/cjs/core/jwt/signToken.js +26 -0
  24. package/dist/cjs/core/jwt/validateToken.d.ts +13 -0
  25. package/dist/cjs/core/jwt/validateToken.js +37 -0
  26. package/dist/cjs/core/jwt/verify.d.ts +13 -0
  27. package/dist/cjs/core/jwt/verify.js +24 -0
  28. package/dist/cjs/core/password/hash.d.ts +10 -0
  29. package/dist/cjs/core/password/hash.js +45 -0
  30. package/dist/cjs/core/password/index.d.ts +3 -0
  31. package/dist/cjs/core/password/index.js +19 -0
  32. package/dist/cjs/core/password/strength.d.ts +2 -0
  33. package/dist/cjs/core/password/strength.js +21 -0
  34. package/dist/cjs/core/password/types.d.ts +7 -0
  35. package/dist/cjs/core/password/types.js +2 -0
  36. package/dist/cjs/core/password/utils.d.ts +4 -0
  37. package/dist/cjs/core/password/utils.js +38 -0
  38. package/dist/cjs/core/password/verify.d.ts +10 -0
  39. package/dist/cjs/core/password/verify.js +46 -0
  40. package/dist/cjs/index.d.ts +43 -0
  41. package/dist/cjs/index.js +56 -0
  42. package/dist/esm/core/crypto/decrypt.d.ts +1 -0
  43. package/dist/esm/core/crypto/decrypt.js +14 -0
  44. package/dist/esm/core/crypto/encrypt.d.ts +1 -0
  45. package/dist/esm/core/crypto/encrypt.js +9 -0
  46. package/dist/esm/core/crypto/hmac.d.ts +8 -0
  47. package/dist/esm/core/crypto/hmac.js +16 -0
  48. package/dist/esm/core/crypto/index.d.ts +4 -0
  49. package/dist/esm/core/crypto/index.js +4 -0
  50. package/dist/esm/core/crypto/random.d.ts +8 -0
  51. package/dist/esm/core/crypto/random.js +13 -0
  52. package/dist/esm/core/jwt/decode.d.ts +12 -0
  53. package/dist/esm/core/jwt/decode.js +21 -0
  54. package/dist/esm/core/jwt/extractToken.d.ts +11 -0
  55. package/dist/esm/core/jwt/extractToken.js +46 -0
  56. package/dist/esm/core/jwt/generateTokens.d.ts +7 -0
  57. package/dist/esm/core/jwt/generateTokens.js +18 -0
  58. package/dist/esm/core/jwt/index.d.ts +7 -0
  59. package/dist/esm/core/jwt/index.js +7 -0
  60. package/dist/esm/core/jwt/parseDuration.d.ts +1 -0
  61. package/dist/esm/core/jwt/parseDuration.js +26 -0
  62. package/dist/esm/core/jwt/signToken.d.ts +2 -0
  63. package/dist/esm/core/jwt/signToken.js +22 -0
  64. package/dist/esm/core/jwt/validateToken.d.ts +13 -0
  65. package/dist/esm/core/jwt/validateToken.js +33 -0
  66. package/dist/esm/core/jwt/verify.d.ts +13 -0
  67. package/dist/esm/core/jwt/verify.js +19 -0
  68. package/dist/esm/core/password/hash.d.ts +10 -0
  69. package/dist/esm/core/password/hash.js +35 -0
  70. package/dist/esm/core/password/index.d.ts +3 -0
  71. package/dist/esm/core/password/index.js +3 -0
  72. package/dist/esm/core/password/strength.d.ts +2 -0
  73. package/dist/esm/core/password/strength.js +17 -0
  74. package/dist/esm/core/password/types.d.ts +7 -0
  75. package/dist/esm/core/password/types.js +1 -0
  76. package/dist/esm/core/password/utils.d.ts +4 -0
  77. package/dist/esm/core/password/utils.js +29 -0
  78. package/dist/esm/core/password/verify.d.ts +10 -0
  79. package/dist/esm/core/password/verify.js +36 -0
  80. package/dist/esm/index.d.ts +43 -0
  81. package/dist/esm/index.js +13 -0
  82. package/dist/types/core/crypto/decrypt.d.ts +1 -0
  83. package/dist/types/core/crypto/encrypt.d.ts +1 -0
  84. package/dist/types/core/crypto/hmac.d.ts +8 -0
  85. package/dist/types/core/crypto/index.d.ts +4 -0
  86. package/dist/types/core/crypto/random.d.ts +8 -0
  87. package/dist/types/core/jwt/decode.d.ts +12 -0
  88. package/dist/types/core/jwt/extractToken.d.ts +11 -0
  89. package/dist/types/core/jwt/generateTokens.d.ts +7 -0
  90. package/dist/types/core/jwt/index.d.ts +7 -0
  91. package/dist/types/core/jwt/parseDuration.d.ts +1 -0
  92. package/dist/types/core/jwt/signToken.d.ts +2 -0
  93. package/dist/types/core/jwt/validateToken.d.ts +13 -0
  94. package/dist/types/core/jwt/verify.d.ts +13 -0
  95. package/dist/types/core/password/hash.d.ts +10 -0
  96. package/dist/types/core/password/index.d.ts +3 -0
  97. package/dist/types/core/password/strength.d.ts +2 -0
  98. package/dist/types/core/password/types.d.ts +7 -0
  99. package/dist/types/core/password/utils.d.ts +4 -0
  100. package/dist/types/core/password/verify.d.ts +10 -0
  101. package/dist/types/index.d.ts +43 -0
  102. package/package.json +34 -10
  103. package/dist/index.d.ts +0 -5
  104. package/dist/index.js +0 -27
  105. package/src/index.ts +0 -21
  106. package/tsconfig.json +0 -20
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.parseDuration = parseDuration;
4
+ const TIME_UNITS = {
5
+ s: 1,
6
+ m: 60,
7
+ h: 3600,
8
+ d: 86400,
9
+ w: 604800
10
+ };
11
+ function parseDuration(input) {
12
+ if (typeof input === "number")
13
+ return input;
14
+ const regex = /(\d+)\s*(s|m|h|d|w)/gi;
15
+ let totalSeconds = 0;
16
+ let match;
17
+ while ((match = regex.exec(input)) !== null) {
18
+ const value = parseInt(match[1], 10);
19
+ const unit = match[2].toLowerCase();
20
+ if (!TIME_UNITS[unit]) {
21
+ throw new Error(`Invalid time unit: ${unit}`);
22
+ }
23
+ totalSeconds += value * TIME_UNITS[unit];
24
+ }
25
+ if (totalSeconds === 0) {
26
+ throw new Error(`Invalid expiry format: "${input}"`);
27
+ }
28
+ return totalSeconds;
29
+ }
@@ -0,0 +1,2 @@
1
+ import { Secret, SignOptions } from "jsonwebtoken";
2
+ export declare const signToken: (payload: Record<string, any>, secret: Secret, expiresIn?: string | number, options?: SignOptions) => string;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.signToken = void 0;
4
+ const jsonwebtoken_1 = require("jsonwebtoken");
5
+ const parseDuration_1 = require("./parseDuration");
6
+ function getExpiryTimestamp(seconds) {
7
+ return Math.floor(Date.now() / 1000) + seconds;
8
+ }
9
+ const signToken = (payload, secret, expiresIn = "1h", options = {}) => {
10
+ const seconds = (0, parseDuration_1.parseDuration)(expiresIn);
11
+ if (!seconds || seconds < 10) {
12
+ throw new Error("Token expiry too small");
13
+ }
14
+ const tokenPayload = {
15
+ ...payload
16
+ };
17
+ if (!("exp" in payload))
18
+ tokenPayload.exp = getExpiryTimestamp(seconds);
19
+ if (!("iat" in payload))
20
+ tokenPayload.iat = Math.floor(Date.now() / 1000);
21
+ return (0, jsonwebtoken_1.sign)(tokenPayload, secret, {
22
+ algorithm: "HS256",
23
+ ...options
24
+ });
25
+ };
26
+ exports.signToken = signToken;
@@ -0,0 +1,13 @@
1
+ import { JwtPayload } from "node_modules/@types/jsonwebtoken";
2
+ export interface TokenRequirements {
3
+ requiredFields?: string[];
4
+ forbiddenFields?: string[];
5
+ validateTypes?: Record<string, "string" | "number" | "boolean">;
6
+ }
7
+ export declare function validateTokenPayload(payload: Record<string, any>, rules?: TokenRequirements): {
8
+ valid: true;
9
+ } | {
10
+ valid: false;
11
+ error: string;
12
+ };
13
+ export declare function isTokenExpired(payload: JwtPayload): boolean;
@@ -0,0 +1,37 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.validateTokenPayload = validateTokenPayload;
4
+ exports.isTokenExpired = isTokenExpired;
5
+ function validateTokenPayload(payload, rules = {
6
+ requiredFields: ["exp", "iat"]
7
+ }) {
8
+ const { requiredFields = [], forbiddenFields = [], validateTypes = {} } = rules;
9
+ // 1. Required fields
10
+ for (const field of requiredFields) {
11
+ if (!(field in payload)) {
12
+ return { valid: false, error: `Missing required field: ${field}` };
13
+ }
14
+ }
15
+ // 2. Forbidden fields
16
+ for (const field of forbiddenFields) {
17
+ if (field in payload) {
18
+ return { valid: false, error: `Forbidden field in token: ${field}` };
19
+ }
20
+ }
21
+ // 3. Type validation
22
+ for (const key in validateTypes) {
23
+ const expectedType = validateTypes[key];
24
+ if (key in payload && typeof payload[key] !== expectedType) {
25
+ return {
26
+ valid: false,
27
+ error: `Invalid type for ${key}. Expected ${expectedType}.`
28
+ };
29
+ }
30
+ }
31
+ return { valid: true };
32
+ }
33
+ function isTokenExpired(payload) {
34
+ if (!payload.exp)
35
+ return true;
36
+ return Date.now() >= payload.exp * 1000;
37
+ }
@@ -0,0 +1,13 @@
1
+ import { Secret, JwtPayload } from "jsonwebtoken";
2
+ /**
3
+ * Verify token (throws if invalid or expired)
4
+ */
5
+ export declare const verifyToken: (token: string, secret: Secret) => string | JwtPayload;
6
+ /**
7
+ * Safe verify — never throws, returns { valid, payload?, error? }
8
+ */
9
+ export declare const safeVerifyToken: (token: string, secret: Secret) => {
10
+ valid: boolean;
11
+ payload?: string | JwtPayload;
12
+ error?: unknown;
13
+ };
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.safeVerifyToken = exports.verifyToken = void 0;
4
+ const jsonwebtoken_1 = require("jsonwebtoken");
5
+ /**
6
+ * Verify token (throws if invalid or expired)
7
+ */
8
+ const verifyToken = (token, secret) => {
9
+ return (0, jsonwebtoken_1.verify)(token, secret);
10
+ };
11
+ exports.verifyToken = verifyToken;
12
+ /**
13
+ * Safe verify — never throws, returns { valid, payload?, error? }
14
+ */
15
+ const safeVerifyToken = (token, secret) => {
16
+ try {
17
+ const decoded = (0, jsonwebtoken_1.verify)(token, secret);
18
+ return { valid: true, payload: decoded };
19
+ }
20
+ catch (error) {
21
+ return { valid: false, error };
22
+ }
23
+ };
24
+ exports.safeVerifyToken = safeVerifyToken;
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Hash a password asynchronously using bcrypt.
3
+ */
4
+ export declare const hashPassword: (password: string, saltRounds?: number) => Promise<string>;
5
+ export declare function hashPasswordWithPepper(password: string, pepper: string): Promise<string>;
6
+ /**
7
+ * Hash a password synchronously using bcrypt.
8
+ */
9
+ export declare const hashPasswordSync: (password: string, saltRounds?: number) => string;
10
+ export declare function hashPasswordWithPepperSync(password: string, pepper: string): string;
@@ -0,0 +1,45 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.hashPasswordSync = exports.hashPassword = void 0;
7
+ exports.hashPasswordWithPepper = hashPasswordWithPepper;
8
+ exports.hashPasswordWithPepperSync = hashPasswordWithPepperSync;
9
+ const bcryptjs_1 = __importDefault(require("bcryptjs"));
10
+ const utils_1 = require("./utils");
11
+ const errors_utils_1 = require("@naman_deep_singh/errors-utils");
12
+ /**
13
+ * Hash a password asynchronously using bcrypt.
14
+ */
15
+ const hashPassword = async (password, saltRounds = 10) => {
16
+ try {
17
+ (0, utils_1.ensureValidPassword)(password);
18
+ const salt = await bcryptjs_1.default.genSalt(saltRounds);
19
+ return bcryptjs_1.default.hash(password, salt);
20
+ }
21
+ catch (err) {
22
+ throw new errors_utils_1.InternalServerError('Password hashing failed');
23
+ }
24
+ };
25
+ exports.hashPassword = hashPassword;
26
+ function hashPasswordWithPepper(password, pepper) {
27
+ return (0, exports.hashPassword)(password + pepper);
28
+ }
29
+ /**
30
+ * Hash a password synchronously using bcrypt.
31
+ */
32
+ const hashPasswordSync = (password, saltRounds = 10) => {
33
+ try {
34
+ (0, utils_1.ensureValidPassword)(password);
35
+ const salt = bcryptjs_1.default.genSaltSync(saltRounds);
36
+ return bcryptjs_1.default.hashSync(password, salt);
37
+ }
38
+ catch (error) {
39
+ throw new errors_utils_1.InternalServerError('Password hashing failed');
40
+ }
41
+ };
42
+ exports.hashPasswordSync = hashPasswordSync;
43
+ function hashPasswordWithPepperSync(password, pepper) {
44
+ return (0, exports.hashPasswordSync)(password + pepper);
45
+ }
@@ -0,0 +1,3 @@
1
+ export * from "./hash";
2
+ export * from "./strength";
3
+ export * from "./verify";
@@ -0,0 +1,19 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./hash"), exports);
18
+ __exportStar(require("./strength"), exports);
19
+ __exportStar(require("./verify"), exports);
@@ -0,0 +1,2 @@
1
+ import { PasswordStrengthOptions } from "./types";
2
+ export declare const isPasswordStrong: (password: string, options?: PasswordStrengthOptions) => boolean;
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.isPasswordStrong = void 0;
4
+ const errors_utils_1 = require("@naman_deep_singh/errors-utils");
5
+ const isPasswordStrong = (password, options = {}) => {
6
+ if (!password)
7
+ throw new errors_utils_1.BadRequestError('Invalid password provided');
8
+ const { minLength = 8, requireUppercase = true, requireLowercase = true, requireNumbers = true, requireSymbols = false, } = options;
9
+ if (password.length < minLength)
10
+ throw new errors_utils_1.ValidationError(`Password must be at least ${minLength} characters`);
11
+ if (requireUppercase && !/[A-Z]/.test(password))
12
+ throw new errors_utils_1.ValidationError("Password must include uppercase letters");
13
+ if (requireLowercase && !/[a-z]/.test(password))
14
+ throw new errors_utils_1.ValidationError("Password must include lowercase letters");
15
+ if (requireNumbers && !/[0-9]/.test(password))
16
+ throw new errors_utils_1.ValidationError("Password must include numbers");
17
+ if (requireSymbols && !/[^A-Za-z0-9]/.test(password))
18
+ throw new errors_utils_1.ValidationError("Password must include symbols");
19
+ return true;
20
+ };
21
+ exports.isPasswordStrong = isPasswordStrong;
@@ -0,0 +1,7 @@
1
+ export interface PasswordStrengthOptions {
2
+ minLength?: number;
3
+ requireUppercase?: boolean;
4
+ requireLowercase?: boolean;
5
+ requireNumbers?: boolean;
6
+ requireSymbols?: boolean;
7
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -0,0 +1,4 @@
1
+ export declare function ensureValidPassword(password: string): void;
2
+ export declare function safeCompare(a: string, b: string): boolean;
3
+ export declare function estimatePasswordEntropy(password: string): number;
4
+ export declare function normalizePassword(password: string): string;
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.ensureValidPassword = ensureValidPassword;
7
+ exports.safeCompare = safeCompare;
8
+ exports.estimatePasswordEntropy = estimatePasswordEntropy;
9
+ exports.normalizePassword = normalizePassword;
10
+ const crypto_1 = __importDefault(require("crypto"));
11
+ const errors_utils_1 = require("@naman_deep_singh/errors-utils");
12
+ function ensureValidPassword(password) {
13
+ if (!password || typeof password !== "string") {
14
+ throw new errors_utils_1.BadRequestError('Invalid password provided');
15
+ }
16
+ }
17
+ function safeCompare(a, b) {
18
+ const bufA = Buffer.from(a);
19
+ const bufB = Buffer.from(b);
20
+ if (bufA.length !== bufB.length)
21
+ return false;
22
+ return crypto_1.default.timingSafeEqual(bufA, bufB);
23
+ }
24
+ function estimatePasswordEntropy(password) {
25
+ let pool = 0;
26
+ if (/[a-z]/.test(password))
27
+ pool += 26;
28
+ if (/[A-Z]/.test(password))
29
+ pool += 26;
30
+ if (/[0-9]/.test(password))
31
+ pool += 10;
32
+ if (/[^A-Za-z0-9]/.test(password))
33
+ pool += 32;
34
+ return password.length * Math.log2(pool);
35
+ }
36
+ function normalizePassword(password) {
37
+ return password.normalize("NFKC");
38
+ }
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Compare a password with a stored hash asynchronously.
3
+ */
4
+ export declare const verifyPassword: (password: string, hash: string) => Promise<boolean>;
5
+ export declare function verifyPasswordWithPepper(password: string, pepper: string, hash: string): Promise<boolean>;
6
+ /**
7
+ * Compare a password with a stored hash synchronously.
8
+ */
9
+ export declare const verifyPasswordSync: (password: string, hash: string) => boolean;
10
+ export declare function verifyPasswordWithPepperSync(password: string, pepper: string, hash: string): Promise<boolean>;
@@ -0,0 +1,46 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.verifyPasswordSync = exports.verifyPassword = void 0;
7
+ exports.verifyPasswordWithPepper = verifyPasswordWithPepper;
8
+ exports.verifyPasswordWithPepperSync = verifyPasswordWithPepperSync;
9
+ const bcryptjs_1 = __importDefault(require("bcryptjs"));
10
+ const errors_utils_1 = require("@naman_deep_singh/errors-utils");
11
+ /**
12
+ * Compare a password with a stored hash asynchronously.
13
+ */
14
+ const verifyPassword = async (password, hash) => {
15
+ try {
16
+ const result = await bcryptjs_1.default.compare(password, hash);
17
+ if (!result)
18
+ throw new errors_utils_1.UnauthorizedError('Password verification failed');
19
+ return result;
20
+ }
21
+ catch {
22
+ throw new errors_utils_1.UnauthorizedError('Password verification failed');
23
+ }
24
+ };
25
+ exports.verifyPassword = verifyPassword;
26
+ async function verifyPasswordWithPepper(password, pepper, hash) {
27
+ return (0, exports.verifyPassword)(password + pepper, hash);
28
+ }
29
+ /**
30
+ * Compare a password with a stored hash synchronously.
31
+ */
32
+ const verifyPasswordSync = (password, hash) => {
33
+ try {
34
+ const result = bcryptjs_1.default.compareSync(password, hash);
35
+ if (!result)
36
+ throw new errors_utils_1.UnauthorizedError('Password verification failed');
37
+ return result;
38
+ }
39
+ catch (error) {
40
+ throw new errors_utils_1.UnauthorizedError('Password verification failed');
41
+ }
42
+ };
43
+ exports.verifyPasswordSync = verifyPasswordSync;
44
+ async function verifyPasswordWithPepperSync(password, pepper, hash) {
45
+ return (0, exports.verifyPasswordSync)(password + pepper, hash);
46
+ }
@@ -0,0 +1,43 @@
1
+ export * from "./core/password";
2
+ export * from "./core/jwt";
3
+ export * from "./core/crypto";
4
+ export { BadRequestError, UnauthorizedError, ValidationError, InternalServerError } from "@naman_deep_singh/errors-utils";
5
+ import * as JWTUtils from "./core/jwt";
6
+ declare const _default: {
7
+ decrypt: (data: string, secret: string) => string;
8
+ encrypt: (text: string, secret: string) => string;
9
+ hmacSign: (message: string, secret: string) => string;
10
+ hmacVerify: (message: string, secret: string, signature: string) => boolean;
11
+ randomToken: (length?: number) => string;
12
+ generateStrongPassword: (length?: number) => string;
13
+ decodeToken(token: string): null | string | import("node_modules/@types/jsonwebtoken").JwtPayload;
14
+ decodeTokenStrict(token: string): import("node_modules/@types/jsonwebtoken").JwtPayload;
15
+ extractToken(sources: JWTUtils.TokenSources): string | null;
16
+ rotateRefreshToken(oldToken: string, secret: import("node_modules/@types/jsonwebtoken").Secret): string;
17
+ generateTokens: (payload: object, accessSecret: import("node_modules/@types/jsonwebtoken").Secret, refreshSecret: import("node_modules/@types/jsonwebtoken").Secret, accessExpiry?: string | number, refreshExpiry?: string | number) => JWTUtils.TokenPair;
18
+ parseDuration(input: string | number): number;
19
+ signToken: (payload: Record<string, any>, secret: import("node_modules/@types/jsonwebtoken").Secret, expiresIn?: string | number, options?: import("node_modules/@types/jsonwebtoken").SignOptions) => string;
20
+ validateTokenPayload(payload: Record<string, any>, rules?: JWTUtils.TokenRequirements): {
21
+ valid: true;
22
+ } | {
23
+ valid: false;
24
+ error: string;
25
+ };
26
+ isTokenExpired(payload: import("node_modules/@types/jsonwebtoken").JwtPayload): boolean;
27
+ verifyToken: (token: string, secret: import("node_modules/@types/jsonwebtoken").Secret) => string | import("node_modules/@types/jsonwebtoken").JwtPayload;
28
+ safeVerifyToken: (token: string, secret: import("node_modules/@types/jsonwebtoken").Secret) => {
29
+ valid: boolean;
30
+ payload?: string | import("node_modules/@types/jsonwebtoken").JwtPayload;
31
+ error?: unknown;
32
+ };
33
+ hashPasswordWithPepper(password: string, pepper: string): Promise<string>;
34
+ hashPasswordWithPepperSync(password: string, pepper: string): string;
35
+ hashPassword: (password: string, saltRounds?: number) => Promise<string>;
36
+ hashPasswordSync: (password: string, saltRounds?: number) => string;
37
+ isPasswordStrong: (password: string, options?: import("./core/password/types").PasswordStrengthOptions) => boolean;
38
+ verifyPasswordWithPepper(password: string, pepper: string, hash: string): Promise<boolean>;
39
+ verifyPasswordWithPepperSync(password: string, pepper: string, hash: string): Promise<boolean>;
40
+ verifyPassword: (password: string, hash: string) => Promise<boolean>;
41
+ verifyPasswordSync: (password: string, hash: string) => boolean;
42
+ };
43
+ export default _default;
@@ -0,0 +1,56 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
19
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
20
+ };
21
+ var __importStar = (this && this.__importStar) || (function () {
22
+ var ownKeys = function(o) {
23
+ ownKeys = Object.getOwnPropertyNames || function (o) {
24
+ var ar = [];
25
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
26
+ return ar;
27
+ };
28
+ return ownKeys(o);
29
+ };
30
+ return function (mod) {
31
+ if (mod && mod.__esModule) return mod;
32
+ var result = {};
33
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
34
+ __setModuleDefault(result, mod);
35
+ return result;
36
+ };
37
+ })();
38
+ Object.defineProperty(exports, "__esModule", { value: true });
39
+ exports.InternalServerError = exports.ValidationError = exports.UnauthorizedError = exports.BadRequestError = void 0;
40
+ __exportStar(require("./core/password"), exports);
41
+ __exportStar(require("./core/jwt"), exports);
42
+ __exportStar(require("./core/crypto"), exports);
43
+ // Re-export common errors for convenience
44
+ var errors_utils_1 = require("@naman_deep_singh/errors-utils");
45
+ Object.defineProperty(exports, "BadRequestError", { enumerable: true, get: function () { return errors_utils_1.BadRequestError; } });
46
+ Object.defineProperty(exports, "UnauthorizedError", { enumerable: true, get: function () { return errors_utils_1.UnauthorizedError; } });
47
+ Object.defineProperty(exports, "ValidationError", { enumerable: true, get: function () { return errors_utils_1.ValidationError; } });
48
+ Object.defineProperty(exports, "InternalServerError", { enumerable: true, get: function () { return errors_utils_1.InternalServerError; } });
49
+ const PasswordUtils = __importStar(require("./core/password"));
50
+ const JWTUtils = __importStar(require("./core/jwt"));
51
+ const CryptoUtils = __importStar(require("./core/crypto"));
52
+ exports.default = {
53
+ ...PasswordUtils,
54
+ ...JWTUtils,
55
+ ...CryptoUtils,
56
+ };
@@ -0,0 +1 @@
1
+ export declare const decrypt: (data: string, secret: string) => string;
@@ -0,0 +1,14 @@
1
+ import crypto from "crypto";
2
+ const ALGO = "AES-256-GCM";
3
+ export const decrypt = (data, secret) => {
4
+ const [ivHex, encryptedHex] = data.split(":");
5
+ const iv = Buffer.from(ivHex, "hex");
6
+ const encrypted = Buffer.from(encryptedHex, "hex");
7
+ const key = crypto.createHash("sha256").update(secret).digest();
8
+ const decipher = crypto.createDecipheriv(ALGO, key, iv);
9
+ const decrypted = Buffer.concat([
10
+ decipher.update(encrypted),
11
+ decipher.final(),
12
+ ]);
13
+ return decrypted.toString("utf8");
14
+ };
@@ -0,0 +1 @@
1
+ export declare const encrypt: (text: string, secret: string) => string;
@@ -0,0 +1,9 @@
1
+ import crypto from "crypto";
2
+ const ALGO = "AES-256-GCM";
3
+ export const encrypt = (text, secret) => {
4
+ const key = crypto.createHash("sha256").update(secret).digest();
5
+ const iv = crypto.randomBytes(16);
6
+ const cipher = crypto.createCipheriv(ALGO, key, iv);
7
+ const encrypted = Buffer.concat([cipher.update(text, "utf8"), cipher.final()]);
8
+ return `${iv.toString("hex")}:${encrypted.toString("hex")}`;
9
+ };
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Sign message using HMAC SHA-256
3
+ */
4
+ export declare const hmacSign: (message: string, secret: string) => string;
5
+ /**
6
+ * Verify HMAC signature
7
+ */
8
+ export declare const hmacVerify: (message: string, secret: string, signature: string) => boolean;
@@ -0,0 +1,16 @@
1
+ import crypto from "crypto";
2
+ /**
3
+ * Sign message using HMAC SHA-256
4
+ */
5
+ export const hmacSign = (message, secret) => {
6
+ return crypto.createHmac("sha256", secret).update(message).digest("hex");
7
+ };
8
+ /**
9
+ * Verify HMAC signature
10
+ */
11
+ export const hmacVerify = (message, secret, signature) => {
12
+ const expected = hmacSign(message, secret);
13
+ if (signature.length !== expected.length)
14
+ return false;
15
+ return crypto.timingSafeEqual(Buffer.from(signature), Buffer.from(expected));
16
+ };
@@ -0,0 +1,4 @@
1
+ export * from "./decrypt";
2
+ export * from "./encrypt";
3
+ export * from "./hmac";
4
+ export * from "./random";
@@ -0,0 +1,4 @@
1
+ export * from "./decrypt";
2
+ export * from "./encrypt";
3
+ export * from "./hmac";
4
+ export * from "./random";
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Generate cryptographically secure random string
3
+ */
4
+ export declare const randomToken: (length?: number) => string;
5
+ /**
6
+ * Generate a strong random password
7
+ */
8
+ export declare const generateStrongPassword: (length?: number) => string;
@@ -0,0 +1,13 @@
1
+ import crypto from "crypto";
2
+ /**
3
+ * Generate cryptographically secure random string
4
+ */
5
+ export const randomToken = (length = 32) => {
6
+ return crypto.randomBytes(length).toString("hex");
7
+ };
8
+ /**
9
+ * Generate a strong random password
10
+ */
11
+ export const generateStrongPassword = (length = 16) => {
12
+ return crypto.randomBytes(length).toString("hex").slice(0, length);
13
+ };
@@ -0,0 +1,12 @@
1
+ import { JwtPayload } from "jsonwebtoken";
2
+ /**
3
+ * Flexible decode
4
+ * Returns: null | string | JwtPayload
5
+ * Mirrors jsonwebtoken.decode()
6
+ */
7
+ export declare function decodeToken(token: string): null | string | JwtPayload;
8
+ /**
9
+ * Strict decode
10
+ * Always returns JwtPayload or throws error
11
+ */
12
+ export declare function decodeTokenStrict(token: string): JwtPayload;
@@ -0,0 +1,21 @@
1
+ // src/jwt/decodeToken.ts
2
+ import { decode } from "jsonwebtoken";
3
+ /**
4
+ * Flexible decode
5
+ * Returns: null | string | JwtPayload
6
+ * Mirrors jsonwebtoken.decode()
7
+ */
8
+ export function decodeToken(token) {
9
+ return decode(token);
10
+ }
11
+ /**
12
+ * Strict decode
13
+ * Always returns JwtPayload or throws error
14
+ */
15
+ export function decodeTokenStrict(token) {
16
+ const decoded = decode(token);
17
+ if (!decoded || typeof decoded === "string") {
18
+ throw new Error("Invalid JWT payload structure");
19
+ }
20
+ return decoded;
21
+ }
@@ -0,0 +1,11 @@
1
+ export interface TokenSources {
2
+ header?: string | undefined | null;
3
+ cookies?: Record<string, string> | undefined;
4
+ query?: Record<string, string | undefined> | undefined;
5
+ body?: Record<string, any> | undefined;
6
+ wsMessage?: string | Record<string, any> | undefined;
7
+ }
8
+ /**
9
+ * Universal token extractor
10
+ */
11
+ export declare function extractToken(sources: TokenSources): string | null;