Guardian is a powerful JavaScript library designed to fortify your application's security with essential features for password management, authentication, and rate limiting. Whether you're developing user authentication, safeguarding against brute-force attacks, or enforcing password policies, Guardian has got you covered.
This library is easy to integrate into your project and offers a comprehensive set of features, making it a reliable choice for enhancing the security of your application.
you can run it with TypeScript or JavaScript.
To install Guardian, simply run the following command using npm:
npm i auth-guardian
Guardian simplifies the generation and verification of JSON Web Tokens (JWTs) with its JwtAuth
class. JWTs are widely used for user authentication, single sign-on, and session management. Guardian streamlines JWT handling to ensure a secure authentication process in your application.
With Guardian's PassPolicy
class, you can enforce strong password complexity rules. Define minimum length, character requirements, and other policy settings to ensure that your users' passwords meet stringent security standards. Guardian helps protect your users' accounts by enforcing robust password policies.
Guardian simplifies the secure hashing and verification of passwords using the bcrypt algorithm through its PassCheck
class. Bcrypt is a well-established and trusted password hashing algorithm. Guardian ensures that your password management is secure, enhancing the overall safety of your user data.
Guardian offers a RateLimiter
class to implement rate limits on actions like login attempts. Protect your application from brute-force attacks and excessive usage by controlling the rate at which users can perform specific actions. Guardian adds an extra layer of security to your application by preventing abuse.
Guardian's PasswordGenerator
class allows you to generate strong, random passwords based on your specified criteria. This feature is invaluable when creating secure user accounts, managing password resets, or enhancing password security in your application.
const { JwtAuth } = require('guardian');
const { PassPolicy } = require('guardian');
const { PassCheck } = require('guardian');
const { RateLimiter } = require('guardian');
const { PasswordGenerator } = require('guardian');
const { JwtAuth, PassPolicy, PassCheck, RateLimiter, PasswordGenerator } = require('guardian');
const jwtAuth = new JwtAuth('mySecretKey');
const token = await jwtAuth.generateJWT({ userId: 123 }, { expiresIn: '1h' });
const result = await jwtAuth.verifyJWT(token);
const payload = await jwtAuth.decodeJWT(token);
const expirationDate = await jwtAuth.getJWTExpirationDate(token);
const isExpired = await jwtAuth.isJWTExpired(token);
const refreshedToken = await jwtAuth.refreshJWT(token, { expiresIn: '1h' });
const result = await jwtAuth.blacklistJWT(token);
const passPolicy = new PassPolicy({
minLength: 8,
minUpper: 1,
minLower: 1,
minNum: 1,
minSpecial: 1,
specialChars: "!@#$%^&*()_+~`|}{[]:;?><,./-=",
});
const validation = passPolicy.validate('Strong1@Password');
const differenceValidation = passPolicy.checkDifference('NewPassword123', 'OldPassword123', 5);
const passCheck = new PassCheck(10, { minLength: 8, requireDigits: true });
const isMatch = await passCheck.verifyPassword('myPassword', 'hashedPassword');
const hashedPassword = await passCheck.hashPassword('myPassword');
const rateLimiter = new RateLimiter({
login: { max: 5, timespan: 60000 },
signup: { max: 3, timespan: 3600000 },
});
const userLimits = rateLimiter.addUser('user123');
const eventLimits = rateLimiter.addEvent('login', 5, 60000);
rateLimiter.removeUser('user123');
const result = rateLimiter.attemptEvent('user123', 'login');
rateLimiter.resetEventUser('user123', 'login');
rateLimiter.resetEvent('login');
rateLimiter.resetUser('user123');
rateLimiter.resetAll();
const lastAttemptTime = rateLimiter.lastAttempt('user123', 'login');
const allAttempts = rateLimiter.userAttempts('user123', 'login');
const remaining = rateLimiter.remainingAttempts('user123', 'login');
const options = {
minLength: 8,
maxLength: 16,
minLower: 2,
minUpper: 2,
minNum: 2,
minSpecial: 2,
specialChars: "!@#$%^&*()_+~`|}{[]:;?><,./-="
};
const passwordGenerator = new PasswordGenerator(options);
const randomPassword = passwordGenerator.Generate();
-
Guardian offers a comprehensive set of features for enhancing the security of your applications, including JWT handling, password validation, hashing, rate limiting, and password generation.
-
Each feature is encapsulated within its respective class, making it easy to integrate into your project.
-
These classes provide asynchronous methods that return promises, making them suitable for use in asynchronous code.
-
Ensure that you handle errors appropriately, especially when working with rate limiting to account for cases where users or events are not found or other errors occur.
-
The
PassPolicy
class provides methods for validating passwords based on complexity rules and checking the difference between old and new passwords. -
The
PassCheck
class securely hashes passwords using bcrypt for storage and verification. -
The
RateLimiter
class manages rate limits for events, useful for protecting against abuse or overuse of specific functionality. -
The
JwtAuth
class simplifies JWT generation, verification, and management, including a token blacklist. -
The
PasswordGenerator
class creates random passwords based on specified criteria.
Guardian - Secure your applications with confidence!