2024-03-14 19:25:27 +00:00
|
|
|
import type { HttpContext } from '@adonisjs/core/http';
|
2024-04-30 09:50:50 +00:00
|
|
|
import User from '#models/user';
|
2024-07-08 11:52:20 +00:00
|
|
|
import BackupCode from '#models/backup_code';
|
2023-03-03 15:54:28 +00:00
|
|
|
// import Hash from '@ioc:Adonis/Core/Hash';
|
|
|
|
// import InvalidCredentialException from 'App/Exceptions/InvalidCredentialException';
|
2024-05-16 11:47:06 +00:00
|
|
|
import { authValidator } from '#validators/auth';
|
2024-07-08 11:52:20 +00:00
|
|
|
import hash from '@adonisjs/core/services/hash';
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2024-04-23 17:36:45 +00:00
|
|
|
import TwoFactorAuthProvider from '#app/services/TwoFactorAuthProvider';
|
2024-03-14 19:25:27 +00:00
|
|
|
// import { Authenticator } from '@adonisjs/auth';
|
2024-02-16 14:32:47 +00:00
|
|
|
// import { LoginState } from 'Contracts/enums';
|
|
|
|
// import { StatusCodes } from 'http-status-codes';
|
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
// interface MyHttpsContext extends HttpContext {
|
|
|
|
// auth: Authenticator<User>
|
|
|
|
// }
|
2023-03-03 15:54:28 +00:00
|
|
|
export default class AuthController {
|
2024-03-14 19:25:27 +00:00
|
|
|
// login function{ request, auth, response }:HttpContext
|
|
|
|
public async login({ request, response, auth, session }: HttpContext) {
|
2023-06-22 15:20:04 +00:00
|
|
|
// console.log({
|
|
|
|
// registerBody: request.body(),
|
|
|
|
// });
|
2024-05-16 11:47:06 +00:00
|
|
|
// await request.validate(AuthValidator);
|
|
|
|
await request.validateUsing(authValidator);
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2024-02-16 14:32:47 +00:00
|
|
|
// const plainPassword = await request.input('password');
|
|
|
|
// const email = await request.input('email');
|
2023-06-22 15:20:04 +00:00
|
|
|
// grab uid and password values off request body
|
2024-02-16 14:32:47 +00:00
|
|
|
const { email, password } = request.only(['email', 'password']);
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
try {
|
2024-02-16 14:32:47 +00:00
|
|
|
// // attempt to verify credential and login user
|
|
|
|
// await auth.use('web').attempt(email, plainPassword);
|
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
// const user = await auth.use('web').verifyCredentials(email, password);
|
2024-07-08 11:52:20 +00:00
|
|
|
const user = await User.verifyCredentials(email, password);
|
|
|
|
|
2024-02-16 14:32:47 +00:00
|
|
|
if (user.isTwoFactorEnabled) {
|
|
|
|
// session.put("login.id", user.id);
|
|
|
|
// return view.render("pages/two-factor-challenge");
|
|
|
|
|
|
|
|
session.flash('user_id', user.id);
|
|
|
|
return response.redirect().back();
|
2024-07-08 11:52:20 +00:00
|
|
|
|
2024-02-16 14:32:47 +00:00
|
|
|
// let state = LoginState.STATE_VALIDATED;
|
|
|
|
// return response.status(StatusCodes.OK).json({
|
|
|
|
// state: state,
|
|
|
|
// new_user_id: user.id,
|
|
|
|
// });
|
2024-07-08 11:52:20 +00:00
|
|
|
}
|
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
await auth.use('web').login(user);
|
2023-06-22 15:20:04 +00:00
|
|
|
} catch (error) {
|
|
|
|
// if login fails, return vague form message and redirect back
|
|
|
|
session.flash('message', 'Your username, email, or password is incorrect');
|
|
|
|
return response.redirect().back();
|
|
|
|
}
|
2023-03-03 15:54:28 +00:00
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
// otherwise, redirect todashboard
|
2023-12-12 14:22:25 +00:00
|
|
|
response.redirect('/apps/dashboard');
|
2023-06-22 15:20:04 +00:00
|
|
|
}
|
|
|
|
|
2024-03-14 19:25:27 +00:00
|
|
|
public async twoFactorChallenge({ request, session, auth, response }: HttpContext) {
|
2024-07-08 11:52:20 +00:00
|
|
|
const { code, backup_code, login_id } = request.only(['code', 'backup_code', 'login_id']);
|
2024-02-16 14:32:47 +00:00
|
|
|
const user = await User.query().where('id', login_id).firstOrFail();
|
2024-07-08 11:52:20 +00:00
|
|
|
|
2024-02-16 14:32:47 +00:00
|
|
|
if (code) {
|
|
|
|
const isValid = await TwoFactorAuthProvider.validate(user, code);
|
|
|
|
if (isValid) {
|
|
|
|
// login user and redirect to dashboard
|
2024-03-14 19:25:27 +00:00
|
|
|
await auth.use('web').login(user);
|
2024-02-16 14:32:47 +00:00
|
|
|
response.redirect('/apps/dashboard');
|
|
|
|
} else {
|
2024-07-08 11:52:20 +00:00
|
|
|
session.flash('message', 'Your two-factor code is incorrect');
|
|
|
|
return response.redirect().back();
|
2024-02-16 14:32:47 +00:00
|
|
|
}
|
2024-07-08 11:52:20 +00:00
|
|
|
} else if (backup_code) {
|
|
|
|
const codes: BackupCode[] = await user.getBackupCodes();
|
|
|
|
|
|
|
|
// const verifiedBackupCodes = await Promise.all(
|
|
|
|
// codes.map(async (backupCode) => {
|
|
|
|
// let isVerified = await hash.verify(backupCode.code, backup_code);
|
|
|
|
// if (isVerified) {
|
|
|
|
// return backupCode;
|
|
|
|
// }
|
|
|
|
// }),
|
|
|
|
// );
|
|
|
|
// const backupCodeToDelete = verifiedBackupCodes.find(Boolean);
|
|
|
|
|
|
|
|
let backupCodeToDelete = null;
|
|
|
|
for (const backupCode of codes) {
|
|
|
|
const isVerified = await hash.verify(backupCode.code, backup_code);
|
|
|
|
if (isVerified) {
|
|
|
|
backupCodeToDelete = backupCode;
|
|
|
|
break;
|
|
|
|
}
|
2024-02-16 14:32:47 +00:00
|
|
|
}
|
2024-07-08 11:52:20 +00:00
|
|
|
|
|
|
|
if (backupCodeToDelete) {
|
|
|
|
if (backupCodeToDelete.used === false) {
|
|
|
|
backupCodeToDelete.used = true;
|
|
|
|
await backupCodeToDelete.save();
|
|
|
|
console.log(`BackupCode with id ${backupCodeToDelete.id} has been marked as used.`);
|
|
|
|
await auth.use('web').login(user);
|
|
|
|
response.redirect('/apps/dashboard');
|
|
|
|
} else {
|
|
|
|
session.flash('message', 'BackupCode already used');
|
|
|
|
return response.redirect().back();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
session.flash('message', 'BackupCode not found');
|
|
|
|
return response.redirect().back();
|
|
|
|
}
|
2024-02-16 14:32:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-22 15:20:04 +00:00
|
|
|
// logout function
|
2024-03-14 19:25:27 +00:00
|
|
|
public async logout({ auth, response }: HttpContext) {
|
2023-06-22 15:20:04 +00:00
|
|
|
// await auth.logout();
|
|
|
|
await auth.use('web').logout();
|
2024-03-14 19:25:27 +00:00
|
|
|
return response.redirect('/app/login');
|
2023-06-22 15:20:04 +00:00
|
|
|
// return response.status(200);
|
|
|
|
}
|
2023-03-03 15:54:28 +00:00
|
|
|
}
|