@naman_deep_singh/security 1.0.4 → 1.2.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 +315 -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 +54 -0
- package/dist/cjs/core/jwt/generateTokens.d.ts +4 -0
- package/dist/cjs/core/jwt/generateTokens.js +30 -0
- package/dist/cjs/core/jwt/index.d.ts +8 -0
- package/dist/cjs/core/jwt/index.js +24 -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/types.d.ts +22 -0
- package/dist/cjs/core/jwt/types.js +2 -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 +18 -0
- package/dist/cjs/core/jwt/verify.js +44 -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 +41 -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 +51 -0
- package/dist/esm/core/jwt/generateTokens.d.ts +4 -0
- package/dist/esm/core/jwt/generateTokens.js +25 -0
- package/dist/esm/core/jwt/index.d.ts +8 -0
- package/dist/esm/core/jwt/index.js +8 -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/types.d.ts +22 -0
- package/dist/esm/core/jwt/types.js +1 -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 +18 -0
- package/dist/esm/core/jwt/verify.js +37 -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 +41 -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 +4 -0
- package/dist/types/core/jwt/index.d.ts +8 -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/types.d.ts +22 -0
- package/dist/types/core/jwt/validateToken.d.ts +13 -0
- package/dist/types/core/jwt/verify.d.ts +18 -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 +41 -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,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,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, unknown> | undefined;
|
|
6
|
+
wsMessage?: string | Record<string, unknown> | undefined;
|
|
7
|
+
}
|
|
8
|
+
/**
|
|
9
|
+
* Universal token extractor
|
|
10
|
+
*/
|
|
11
|
+
export declare function extractToken(sources: TokenSources): string | null;
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Secret } from "jsonwebtoken";
|
|
2
|
+
import { RefreshToken, TokenPair } from "./types";
|
|
3
|
+
export declare const generateTokens: (payload: Record<string, unknown>, accessSecret: Secret, refreshSecret: Secret, accessExpiry?: string | number, refreshExpiry?: string | number) => TokenPair;
|
|
4
|
+
export declare function rotateRefreshToken(oldToken: string, secret: Secret): RefreshToken;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function parseDuration(input: string | number): number;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { JwtPayload } from "jsonwebtoken";
|
|
2
|
+
export interface AccessTokenBrand {
|
|
3
|
+
readonly access: unique symbol;
|
|
4
|
+
}
|
|
5
|
+
export interface RefreshTokenBrand {
|
|
6
|
+
readonly refresh: unique symbol;
|
|
7
|
+
}
|
|
8
|
+
export type AccessToken = string & AccessTokenBrand;
|
|
9
|
+
export type RefreshToken = string & RefreshTokenBrand;
|
|
10
|
+
export interface TokenPair {
|
|
11
|
+
accessToken: AccessToken;
|
|
12
|
+
refreshToken: RefreshToken;
|
|
13
|
+
}
|
|
14
|
+
export interface VerificationResult<T = JwtPayload> {
|
|
15
|
+
valid: boolean;
|
|
16
|
+
payload?: T | string;
|
|
17
|
+
error?: Error;
|
|
18
|
+
}
|
|
19
|
+
export interface TokenValidationOptions {
|
|
20
|
+
ignoreExpiration?: boolean;
|
|
21
|
+
ignoreIssuedAt?: boolean;
|
|
22
|
+
}
|
|
@@ -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, unknown>, rules?: TokenRequirements): {
|
|
8
|
+
valid: true;
|
|
9
|
+
} | {
|
|
10
|
+
valid: false;
|
|
11
|
+
error: string;
|
|
12
|
+
};
|
|
13
|
+
export declare function isTokenExpired(payload: JwtPayload): boolean;
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import jwt, { Secret, JwtPayload } from "jsonwebtoken";
|
|
2
|
+
import { VerificationResult } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* Verify token (throws if invalid or expired)
|
|
5
|
+
*/
|
|
6
|
+
export declare const verifyToken: (token: string, secret: Secret) => string | JwtPayload;
|
|
7
|
+
/**
|
|
8
|
+
* Safe verify — never throws, returns structured result
|
|
9
|
+
*/
|
|
10
|
+
export declare const safeVerifyToken: (token: string, secret: Secret) => VerificationResult;
|
|
11
|
+
/**
|
|
12
|
+
* Verify token with validation options
|
|
13
|
+
*/
|
|
14
|
+
export declare const verifyTokenWithOptions: (token: string, secret: Secret, options?: jwt.VerifyOptions) => string | JwtPayload;
|
|
15
|
+
/**
|
|
16
|
+
* Safe verify with validation options
|
|
17
|
+
*/
|
|
18
|
+
export declare const safeVerifyTokenWithOptions: (token: string, secret: Secret, options?: jwt.VerifyOptions) => VerificationResult;
|
|
@@ -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,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,41 @@
|
|
|
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): JWTUtils.RefreshToken;
|
|
17
|
+
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;
|
|
18
|
+
parseDuration(input: string | number): number;
|
|
19
|
+
signToken: (payload: Record<string, unknown>, secret: import("node_modules/@types/jsonwebtoken").Secret, expiresIn?: string | number, options?: import("node_modules/@types/jsonwebtoken").SignOptions) => string;
|
|
20
|
+
validateTokenPayload(payload: Record<string, unknown>, 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) => JWTUtils.VerificationResult;
|
|
29
|
+
verifyTokenWithOptions: (token: string, secret: import("node_modules/@types/jsonwebtoken").Secret, options?: import("node_modules/@types/jsonwebtoken").VerifyOptions) => string | import("node_modules/@types/jsonwebtoken").JwtPayload;
|
|
30
|
+
safeVerifyTokenWithOptions: (token: string, secret: import("node_modules/@types/jsonwebtoken").Secret, options?: import("node_modules/@types/jsonwebtoken").VerifyOptions) => JWTUtils.VerificationResult;
|
|
31
|
+
hashPasswordWithPepper(password: string, pepper: string): Promise<string>;
|
|
32
|
+
hashPasswordWithPepperSync(password: string, pepper: string): string;
|
|
33
|
+
hashPassword: (password: string, saltRounds?: number) => Promise<string>;
|
|
34
|
+
hashPasswordSync: (password: string, saltRounds?: number) => string;
|
|
35
|
+
isPasswordStrong: (password: string, options?: import("./core/password/types").PasswordStrengthOptions) => boolean;
|
|
36
|
+
verifyPasswordWithPepper(password: string, pepper: string, hash: string): Promise<boolean>;
|
|
37
|
+
verifyPasswordWithPepperSync(password: string, pepper: string, hash: string): Promise<boolean>;
|
|
38
|
+
verifyPassword: (password: string, hash: string) => Promise<boolean>;
|
|
39
|
+
verifyPasswordSync: (password: string, hash: string) => boolean;
|
|
40
|
+
};
|
|
41
|
+
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.2.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
|
-
}
|