@naman_deep_singh/security 1.3.0 → 1.3.2
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/cryptoManager.js +9 -3
- package/dist/cjs/core/crypto/decrypt.js +6 -6
- package/dist/cjs/core/crypto/encrypt.js +4 -4
- package/dist/cjs/core/crypto/hmac.js +1 -1
- package/dist/cjs/core/crypto/index.d.ts +5 -5
- package/dist/cjs/core/crypto/random.js +2 -2
- package/dist/cjs/core/jwt/decode.d.ts +1 -1
- package/dist/cjs/core/jwt/decode.js +2 -2
- package/dist/cjs/core/jwt/extractToken.js +7 -7
- package/dist/cjs/core/jwt/generateTokens.d.ts +2 -2
- package/dist/cjs/core/jwt/generateTokens.js +10 -6
- package/dist/cjs/core/jwt/index.d.ts +8 -8
- package/dist/cjs/core/jwt/jwtManager.d.ts +3 -2
- package/dist/cjs/core/jwt/jwtManager.js +66 -86
- package/dist/cjs/core/jwt/parseDuration.js +3 -3
- package/dist/cjs/core/jwt/signToken.d.ts +1 -1
- package/dist/cjs/core/jwt/signToken.js +7 -7
- package/dist/cjs/core/jwt/types.d.ts +1 -1
- package/dist/cjs/core/jwt/validateToken.d.ts +2 -2
- package/dist/cjs/core/jwt/validateToken.js +3 -3
- package/dist/cjs/core/jwt/verify.d.ts +3 -2
- package/dist/cjs/core/password/hash.js +1 -1
- package/dist/cjs/core/password/index.d.ts +3 -3
- package/dist/cjs/core/password/passwordManager.d.ts +1 -1
- package/dist/cjs/core/password/passwordManager.js +32 -31
- package/dist/cjs/core/password/strength.d.ts +1 -1
- package/dist/cjs/core/password/strength.js +4 -4
- package/dist/cjs/core/password/utils.js +2 -2
- package/dist/cjs/core/password/verify.js +1 -1
- package/dist/cjs/index.d.ts +6 -6
- package/dist/cjs/index.js +2 -2
- package/dist/cjs/interfaces/jwt.interface.d.ts +1 -1
- package/dist/esm/core/crypto/cryptoManager.js +10 -4
- package/dist/esm/core/crypto/decrypt.js +7 -7
- package/dist/esm/core/crypto/encrypt.js +5 -5
- package/dist/esm/core/crypto/hmac.js +2 -2
- package/dist/esm/core/crypto/index.d.ts +5 -5
- package/dist/esm/core/crypto/index.js +5 -5
- package/dist/esm/core/crypto/random.js +3 -3
- package/dist/esm/core/jwt/decode.d.ts +1 -1
- package/dist/esm/core/jwt/decode.js +3 -3
- package/dist/esm/core/jwt/extractToken.js +7 -7
- package/dist/esm/core/jwt/generateTokens.d.ts +2 -2
- package/dist/esm/core/jwt/generateTokens.js +12 -8
- package/dist/esm/core/jwt/index.d.ts +8 -8
- package/dist/esm/core/jwt/index.js +8 -8
- package/dist/esm/core/jwt/jwtManager.d.ts +3 -2
- package/dist/esm/core/jwt/jwtManager.js +70 -90
- package/dist/esm/core/jwt/parseDuration.js +3 -3
- package/dist/esm/core/jwt/signToken.d.ts +1 -1
- package/dist/esm/core/jwt/signToken.js +9 -9
- package/dist/esm/core/jwt/types.d.ts +1 -1
- package/dist/esm/core/jwt/validateToken.d.ts +2 -2
- package/dist/esm/core/jwt/validateToken.js +3 -3
- package/dist/esm/core/jwt/verify.d.ts +3 -2
- package/dist/esm/core/jwt/verify.js +1 -1
- package/dist/esm/core/password/hash.js +3 -3
- package/dist/esm/core/password/index.d.ts +3 -3
- package/dist/esm/core/password/index.js +3 -3
- package/dist/esm/core/password/passwordManager.d.ts +1 -1
- package/dist/esm/core/password/passwordManager.js +34 -33
- package/dist/esm/core/password/strength.d.ts +1 -1
- package/dist/esm/core/password/strength.js +5 -5
- package/dist/esm/core/password/utils.js +4 -4
- package/dist/esm/core/password/verify.js +2 -2
- package/dist/esm/index.d.ts +6 -6
- package/dist/esm/index.js +7 -7
- package/dist/esm/interfaces/jwt.interface.d.ts +1 -1
- package/dist/types/core/crypto/index.d.ts +5 -5
- package/dist/types/core/jwt/decode.d.ts +1 -1
- package/dist/types/core/jwt/generateTokens.d.ts +2 -2
- package/dist/types/core/jwt/index.d.ts +8 -8
- package/dist/types/core/jwt/jwtManager.d.ts +3 -2
- package/dist/types/core/jwt/signToken.d.ts +1 -1
- package/dist/types/core/jwt/types.d.ts +1 -1
- package/dist/types/core/jwt/validateToken.d.ts +2 -2
- package/dist/types/core/jwt/verify.d.ts +3 -2
- package/dist/types/core/password/index.d.ts +3 -3
- package/dist/types/core/password/passwordManager.d.ts +1 -1
- package/dist/types/core/password/strength.d.ts +1 -1
- package/dist/types/index.d.ts +6 -6
- package/dist/types/interfaces/jwt.interface.d.ts +1 -1
- package/package.json +5 -4
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { BadRequestError, ValidationError } from
|
|
1
|
+
import { BadRequestError, ValidationError, } from '@naman_deep_singh/errors-utils';
|
|
2
2
|
export const isPasswordStrong = (password, options = {}) => {
|
|
3
3
|
if (!password)
|
|
4
4
|
throw new BadRequestError('Invalid password provided');
|
|
@@ -6,12 +6,12 @@ export const isPasswordStrong = (password, options = {}) => {
|
|
|
6
6
|
if (password.length < minLength)
|
|
7
7
|
throw new ValidationError(`Password must be at least ${minLength} characters`);
|
|
8
8
|
if (requireUppercase && !/[A-Z]/.test(password))
|
|
9
|
-
throw new ValidationError(
|
|
9
|
+
throw new ValidationError('Password must include uppercase letters');
|
|
10
10
|
if (requireLowercase && !/[a-z]/.test(password))
|
|
11
|
-
throw new ValidationError(
|
|
11
|
+
throw new ValidationError('Password must include lowercase letters');
|
|
12
12
|
if (requireNumbers && !/[0-9]/.test(password))
|
|
13
|
-
throw new ValidationError(
|
|
13
|
+
throw new ValidationError('Password must include numbers');
|
|
14
14
|
if (requireSymbols && !/[^A-Za-z0-9]/.test(password))
|
|
15
|
-
throw new ValidationError(
|
|
15
|
+
throw new ValidationError('Password must include symbols');
|
|
16
16
|
return true;
|
|
17
17
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import crypto from
|
|
2
|
-
import { BadRequestError } from
|
|
1
|
+
import crypto from 'crypto';
|
|
2
|
+
import { BadRequestError } from '@naman_deep_singh/errors-utils';
|
|
3
3
|
export function ensureValidPassword(password) {
|
|
4
|
-
if (!password || typeof password !==
|
|
4
|
+
if (!password || typeof password !== 'string') {
|
|
5
5
|
throw new BadRequestError('Invalid password provided');
|
|
6
6
|
}
|
|
7
7
|
}
|
|
@@ -25,5 +25,5 @@ export function estimatePasswordEntropy(password) {
|
|
|
25
25
|
return password.length * Math.log2(pool);
|
|
26
26
|
}
|
|
27
27
|
export function normalizePassword(password) {
|
|
28
|
-
return password.normalize(
|
|
28
|
+
return password.normalize('NFKC');
|
|
29
29
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import
|
|
2
|
-
import
|
|
1
|
+
import { UnauthorizedError } from '@naman_deep_singh/errors-utils';
|
|
2
|
+
import bcrypt from 'bcryptjs';
|
|
3
3
|
/**
|
|
4
4
|
* Compare a password with a stored hash asynchronously.
|
|
5
5
|
*/
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export { BadRequestError, UnauthorizedError, ValidationError, InternalServerError } from
|
|
5
|
-
import * as
|
|
6
|
-
import * as
|
|
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 CryptoUtils from './core/crypto';
|
|
6
|
+
import * as JWTUtils from './core/jwt';
|
|
7
7
|
declare const _default: {
|
|
8
8
|
decrypt: (data: string, secret: string) => string;
|
|
9
9
|
encrypt: (text: string, secret: string) => string;
|
package/dist/esm/index.js
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
1
|
+
export * from './core/password';
|
|
2
|
+
export * from './core/jwt';
|
|
3
|
+
export * from './core/crypto';
|
|
4
4
|
// Re-export common errors for convenience
|
|
5
|
-
export { BadRequestError, UnauthorizedError, ValidationError, InternalServerError } from
|
|
6
|
-
import * as
|
|
7
|
-
import * as JWTUtils from
|
|
8
|
-
import * as
|
|
5
|
+
export { BadRequestError, UnauthorizedError, ValidationError, InternalServerError, } from '@naman_deep_singh/errors-utils';
|
|
6
|
+
import * as CryptoUtils from './core/crypto';
|
|
7
|
+
import * as JWTUtils from './core/jwt';
|
|
8
|
+
import * as PasswordUtils from './core/password';
|
|
9
9
|
export default {
|
|
10
10
|
...PasswordUtils,
|
|
11
11
|
...JWTUtils,
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { decrypt } from
|
|
2
|
-
export { encrypt } from
|
|
3
|
-
export { hmacSign, hmacVerify } from
|
|
4
|
-
export { randomToken, generateStrongPassword } from
|
|
5
|
-
export * from
|
|
1
|
+
export { decrypt } from './decrypt';
|
|
2
|
+
export { encrypt } from './encrypt';
|
|
3
|
+
export { hmacSign, hmacVerify } from './hmac';
|
|
4
|
+
export { randomToken, generateStrongPassword } from './random';
|
|
5
|
+
export * from './cryptoManager';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Secret } from
|
|
2
|
-
import { RefreshToken, TokenPair } from
|
|
1
|
+
import { type Secret } from 'jsonwebtoken';
|
|
2
|
+
import type { RefreshToken, TokenPair } from './types';
|
|
3
3
|
export declare const generateTokens: (payload: Record<string, unknown>, accessSecret: Secret, refreshSecret: Secret, accessExpiry?: string | number, refreshExpiry?: string | number) => TokenPair;
|
|
4
4
|
export declare function rotateRefreshToken(oldToken: string, secret: Secret): RefreshToken;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export * from
|
|
5
|
-
export * from
|
|
6
|
-
export * from
|
|
7
|
-
export * from
|
|
8
|
-
export * from
|
|
1
|
+
export * from './decode';
|
|
2
|
+
export * from './extractToken';
|
|
3
|
+
export * from './generateTokens';
|
|
4
|
+
export * from './parseDuration';
|
|
5
|
+
export * from './signToken';
|
|
6
|
+
export * from './types';
|
|
7
|
+
export * from './validateToken';
|
|
8
|
+
export * from './verify';
|
|
@@ -1,11 +1,12 @@
|
|
|
1
|
-
import { JwtPayload, Secret } from
|
|
2
|
-
import {
|
|
1
|
+
import { type JwtPayload, type Secret } from 'jsonwebtoken';
|
|
2
|
+
import type { AccessToken, ITokenManager, JWTConfig, RefreshToken, TokenPair, TokenValidationOptions } from '../../interfaces/jwt.interface';
|
|
3
3
|
export declare class JWTManager implements ITokenManager {
|
|
4
4
|
private accessSecret;
|
|
5
5
|
private refreshSecret;
|
|
6
6
|
private accessExpiry;
|
|
7
7
|
private refreshExpiry;
|
|
8
8
|
private cache?;
|
|
9
|
+
private cacheTTL;
|
|
9
10
|
constructor(config: JWTConfig);
|
|
10
11
|
/**
|
|
11
12
|
* Generate both access and refresh tokens
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { Secret, SignOptions } from
|
|
1
|
+
import { type Secret, type SignOptions } from 'jsonwebtoken';
|
|
2
2
|
export declare const signToken: (payload: Record<string, unknown>, secret: Secret, expiresIn?: string | number, options?: SignOptions) => string;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import { JwtPayload } from
|
|
1
|
+
import type { JwtPayload } from 'node_modules/@types/jsonwebtoken';
|
|
2
2
|
export interface TokenRequirements {
|
|
3
3
|
requiredFields?: string[];
|
|
4
4
|
forbiddenFields?: string[];
|
|
5
|
-
validateTypes?: Record<string,
|
|
5
|
+
validateTypes?: Record<string, 'string' | 'number' | 'boolean'>;
|
|
6
6
|
}
|
|
7
7
|
export declare function validateTokenPayload(payload: Record<string, unknown>, rules?: TokenRequirements): {
|
|
8
8
|
valid: true;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import jwt
|
|
2
|
-
import {
|
|
1
|
+
import type jwt from 'jsonwebtoken';
|
|
2
|
+
import { type JwtPayload, type Secret } from 'jsonwebtoken';
|
|
3
|
+
import type { VerificationResult } from './types';
|
|
3
4
|
/**
|
|
4
5
|
* Verify token (throws if invalid or expired)
|
|
5
6
|
*/
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
1
|
+
export * from './hash';
|
|
2
|
+
export * from './strength';
|
|
3
|
+
export * from './verify';
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { IPasswordManager, PasswordConfig,
|
|
1
|
+
import type { HashedPassword, IPasswordManager, PasswordConfig, PasswordStrength, PasswordValidationResult } from '../../interfaces/password.interface';
|
|
2
2
|
export declare class PasswordManager implements IPasswordManager {
|
|
3
3
|
private defaultConfig;
|
|
4
4
|
constructor(config?: PasswordConfig);
|
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { PasswordStrengthOptions } from
|
|
1
|
+
import type { PasswordStrengthOptions } from './types';
|
|
2
2
|
export declare const isPasswordStrong: (password: string, options?: PasswordStrengthOptions) => boolean;
|
package/dist/types/index.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
export * from
|
|
2
|
-
export * from
|
|
3
|
-
export * from
|
|
4
|
-
export { BadRequestError, UnauthorizedError, ValidationError, InternalServerError } from
|
|
5
|
-
import * as
|
|
6
|
-
import * as
|
|
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 CryptoUtils from './core/crypto';
|
|
6
|
+
import * as JWTUtils from './core/jwt';
|
|
7
7
|
declare const _default: {
|
|
8
8
|
decrypt: (data: string, secret: string) => string;
|
|
9
9
|
encrypt: (text: string, secret: string) => string;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naman_deep_singh/security",
|
|
3
|
-
"version": "1.3.
|
|
3
|
+
"version": "1.3.2",
|
|
4
4
|
"description": "Security utilities for password hashing and JWT token management with TypeScript",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/cjs/index.js",
|
|
@@ -28,15 +28,16 @@
|
|
|
28
28
|
"author": "Naman Deep Singh",
|
|
29
29
|
"license": "ISC",
|
|
30
30
|
"dependencies": {
|
|
31
|
-
"@naman_deep_singh/errors-utils": "^1.
|
|
31
|
+
"@naman_deep_singh/errors-utils": "^1.1.1",
|
|
32
|
+
"@naman_deep_singh/js-extensions": "^1.3.2",
|
|
32
33
|
"bcryptjs": "^3.0.3",
|
|
33
34
|
"jsonwebtoken": "^9.0.2"
|
|
34
35
|
},
|
|
35
36
|
"devDependencies": {
|
|
36
37
|
"@types/bcryptjs": "^2.4.6",
|
|
37
38
|
"@types/jsonwebtoken": "^9.0.10",
|
|
38
|
-
"
|
|
39
|
-
"
|
|
39
|
+
"rimraf": "^5.0.5",
|
|
40
|
+
"typescript": "^5.9.3"
|
|
40
41
|
},
|
|
41
42
|
"scripts": {
|
|
42
43
|
"build": "pnpm run build:types && tsc -p tsconfig.cjs.json && tsc -p tsconfig.esm.json",
|