diff --git a/Code/Module/Totp_check.php b/Code/Module/Totp_check.php new file mode 100644 index 000000000..e4e9c5f4d --- /dev/null +++ b/Code/Module/Totp_check.php @@ -0,0 +1,82 @@ + false]; + + if (!local_channel()) { + json_return_and_die($retval); + } + + $account = App::get_account(); + if (!$account) { + json_return_and_die($retval); + } + $secret = $account['account_external']; + + if (isset($_POST['totp_code'])) { + $otp = TOTP::create($secret); // create TOTP object from the secret. + if ($otp->verify($_POST['totp_code'])) { + + } + $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(array("status" => false)); +} + + + + + function totp_installed() { + $id = local_channel(); + if (!$id) { + return false; + } + return Apps::addon_app_installed($id, 'totp'); + } + function get_secret($acct_id) { + 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'), + [ + '$header' => t('TOTP Two-Step Verification'), + '$desc' => t('Enter the 2-step verification generated by your authenticator app:'), + '$success' => t('Success!'), + '$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 8893f89c5..68c6a40c4 100644 --- a/view/tpl/totp.tpl +++ b/view/tpl/totp.tpl @@ -15,14 +15,16 @@ var totp_success_msg = '{{$success}}'; var totp_fail_msg = '{{$fail}}'; var totp_maxfails_msg = '{{$maxfails}}'; var try_countdown = 3; + $(window).on("load", function() { totp_clear(); - }); +}); + function totp_clear() { - var box = document.getElementById("totp-code"); + let box = document.getElementById("totp-code"); box.value = ""; box.focus(); - } +} function totp_verify() { var code = document.getElementById("totp-code").value; $.post("totp", {totp_code: code},