@naman_deep_singh/security 1.6.0 → 1.7.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 +1 -1
- package/dist/cjs/core/crypto/CryptoManger.js +107 -0
- package/dist/cjs/core/crypto/index.js +13 -26
- package/dist/cjs/core/index.js +44 -0
- package/dist/cjs/core/jwt/{jwtManager.js → JWTManager.js} +8 -8
- package/dist/cjs/core/jwt/decode.js +0 -1
- package/dist/cjs/core/jwt/generateTokens.js +6 -6
- package/dist/cjs/core/jwt/index.js +23 -22
- package/dist/cjs/core/jwt/signToken.js +2 -2
- package/dist/cjs/core/password/{passwordManager.js → PasswordManager.js} +3 -3
- package/dist/cjs/core/password/hash.js +5 -5
- package/dist/cjs/core/password/index.js +20 -17
- package/dist/cjs/index.js +4 -40
- package/dist/cjs/interfaces/index.js +2 -0
- package/dist/esm/core/crypto/CryptoManger.js +100 -0
- package/dist/esm/core/crypto/index.js +5 -5
- package/dist/esm/core/index.js +6 -0
- package/dist/esm/core/jwt/{jwtManager.js → JWTManager.js} +3 -3
- package/dist/esm/core/jwt/decode.js +0 -1
- package/dist/esm/core/jwt/generateTokens.js +2 -2
- package/dist/esm/core/jwt/index.js +8 -8
- package/dist/esm/core/jwt/signToken.js +1 -1
- package/dist/esm/core/password/{passwordManager.js → PasswordManager.js} +1 -1
- package/dist/esm/core/password/hash.js +3 -3
- package/dist/esm/core/password/index.js +5 -3
- package/dist/esm/index.js +4 -13
- package/dist/esm/interfaces/index.js +1 -0
- package/dist/types/core/crypto/CryptoManger.d.ts +36 -0
- package/dist/types/core/crypto/index.d.ts +5 -5
- package/dist/types/core/index.d.ts +3 -0
- package/dist/{esm/core/jwt/jwtManager.d.ts → types/core/jwt/JWTManager.d.ts} +1 -1
- package/dist/types/core/jwt/generateTokens.d.ts +1 -1
- package/dist/types/core/jwt/index.d.ts +9 -8
- package/dist/types/core/jwt/verify.d.ts +1 -1
- package/dist/types/core/password/{passwordManager.d.ts → PasswordManager.d.ts} +1 -1
- package/dist/types/core/password/index.d.ts +6 -3
- package/dist/types/core/password/strength.d.ts +1 -1
- package/dist/types/index.d.ts +1 -40
- package/dist/types/interfaces/index.d.ts +2 -0
- package/package.json +30 -4
- package/dist/cjs/core/crypto/cryptoManager.d.ts +0 -111
- package/dist/cjs/core/crypto/cryptoManager.js +0 -208
- package/dist/cjs/core/crypto/decrypt.d.ts +0 -1
- package/dist/cjs/core/crypto/encrypt.d.ts +0 -1
- package/dist/cjs/core/crypto/hmac.d.ts +0 -8
- package/dist/cjs/core/crypto/index.d.ts +0 -5
- package/dist/cjs/core/crypto/random.d.ts +0 -8
- package/dist/cjs/core/jwt/decode.d.ts +0 -12
- package/dist/cjs/core/jwt/extractToken.d.ts +0 -11
- package/dist/cjs/core/jwt/generateTokens.d.ts +0 -4
- package/dist/cjs/core/jwt/index.d.ts +0 -8
- package/dist/cjs/core/jwt/jwtManager.d.ts +0 -43
- package/dist/cjs/core/jwt/parseDuration.d.ts +0 -1
- package/dist/cjs/core/jwt/signToken.d.ts +0 -2
- package/dist/cjs/core/jwt/types.d.ts +0 -22
- package/dist/cjs/core/jwt/validateToken.d.ts +0 -16
- package/dist/cjs/core/jwt/verify.d.ts +0 -18
- package/dist/cjs/core/password/hash.d.ts +0 -10
- package/dist/cjs/core/password/index.d.ts +0 -3
- package/dist/cjs/core/password/passwordManager.d.ts +0 -29
- package/dist/cjs/core/password/strength.d.ts +0 -2
- package/dist/cjs/core/password/types.d.ts +0 -7
- package/dist/cjs/core/password/utils.d.ts +0 -16
- package/dist/cjs/core/password/verify.d.ts +0 -10
- package/dist/cjs/index.d.ts +0 -40
- package/dist/cjs/interfaces/jwt.interface.d.ts +0 -47
- package/dist/cjs/interfaces/password.interface.d.ts +0 -60
- package/dist/esm/core/crypto/cryptoManager.d.ts +0 -111
- package/dist/esm/core/crypto/cryptoManager.js +0 -203
- package/dist/esm/core/crypto/decrypt.d.ts +0 -1
- package/dist/esm/core/crypto/encrypt.d.ts +0 -1
- package/dist/esm/core/crypto/hmac.d.ts +0 -8
- package/dist/esm/core/crypto/index.d.ts +0 -5
- package/dist/esm/core/crypto/random.d.ts +0 -8
- package/dist/esm/core/jwt/decode.d.ts +0 -12
- package/dist/esm/core/jwt/extractToken.d.ts +0 -11
- package/dist/esm/core/jwt/generateTokens.d.ts +0 -4
- package/dist/esm/core/jwt/index.d.ts +0 -8
- package/dist/esm/core/jwt/parseDuration.d.ts +0 -1
- package/dist/esm/core/jwt/signToken.d.ts +0 -2
- package/dist/esm/core/jwt/types.d.ts +0 -22
- package/dist/esm/core/jwt/validateToken.d.ts +0 -16
- package/dist/esm/core/jwt/verify.d.ts +0 -18
- package/dist/esm/core/password/hash.d.ts +0 -10
- package/dist/esm/core/password/index.d.ts +0 -3
- package/dist/esm/core/password/passwordManager.d.ts +0 -29
- package/dist/esm/core/password/strength.d.ts +0 -2
- package/dist/esm/core/password/types.d.ts +0 -7
- package/dist/esm/core/password/utils.d.ts +0 -16
- package/dist/esm/core/password/verify.d.ts +0 -10
- package/dist/esm/index.d.ts +0 -40
- package/dist/esm/interfaces/jwt.interface.d.ts +0 -47
- package/dist/esm/interfaces/password.interface.d.ts +0 -60
- package/dist/types/core/crypto/cryptoManager.d.ts +0 -111
- package/dist/types/core/jwt/jwtManager.d.ts +0 -43
|
@@ -1,208 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.cryptoManager = exports.createCryptoManager = exports.CryptoManager = void 0;
|
|
4
|
-
const errors_1 = require("@naman_deep_singh/errors");
|
|
5
|
-
const index_1 = require("./index");
|
|
6
|
-
/**
|
|
7
|
-
* Default configuration
|
|
8
|
-
*/
|
|
9
|
-
const DEFAULT_CONFIG = {
|
|
10
|
-
defaultAlgorithm: 'aes-256-gcm',
|
|
11
|
-
defaultEncoding: 'utf8',
|
|
12
|
-
hmacAlgorithm: 'sha256',
|
|
13
|
-
};
|
|
14
|
-
/**
|
|
15
|
-
* CryptoManager - Class-based wrapper for all cryptographic operations
|
|
16
|
-
* Provides a consistent interface for encryption, decryption, HMAC generation, and secure random generation
|
|
17
|
-
*/
|
|
18
|
-
class CryptoManager {
|
|
19
|
-
constructor(config = {}) {
|
|
20
|
-
this.config = { ...DEFAULT_CONFIG, ...config };
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* Update configuration
|
|
24
|
-
*/
|
|
25
|
-
updateConfig(config) {
|
|
26
|
-
this.config = { ...this.config, ...config };
|
|
27
|
-
}
|
|
28
|
-
/**
|
|
29
|
-
* Get current configuration
|
|
30
|
-
*/
|
|
31
|
-
getConfig() {
|
|
32
|
-
return { ...this.config };
|
|
33
|
-
}
|
|
34
|
-
/**
|
|
35
|
-
* Encrypt data using the default or specified algorithm
|
|
36
|
-
*/
|
|
37
|
-
encrypt(plaintext, key, _options) {
|
|
38
|
-
try {
|
|
39
|
-
return (0, index_1.encrypt)(plaintext, key);
|
|
40
|
-
}
|
|
41
|
-
catch (error) {
|
|
42
|
-
throw new errors_1.CryptoIntegrityError({
|
|
43
|
-
reason: 'Encryption failed',
|
|
44
|
-
}, error instanceof Error ? error : undefined);
|
|
45
|
-
}
|
|
46
|
-
}
|
|
47
|
-
/**
|
|
48
|
-
* Decrypt data using the default or specified algorithm
|
|
49
|
-
*/
|
|
50
|
-
decrypt(encryptedData, key, _options) {
|
|
51
|
-
try {
|
|
52
|
-
return (0, index_1.decrypt)(encryptedData, key);
|
|
53
|
-
}
|
|
54
|
-
catch (error) {
|
|
55
|
-
throw new errors_1.CryptoIntegrityError({
|
|
56
|
-
reason: 'Decryption failed',
|
|
57
|
-
}, error instanceof Error ? error : undefined);
|
|
58
|
-
}
|
|
59
|
-
}
|
|
60
|
-
/**
|
|
61
|
-
* Generate HMAC signature
|
|
62
|
-
*/
|
|
63
|
-
generateHmac(data, secret, _options) {
|
|
64
|
-
// Use the basic HMAC sign function for now
|
|
65
|
-
// TODO: Add support for different algorithms
|
|
66
|
-
return (0, index_1.hmacSign)(data, secret);
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Generate cryptographically secure random bytes
|
|
70
|
-
*/
|
|
71
|
-
generateSecureRandom(length, _encoding = 'hex') {
|
|
72
|
-
// Use the basic random token function
|
|
73
|
-
return (0, index_1.randomToken)(length);
|
|
74
|
-
}
|
|
75
|
-
/**
|
|
76
|
-
* Verify HMAC signature
|
|
77
|
-
*/
|
|
78
|
-
verifyHmac(data, secret, signature, _options) {
|
|
79
|
-
// Use the basic HMAC verify function
|
|
80
|
-
return (0, index_1.hmacVerify)(data, secret, signature);
|
|
81
|
-
}
|
|
82
|
-
/**
|
|
83
|
-
* Create a key derivation function using PBKDF2
|
|
84
|
-
*/
|
|
85
|
-
deriveKey(password, salt, iterations = 100000, keyLength = 32) {
|
|
86
|
-
return new Promise((resolve, reject) => {
|
|
87
|
-
const crypto = require('crypto');
|
|
88
|
-
crypto.pbkdf2(password, salt, iterations, keyLength, 'sha256', (error, derivedKey) => {
|
|
89
|
-
if (error) {
|
|
90
|
-
reject(new errors_1.CryptoIntegrityError({
|
|
91
|
-
reason: 'Key derivation failed',
|
|
92
|
-
}, error instanceof Error ? error : undefined));
|
|
93
|
-
}
|
|
94
|
-
else {
|
|
95
|
-
resolve(derivedKey.toString('hex'));
|
|
96
|
-
}
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
}
|
|
100
|
-
/**
|
|
101
|
-
* Hash data using SHA-256
|
|
102
|
-
*/
|
|
103
|
-
sha256(data, encoding = 'hex') {
|
|
104
|
-
const crypto = require('crypto');
|
|
105
|
-
return crypto.createHash('sha256').update(data).digest(encoding);
|
|
106
|
-
}
|
|
107
|
-
/**
|
|
108
|
-
* Hash data using SHA-512
|
|
109
|
-
*/
|
|
110
|
-
sha512(data, encoding = 'hex') {
|
|
111
|
-
const crypto = require('crypto');
|
|
112
|
-
return crypto.createHash('sha512').update(data).digest(encoding);
|
|
113
|
-
}
|
|
114
|
-
/**
|
|
115
|
-
* Generate a secure key pair for asymmetric encryption
|
|
116
|
-
*/
|
|
117
|
-
generateKeyPair(options) {
|
|
118
|
-
return new Promise((resolve, _reject) => {
|
|
119
|
-
const crypto = require('crypto');
|
|
120
|
-
const keyPair = crypto.generateKeyPairSync('rsa', {
|
|
121
|
-
modulusLength: options?.modulusLength || 2048,
|
|
122
|
-
publicKeyEncoding: options?.publicKeyEncoding || {
|
|
123
|
-
type: 'spki',
|
|
124
|
-
format: 'pem',
|
|
125
|
-
},
|
|
126
|
-
privateKeyEncoding: options?.privateKeyEncoding || {
|
|
127
|
-
type: 'pkcs8',
|
|
128
|
-
format: 'pem',
|
|
129
|
-
},
|
|
130
|
-
});
|
|
131
|
-
resolve(keyPair);
|
|
132
|
-
});
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Encrypt data using RSA public key
|
|
136
|
-
*/
|
|
137
|
-
rsaEncrypt(data, publicKey) {
|
|
138
|
-
return new Promise((resolve, _reject) => {
|
|
139
|
-
const crypto = require('crypto');
|
|
140
|
-
const buffer = Buffer.from(data, 'utf8');
|
|
141
|
-
const encrypted = crypto.publicEncrypt(publicKey, buffer);
|
|
142
|
-
resolve(encrypted.toString('base64'));
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
/**
|
|
146
|
-
* Decrypt data using RSA private key
|
|
147
|
-
*/
|
|
148
|
-
rsaDecrypt(encryptedData, privateKey) {
|
|
149
|
-
return new Promise((resolve, _reject) => {
|
|
150
|
-
const crypto = require('crypto');
|
|
151
|
-
const buffer = Buffer.from(encryptedData, 'base64');
|
|
152
|
-
const decrypted = crypto.privateDecrypt(privateKey, buffer);
|
|
153
|
-
resolve(decrypted.toString('utf8'));
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* Create digital signature using RSA private key
|
|
158
|
-
*/
|
|
159
|
-
rsaSign(data, privateKey, algorithm = 'sha256') {
|
|
160
|
-
return new Promise((resolve, reject) => {
|
|
161
|
-
const crypto = require('crypto');
|
|
162
|
-
try {
|
|
163
|
-
const sign = crypto.createSign(algorithm);
|
|
164
|
-
sign.update(data);
|
|
165
|
-
sign.end();
|
|
166
|
-
const signature = sign.sign(privateKey, 'base64');
|
|
167
|
-
resolve(signature);
|
|
168
|
-
}
|
|
169
|
-
catch (error) {
|
|
170
|
-
reject(new errors_1.CryptoIntegrityError({
|
|
171
|
-
reason: 'RSA signing failed',
|
|
172
|
-
}, error instanceof Error ? error : undefined));
|
|
173
|
-
}
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
/**
|
|
177
|
-
* Verify digital signature using RSA public key
|
|
178
|
-
*/
|
|
179
|
-
rsaVerify(data, signature, publicKey, algorithm = 'sha256') {
|
|
180
|
-
return new Promise((resolve, reject) => {
|
|
181
|
-
const crypto = require('crypto');
|
|
182
|
-
try {
|
|
183
|
-
const verify = crypto.createVerify(algorithm);
|
|
184
|
-
verify.update(data);
|
|
185
|
-
verify.end();
|
|
186
|
-
const isValid = verify.verify(publicKey, signature, 'base64');
|
|
187
|
-
resolve(isValid);
|
|
188
|
-
}
|
|
189
|
-
catch (error) {
|
|
190
|
-
reject(new errors_1.CryptoIntegrityError({
|
|
191
|
-
reason: 'RSA verification failed',
|
|
192
|
-
}, error instanceof Error ? error : undefined));
|
|
193
|
-
}
|
|
194
|
-
});
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
exports.CryptoManager = CryptoManager;
|
|
198
|
-
/**
|
|
199
|
-
* Create a CryptoManager instance with default configuration
|
|
200
|
-
*/
|
|
201
|
-
const createCryptoManager = (config) => {
|
|
202
|
-
return new CryptoManager(config);
|
|
203
|
-
};
|
|
204
|
-
exports.createCryptoManager = createCryptoManager;
|
|
205
|
-
/**
|
|
206
|
-
* Default CryptoManager instance
|
|
207
|
-
*/
|
|
208
|
-
exports.cryptoManager = new CryptoManager();
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const decrypt: (data: string, secret: string) => string;
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const encrypt: (text: string, secret: string) => string;
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { type 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;
|
|
@@ -1,11 +0,0 @@
|
|
|
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;
|
|
@@ -1,4 +0,0 @@
|
|
|
1
|
-
import type { Secret } from 'jsonwebtoken';
|
|
2
|
-
import type { 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;
|
|
@@ -1,43 +0,0 @@
|
|
|
1
|
-
import { type JwtPayload, type Secret } from 'jsonwebtoken';
|
|
2
|
-
import type { AccessToken, ITokenManager, JWTConfig, RefreshToken, TokenPair } from '../../interfaces/jwt.interface';
|
|
3
|
-
export declare class JWTManager implements ITokenManager {
|
|
4
|
-
private accessSecret;
|
|
5
|
-
private refreshSecret;
|
|
6
|
-
private accessExpiry;
|
|
7
|
-
private refreshExpiry;
|
|
8
|
-
private cache?;
|
|
9
|
-
private cacheTTL;
|
|
10
|
-
constructor(config: JWTConfig);
|
|
11
|
-
/** Generate both access and refresh tokens */
|
|
12
|
-
generateTokens(payload: Record<string, unknown>): Promise<TokenPair>;
|
|
13
|
-
/** Generate access token */
|
|
14
|
-
generateAccessToken(payload: Record<string, unknown>): Promise<AccessToken>;
|
|
15
|
-
/** Generate refresh token */
|
|
16
|
-
generateRefreshToken(payload: Record<string, unknown>): Promise<RefreshToken>;
|
|
17
|
-
/** Verify access token */
|
|
18
|
-
verifyAccessToken(token: string): Promise<JwtPayload>;
|
|
19
|
-
/** Verify refresh token */
|
|
20
|
-
verifyRefreshToken(token: string): Promise<JwtPayload>;
|
|
21
|
-
/** Decode token without verification */
|
|
22
|
-
decodeToken(token: string, complete?: boolean): JwtPayload | string | null;
|
|
23
|
-
/** Extract token from Authorization header */
|
|
24
|
-
extractTokenFromHeader(authHeader: string): string | null;
|
|
25
|
-
/** Validate token without throwing exceptions */
|
|
26
|
-
validateToken(token: string, secret: Secret): boolean;
|
|
27
|
-
/** Rotate refresh token */
|
|
28
|
-
rotateRefreshToken(oldToken: string): Promise<RefreshToken>;
|
|
29
|
-
/** Check if token is expired */
|
|
30
|
-
isTokenExpired(token: string): boolean;
|
|
31
|
-
/** Get token expiration date */
|
|
32
|
-
getTokenExpiration(token: string): Date | null;
|
|
33
|
-
/** Clear token cache */
|
|
34
|
-
clearCache(): void;
|
|
35
|
-
/** Get cache statistics */
|
|
36
|
-
getCacheStats(): {
|
|
37
|
-
size: number;
|
|
38
|
-
maxSize: number;
|
|
39
|
-
} | null;
|
|
40
|
-
/** Private helper methods */
|
|
41
|
-
private validatePayload;
|
|
42
|
-
private verifyTokenWithCache;
|
|
43
|
-
}
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare function parseDuration(input: string | number): number;
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import type { 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
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
import type { JwtPayload } from 'jsonwebtoken';
|
|
2
|
-
export interface TokenRequirements {
|
|
3
|
-
requiredFields?: string[];
|
|
4
|
-
forbiddenFields?: string[];
|
|
5
|
-
validateTypes?: Record<string, 'string' | 'number' | 'boolean'>;
|
|
6
|
-
}
|
|
7
|
-
/**
|
|
8
|
-
* Validates a JWT payload according to the provided rules.
|
|
9
|
-
* Throws ValidationError if validation fails.
|
|
10
|
-
*/
|
|
11
|
-
export declare function validateTokenPayload(payload: Record<string, unknown>, rules?: TokenRequirements): void;
|
|
12
|
-
/**
|
|
13
|
-
* Checks if a JWT payload is expired.
|
|
14
|
-
* Returns true if expired or missing 'exp'.
|
|
15
|
-
*/
|
|
16
|
-
export declare function isTokenExpired(payload: JwtPayload): boolean;
|
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { type JwtPayload, type Secret, type VerifyOptions } from 'jsonwebtoken';
|
|
2
|
-
import type { VerificationResult } from './types';
|
|
3
|
-
/**
|
|
4
|
-
* Verify token (throws UnauthorizedError if invalid or expired)
|
|
5
|
-
*/
|
|
6
|
-
export declare const verifyToken: (token: string, secret: Secret) => string | JwtPayload;
|
|
7
|
-
/**
|
|
8
|
-
* Verify token with options
|
|
9
|
-
*/
|
|
10
|
-
export declare const verifyTokenWithOptions: (token: string, secret: Secret, options?: VerifyOptions) => string | JwtPayload;
|
|
11
|
-
/**
|
|
12
|
-
* Safe verify — never throws, returns structured result with UnauthorizedError on failure
|
|
13
|
-
*/
|
|
14
|
-
export declare const safeVerifyToken: (token: string, secret: Secret) => VerificationResult;
|
|
15
|
-
/**
|
|
16
|
-
* Safe verify with options — never throws, returns structured result with UnauthorizedError on failure
|
|
17
|
-
*/
|
|
18
|
-
export declare const safeVerifyTokenWithOptions: (token: string, secret: Secret, options?: VerifyOptions) => VerificationResult;
|
|
@@ -1,10 +0,0 @@
|
|
|
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;
|
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import type { HashedPassword, IPasswordManager, PasswordConfig, PasswordStrength, PasswordValidationResult } from '../../interfaces/password.interface';
|
|
2
|
-
export declare class PasswordManager implements IPasswordManager {
|
|
3
|
-
private defaultConfig;
|
|
4
|
-
constructor(config?: PasswordConfig);
|
|
5
|
-
/**
|
|
6
|
-
* Hash a password asynchronously using bcrypt
|
|
7
|
-
*/
|
|
8
|
-
hash(password: string, salt?: string): Promise<HashedPassword>;
|
|
9
|
-
/**
|
|
10
|
-
* Verify password against hash and salt
|
|
11
|
-
*/
|
|
12
|
-
verify(password: string, hash: string, salt: string): Promise<boolean>;
|
|
13
|
-
/**
|
|
14
|
-
* Generate a random password
|
|
15
|
-
*/
|
|
16
|
-
generate(length?: number, options?: PasswordConfig): string;
|
|
17
|
-
/**
|
|
18
|
-
* Validate password against configuration
|
|
19
|
-
*/
|
|
20
|
-
validate(password: string, config?: PasswordConfig): PasswordValidationResult;
|
|
21
|
-
/**
|
|
22
|
-
* Check password strength
|
|
23
|
-
*/
|
|
24
|
-
checkStrength(password: string): PasswordStrength;
|
|
25
|
-
/**
|
|
26
|
-
* Check if password hash needs upgrade (saltRounds change)
|
|
27
|
-
*/
|
|
28
|
-
needsUpgrade(_hash: string, _currentConfig: PasswordConfig): boolean;
|
|
29
|
-
}
|
|
@@ -1,16 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Ensure password is a valid non-empty string
|
|
3
|
-
*/
|
|
4
|
-
export declare function ensureValidPassword(password: string): void;
|
|
5
|
-
/**
|
|
6
|
-
* Timing-safe comparison between two strings
|
|
7
|
-
*/
|
|
8
|
-
export declare function safeCompare(a: string, b: string): boolean;
|
|
9
|
-
/**
|
|
10
|
-
* Estimate password entropy based on character pool
|
|
11
|
-
*/
|
|
12
|
-
export declare function estimatePasswordEntropy(password: string): number;
|
|
13
|
-
/**
|
|
14
|
-
* Normalize password string to a consistent form
|
|
15
|
-
*/
|
|
16
|
-
export declare function normalizePassword(password: string): string;
|
|
@@ -1,10 +0,0 @@
|
|
|
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>;
|
package/dist/cjs/index.d.ts
DELETED
|
@@ -1,40 +0,0 @@
|
|
|
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';
|
|
5
|
-
import * as CryptoUtils from './core/crypto';
|
|
6
|
-
import * as JWTUtils from './core/jwt';
|
|
7
|
-
declare const _default: {
|
|
8
|
-
decrypt: (data: string, secret: string) => string;
|
|
9
|
-
encrypt: (text: string, secret: string) => string;
|
|
10
|
-
hmacSign: (message: string, secret: string) => string;
|
|
11
|
-
hmacVerify: (message: string, secret: string, signature: string) => boolean;
|
|
12
|
-
randomToken: (length?: number) => string;
|
|
13
|
-
generateStrongPassword: (length?: number) => string;
|
|
14
|
-
CryptoManager: typeof CryptoUtils.CryptoManager;
|
|
15
|
-
createCryptoManager: (config?: CryptoUtils.CryptoManagerConfig) => CryptoUtils.CryptoManager;
|
|
16
|
-
cryptoManager: CryptoUtils.CryptoManager;
|
|
17
|
-
decodeToken(token: string): null | string | import("node_modules/@types/jsonwebtoken").JwtPayload;
|
|
18
|
-
decodeTokenStrict(token: string): import("node_modules/@types/jsonwebtoken").JwtPayload;
|
|
19
|
-
extractToken(sources: JWTUtils.TokenSources): string | null;
|
|
20
|
-
rotateRefreshToken(oldToken: string, secret: import("node_modules/@types/jsonwebtoken").Secret): JWTUtils.RefreshToken;
|
|
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
|
-
parseDuration(input: string | number): number;
|
|
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): void;
|
|
25
|
-
isTokenExpired(payload: import("node_modules/@types/jsonwebtoken").JwtPayload): boolean;
|
|
26
|
-
verifyToken: (token: string, secret: import("node_modules/@types/jsonwebtoken").Secret) => string | import("node_modules/@types/jsonwebtoken").JwtPayload;
|
|
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;
|
|
29
|
-
safeVerifyTokenWithOptions: (token: string, secret: import("node_modules/@types/jsonwebtoken").Secret, options?: import("node_modules/@types/jsonwebtoken").VerifyOptions) => JWTUtils.VerificationResult;
|
|
30
|
-
hashPasswordWithPepper(password: string, pepper: string): Promise<string>;
|
|
31
|
-
hashPasswordWithPepperSync(password: string, pepper: string): string;
|
|
32
|
-
hashPassword: (password: string, saltRounds?: number) => Promise<string>;
|
|
33
|
-
hashPasswordSync: (password: string, saltRounds?: number) => string;
|
|
34
|
-
isPasswordStrong: (password: string, options?: import("./core/password/types").PasswordStrengthOptions) => boolean;
|
|
35
|
-
verifyPasswordWithPepper(password: string, pepper: string, hash: string): Promise<boolean>;
|
|
36
|
-
verifyPasswordWithPepperSync(password: string, pepper: string, hash: string): Promise<boolean>;
|
|
37
|
-
verifyPassword: (password: string, hash: string) => Promise<boolean>;
|
|
38
|
-
verifyPasswordSync: (password: string, hash: string) => boolean;
|
|
39
|
-
};
|
|
40
|
-
export default _default;
|
|
@@ -1,47 +0,0 @@
|
|
|
1
|
-
import type { JwtPayload, Secret } from 'jsonwebtoken';
|
|
2
|
-
export interface AccessToken extends String {
|
|
3
|
-
readonly __type: 'AccessToken';
|
|
4
|
-
}
|
|
5
|
-
export interface RefreshToken extends String {
|
|
6
|
-
readonly __type: 'RefreshToken';
|
|
7
|
-
}
|
|
8
|
-
export interface TokenPair {
|
|
9
|
-
accessToken: AccessToken;
|
|
10
|
-
refreshToken: RefreshToken;
|
|
11
|
-
}
|
|
12
|
-
export interface JWTConfig {
|
|
13
|
-
accessSecret: Secret;
|
|
14
|
-
refreshSecret: Secret;
|
|
15
|
-
accessExpiry?: string | number;
|
|
16
|
-
refreshExpiry?: string | number;
|
|
17
|
-
enableCaching?: boolean;
|
|
18
|
-
maxCacheSize?: number;
|
|
19
|
-
}
|
|
20
|
-
export interface TokenValidationOptions {
|
|
21
|
-
ignoreExpiration?: boolean;
|
|
22
|
-
ignoreNotBefore?: boolean;
|
|
23
|
-
audience?: string | string[];
|
|
24
|
-
issuer?: string;
|
|
25
|
-
algorithms?: string[];
|
|
26
|
-
}
|
|
27
|
-
export interface TokenGenerationOptions {
|
|
28
|
-
algorithm?: string;
|
|
29
|
-
expiresIn?: string | number;
|
|
30
|
-
audience?: string | string[];
|
|
31
|
-
issuer?: string;
|
|
32
|
-
subject?: string;
|
|
33
|
-
kid?: string;
|
|
34
|
-
}
|
|
35
|
-
export interface ITokenManager {
|
|
36
|
-
generateTokens(payload: Record<string, unknown>): Promise<TokenPair>;
|
|
37
|
-
generateAccessToken(payload: Record<string, unknown>): Promise<AccessToken>;
|
|
38
|
-
generateRefreshToken(payload: Record<string, unknown>): Promise<RefreshToken>;
|
|
39
|
-
verifyAccessToken(token: string): Promise<JwtPayload | string>;
|
|
40
|
-
verifyRefreshToken(token: string): Promise<JwtPayload | string>;
|
|
41
|
-
decodeToken(token: string, complete?: boolean): JwtPayload | string | null;
|
|
42
|
-
extractTokenFromHeader(authHeader: string): string | null;
|
|
43
|
-
validateToken(token: string, secret: Secret, options?: TokenValidationOptions): boolean;
|
|
44
|
-
rotateRefreshToken(oldToken: string): Promise<RefreshToken>;
|
|
45
|
-
isTokenExpired(token: string): boolean;
|
|
46
|
-
getTokenExpiration(token: string): Date | null;
|
|
47
|
-
}
|
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
export interface PasswordConfig {
|
|
2
|
-
saltRounds?: number;
|
|
3
|
-
minLength?: number;
|
|
4
|
-
maxLength?: number;
|
|
5
|
-
requireUppercase?: boolean;
|
|
6
|
-
requireLowercase?: boolean;
|
|
7
|
-
requireNumbers?: boolean;
|
|
8
|
-
requireSpecialChars?: boolean;
|
|
9
|
-
customRules?: PasswordRule[];
|
|
10
|
-
}
|
|
11
|
-
export interface PasswordRule {
|
|
12
|
-
test: (password: string) => boolean;
|
|
13
|
-
message: string;
|
|
14
|
-
}
|
|
15
|
-
export interface PasswordStrength {
|
|
16
|
-
score: number;
|
|
17
|
-
label: 'very-weak' | 'weak' | 'fair' | 'good' | 'strong';
|
|
18
|
-
feedback: string[];
|
|
19
|
-
suggestions: string[];
|
|
20
|
-
}
|
|
21
|
-
export interface PasswordValidationResult {
|
|
22
|
-
isValid: boolean;
|
|
23
|
-
errors: string[];
|
|
24
|
-
strength: PasswordStrength;
|
|
25
|
-
}
|
|
26
|
-
export interface HashedPassword {
|
|
27
|
-
hash: string;
|
|
28
|
-
salt: string;
|
|
29
|
-
}
|
|
30
|
-
export interface IPasswordManager {
|
|
31
|
-
hash(password: string, salt?: string): Promise<HashedPassword>;
|
|
32
|
-
verify(password: string, hash: string, salt: string): Promise<boolean>;
|
|
33
|
-
generate(length?: number, options?: PasswordConfig): string;
|
|
34
|
-
validate(password: string, config?: PasswordConfig): PasswordValidationResult;
|
|
35
|
-
checkStrength(password: string): PasswordStrength;
|
|
36
|
-
needsUpgrade(hash: string, currentConfig: PasswordConfig): boolean;
|
|
37
|
-
}
|
|
38
|
-
export interface IPasswordStrengthChecker {
|
|
39
|
-
analyze(password: string): PasswordStrength;
|
|
40
|
-
checkLength(password: string): {
|
|
41
|
-
valid: boolean;
|
|
42
|
-
message: string;
|
|
43
|
-
};
|
|
44
|
-
checkComplexity(password: string, config: PasswordConfig): {
|
|
45
|
-
valid: boolean;
|
|
46
|
-
message: string;
|
|
47
|
-
}[];
|
|
48
|
-
checkCommonPasswords(password: string): {
|
|
49
|
-
valid: boolean;
|
|
50
|
-
message: string;
|
|
51
|
-
};
|
|
52
|
-
checkSequential(password: string): {
|
|
53
|
-
valid: boolean;
|
|
54
|
-
message: string;
|
|
55
|
-
};
|
|
56
|
-
checkRepetition(password: string): {
|
|
57
|
-
valid: boolean;
|
|
58
|
-
message: string;
|
|
59
|
-
};
|
|
60
|
-
}
|