-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
made using it easier, by lowering the initialisating protocol
- Loading branch information
1 parent
ba758e4
commit 61bb06b
Showing
9 changed files
with
288 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,12 +5,14 @@ const speakeasy = require('speakeasy'); | |
const fs = require('fs'); | ||
|
||
const mockUser = { | ||
username: "test", | ||
email: "[email protected]", | ||
password: "password123", | ||
wants2FA: false, | ||
}; | ||
|
||
const mockUser2FA = { | ||
username: "test2", | ||
email: "[email protected]", | ||
password: "password123", | ||
wants2FA: true, | ||
|
@@ -39,20 +41,24 @@ describe('Authenticator Class Tests', () => { | |
|
||
test('User Registration without 2FA', async () => { | ||
const result = await authenticator.register({ | ||
username: "test", | ||
email: "[email protected]", | ||
password: "password123", | ||
wants2FA: false, | ||
}); | ||
expect(result.username).toBe("test"); | ||
expect(result.email).toBe(mockUser.email); | ||
expect(result.jwt_version).toBe(1); | ||
expect(result.wants2FA).toBe(false); | ||
}); | ||
test('User Registration with 2FA', async () => { | ||
const result = await authenticator.register({ | ||
username: "test2", | ||
email: "[email protected]", | ||
password: "password123", | ||
wants2FA: true, | ||
}); | ||
expect(result.username).toBe("test2"); | ||
expect(result.email).toBe(mockUser2FA.email); | ||
expect(result.jwt_version).toBe(1); | ||
expect(result.wants2FA).toBe(true); | ||
|
@@ -62,7 +68,7 @@ describe('Authenticator Class Tests', () => { | |
}); | ||
|
||
test('User Login', async () => { | ||
const loginResult = await authenticator.login(mockUser.email, mockUser.password); | ||
const loginResult = await authenticator.login(mockUser.username, mockUser.password); | ||
userID = loginResult._id | ||
expect(loginResult.jwt_token).toBeDefined(); | ||
expect(jwt.verify(loginResult.jwt_token, JWT_SECRET)).toBeTruthy(); | ||
|
@@ -74,38 +80,38 @@ describe('Authenticator Class Tests', () => { | |
secret: SECRET2FA, | ||
encoding: 'base32', | ||
}) | ||
const loginResult = await authenticator.login(mockUser2FA.email, mockUser2FA.password, twoFactorCode); | ||
const loginResult = await authenticator.login(mockUser2FA.username, mockUser2FA.password, twoFactorCode); | ||
userID2FA = loginResult._id | ||
expect(loginResult.jwt_token).toBeDefined(); | ||
expect(jwt.verify(loginResult.jwt_token, JWT_SECRET)).toBeTruthy(); | ||
}); | ||
|
||
test('User Login with invalid 2FA ', async () => { | ||
const loginResult = await authenticator.login(mockUser2FA.email, mockUser2FA.password, 100000); | ||
const loginResult = await authenticator.login(mockUser2FA.username, mockUser2FA.password, 100000); | ||
expect(loginResult.jwt_token).not.toBeDefined(); | ||
}); | ||
test('User Login with no 2FA (for a 2FA user) ', async () => { | ||
const loginResult = await authenticator.login(mockUser2FA.email, mockUser2FA.password, 100000); | ||
const loginResult = await authenticator.login(mockUser2FA.username, mockUser2FA.password, 100000); | ||
expect(loginResult.jwt_token).not.toBeDefined(); | ||
}); | ||
|
||
test('Login with incorrect password', async () => { | ||
const result = await authenticator.login(mockUser.email, 'wrongpassword'); | ||
const result = await authenticator.login(mockUser.username, 'wrongpassword'); | ||
expect(result).toBe(null); | ||
}); | ||
|
||
test('Get Info From User', async () => { | ||
const info = await authenticator.getInfoFromUser(userID) | ||
expect(info.email).toBe(mockUser.email); | ||
expect(info.username).toBe(mockUser.username); | ||
}) | ||
|
||
test('Get Info From Email', async () => { | ||
const info = await authenticator.getInfoFromEmail(mockUser.email) | ||
test('Get Info From Custom Property', async () => { | ||
const info = await authenticator.getInfoFromCustom("email", mockUser.email) | ||
expect(info.email).toBe(mockUser.email); | ||
}) | ||
|
||
test('Verify JWT Token', async () => { | ||
const loginResult = await authenticator.login(mockUser.email, mockUser.password); | ||
const loginResult = await authenticator.login(mockUser.username, mockUser.password); | ||
const tokenVerification = await authenticator.verifyToken(loginResult.jwt_token); | ||
expect(tokenVerification).toBeDefined() | ||
}); | ||
|
@@ -144,9 +150,9 @@ describe('Authenticator Class Tests', () => { | |
}) | ||
|
||
test('Lock user after max login attempts', async () => { | ||
await authenticator.login(mockUser.email, 'wrongpassword'); | ||
await authenticator.login(mockUser.email, 'wrongpassword'); | ||
const result = await authenticator.login(mockUser.email, 'wrongpassword'); | ||
await authenticator.login(mockUser.username, 'wrongpassword'); | ||
await authenticator.login(mockUser.username, 'wrongpassword'); | ||
const result = await authenticator.login(mockUser.username, 'wrongpassword'); | ||
if (result === 'User is locked') { | ||
expect(result).toBe('User is locked'); | ||
} else { | ||
|
@@ -187,19 +193,44 @@ describe('Authenticator Class Tests', () => { | |
|
||
}) | ||
|
||
test('Check if user is authenticated', async () => { | ||
await authenticator.register({ | ||
email: "[email protected]", | ||
password: "test", | ||
wants2FA: false, | ||
test('Check if user is authenticated', | ||
async () => { | ||
await authenticator.register({ | ||
username: "test3", | ||
email: "[email protected]", | ||
password: "test3", | ||
wants2FA: false, | ||
}) | ||
let user = await authenticator.login("test3", "test3") | ||
console.log(user) | ||
|
||
let req = { | ||
headers: { | ||
"host": "127.0.0.1:3000", | ||
"connection": "keep-alive", | ||
"cache-control": "max-age=0", | ||
"sec-ch-ua": "\"Chromium\";v=\"130\", \"Brave\";v=\"130\", \"Not?A_Brand\";v=\"99\"", | ||
"sec-ch-ua-mobile": "?0", | ||
"sec-ch-ua-platform": "\"Windows\"", | ||
"dnt": "1", | ||
"upgrade-insecure-requests": "1", | ||
"user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36", | ||
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8", | ||
"sec-gpc": "1", | ||
"accept-language": "nl-NL,nl", | ||
"sec-fetch-site": "same-origin", | ||
"sec-fetch-mode": "navigate", | ||
"sec-fetch-user": "?1", | ||
"sec-fetch-dest": "document", | ||
"referer": "http://127.0.0.1:3000/login", | ||
"accept-encoding": "gzip, deflate, br, zstd", | ||
"cookie": `token=${user.jwt_token}`, | ||
"if-none-match": "W/\"14-VDnz0WejlS4iemsxsVhn1S8IIDE\"" | ||
} | ||
} | ||
let response = await authenticator.isAuthenticated(req) | ||
expect(response).toBe(true) | ||
}) | ||
let user = await authenticator.login("[email protected]", "test") | ||
console.log(user) | ||
|
||
let req = { headers: { "host": "127.0.0.1:3000", "connection": "keep-alive", "cache-control": "max-age=0", "sec-ch-ua": "\"Chromium\";v=\"130\", \"Brave\";v=\"130\", \"Not?A_Brand\";v=\"99\"", "sec-ch-ua-mobile": "?0", "sec-ch-ua-platform": "\"Windows\"", "dnt": "1", "upgrade-insecure-requests": "1", "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/130.0.0.0 Safari/537.36", "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8", "sec-gpc": "1", "accept-language": "nl-NL,nl", "sec-fetch-site": "same-origin", "sec-fetch-mode": "navigate", "sec-fetch-user": "?1", "sec-fetch-dest": "document", "referer": "http://127.0.0.1:3000/login", "accept-encoding": "gzip, deflate, br, zstd", "cookie": `token=${user.jwt_token}`, "if-none-match": "W/\"14-VDnz0WejlS4iemsxsVhn1S8IIDE\"" } } | ||
let response = await authenticator.isAuthenticated(req) | ||
expect(response).toBe(true) | ||
}) | ||
|
||
test('Revoke All User Tokens', async () => { | ||
await authenticator.revokeUserTokens(userID) | ||
|
@@ -215,7 +246,7 @@ describe('Authenticator Class Tests', () => { | |
|
||
|
||
afterAll(async () => { | ||
console.log(await authenticator.dumpDB()) | ||
//console.log(await authenticator.dumpDB()) | ||
fs.unlinkSync(authenticator.DB_FILE_PATH) | ||
}); | ||
|
||
|
Oops, something went wrong.