From 590aa5fc265ab3aba34369a17c15162808aa7a77 Mon Sep 17 00:00:00 2001 From: Mike Macgirvin Date: Sat, 4 Mar 2023 16:44:40 +1100 Subject: [PATCH] push up some more multifactor work --- Code/Module/Totp_check.php | 43 ++++++++++++++------------------------ view/tpl/totp.tpl | 38 ++++++++++++++++++--------------- 2 files changed, 37 insertions(+), 44 deletions(-) diff --git a/Code/Module/Totp_check.php b/Code/Module/Totp_check.php index e4e9c5f4d..d6441c59c 100644 --- a/Code/Module/Totp_check.php +++ b/Code/Module/Totp_check.php @@ -10,9 +10,11 @@ use Code\Render\Theme; use Code\Web\Controller; use OTPHP\TOTP; -class Totp_check extends Controller { +class Totp_check extends Controller +{ - function post() { + function post() + { $retval = ['status' => false]; if (!local_channel()) { @@ -25,23 +27,16 @@ class Totp_check extends Controller { } $secret = $account['account_external']; - if (isset($_POST['totp_code'])) { + if ($secret && isset($_POST['totp_code'])) { $otp = TOTP::create($secret); // create TOTP object from the secret. if ($otp->verify($_POST['totp_code'])) { - + $_SESSION['2FA_VERIFIED'] = true; + $retval['status'] = true; + json_return_and_die($retval); } - $otp->verify($input); // Returns true if the input is verified, otherwise false. - require_once("addon/totp/class_totp.php"); - $ref = intval($_POST['totp_code']); - - $totp = new \TOTP(ucfirst(System::get_platform_name()), - $account['account_email'], $secret, 30, 6); - $match = ($totp->authcode($totp->timestamp()) == $ref); - if ($match) $_SESSION['2FA_VERIFIED'] = true; - json_return_and_die(array("match" => ($match ? "1" : "0"))); + } + json_return_and_die($retval); } - json_return_and_die(array("status" => false)); -} @@ -57,16 +52,11 @@ class Totp_check extends Controller { return AConfig::get($acct_id, 'totp', 'secret', null); } function get() { - if (!$this->totp_installed()) { - //Do not display any associated widgets at this point - App::$pdl = ''; - $papp = Apps::get_papp('TOTP'); - return Apps::app_render($papp, 'module'); - } - $account = App::get_account(); - if (!$account) goaway(z_root()); - $o .= replace_macros(Theme::get_template('totp.tpl','addon/totp'), + if (!$account) { + return t('Account not found.'); + } + return replace_macros(Theme::get_template('totp.tpl'), [ '$header' => t('TOTP Two-Step Verification'), '$desc' => t('Enter the 2-step verification generated by your authenticator app:'), @@ -74,9 +64,8 @@ class Totp_check extends Controller { '$fail' => t('Invalid code, please try again.'), '$maxfails' => t('Too many invalid codes...'), '$submit' => t('Verify') - ]); - return $o; + ] + ); } - } diff --git a/view/tpl/totp.tpl b/view/tpl/totp.tpl index 68c6a40c4..3a7dd1e83 100644 --- a/view/tpl/totp.tpl +++ b/view/tpl/totp.tpl @@ -1,20 +1,22 @@ -
-

{{$header}}

+
+
+

{{$header}}

{{$desc}}
-
- - -
-
+
+ +
+ + +