Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
$sesskey = optional_param('sesskey', '_none_', PARAM_RAW);
46
 
47
if ($logout) {
48
    if (!confirm_sesskey($sesskey)) {
49
        echo $OUTPUT->header();
50
        echo $OUTPUT->confirm(
51
            get_string('logoutconfirm'),
52
            new moodle_url($PAGE->url, ['logout' => 1, 'sesskey' => sesskey()]),
53
            new moodle_url('/'),
54
        );
55
        echo $OUTPUT->footer();
56
        die;
57
    }
58
 
59
    if (!empty($SESSION->wantsurl)) {
60
        // If we have the wantsurl, we should redirect there, to keep it intact.
61
        $wantsurl = $SESSION->wantsurl;
62
    } else {
63
        // Else redirect home.
64
        $wantsurl = new \moodle_url($CFG->wwwroot);
65
    }
66
 
67
    manager::mfa_logout();
68
    redirect($wantsurl);
69
}
70
 
71
$currenturl = new moodle_url('/admin/tool/mfa/auth.php');
72
 
73
// Perform state check.
74
manager::resolve_mfa_status();
75
 
76
// We have a valid landing here, before doing any actions, clear any redir loop progress.
77
manager::clear_redirect_counter();
78
 
79
// If a specific factor was requested, use it.
80
$pickedname = optional_param('factorname', false, PARAM_ALPHA);
81
$pickedfactor = factor::get_factor($pickedname);
82
$formfactor = optional_param('factor', false, PARAM_ALPHA);
83
 
84
if ($pickedfactor && $pickedfactor->has_input() && $pickedfactor->get_state() == factor::STATE_UNKNOWN) {
85
    $factor = $pickedfactor;
86
} else if ($formfactor) {
87
    // Check if a factor was supplied by the form, such as for a form submission.
88
    $factor = factor::get_factor($formfactor);
89
} else {
90
    // Else, get the next factor that requires input.
91
    $factor = factor::get_next_user_login_factor();
92
}
93
 
94
// If ok, perform form actions for input factor.
95
$form = new login_form($currenturl, ['factor' => $factor], 'post', '', ['class' => 'ignoredirty']);
96
if ($form->is_submitted()) {
97
    if (!$form->is_validated() && !$form->is_cancelled()) {
98
        // Increment the fail counter for the factor,
99
        // And let the factor handle locking logic.
100
        $factor->increment_lock_counter();
101
        manager::resolve_mfa_status(false);
102
    } else {
103
        // Set state from user actions.
104
        if ($form->is_cancelled()) {
105
            $factor->process_cancel_action();
106
            // Move to next factor.
107
            manager::resolve_mfa_status(true);
108
        } else {
109
            if ($data = $form->get_data()) {
110
                // Validation has passed, so before processing, lets action the global form submissions as well.
111
                $form->globalmanager->submit($data);
112
 
113
                // Did user submit something that causes a fail state?
114
                if ($factor->get_state() == factor::STATE_FAIL) {
115
                    manager::resolve_mfa_status(true);
116
                }
117
 
118
                $factor->set_state(factor::STATE_PASS);
119
                // Move to next factor.
120
                manager::resolve_mfa_status(true);
121
            }
122
        }
123
    }
124
}
125
 
126
$renderer = $PAGE->get_renderer('tool_mfa');
127
echo $OUTPUT->header();
128
manager::display_debug_notification();
129
echo $renderer->verification_form($factor, $form);
130
echo $OUTPUT->footer();