ff-admin-server/src/controller/authController.ts

151 lines
4.5 KiB
TypeScript
Raw Normal View History

import { Request, Response } from "express";
import { JWTHelper } from "../helpers/jwtHelper";
import { JWTToken } from "../type/jwtTypes";
import InternalException from "../exceptions/internalException";
import RefreshCommandHandler from "../command/refreshCommandHandler";
2024-08-25 08:09:57 +00:00
import { CreateRefreshCommand, DeleteRefreshCommand } from "../command/refreshCommand";
import UserService from "../service/userService";
import speakeasy from "speakeasy";
import UnauthorizedRequestException from "../exceptions/unauthorizedRequestException";
2024-08-23 12:42:47 +00:00
import RefreshService from "../service/refreshService";
2024-08-27 15:54:59 +00:00
import UserPermissionService from "../service/userPermissionService";
import PermissionHelper from "../helpers/permissionHelper";
2024-08-27 15:54:59 +00:00
import RolePermissionService from "../service/rolePermissionService";
/**
* @description Check authentication status by token
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function login(req: Request, res: Response): Promise<any> {
let username = req.body.username;
let totp = req.body.totp;
let { id, secret, mail, firstname, lastname } = await UserService.getByUsername(username);
let valid = speakeasy.totp.verify({
secret: secret,
encoding: "base32",
token: totp,
window: 2,
});
if (!valid) {
throw new UnauthorizedRequestException("Token not valid or expired");
}
2024-08-27 15:54:59 +00:00
let userPermissions = await UserPermissionService.getByUser(id);
let userPermissionStrings = userPermissions.map((e) => e.permission);
let userRoles = await UserService.getAssignedRolesByUserId(id);
let rolePermissions = await RolePermissionService.getByRoles(userRoles.map((e) => e.id));
let rolePermissionStrings = rolePermissions.map((e) => e.permission);
let permissionObject = PermissionHelper.convertToObject([...userPermissionStrings, ...rolePermissionStrings]);
let jwtData: JWTToken = {
userId: id,
mail: mail,
username: username,
firstname: firstname,
lastname: lastname,
permissions: permissionObject,
};
let accessToken: string;
let refreshToken: string;
JWTHelper.create(jwtData)
.then((result) => {
accessToken = result;
})
.catch((err) => {
console.log(err);
throw new InternalException("Failed accessToken creation");
});
let refreshCommand: CreateRefreshCommand = {
userId: id,
};
refreshToken = await RefreshCommandHandler.create(refreshCommand);
res.json({
accessToken,
refreshToken,
});
}
/**
* @description logout user by token (invalidate refresh token)
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function logout(req: Request, res: Response): Promise<any> {}
/**
* @description refresh expired token
* @param req {Request} Express req object
* @param res {Response} Express res object
* @returns {Promise<*>}
*/
export async function refresh(req: Request, res: Response): Promise<any> {
2024-08-25 08:09:57 +00:00
let token = req.body.accessToken;
let refresh = req.body.refreshToken;
2024-08-23 12:42:47 +00:00
const tokenUser = await JWTHelper.decode(token);
if (typeof tokenUser == "string" || !tokenUser) {
throw new InternalException("process failed");
}
let tokenUserId = (tokenUser as JWTToken).userId;
let { user } = await RefreshService.getByToken(refresh);
if (tokenUserId != user.id) {
throw new UnauthorizedRequestException("user not identified with token and refresh");
}
let { id, username, mail, firstname, lastname } = await UserService.getById(tokenUserId);
2024-08-27 15:54:59 +00:00
let permissions = await UserPermissionService.getByUser(id);
let permissionStrings = permissions.map((e) => e.permission);
let permissionObject = PermissionHelper.convertToObject(permissionStrings);
2024-08-23 12:42:47 +00:00
let jwtData: JWTToken = {
userId: id,
mail: mail,
2024-08-23 12:42:47 +00:00
username: username,
firstname: firstname,
lastname: lastname,
permissions: permissionObject,
2024-08-23 12:42:47 +00:00
};
let accessToken: string;
let refreshToken: string;
JWTHelper.create(jwtData)
.then((result) => {
accessToken = result;
})
.catch((err) => {
console.log(err);
throw new InternalException("Failed accessToken creation");
});
let refreshCommand: CreateRefreshCommand = {
userId: id,
};
refreshToken = await RefreshCommandHandler.create(refreshCommand);
2024-08-25 08:09:57 +00:00
let removeToken: DeleteRefreshCommand = {
userId: id,
token: refresh,
};
await RefreshCommandHandler.deleteByToken(removeToken);
2024-08-23 12:42:47 +00:00
res.json({
accessToken,
refreshToken,
});
}