types added to return
This commit is contained in:
+10
-10
@@ -16,17 +16,17 @@ describe('JWT', () => {
|
|||||||
expect(token).toBeDefined();
|
expect(token).toBeDefined();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should verify a JWT', () => {
|
test('should return token object when valid token is provided', () => {
|
||||||
const token = jwt.generateJWT({ username: user.username, password: user.password });
|
// Arrange
|
||||||
const verified = jwt.verifyJWT(token);
|
const validToken = jwt.generateJWT(user);
|
||||||
//check if username inside the token is equal to the username of the user check this in two lines of code
|
|
||||||
expect(verified).toBeDefined();
|
// Act
|
||||||
if (typeof verified !== 'string') {
|
const result = jwt.verifyJWT(validToken);
|
||||||
// verified is JwtPayload
|
|
||||||
if(verified == null) fail('Token verification failed');
|
// Assert
|
||||||
expect(verified.username).toEqual(user.username);
|
if(result instanceof Object) {
|
||||||
|
expect(result.username).toEqual(user.username);
|
||||||
} else {
|
} else {
|
||||||
// Handle the case where verified is a string (token is blacklisted or invalid)
|
|
||||||
fail('Token verification failed');
|
fail('Token verification failed');
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
+17
-11
@@ -19,7 +19,7 @@ export default class JwtAuth{
|
|||||||
const vertoken = jwt.verify(token, secretKey);
|
const vertoken = jwt.verify(token, secretKey);
|
||||||
|
|
||||||
if(vertoken instanceof Object) {
|
if(vertoken instanceof Object) {
|
||||||
return vertoken;
|
return (vertoken);
|
||||||
} else {
|
} else {
|
||||||
return undefined;
|
return undefined;
|
||||||
}
|
}
|
||||||
@@ -30,22 +30,28 @@ export default class JwtAuth{
|
|||||||
|
|
||||||
decodeJWT(token: string) {
|
decodeJWT(token: string) {
|
||||||
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);
|
const decoded = jwt.decode(token);
|
||||||
|
if(decoded instanceof Object) {
|
||||||
|
return decoded;
|
||||||
|
} else {
|
||||||
|
return {valid: false, message: "Token is invalid."};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getJWTExpirationDate(token: string) {
|
getJWTExpirationDate(token: string): number | { valid: boolean, message: string} {
|
||||||
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 = this.decodeJWT(token) as { [key: string]: any };
|
const decoded = this.decodeJWT(token) as { [key: string]: any };
|
||||||
return decoded.exp ;
|
return decoded.exp ;
|
||||||
}
|
}
|
||||||
|
|
||||||
isJWTExpired(token: string) {
|
isJWTExpired(token: string): { valid: boolean, message: string} | boolean{
|
||||||
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 = this.getJWTExpirationDate(token);
|
const expirationDate = this.getJWTExpirationDate(token);
|
||||||
return expirationDate < Date.now();
|
if(expirationDate instanceof Object) return expirationDate;
|
||||||
|
return expirationDate < (Date.now() / 1000);
|
||||||
}
|
}
|
||||||
|
|
||||||
refreshJWT(token: string, settings: jwt.SignOptions = {}, secretKey = this.JWTSecretKey) {
|
refreshJWT(token: string, settings: jwt.SignOptions = {}, secretKey = this.JWTSecretKey): string | { valid: boolean, message: string} {
|
||||||
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 = this.verifyJWT(token, secretKey);
|
const decoded = this.verifyJWT(token, secretKey);
|
||||||
|
|
||||||
@@ -66,7 +72,7 @@ export default class JwtAuth{
|
|||||||
return { valid: false, message: "Token is invalid." };
|
return { valid: false, message: "Token is invalid." };
|
||||||
}
|
}
|
||||||
|
|
||||||
BlackListJWT(token: string) {
|
BlackListJWT(token: string): { valid: boolean, message: string}{
|
||||||
//check if token is already blacklisted
|
//check if token is already blacklisted
|
||||||
if (this.blacklist.includes(token)) {
|
if (this.blacklist.includes(token)) {
|
||||||
return { valid: false, message: "Token is already blacklisted." };
|
return { valid: false, message: "Token is already blacklisted." };
|
||||||
@@ -76,18 +82,18 @@ export default class JwtAuth{
|
|||||||
return { valid: true, message: "Token successfully blacklisted."};
|
return { valid: true, message: "Token successfully blacklisted."};
|
||||||
}
|
}
|
||||||
|
|
||||||
ClearBlackList() {
|
ClearBlackList(): { valid: boolean, message: string}{
|
||||||
//clear blacklist
|
//clear blacklist
|
||||||
this.blacklist = [];
|
this.blacklist = [];
|
||||||
return { valid: true, message: "Blacklist successfully cleared."};
|
return { valid: true, message: "Blacklist successfully cleared."};
|
||||||
}
|
}
|
||||||
|
|
||||||
GetBlackList() {
|
GetBlackList(): string[] {
|
||||||
//return blacklist
|
//return blacklist
|
||||||
return this.blacklist;
|
return this.blacklist;
|
||||||
}
|
}
|
||||||
|
|
||||||
RemoveFromBlackList(token: string) {
|
RemoveFromBlackList(token: string): { valid: boolean, message: string}{
|
||||||
//remove token from blacklist
|
//remove token from blacklist
|
||||||
if (this.blacklist.includes(token)) {
|
if (this.blacklist.includes(token)) {
|
||||||
this.blacklist = this.blacklist.filter((item) => item !== token);
|
this.blacklist = this.blacklist.filter((item) => item !== token);
|
||||||
@@ -96,7 +102,7 @@ export default class JwtAuth{
|
|||||||
return { valid: false, message: "Token is not blacklisted." };
|
return { valid: false, message: "Token is not blacklisted." };
|
||||||
}
|
}
|
||||||
|
|
||||||
IsBlackListed(token: string) {
|
IsBlackListed(token: string): { valid: boolean, message: string}{
|
||||||
//check if token is blacklisted
|
//check if token is blacklisted
|
||||||
if (this.blacklist.includes(token)) {
|
if (this.blacklist.includes(token)) {
|
||||||
return { valid: true, message: "Token is blacklisted." };
|
return { valid: true, message: "Token is blacklisted." };
|
||||||
|
|||||||
+1
-1
@@ -13,7 +13,7 @@ export default class PasswordGenerator{
|
|||||||
this.options = { ...defaultOptions, ...options };
|
this.options = { ...defaultOptions, ...options };
|
||||||
}
|
}
|
||||||
|
|
||||||
Generate(length: number = 0){
|
Generate(length: number = 0): string{
|
||||||
// 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;
|
||||||
|
|
||||||
|
|||||||
+2
-2
@@ -13,7 +13,7 @@ export default class PassPolicy {
|
|||||||
this.options = { ...defaultOptions, ...options };
|
this.options = { ...defaultOptions, ...options };
|
||||||
}
|
}
|
||||||
|
|
||||||
validate(password: string) {
|
validate(password: string): { valid: boolean, message?: string }{
|
||||||
const { minLength, maxLength, minLower, minUpper, minNum, minSpecial, specialChars } = this.options;
|
const { minLength, maxLength, minLower, minUpper, minNum, minSpecial, specialChars } = this.options;
|
||||||
|
|
||||||
if (password.length < minLength || password.length > maxLength) {
|
if (password.length < minLength || password.length > maxLength) {
|
||||||
@@ -46,7 +46,7 @@ export default class PassPolicy {
|
|||||||
return { valid: true };
|
return { valid: true };
|
||||||
}
|
}
|
||||||
|
|
||||||
CheckDifference(newPassword: string, oldPassword: string, neededDifference: number = 3) {
|
CheckDifference(newPassword: string, oldPassword: string, neededDifference: number = 3): { valid: boolean, message?: string }{
|
||||||
//check if new password is different from old password
|
//check if new password is different from old password
|
||||||
if (newPassword === oldPassword) {
|
if (newPassword === oldPassword) {
|
||||||
return { valid: false, message: "New password must be different from old password." };
|
return { valid: false, message: "New password must be different from old password." };
|
||||||
|
|||||||
@@ -9,11 +9,11 @@ export default class PassCheck{
|
|||||||
this.PassPolicy = new PassPolicy(PassPolicyOptions);
|
this.PassPolicy = new PassPolicy(PassPolicyOptions);
|
||||||
}
|
}
|
||||||
|
|
||||||
async verifyPassword(password: string, hash: string) {
|
async verifyPassword(password: string, hash: string): Promise<boolean> {
|
||||||
return await bcrypt.compare(password, hash);
|
return await bcrypt.compare(password, hash);
|
||||||
}
|
}
|
||||||
|
|
||||||
async hashPassword(password: string) {
|
async hashPassword(password: string): Promise<string> {
|
||||||
const salt = await bcrypt.genSalt(this.BcryptSaltRounds);
|
const salt = await bcrypt.genSalt(this.BcryptSaltRounds);
|
||||||
const hash = await bcrypt.hash(password, salt);
|
const hash = await bcrypt.hash(password, salt);
|
||||||
return hash;
|
return hash;
|
||||||
|
|||||||
+15
-15
@@ -17,7 +17,7 @@ export default class RateLimit {
|
|||||||
this.events = events;
|
this.events = events;
|
||||||
}
|
}
|
||||||
|
|
||||||
public addEvent(event: Event) {
|
public addEvent(event: Event): boolean {
|
||||||
try{
|
try{
|
||||||
this.events[event.name] = event;
|
this.events[event.name] = event;
|
||||||
|
|
||||||
@@ -32,7 +32,7 @@ export default class RateLimit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public removeEvent(name: string) {
|
public removeEvent(name: string): boolean {
|
||||||
try{
|
try{
|
||||||
delete this.events[name];
|
delete this.events[name];
|
||||||
|
|
||||||
@@ -47,7 +47,7 @@ export default class RateLimit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public addUser(token: string) {
|
public addUser(token: string): boolean {
|
||||||
try{
|
try{
|
||||||
this.users[token] = { token, events: this.events };
|
this.users[token] = { token, events: this.events };
|
||||||
return true;
|
return true;
|
||||||
@@ -56,7 +56,7 @@ export default class RateLimit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public removeUser(token: string) {
|
public removeUser(token: string): boolean {
|
||||||
try{
|
try{
|
||||||
delete this.users[token];
|
delete this.users[token];
|
||||||
return true;
|
return true;
|
||||||
@@ -65,7 +65,7 @@ export default class RateLimit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public attempt(token: string, name: string) {
|
public attempt(token: string, name: string): boolean {
|
||||||
try{
|
try{
|
||||||
if (!this.users[token] || !this.users[token].events[name]) {
|
if (!this.users[token] || !this.users[token].events[name]) {
|
||||||
return false;
|
return false;
|
||||||
@@ -96,23 +96,23 @@ export default class RateLimit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public getEvents() {
|
public getEvents(): { [name: string]: Event}{
|
||||||
return this.events;
|
return this.events;
|
||||||
}
|
}
|
||||||
|
|
||||||
public getUsers() {
|
public getUsers(): { [token: string]: user}{
|
||||||
return this.users;
|
return this.users;
|
||||||
}
|
}
|
||||||
|
|
||||||
public getEvent(name: string) {
|
public getEvent(name: string): Event {
|
||||||
return this.events[name];
|
return this.events[name];
|
||||||
}
|
}
|
||||||
|
|
||||||
public getUser(token: string) {
|
public getUser(token: string): user {
|
||||||
return this.users[token];
|
return this.users[token];
|
||||||
}
|
}
|
||||||
|
|
||||||
public remainingAttempts(token: string, name: string) {
|
public remainingAttempts(token: string, name: string): number {
|
||||||
try{
|
try{
|
||||||
if (!this.users[token] || !this.users[token].events[name]) {
|
if (!this.users[token] || !this.users[token].events[name]) {
|
||||||
return -1;
|
return -1;
|
||||||
@@ -130,7 +130,7 @@ export default class RateLimit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public resetAttempts(token: string, name: string) {
|
public resetAttempts(token: string, name: string): boolean {
|
||||||
try{
|
try{
|
||||||
if (!this.users[token] || !this.users[token].events[name]) {
|
if (!this.users[token] || !this.users[token].events[name]) {
|
||||||
return false;
|
return false;
|
||||||
@@ -145,7 +145,7 @@ export default class RateLimit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public resetAllAttempts(token: string) {
|
public resetAllAttempts(token: string): boolean {
|
||||||
try{
|
try{
|
||||||
if (!this.users[token]) {
|
if (!this.users[token]) {
|
||||||
return false;
|
return false;
|
||||||
@@ -161,7 +161,7 @@ export default class RateLimit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public resetAllUsers() {
|
public resetAllUsers(): boolean {
|
||||||
try{
|
try{
|
||||||
for (const token in this.users) {
|
for (const token in this.users) {
|
||||||
this.resetAllAttempts(token);
|
this.resetAllAttempts(token);
|
||||||
@@ -173,7 +173,7 @@ export default class RateLimit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public resetEvent(name: string) {
|
public resetEvent(name: string): boolean {
|
||||||
try{
|
try{
|
||||||
for (const token in this.users) {
|
for (const token in this.users) {
|
||||||
this.resetAttempts(token, name);
|
this.resetAttempts(token, name);
|
||||||
@@ -185,7 +185,7 @@ export default class RateLimit {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public resetUser(token: string) {
|
public resetUser(token: string): boolean {
|
||||||
try{
|
try{
|
||||||
for (const name in this.users[token].events) {
|
for (const name in this.users[token].events) {
|
||||||
this.resetAttempts(token, name);
|
this.resetAttempts(token, name);
|
||||||
|
|||||||
Reference in New Issue
Block a user