From 4256fe9a7288e44195ca3b43ea389ca6fd141616 Mon Sep 17 00:00:00 2001 From: kajvans Date: Sat, 3 Feb 2024 12:35:00 +0100 Subject: [PATCH] types added to return --- src/jwt.test.ts | 20 ++++++++++---------- src/jwt.ts | 30 ++++++++++++++++++------------ src/passgen.ts | 2 +- src/passpolicy.ts | 4 ++-- src/passwordcheck.ts | 4 ++-- src/ratelimit.ts | 30 +++++++++++++++--------------- 6 files changed, 48 insertions(+), 42 deletions(-) diff --git a/src/jwt.test.ts b/src/jwt.test.ts index f938ee4..5820ca9 100644 --- a/src/jwt.test.ts +++ b/src/jwt.test.ts @@ -16,17 +16,17 @@ describe('JWT', () => { expect(token).toBeDefined(); }); - it('should verify a JWT', () => { - const token = jwt.generateJWT({ username: user.username, password: user.password }); - const verified = jwt.verifyJWT(token); - //check if username inside the token is equal to the username of the user check this in two lines of code - expect(verified).toBeDefined(); - if (typeof verified !== 'string') { - // verified is JwtPayload - if(verified == null) fail('Token verification failed'); - expect(verified.username).toEqual(user.username); + test('should return token object when valid token is provided', () => { + // Arrange + const validToken = jwt.generateJWT(user); + + // Act + const result = jwt.verifyJWT(validToken); + + // Assert + if(result instanceof Object) { + expect(result.username).toEqual(user.username); } else { - // Handle the case where verified is a string (token is blacklisted or invalid) fail('Token verification failed'); } }); diff --git a/src/jwt.ts b/src/jwt.ts index b05e3a0..719ab4c 100644 --- a/src/jwt.ts +++ b/src/jwt.ts @@ -19,7 +19,7 @@ export default class JwtAuth{ const vertoken = jwt.verify(token, secretKey); if(vertoken instanceof Object) { - return vertoken; + return (vertoken); } else { return undefined; } @@ -30,22 +30,28 @@ export default class JwtAuth{ decodeJWT(token: string) { 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." }; 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." }; 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." }; const decoded = this.verifyJWT(token, secretKey); @@ -66,7 +72,7 @@ export default class JwtAuth{ return { valid: false, message: "Token is invalid." }; } - BlackListJWT(token: string) { + BlackListJWT(token: string): { valid: boolean, message: string}{ //check if token is already blacklisted if (this.blacklist.includes(token)) { return { valid: false, message: "Token is already blacklisted." }; @@ -76,18 +82,18 @@ export default class JwtAuth{ return { valid: true, message: "Token successfully blacklisted."}; } - ClearBlackList() { + ClearBlackList(): { valid: boolean, message: string}{ //clear blacklist this.blacklist = []; return { valid: true, message: "Blacklist successfully cleared."}; } - GetBlackList() { + GetBlackList(): string[] { //return blacklist return this.blacklist; } - RemoveFromBlackList(token: string) { + RemoveFromBlackList(token: string): { valid: boolean, message: string}{ //remove token from blacklist if (this.blacklist.includes(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." }; } - IsBlackListed(token: string) { + IsBlackListed(token: string): { valid: boolean, message: string}{ //check if token is blacklisted if (this.blacklist.includes(token)) { return { valid: true, message: "Token is blacklisted." }; diff --git a/src/passgen.ts b/src/passgen.ts index d04fbb1..5eb29fa 100644 --- a/src/passgen.ts +++ b/src/passgen.ts @@ -13,7 +13,7 @@ export default class PasswordGenerator{ this.options = { ...defaultOptions, ...options }; } - Generate(length: number = 0){ + Generate(length: number = 0): string{ // Generate random password that complies with the options const { minLength, maxLength, minLower, minUpper, minNum, minSpecial, specialChars } = this.options; diff --git a/src/passpolicy.ts b/src/passpolicy.ts index cb561b4..ea1e085 100644 --- a/src/passpolicy.ts +++ b/src/passpolicy.ts @@ -13,7 +13,7 @@ export default class PassPolicy { this.options = { ...defaultOptions, ...options }; } - validate(password: string) { + validate(password: string): { valid: boolean, message?: string }{ const { minLength, maxLength, minLower, minUpper, minNum, minSpecial, specialChars } = this.options; if (password.length < minLength || password.length > maxLength) { @@ -46,7 +46,7 @@ export default class PassPolicy { 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 if (newPassword === oldPassword) { return { valid: false, message: "New password must be different from old password." }; diff --git a/src/passwordcheck.ts b/src/passwordcheck.ts index 314ef7b..34e4715 100644 --- a/src/passwordcheck.ts +++ b/src/passwordcheck.ts @@ -9,11 +9,11 @@ export default class PassCheck{ this.PassPolicy = new PassPolicy(PassPolicyOptions); } - async verifyPassword(password: string, hash: string) { + async verifyPassword(password: string, hash: string): Promise { return await bcrypt.compare(password, hash); } - async hashPassword(password: string) { + async hashPassword(password: string): Promise { const salt = await bcrypt.genSalt(this.BcryptSaltRounds); const hash = await bcrypt.hash(password, salt); return hash; diff --git a/src/ratelimit.ts b/src/ratelimit.ts index 3409b76..cbcbdc2 100644 --- a/src/ratelimit.ts +++ b/src/ratelimit.ts @@ -17,7 +17,7 @@ export default class RateLimit { this.events = events; } - public addEvent(event: Event) { + public addEvent(event: Event): boolean { try{ this.events[event.name] = event; @@ -32,7 +32,7 @@ export default class RateLimit { } } - public removeEvent(name: string) { + public removeEvent(name: string): boolean { try{ delete this.events[name]; @@ -47,7 +47,7 @@ export default class RateLimit { } } - public addUser(token: string) { + public addUser(token: string): boolean { try{ this.users[token] = { token, events: this.events }; return true; @@ -56,7 +56,7 @@ export default class RateLimit { } } - public removeUser(token: string) { + public removeUser(token: string): boolean { try{ delete this.users[token]; return true; @@ -65,7 +65,7 @@ export default class RateLimit { } } - public attempt(token: string, name: string) { + public attempt(token: string, name: string): boolean { try{ if (!this.users[token] || !this.users[token].events[name]) { return false; @@ -96,23 +96,23 @@ export default class RateLimit { } } - public getEvents() { + public getEvents(): { [name: string]: Event}{ return this.events; } - public getUsers() { + public getUsers(): { [token: string]: user}{ return this.users; } - public getEvent(name: string) { + public getEvent(name: string): Event { return this.events[name]; } - public getUser(token: string) { + public getUser(token: string): user { return this.users[token]; } - public remainingAttempts(token: string, name: string) { + public remainingAttempts(token: string, name: string): number { try{ if (!this.users[token] || !this.users[token].events[name]) { return -1; @@ -130,7 +130,7 @@ export default class RateLimit { } } - public resetAttempts(token: string, name: string) { + public resetAttempts(token: string, name: string): boolean { try{ if (!this.users[token] || !this.users[token].events[name]) { return false; @@ -145,7 +145,7 @@ export default class RateLimit { } } - public resetAllAttempts(token: string) { + public resetAllAttempts(token: string): boolean { try{ if (!this.users[token]) { return false; @@ -161,7 +161,7 @@ export default class RateLimit { } } - public resetAllUsers() { + public resetAllUsers(): boolean { try{ for (const token in this.users) { this.resetAllAttempts(token); @@ -173,7 +173,7 @@ export default class RateLimit { } } - public resetEvent(name: string) { + public resetEvent(name: string): boolean { try{ for (const token in this.users) { this.resetAttempts(token, name); @@ -185,7 +185,7 @@ export default class RateLimit { } } - public resetUser(token: string) { + public resetUser(token: string): boolean { try{ for (const name in this.users[token].events) { this.resetAttempts(token, name);