diff --git a/PageProtector.module.php b/PageProtector.module.php index d206b09..7810f65 100644 --- a/PageProtector.module.php +++ b/PageProtector.module.php @@ -288,43 +288,102 @@ public function protectedCheck(HookEvent $event) { } elseif($this->wire('input')->post->username && $this->wire('input')->post->pass) { $username = $this->wire('sanitizer')->username($this->wire('input')->post->username); - $user = $this->wire('session')->login($username, $this->wire('input')->post->pass); - if(!$user) $this->wire('session')->loginFailed = true; - $this->wire('session')->redirect(htmlspecialchars($_SERVER['REQUEST_URI'])); + $user = $this->wire('users')->get($username); + $has_tfa = false; + + // if the user exist, override the $has_tfa var with a not dummy value + if ($user->id) { + $has_tfa = $user->hasTfa(); + } + + if (!empty($has_tfa)) { + // $tfa = $this->wire('modules')->get('ProcessLogin')->getTfa(); + $tfa = $user->hasTfa(true); + // get the actual Tfa instance we need + $this->wire('session')->loginFailed = !$tfa->start($username, $this->wire('input')->post->pass); + if ($this->wire('session')->loginFailed) { + $this->wire('session')->redirect(htmlspecialchars($_SERVER['REQUEST_URI'])); + } + //tfa login supercedes normal login function + } + else { + $user = $this->wire('session')->login($username, $this->wire('input')->post->pass); + if(!$user) $this->wire('session')->loginFailed = true; + $this->wire('session')->redirect(htmlspecialchars($_SERVER['REQUEST_URI'])); + } } else { - $loginForm = " - -
- " . ($this->wire('session')->loginFailed ? "

" . __("Login Failed, please try again!") . "

" : "") . nl2br($this->getMessage($p, 'message', $lang)) . "
- data['usernamePlaceholder'.$lang] : $this->data['usernamePlaceholder'])."' required /> - data['passwordPlaceholder'.$lang] : $this->data['passwordPlaceholder'])."' required /> -

-
- "; + $tfa = $this->wire('modules')->get('ProcessLogin')->getTfa(); - if($this->wire('session')->loginFailed) $this->wire('session')->loginFailed = false; - - if($this->data['login_template'] == '') { - $event->return = " - - - - - - -
- $loginForm -
- - - "; + if (!empty($tfa) && $tfa->active()) { + if ($tfa->success()) { + //do nothing? + //TFA Session success; + } + else { + //Put TFA form in template for completion + if ($this->data['login_template'] != '') { + $p->loginForm = " + " . + '
' . + $tfa->render() . + '
'; $p->template->filename = $this->wire('config')->paths->templates . $this->data['login_template']; + } else { + $event->return = " + + + + + + + +
+

" . $tfa->render() . + "

+
+ + "; + } + } } else { - $p->loginForm = $loginForm; - $p->template->filename = $this->wire('config')->paths->templates . $this->data['login_template']; + $loginForm = " + +
+ " . ($this->wire('session')->loginFailed ? "

" . __("Login Failed, please try again!") . "

" : "") . nl2br($this->getMessage($p, 'message', $lang)) . "
+ data['usernamePlaceholder'.$lang] : $this->data['usernamePlaceholder'])."' required /> + data['passwordPlaceholder'.$lang] : $this->data['passwordPlaceholder'])."' required /> +

+
+ "; + + if($this->wire('session')->loginFailed) $this->wire('session')->loginFailed = false; + + if($this->data['login_template'] == '') { + $event->return = " + + + + + + +
+ $loginForm +
+ + + "; + } + else { + $p->loginForm = $loginForm; + $p->template->filename = $this->wire('config')->paths->templates . $this->data['login_template']; + } } } } @@ -751,3 +810,4 @@ public function getModuleConfigInputfields(array $data) { } } +