2024-11-20 08:32:43 +00:00
|
|
|
import { Request, Response } from "express";
|
|
|
|
import speakeasy from "speakeasy";
|
|
|
|
import QRCode from "qrcode";
|
|
|
|
import InternalException from "../exceptions/internalException";
|
|
|
|
import { CLUB_NAME } from "../env.defaults";
|
|
|
|
import UserService from "../service/userService";
|
2024-11-20 09:02:34 +00:00
|
|
|
import UserFactory from "../factory/admin/user";
|
|
|
|
import { UpdateUserCommand } from "../command/userCommand";
|
|
|
|
import UserCommandHandler from "../command/userCommandHandler";
|
2024-11-20 08:32:43 +00:00
|
|
|
|
|
|
|
/**
|
2024-11-20 09:02:34 +00:00
|
|
|
* @description get my by id
|
2024-11-20 08:32:43 +00:00
|
|
|
* @param req {Request} Express req object
|
|
|
|
* @param res {Response} Express res object
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
2024-11-20 09:02:34 +00:00
|
|
|
export async function getMeById(req: Request, res: Response): Promise<any> {
|
|
|
|
const id = parseInt(req.userId);
|
|
|
|
let user = await UserService.getById(id);
|
|
|
|
|
|
|
|
res.json(UserFactory.mapToSingle(user));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @description get my totp
|
|
|
|
* @param req {Request} Express req object
|
|
|
|
* @param res {Response} Express res object
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
|
|
|
export async function getMyTotp(req: Request, res: Response): Promise<any> {
|
2024-11-20 08:32:43 +00:00
|
|
|
const userId = parseInt(req.userId);
|
|
|
|
|
|
|
|
let { secret } = await UserService.getById(userId);
|
|
|
|
|
|
|
|
const url = `otpauth://totp/Mitgliederverwaltung ${CLUB_NAME}?secret=${secret}`;
|
|
|
|
|
|
|
|
QRCode.toDataURL(url)
|
|
|
|
.then((result) => {
|
|
|
|
res.json({
|
|
|
|
dataUrl: result,
|
|
|
|
otp: secret,
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
throw new InternalException("QRCode not created", err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-11-20 09:02:34 +00:00
|
|
|
* @description verify my totp
|
2024-11-20 08:32:43 +00:00
|
|
|
* @param req {Request} Express req object
|
|
|
|
* @param res {Response} Express res object
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
2024-11-20 09:02:34 +00:00
|
|
|
export async function verifyMyTotp(req: Request, res: Response): Promise<any> {
|
2024-11-20 08:32:43 +00:00
|
|
|
const userId = parseInt(req.userId);
|
|
|
|
let totp = req.body.totp;
|
|
|
|
|
|
|
|
let { secret } = await UserService.getById(userId);
|
|
|
|
let valid = speakeasy.totp.verify({
|
|
|
|
secret: secret,
|
|
|
|
encoding: "base32",
|
|
|
|
token: totp,
|
|
|
|
window: 2,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!valid) {
|
|
|
|
throw new InternalException("Token not valid or expired");
|
|
|
|
}
|
|
|
|
res.sendStatus(204);
|
|
|
|
}
|
2024-11-20 09:02:34 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @description update my data
|
|
|
|
* @param req {Request} Express req object
|
|
|
|
* @param res {Response} Express res object
|
|
|
|
* @returns {Promise<*>}
|
|
|
|
*/
|
|
|
|
export async function updateMe(req: Request, res: Response): Promise<any> {
|
|
|
|
const id = parseInt(req.userId);
|
|
|
|
let mail = req.body.mail;
|
|
|
|
let firstname = req.body.firstname;
|
|
|
|
let lastname = req.body.lastname;
|
|
|
|
let username = req.body.username;
|
|
|
|
|
|
|
|
let updateUser: UpdateUserCommand = {
|
|
|
|
id: id,
|
|
|
|
mail: mail,
|
|
|
|
firstname: firstname,
|
|
|
|
lastname: lastname,
|
|
|
|
username: username,
|
|
|
|
};
|
|
|
|
await UserCommandHandler.update(updateUser);
|
|
|
|
|
|
|
|
res.sendStatus(204);
|
|
|
|
}
|