-
-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: migrated 2FA to controller (#3257)
- Loading branch information
Showing
9 changed files
with
150 additions
and
100 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -1,53 +1,52 @@ | ||
{% if ( requestIsSecure == true ) or ( security.useSslForLogins == false ) %} | ||
<div class="container py-5"> | ||
<div class="row"> | ||
<div class="col-lg-12"> | ||
<div class="row"> | ||
<div class="col-lg-6 mx-auto"> | ||
<div class="card rounded-0" id="login-form"> | ||
<div class="card-header"> | ||
<h3 class="mb-0"> | ||
{{ msgTwofactorEnabled }} | ||
</h3> | ||
{% if actionIsLogout == true %} | ||
<p class="alert alert-danger alert-dismissible fade show mt-3"> | ||
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span></button> | ||
{{ ad_logout }} | ||
</p> | ||
{% endif %} | ||
{% if ( error is defined ) and ( error|length > 0 ) %} | ||
<p class="alert alert-danger alert-dismissible fade show mt-3"> | ||
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span></button> | ||
{{ error }} | ||
</p> | ||
{% endif %} | ||
{% extends '@admin/index.twig' %} | ||
|
||
</div> | ||
<div class="card-body"> | ||
<form action="{{ systemUri }}admin/index.php" method="post" | ||
accept-charset="utf-8" role="form" class="pmf-form-login"> | ||
<input type="hidden" name="userid" id="userid" value="<?= $userid ?>"> | ||
<input type="hidden" name="redirect-action" value="<?= $action ?>"> | ||
<div class="form-group"> | ||
<label for="token">{{ msgEnterTwofactorToken }}</label> | ||
<div class="col-4 mx-auto my-2"> | ||
<input type="text" class="form-control form-control-lg text-center rounded-0" name="token" | ||
id="token" autocomplete="off" maxlength="6" autofocus required> | ||
{% block content %} | ||
{% if ( requestIsSecure == true ) or ( security.useSslForLogins == false ) %} | ||
<div class="container py-5"> | ||
<div class="row"> | ||
<div class="col-lg-12"> | ||
<div class="row"> | ||
<div class="col-lg-6 mx-auto"> | ||
<div class="card rounded-0" id="login-form"> | ||
<div class="card-header"> | ||
<h3 class="mb-0"> | ||
{{ msgTwofactorEnabled }} | ||
</h3> | ||
{% if ( error is defined ) and ( error|length > 0 ) %} | ||
xxx | ||
<p class="alert alert-danger alert-dismissible fade show mt-3"> | ||
<button type="button" class="close" data-dismiss="alert"><span aria-hidden="true">×</span></button> | ||
{{ error }} | ||
</p> | ||
{% endif %} | ||
|
||
</div> | ||
<div class="card-body"> | ||
<form action="{{ systemUri }}admin/check" method="post" | ||
accept-charset="utf-8" role="form" class="pmf-form-login"> | ||
<input type="hidden" name="user-id" id="user-id" value="{{ userId }}"> | ||
<input type="hidden" name="redirect-action" value="{{ redirectAction }}"> | ||
<div class="form-group"> | ||
<label for="token">{{ msgEnterTwofactorToken }}</label> | ||
<div class="col-4 mx-auto my-2"> | ||
<input type="text" class="form-control form-control-lg text-center rounded-0" name="token" | ||
id="token" autocomplete="off" maxlength="6" autofocus required> | ||
</div> | ||
</div> | ||
<div class="d-grid gap-2 col-6 mx-auto"> | ||
<button type="submit" class="btn btn-success btn-lg float-right" id="btnLogin"> | ||
{{ msgTwofactorCheck }} | ||
</button> | ||
</div> | ||
</div> | ||
<div class="d-grid gap-2 col-6 mx-auto"> | ||
<button type="submit" class="btn btn-success btn-lg float-right" id="btnLogin"> | ||
{{ msgTwofactorCheck }} | ||
</button> | ||
</div> | ||
</form> | ||
</form> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
</div> | ||
{% else %} | ||
<p><a href="https://{{ requestHost }}{{ requestUri }}">{{ msgSecureSwitch }}</a></p> | ||
{% endif %} | ||
{% else %} | ||
<p><a href="https://{{ requestHost }}{{ requestUri }}">{{ msgSecureSwitch }}</a></p> | ||
{% endif %} | ||
{% endblock %} |
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 |
---|---|---|
@@ -1,5 +1,20 @@ | ||
<?php | ||
|
||
/** | ||
* The Administration Authentication Controller | ||
* | ||
* This Source Code Form is subject to the terms of the Mozilla Public License, | ||
* v. 2.0. If a copy of the MPL was not distributed with this file, You can | ||
* obtain one at https://mozilla.org/MPL/2.0/. | ||
* | ||
* @package phpMyFAQ | ||
* @author Thorsten Rinne <[email protected]> | ||
* @copyright 2024 phpMyFAQ Team | ||
* @license https://www.mozilla.org/MPL/2.0/ Mozilla Public License Version 2.0 | ||
* @link https://www.phpmyfaq.de | ||
* @since 2024-12-28 | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpMyFAQ\Controller\Administration; | ||
|
@@ -47,12 +62,12 @@ public function authenticate(Request $request): Response | |
try { | ||
$this->currentUser = $userAuth->authenticate($username, $password); | ||
if ($userAuth->hasTwoFactorAuthentication()) { | ||
return new RedirectResponse('./2fa'); | ||
return new RedirectResponse('./token?user-id=' . $this->currentUser->getUserId()); | ||
} | ||
} catch (Exception $e) { | ||
$logging->log( | ||
$this->currentUser, | ||
'Login-error\nLogin: ' . $username . '\nErrors: ' . implode(', ', $this->configuration->errors) | ||
'Login-error\nLogin: ' . $username . '\nErrors: ' . implode(', ', $this->currentUser->errors) | ||
); | ||
//$error = $e->getMessage(); | ||
return new RedirectResponse('./login'); | ||
|
@@ -116,6 +131,7 @@ public function logout(Request $request): Response | |
|
||
$csrfToken = Filter::filterVar($request->get('csrf'), FILTER_SANITIZE_SPECIAL_CHARS); | ||
if (!Token::getInstance($this->container->get('session'))->verifyToken('admin-logout', $csrfToken)) { | ||
// add an error message | ||
return $redirect->send(); | ||
} | ||
|
||
|
@@ -128,4 +144,68 @@ public function logout(Request $request): Response | |
|
||
return $redirect->send(); | ||
} | ||
|
||
/** | ||
* @throws \Exception | ||
*/ | ||
#[Route('/token', name: 'admin.auth.token', methods: ['GET'])] | ||
public function token(Request $request): Response | ||
{ | ||
if ($this->currentUser->isLoggedIn()) { | ||
return new RedirectResponse('./'); | ||
} | ||
|
||
$userId = Filter::filterVar($request->get('user-id'), FILTER_VALIDATE_INT); | ||
|
||
return $this->render( | ||
'@admin/user/twofactor.twig', | ||
[ | ||
... $this->getHeader($request), | ||
... $this->getFooter(), | ||
'msgTwofactorEnabled' => Translation::get('msgTwofactorEnabled'), | ||
'msgTwofactorCheck' => Translation::get('msgTwofactorCheck'), | ||
'msgEnterTwofactorToken' => Translation::get('msgEnterTwofactorToken'), | ||
'requestIsSecure' => $request->isSecure(), | ||
'security.useSslForLogins' => $this->configuration->get('security.useSslForLogins'), | ||
'requestHost' => $request->getHost(), | ||
'requestUri' => $request->getRequestUri(), | ||
'userId' => $userId, | ||
'msgSecureSwitch' => Translation::get('msgSecureSwitch'), | ||
'systemUri' => $this->configuration->getDefaultUrl() | ||
] | ||
); | ||
} | ||
|
||
/** | ||
* @throws \Exception | ||
*/ | ||
#[Route('/check', name: 'admin.auth.check', methods: ['POST'])] | ||
public function check(Request $request): Response | ||
{ | ||
if ($this->currentUser->isLoggedIn()) { | ||
return new RedirectResponse('./'); | ||
} | ||
|
||
$token = Filter::filterVar($request->get('token'), FILTER_VALIDATE_INT); | ||
$userId = Filter::filterVar($request->get('user-id'), FILTER_VALIDATE_INT); | ||
|
||
$user = $this->container->get('phpmyfaq.user.current_user'); | ||
$user->getUserById($userId); | ||
|
||
if (strlen((string) $token) === 6) { | ||
$tfa = $this->container->get('phpmyfaq.user.two-factor'); | ||
$result = $tfa->validateToken($token, $userId); | ||
|
||
if ($result) { | ||
$user->twoFactorSuccess(); | ||
return new RedirectResponse('./'); | ||
} else { | ||
// add an error message | ||
return new RedirectResponse('./token?user-id=' . $userId); | ||
} | ||
} else { | ||
// add an error message | ||
return new RedirectResponse('./token?user-id=' . $userId); | ||
} | ||
} | ||
} |
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