@naman_deep_singh/security 1.0.4 → 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.
- package/README.md +311 -80
- package/dist/cjs/core/crypto/decrypt.d.ts +1 -0
- package/dist/cjs/core/crypto/decrypt.js +21 -0
- package/dist/cjs/core/crypto/encrypt.d.ts +1 -0
- package/dist/cjs/core/crypto/encrypt.js +16 -0
- package/dist/cjs/core/crypto/hmac.d.ts +8 -0
- package/dist/cjs/core/crypto/hmac.js +24 -0
- package/dist/cjs/core/crypto/index.d.ts +4 -0
- package/dist/cjs/core/crypto/index.js +20 -0
- package/dist/cjs/core/crypto/random.d.ts +8 -0
- package/dist/cjs/core/crypto/random.js +21 -0
- package/dist/cjs/core/jwt/decode.d.ts +12 -0
- package/dist/cjs/core/jwt/decode.js +25 -0
- package/dist/cjs/core/jwt/extractToken.d.ts +11 -0
- package/dist/cjs/core/jwt/extractToken.js +49 -0
- package/dist/cjs/core/jwt/generateTokens.d.ts +7 -0
- package/dist/cjs/core/jwt/generateTokens.js +23 -0
- package/dist/cjs/core/jwt/index.d.ts +7 -0
- package/dist/cjs/core/jwt/index.js +23 -0
- package/dist/cjs/core/jwt/parseDuration.d.ts +1 -0
- package/dist/cjs/core/jwt/parseDuration.js +29 -0
- package/dist/cjs/core/jwt/signToken.d.ts +2 -0
- package/dist/cjs/core/jwt/signToken.js +26 -0
- package/dist/cjs/core/jwt/validateToken.d.ts +13 -0
- package/dist/cjs/core/jwt/validateToken.js +37 -0
- package/dist/cjs/core/jwt/verify.d.ts +13 -0
- package/dist/cjs/core/jwt/verify.js +24 -0
- package/dist/cjs/core/password/hash.d.ts +10 -0
- package/dist/cjs/core/password/hash.js +45 -0
- package/dist/cjs/core/password/index.d.ts +3 -0
- package/dist/cjs/core/password/index.js +19 -0
- package/dist/cjs/core/password/strength.d.ts +2 -0
- package/dist/cjs/core/password/strength.js +21 -0
- package/dist/cjs/core/password/types.d.ts +7 -0
- package/dist/cjs/core/password/types.js +2 -0
- package/dist/cjs/core/password/utils.d.ts +4 -0
- package/dist/cjs/core/password/utils.js +38 -0
- package/dist/cjs/core/password/verify.d.ts +10 -0
- package/dist/cjs/core/password/verify.js +46 -0
- package/dist/cjs/index.d.ts +43 -0
- package/dist/cjs/index.js +56 -0
- package/dist/esm/core/crypto/decrypt.d.ts +1 -0
- package/dist/esm/core/crypto/decrypt.js +14 -0
- package/dist/esm/core/crypto/encrypt.d.ts +1 -0
- package/dist/esm/core/crypto/encrypt.js +9 -0
- package/dist/esm/core/crypto/hmac.d.ts +8 -0
- package/dist/esm/core/crypto/hmac.js +16 -0
- package/dist/esm/core/crypto/index.d.ts +4 -0
- package/dist/esm/core/crypto/index.js +4 -0
- package/dist/esm/core/crypto/random.d.ts +8 -0
- package/dist/esm/core/crypto/random.js +13 -0
- package/dist/esm/core/jwt/decode.d.ts +12 -0
- package/dist/esm/core/jwt/decode.js +21 -0
- package/dist/esm/core/jwt/extractToken.d.ts +11 -0
- package/dist/esm/core/jwt/extractToken.js +46 -0
- package/dist/esm/core/jwt/generateTokens.d.ts +7 -0
- package/dist/esm/core/jwt/generateTokens.js +18 -0
- package/dist/esm/core/jwt/index.d.ts +7 -0
- package/dist/esm/core/jwt/index.js +7 -0
- package/dist/esm/core/jwt/parseDuration.d.ts +1 -0
- package/dist/esm/core/jwt/parseDuration.js +26 -0
- package/dist/esm/core/jwt/signToken.d.ts +2 -0
- package/dist/esm/core/jwt/signToken.js +22 -0
- package/dist/esm/core/jwt/validateToken.d.ts +13 -0
- package/dist/esm/core/jwt/validateToken.js +33 -0
- package/dist/esm/core/jwt/verify.d.ts +13 -0
- package/dist/esm/core/jwt/verify.js +19 -0
- package/dist/esm/core/password/hash.d.ts +10 -0
- package/dist/esm/core/password/hash.js +35 -0
- package/dist/esm/core/password/index.d.ts +3 -0
- package/dist/esm/core/password/index.js +3 -0
- package/dist/esm/core/password/strength.d.ts +2 -0
- package/dist/esm/core/password/strength.js +17 -0
- package/dist/esm/core/password/types.d.ts +7 -0
- package/dist/esm/core/password/types.js +1 -0
- package/dist/esm/core/password/utils.d.ts +4 -0
- package/dist/esm/core/password/utils.js +29 -0
- package/dist/esm/core/password/verify.d.ts +10 -0
- package/dist/esm/core/password/verify.js +36 -0
- package/dist/esm/index.d.ts +43 -0
- package/dist/esm/index.js +13 -0
- package/dist/types/core/crypto/decrypt.d.ts +1 -0
- package/dist/types/core/crypto/encrypt.d.ts +1 -0
- package/dist/types/core/crypto/hmac.d.ts +8 -0
- package/dist/types/core/crypto/index.d.ts +4 -0
- package/dist/types/core/crypto/random.d.ts +8 -0
- package/dist/types/core/jwt/decode.d.ts +12 -0
- package/dist/types/core/jwt/extractToken.d.ts +11 -0
- package/dist/types/core/jwt/generateTokens.d.ts +7 -0
- package/dist/types/core/jwt/index.d.ts +7 -0
- package/dist/types/core/jwt/parseDuration.d.ts +1 -0
- package/dist/types/core/jwt/signToken.d.ts +2 -0
- package/dist/types/core/jwt/validateToken.d.ts +13 -0
- package/dist/types/core/jwt/verify.d.ts +13 -0
- package/dist/types/core/password/hash.d.ts +10 -0
- package/dist/types/core/password/index.d.ts +3 -0
- package/dist/types/core/password/strength.d.ts +2 -0
- package/dist/types/core/password/types.d.ts +7 -0
- package/dist/types/core/password/utils.d.ts +4 -0
- package/dist/types/core/password/verify.d.ts +10 -0
- package/dist/types/index.d.ts +43 -0
- package/package.json +23 -7
- package/dist/index.d.ts +0 -16
- package/dist/index.js +0 -41
- package/src/index.ts +0 -39
- package/tsconfig.json +0 -21
|
@@ -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,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,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;
|
package/package.json
CHANGED
|
@@ -1,12 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naman_deep_singh/security",
|
|
3
|
-
"version": "1.0
|
|
3
|
+
"version": "1.1.0",
|
|
4
4
|
"description": "Security utilities for password hashing and JWT token management with TypeScript",
|
|
5
|
-
"
|
|
6
|
-
"
|
|
7
|
-
"
|
|
8
|
-
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./dist/cjs/index.js",
|
|
7
|
+
"module": "./dist/esm/index.js",
|
|
8
|
+
"exports": {
|
|
9
|
+
".": {
|
|
10
|
+
"import": "./dist/esm/index.js",
|
|
11
|
+
"require": "./dist/cjs/index.js",
|
|
12
|
+
"types": "./dist/types/index.d.ts"
|
|
13
|
+
}
|
|
9
14
|
},
|
|
15
|
+
"sideEffects": false,
|
|
16
|
+
"files": [
|
|
17
|
+
"dist",
|
|
18
|
+
"README.md"
|
|
19
|
+
],
|
|
10
20
|
"keywords": [
|
|
11
21
|
"security",
|
|
12
22
|
"jwt",
|
|
@@ -17,14 +27,20 @@
|
|
|
17
27
|
],
|
|
18
28
|
"author": "Naman Deep Singh",
|
|
19
29
|
"license": "ISC",
|
|
20
|
-
"packageManager": "pnpm@10.20.0",
|
|
21
30
|
"dependencies": {
|
|
31
|
+
"@naman_deep_singh/errors-utils": "^1.0.0",
|
|
22
32
|
"bcryptjs": "^3.0.3",
|
|
23
33
|
"jsonwebtoken": "^9.0.2"
|
|
24
34
|
},
|
|
25
35
|
"devDependencies": {
|
|
26
36
|
"@types/bcryptjs": "^2.4.6",
|
|
27
37
|
"@types/jsonwebtoken": "^9.0.10",
|
|
28
|
-
"typescript": "^5.9.3"
|
|
38
|
+
"typescript": "^5.9.3",
|
|
39
|
+
"rimraf": "^5.0.5"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "pnpm run build:types && tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json",
|
|
43
|
+
"build:types": "tsc -p tsconfig.base.json --emitDeclarationOnly --outDir dist/types",
|
|
44
|
+
"clean": "rimraf dist"
|
|
29
45
|
}
|
|
30
46
|
}
|
package/dist/index.d.ts
DELETED
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import { Secret, JwtPayload } from "jsonwebtoken";
|
|
2
|
-
export declare const hashPassword: (password: string) => Promise<string>;
|
|
3
|
-
export declare const verifyPassword: (password: string, hash: string) => Promise<boolean>;
|
|
4
|
-
export declare const comparePassword: (password: string, hash: string) => Promise<boolean>;
|
|
5
|
-
export declare const generateToken: (payload: Record<string, unknown>, secret: Secret, expiresIn?: string) => string;
|
|
6
|
-
export declare const verifyToken: (token: string, secret: Secret) => string | JwtPayload;
|
|
7
|
-
export declare const signToken: (payload: Record<string, unknown>, secret: Secret, expiresIn?: string) => string;
|
|
8
|
-
declare const SecurityUtils: {
|
|
9
|
-
hashPassword: (password: string) => Promise<string>;
|
|
10
|
-
verifyPassword: (password: string, hash: string) => Promise<boolean>;
|
|
11
|
-
comparePassword: (password: string, hash: string) => Promise<boolean>;
|
|
12
|
-
generateToken: (payload: Record<string, unknown>, secret: Secret, expiresIn?: string) => string;
|
|
13
|
-
verifyToken: (token: string, secret: Secret) => string | JwtPayload;
|
|
14
|
-
signToken: (payload: Record<string, unknown>, secret: Secret, expiresIn?: string) => string;
|
|
15
|
-
};
|
|
16
|
-
export default SecurityUtils;
|
package/dist/index.js
DELETED
|
@@ -1,41 +0,0 @@
|
|
|
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.signToken = exports.verifyToken = exports.generateToken = exports.comparePassword = exports.verifyPassword = exports.hashPassword = void 0;
|
|
7
|
-
const jsonwebtoken_1 = __importDefault(require("jsonwebtoken"));
|
|
8
|
-
const bcryptjs_1 = __importDefault(require("bcryptjs"));
|
|
9
|
-
// 🧱 Password helpers
|
|
10
|
-
const hashPassword = async (password) => {
|
|
11
|
-
const salt = await bcryptjs_1.default.genSalt(10);
|
|
12
|
-
return bcryptjs_1.default.hash(password, salt);
|
|
13
|
-
};
|
|
14
|
-
exports.hashPassword = hashPassword;
|
|
15
|
-
const verifyPassword = async (password, hash) => {
|
|
16
|
-
return bcryptjs_1.default.compare(password, hash);
|
|
17
|
-
};
|
|
18
|
-
exports.verifyPassword = verifyPassword;
|
|
19
|
-
// For backward compatibility
|
|
20
|
-
exports.comparePassword = exports.verifyPassword;
|
|
21
|
-
// 🧩 JWT helpers
|
|
22
|
-
const generateToken = (payload, secret, expiresIn = "1h") => {
|
|
23
|
-
return jsonwebtoken_1.default.sign(payload, secret, { expiresIn, algorithm: "HS256" });
|
|
24
|
-
};
|
|
25
|
-
exports.generateToken = generateToken;
|
|
26
|
-
const verifyToken = (token, secret) => {
|
|
27
|
-
return jsonwebtoken_1.default.verify(token, secret);
|
|
28
|
-
};
|
|
29
|
-
exports.verifyToken = verifyToken;
|
|
30
|
-
// For backward compatibility
|
|
31
|
-
exports.signToken = exports.generateToken;
|
|
32
|
-
// Default export for namespace usage
|
|
33
|
-
const SecurityUtils = {
|
|
34
|
-
hashPassword: exports.hashPassword,
|
|
35
|
-
verifyPassword: exports.verifyPassword,
|
|
36
|
-
comparePassword: exports.comparePassword,
|
|
37
|
-
generateToken: exports.generateToken,
|
|
38
|
-
verifyToken: exports.verifyToken,
|
|
39
|
-
signToken: exports.signToken
|
|
40
|
-
};
|
|
41
|
-
exports.default = SecurityUtils;
|
package/src/index.ts
DELETED
|
@@ -1,39 +0,0 @@
|
|
|
1
|
-
import jwt, { Secret, JwtPayload } from "jsonwebtoken";
|
|
2
|
-
import bcrypt from "bcryptjs";
|
|
3
|
-
|
|
4
|
-
// 🧱 Password helpers
|
|
5
|
-
export const hashPassword = async (password: string): Promise<string> => {
|
|
6
|
-
const salt = await bcrypt.genSalt(10);
|
|
7
|
-
return bcrypt.hash(password, salt);
|
|
8
|
-
};
|
|
9
|
-
|
|
10
|
-
export const verifyPassword = async (password: string, hash: string): Promise<boolean> => {
|
|
11
|
-
return bcrypt.compare(password, hash);
|
|
12
|
-
};
|
|
13
|
-
|
|
14
|
-
// For backward compatibility
|
|
15
|
-
export const comparePassword = verifyPassword;
|
|
16
|
-
|
|
17
|
-
// 🧩 JWT helpers
|
|
18
|
-
export const generateToken = (payload: Record<string, unknown>, secret: Secret, expiresIn = "1h"): string => {
|
|
19
|
-
return jwt.sign(payload, secret, { expiresIn, algorithm: "HS256" } as jwt.SignOptions);
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
export const verifyToken = (token: string, secret: Secret): string | JwtPayload => {
|
|
23
|
-
return jwt.verify(token, secret);
|
|
24
|
-
};
|
|
25
|
-
|
|
26
|
-
// For backward compatibility
|
|
27
|
-
export const signToken = generateToken;
|
|
28
|
-
|
|
29
|
-
// Default export for namespace usage
|
|
30
|
-
const SecurityUtils = {
|
|
31
|
-
hashPassword,
|
|
32
|
-
verifyPassword,
|
|
33
|
-
comparePassword,
|
|
34
|
-
generateToken,
|
|
35
|
-
verifyToken,
|
|
36
|
-
signToken
|
|
37
|
-
};
|
|
38
|
-
|
|
39
|
-
export default SecurityUtils;
|
package/tsconfig.json
DELETED
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"compilerOptions": {
|
|
3
|
-
"target": "ES2020",
|
|
4
|
-
"module": "CommonJS",
|
|
5
|
-
"moduleResolution": "node",
|
|
6
|
-
"rootDir": "./src",
|
|
7
|
-
"outDir": "./dist",
|
|
8
|
-
"strict": true,
|
|
9
|
-
"esModuleInterop": true,
|
|
10
|
-
"allowSyntheticDefaultImports": true,
|
|
11
|
-
"skipLibCheck": true,
|
|
12
|
-
"forceConsistentCasingInFileNames": true,
|
|
13
|
-
"declaration": true,
|
|
14
|
-
"baseUrl": ".",
|
|
15
|
-
"paths": {
|
|
16
|
-
"*": ["*", "*.ts", "*.js"]
|
|
17
|
-
}
|
|
18
|
-
},
|
|
19
|
-
"include": ["src/**/*"],
|
|
20
|
-
"exclude": ["node_modules", "dist"]
|
|
21
|
-
}
|