removed most async funtions

This commit is contained in:
2024-01-23 15:48:05 +01:00
parent 23b873767f
commit ae41f81aaa
6 changed files with 62 additions and 28 deletions
+41 -2
View File
@@ -13,7 +13,7 @@ This library is easy to integrate into your project and offers a comprehensive s
To install Guardian, simply run the following command using npm: To install Guardian, simply run the following command using npm:
```bash ```bash
npm install Guardian npm install auth-guardian
``` ```
## Features ## Features
@@ -40,6 +40,44 @@ Guardian's `PasswordGenerator` class allows you to generate strong, random passw
## Usage/Examples ## Usage/Examples
### for imports
#### import only jwtAuth
```javascript
const { JwtAuth } = require('guardian');
```
#### import only passPolicy
```javascript
const { PassPolicy } = require('guardian');
```
#### import only passCheck
```javascript
const { PassCheck } = require('guardian');
```
#### import only rateLimiter
```javascript
const { RateLimiter } = require('guardian');
```
#### import only passwordGenerator
```javascript
const { PasswordGenerator } = require('guardian');
```
#### import all
```javascript
const { JwtAuth, PassPolicy, PassCheck, RateLimiter, PasswordGenerator } = require('guardian');
```
### JSON Web Token (JWT) Management - JwtAuth ### JSON Web Token (JWT) Management - JwtAuth
#### Initialize JwtAuth #### Initialize JwtAuth
@@ -257,7 +295,8 @@ const randomPassword = passwordGenerator.Generate();
## Authors ## Authors
- [kajvan](https://www.github.com/kajvan) - [kajvan](https://www.github.com/kajvans)
- [kajvan](https://gitea.quiztimes.nl/kajvans)
## License ## License
+2 -2
View File
@@ -1,12 +1,12 @@
const PassCheck = require('./passwordcheck'); const PassCheck = require('./passwordcheck');
const JWTManager = require('./jwt'); const JWTAuth = require('./jwt');
const RateLimiter = require('./ratelimit'); const RateLimiter = require('./ratelimit');
const PassPolicy = require('./passpolicy'); const PassPolicy = require('./passpolicy');
const PasswordGenerator = require('./passgen'); const PasswordGenerator = require('./passgen');
module.exports = { module.exports = {
PassCheck, PassCheck,
JWTManager, JWTAuth,
RateLimiter, RateLimiter,
PassPolicy, PassPolicy,
PasswordGenerator PasswordGenerator
+10 -10
View File
@@ -6,36 +6,36 @@ class JwtAuth{
this.blacklist = []; this.blacklist = [];
} }
async generateJWT(payload, settings, secretKey = this.JWTSecretKey) { generateJWT(payload, settings, secretKey = this.JWTSecretKey) {
return jwt.sign(payload, secretKey, settings); return jwt.sign(payload, secretKey, settings);
} }
async verifyJWT(token, secretKey = this.JWTSecretKey) { verifyJWT(token, secretKey = this.JWTSecretKey) {
if(this.blacklist.includes(token)) return { valid: false, message: "Token is blacklisted." }; if(this.blacklist.includes(token)) return { valid: false, message: "Token is blacklisted." };
return jwt.verify(token, secretKey); return jwt.verify(token, secretKey);
} }
async decodeJWT(token) { decodeJWT(token) {
if(this.blacklist.includes(token)) return { valid: false, message: "Token is blacklisted." }; if(this.blacklist.includes(token)) return { valid: false, message: "Token is blacklisted." };
return jwt.decode(token); return jwt.decode(token);
} }
async getJWTExpirationDate(token) { getJWTExpirationDate(token) {
if(this.blacklist.includes(token)) return { valid: false, message: "Token is blacklisted." }; if(this.blacklist.includes(token)) return { valid: false, message: "Token is blacklisted." };
const decoded = await this.decodeJWT(token); const decoded = this.decodeJWT(token);
return decoded.exp; return decoded.exp;
} }
async isJWTExpired(token) { isJWTExpired(token) {
if(this.blacklist.includes(token)) return { valid: false, message: "Token is blacklisted." }; if(this.blacklist.includes(token)) return { valid: false, message: "Token is blacklisted." };
const expirationDate = await this.getJWTExpirationDate(token); const expirationDate = this.getJWTExpirationDate(token);
return expirationDate < Date.now(); return expirationDate < Date.now();
} }
async refreshJWT(token, settings, secretKey = this.JWTSecretKey) { refreshJWT(token, settings, secretKey = this.JWTSecretKey) {
if(this.blacklist.includes(token)) return { valid: false, message: "Token is blacklisted." }; if(this.blacklist.includes(token)) return { valid: false, message: "Token is blacklisted." };
const decoded = await this.verifyJWT(token, secretKey); const decoded = this.verifyJWT(token, secretKey);
const newToken = await this.generateJWT(decoded, settings, secretKey); const newToken = this.generateJWT(decoded, settings, secretKey);
return newToken; return newToken;
} }
+7 -12
View File
@@ -1,12 +1,12 @@
{ {
"name": "guardian", "name": "auth-guardian",
"version": "0.0.1", "version": "1.0.2",
"lockfileVersion": 3, "lockfileVersion": 3,
"requires": true, "requires": true,
"packages": { "packages": {
"": { "": {
"name": "guardian", "name": "auth-guardian",
"version": "0.0.1", "version": "1.0.2",
"license": "MIT", "license": "MIT",
"dependencies": { "dependencies": {
"bcrypt": "^5.1.1", "bcrypt": "^5.1.1",
@@ -147,11 +147,6 @@
} }
} }
}, },
"node_modules/debug/node_modules/ms": {
"version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
},
"node_modules/delegates": { "node_modules/delegates": {
"version": "1.0.0", "version": "1.0.0",
"resolved": "https://registry.npmjs.org/delegates/-/delegates-1.0.0.tgz", "resolved": "https://registry.npmjs.org/delegates/-/delegates-1.0.0.tgz",
@@ -444,9 +439,9 @@
} }
}, },
"node_modules/ms": { "node_modules/ms": {
"version": "2.1.3", "version": "2.1.2",
"resolved": "https://registry.npmjs.org/ms/-/ms-2.1.3.tgz", "resolved": "https://registry.npmjs.org/ms/-/ms-2.1.2.tgz",
"integrity": "sha512-6FlzubTLZG3J2a/NVCAleEhjzq5oxgHyaCU9yYXvcLsvoVaHJq/s5xXI6/XXP6tz7R9xAOtHnSO/tXtF3WRTlA==" "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w=="
}, },
"node_modules/node-addon-api": { "node_modules/node-addon-api": {
"version": "5.1.0", "version": "5.1.0",
+1 -1
View File
@@ -1,6 +1,6 @@
{ {
"name": "auth-guardian", "name": "auth-guardian",
"version": "1.0.1", "version": "1.0.2",
"description": "auth-guardian a library for enhancing application security", "description": "auth-guardian a library for enhancing application security",
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
+1 -1
View File
@@ -12,7 +12,7 @@ class PasswordGenerator{
this.options = { ...defaultOptions, ...options }; this.options = { ...defaultOptions, ...options };
} }
Generate(){ async Generate(){
// Generate random password that complies with the options // Generate random password that complies with the options
const { minLength, maxLength, minLower, minUpper, minNum, minSpecial, specialChars } = this.options; const { minLength, maxLength, minLower, minUpper, minNum, minSpecial, specialChars } = this.options;