|
|
|
@ -50,10 +50,12 @@ export class LoginController {
|
|
|
|
|
public async loginRequest(
|
|
|
|
|
@Req() req: Request,
|
|
|
|
|
@Res() res: Response,
|
|
|
|
|
@Body() body: { username: string; password: string },
|
|
|
|
|
@Body() body: { username: string; password: string; remember: boolean },
|
|
|
|
|
@Query('redirectTo') redirectTo?: string,
|
|
|
|
|
) {
|
|
|
|
|
const { username, password } = this.formUtil.trimmed(body, ['username']);
|
|
|
|
|
const { username, password, remember } = this.formUtil.trimmed(body, [
|
|
|
|
|
'username',
|
|
|
|
|
]);
|
|
|
|
|
const user = await this.userService.getByUsername(username);
|
|
|
|
|
|
|
|
|
|
// User exists and password matches
|
|
|
|
@ -73,7 +75,7 @@ export class LoginController {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (await this.totpService.userHasTOTP(user)) {
|
|
|
|
|
const challenge = { type: 'verify', user: user.uuid };
|
|
|
|
|
const challenge = { type: 'verify', user: user.uuid, remember };
|
|
|
|
|
req.session.challenge = await this.token.encryptChallenge(challenge);
|
|
|
|
|
res.redirect(
|
|
|
|
|
'/login/verify' + (redirectTo ? '?redirectTo=' + redirectTo : ''),
|
|
|
|
@ -81,6 +83,13 @@ export class LoginController {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extend session cookie to a month
|
|
|
|
|
if (remember) {
|
|
|
|
|
const month = 30 * 24 * 60 * 60 * 1000;
|
|
|
|
|
req.session.cookie.maxAge = month;
|
|
|
|
|
req.session.cookie.expires = new Date(Date.now() + month);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
req.session.user = user.uuid;
|
|
|
|
|
res.redirect(redirectTo ? decodeURIComponent(redirectTo) : '/');
|
|
|
|
|
}
|
|
|
|
@ -114,6 +123,7 @@ export class LoginController {
|
|
|
|
|
@Query('redirectTo') redirectTo?: string,
|
|
|
|
|
) {
|
|
|
|
|
let user: User;
|
|
|
|
|
let remember = false;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
if (!session.challenge) {
|
|
|
|
@ -129,6 +139,8 @@ export class LoginController {
|
|
|
|
|
if (!user) {
|
|
|
|
|
throw new Error('Bad challenge');
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
remember = challenge.remember;
|
|
|
|
|
} catch (e: any) {
|
|
|
|
|
req.flash('message', {
|
|
|
|
|
error: true,
|
|
|
|
@ -154,6 +166,12 @@ export class LoginController {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Extend session cookie to a month
|
|
|
|
|
if (remember) {
|
|
|
|
|
const month = 30 * 24 * 60 * 60 * 1000;
|
|
|
|
|
req.session.cookie.maxAge = month;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
session.challenge = null;
|
|
|
|
|
session.user = user.uuid;
|
|
|
|
|
res.redirect(redirectTo ? decodeURIComponent(redirectTo) : '/');
|
|
|
|
|