Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   1  <?php
   2  // This file is part of Moodle - http://moodle.org/
   3  //
   4  // Moodle is free software: you can redistribute it and/or modify
   5  // it under the terms of the GNU General Public License as published by
   6  // the Free Software Foundation, either version 3 of the License, or
   7  // (at your option) any later version.
   8  //
   9  // Moodle is distributed in the hope that it will be useful,
  10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12  // GNU General Public License for more details.
  13  //
  14  // You should have received a copy of the GNU General Public License
  15  // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
  16  /**
  17   * MFA page
  18   *
  19   * @package     tool_mfa
  20   * @author      Mikhail Golenkov <golenkovm@gmail.com>
  21   * @copyright   Catalyst IT
  22   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  23   */
  24  
  25  require_once(__DIR__ . '/../../../config.php');
  26  require_once($CFG->dirroot . '/admin/tool/mfa/lib.php');
  27  require_once($CFG->libdir.'/adminlib.php');
  28  
  29  use tool_mfa\local\form\login_form;
  30  use tool_mfa\manager;
  31  use tool_mfa\plugininfo\factor;
  32  
  33  require_login(null, false);
  34  
  35  $context = context_user::instance($USER->id);
  36  $PAGE->set_context($context);
  37  $PAGE->set_url('/admin/tool/mfa/auth.php');
  38  $PAGE->set_pagelayout('login');
  39  $PAGE->blocks->show_only_fake_blocks();
  40  $pagetitle = $SITE->shortname.': '.get_string('mfa', 'tool_mfa');
  41  $PAGE->set_title($pagetitle);
  42  
  43  // Logout if it was requested.
  44  $logout = optional_param('logout', false, PARAM_BOOL);
  45  if ($logout) {
  46      if (!empty($SESSION->wantsurl)) {
  47          // If we have the wantsurl, we should redirect there, to keep it intact.
  48          $wantsurl = $SESSION->wantsurl;
  49      } else {
  50          // Else redirect home.
  51          $wantsurl = new \moodle_url($CFG->wwwroot);
  52      }
  53  
  54      manager::mfa_logout();
  55      redirect($wantsurl);
  56  }
  57  
  58  $currenturl = new moodle_url('/admin/tool/mfa/auth.php');
  59  
  60  // Perform state check.
  61  manager::resolve_mfa_status();
  62  
  63  // We have a valid landing here, before doing any actions, clear any redir loop progress.
  64  manager::clear_redirect_counter();
  65  
  66  // If a specific factor was requested, use it.
  67  $pickedname = optional_param('factorname', false, PARAM_ALPHA);
  68  $pickedfactor = factor::get_factor($pickedname);
  69  $formfactor = optional_param('factor', false, PARAM_ALPHA);
  70  
  71  if ($pickedfactor && $pickedfactor->has_input() && $pickedfactor->get_state() == factor::STATE_UNKNOWN) {
  72      $factor = $pickedfactor;
  73  } else if ($formfactor) {
  74      // Check if a factor was supplied by the form, such as for a form submission.
  75      $factor = factor::get_factor($formfactor);
  76  } else {
  77      // Else, get the next factor that requires input.
  78      $factor = factor::get_next_user_login_factor();
  79  }
  80  
  81  // If ok, perform form actions for input factor.
  82  $form = new login_form($currenturl, ['factor' => $factor], 'post', '', ['class' => 'ignoredirty']);
  83  if ($form->is_submitted()) {
  84      if (!$form->is_validated() && !$form->is_cancelled()) {
  85          // Increment the fail counter for the factor,
  86          // And let the factor handle locking logic.
  87          $factor->increment_lock_counter();
  88          manager::resolve_mfa_status(false);
  89      } else {
  90          // Set state from user actions.
  91          if ($form->is_cancelled()) {
  92              $factor->process_cancel_action();
  93              // Move to next factor.
  94              manager::resolve_mfa_status(true);
  95          } else {
  96              if ($data = $form->get_data()) {
  97                  // Validation has passed, so before processing, lets action the global form submissions as well.
  98                  $form->globalmanager->submit($data);
  99  
 100                  // Did user submit something that causes a fail state?
 101                  if ($factor->get_state() == factor::STATE_FAIL) {
 102                      manager::resolve_mfa_status(true);
 103                  }
 104  
 105                  $factor->set_state(factor::STATE_PASS);
 106                  // Move to next factor.
 107                  manager::resolve_mfa_status(true);
 108              }
 109          }
 110      }
 111  }
 112  
 113  $renderer = $PAGE->get_renderer('tool_mfa');
 114  echo $OUTPUT->header();
 115  manager::display_debug_notification();
 116  echo $renderer->verification_form($factor, $form);
 117  echo $OUTPUT->footer();