Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | 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
namespace factor_sms;
18
 
19
use moodle_url;
20
use stdClass;
21
use tool_mfa\local\factor\object_factor_base;
1441 ariadna 22
use tool_mfa\local\secret_manager;
1 efrain 23
 
24
/**
25
 * SMS Factor implementation.
26
 *
27
 * @package     factor_sms
28
 * @subpackage  tool_mfa
29
 * @author      Peter Burnett <peterburnett@catalyst-au.net>
30
 * @copyright   Catalyst IT
31
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
32
 */
33
class factor extends object_factor_base {
34
 
35
    /** @var string Factor icon */
36
    protected $icon = 'fa-commenting-o';
37
 
38
    /**
39
     * Defines login form definition page for SMS Factor.
40
     *
41
     * @param \MoodleQuickForm $mform
42
     * @return \MoodleQuickForm $mform
43
     */
44
    public function login_form_definition(\MoodleQuickForm $mform): \MoodleQuickForm {
45
        $mform->addElement(new \tool_mfa\local\form\verification_field());
46
        $mform->setType('verificationcode', PARAM_ALPHANUM);
47
        return $mform;
48
    }
49
 
50
    /**
51
     * Defines login form definition page after form data has been set.
52
     *
53
     * @param \MoodleQuickForm $mform Form to inject global elements into.
54
     * @return \MoodleQuickForm $mform
55
     */
56
    public function login_form_definition_after_data(\MoodleQuickForm $mform): \MoodleQuickForm {
57
        $this->generate_and_sms_code();
58
 
59
        // Disable the form check prompt.
60
        $mform->disable_form_change_checker();
61
        return $mform;
62
    }
63
 
64
    /**
65
     * Implements login form validation for SMS Factor.
66
     *
67
     * @param array $data
68
     * @return array
69
     */
70
    public function login_form_validation(array $data): array {
71
        $return = [];
72
 
73
        if (!$this->check_verification_code($data['verificationcode'])) {
74
            $return['verificationcode'] = get_string('error:wrongverification', 'factor_sms');
75
        }
76
 
77
        return $return;
78
    }
79
 
80
    /**
81
     * Gets the string for setup button on preferences page.
82
     *
83
     * @return string
84
     */
85
    public function get_setup_string(): string {
86
        return get_string('setupfactorbutton', 'factor_sms');
87
    }
88
 
89
    /**
90
     * Gets the string for manage button on preferences page.
91
     *
92
     * @return string
93
     */
94
    public function get_manage_string(): string {
95
        return get_string('managefactorbutton', 'factor_sms');
96
    }
97
 
98
    /**
99
     * Defines setup_factor form definition page for SMS Factor.
100
     *
101
     * @param \MoodleQuickForm $mform
102
     * @return \MoodleQuickForm $mform
103
     */
104
    public function setup_factor_form_definition(\MoodleQuickForm $mform): \MoodleQuickForm {
105
        global $OUTPUT, $USER, $DB;
106
 
107
        if (!empty(
108
            $phonenumber = $DB->get_field('tool_mfa', 'label', ['factor' => $this->name, 'userid' => $USER->id, 'revoked' => 0])
109
        )) {
110
            redirect(
111
                new \moodle_url('/admin/tool/mfa/user_preferences.php'),
112
                get_string('factorsetup', 'tool_mfa', $phonenumber),
113
                null,
114
                \core\output\notification::NOTIFY_SUCCESS);
115
        }
116
 
117
        $mform->addElement('html', $OUTPUT->heading(get_string('setupfactor', 'factor_sms'), 2));
118
 
119
        if (empty($this->get_phonenumber())) {
120
            $mform->addElement('hidden', 'verificationcode', 0);
121
            $mform->setType('verificationcode', PARAM_ALPHANUM);
122
 
123
            // Add field for phone number setup.
124
            $mform->addElement('text', 'phonenumber', get_string('addnumber', 'factor_sms'),
125
                [
126
                    'autocomplete' => 'tel',
127
                    'inputmode' => 'tel',
128
                ]);
129
            $mform->setType('phonenumber', PARAM_TEXT);
130
 
131
            // HTML to display a message about the phone number.
132
            $message = \html_writer::tag('div', '', ['class' => 'col-md-3']);
133
            $message .= \html_writer::tag(
134
                'div', \html_writer::tag('p', get_string('phonehelp', 'factor_sms')), ['class' => 'col-md-9']);
135
            $mform->addElement('html', \html_writer::tag('div', $message, ['class' => 'row']));
136
        }
137
 
138
        return $mform;
139
    }
140
 
141
    /**
142
     * Defines setup_factor form definition page after form data has been set.
143
     *
144
     * @param \MoodleQuickForm $mform
145
     * @return \MoodleQuickForm $mform
146
     */
147
    public function setup_factor_form_definition_after_data(\MoodleQuickForm $mform): \MoodleQuickForm {
148
        global $OUTPUT;
149
 
150
        $phonenumber = $this->get_phonenumber();
151
        if (empty($phonenumber)) {
152
            return $mform;
153
        }
154
 
155
        $duration = get_config('factor_sms', 'duration');
156
        $code = $this->secretmanager->create_secret($duration, true);
157
        if (!empty($code)) {
158
            $this->sms_verification_code($code, $phonenumber);
159
        }
160
        $message = get_string('logindesc', 'factor_sms', '<b>' . $phonenumber . '</b><br/>');
161
        $message .= get_string('editphonenumberinfo', 'factor_sms');
162
        $mform->addElement('html', \html_writer::tag('p', $OUTPUT->notification($message, 'success')));
163
 
164
        $mform->addElement(new \tool_mfa\local\form\verification_field());
165
        $mform->setType('verificationcode', PARAM_ALPHANUM);
166
 
167
        $editphonenumber = \html_writer::link(
168
            new \moodle_url('/admin/tool/mfa/factor/sms/editphonenumber.php', ['sesskey' => sesskey()]),
169
            get_string('editphonenumber', 'factor_sms'),
170
            ['class' => 'btn btn-secondary', 'type' => 'button']);
171
 
1441 ariadna 172
        $mform->addElement('html', \html_writer::tag('div', $editphonenumber, ['class' => 'float-sm-start col-md-4']));
1 efrain 173
 
174
        // Disable the form check prompt.
175
        $mform->disable_form_change_checker();
176
 
177
        return $mform;
178
    }
179
 
180
    /**
181
     * Returns the phone number from the current session or from the user profile data.
182
     * @return string|null
183
     */
184
    private function get_phonenumber(): ?string {
185
        global $SESSION, $USER, $DB;
186
 
187
        if (!empty($SESSION->tool_mfa_sms_number)) {
188
            return $SESSION->tool_mfa_sms_number;
189
        }
190
        $phonenumber = $DB->get_field('tool_mfa', 'label', ['factor' => $this->name, 'userid' => $USER->id, 'revoked' => 0]);
191
        if (!empty($phonenumber)) {
192
            return $phonenumber;
193
        }
194
 
195
        return null;
196
    }
197
 
198
    /**
199
     * Returns an array of errors, where array key = field id and array value = error text.
200
     *
201
     * @param array $data
202
     * @return array
203
     */
204
    public function setup_factor_form_validation(array $data): array {
205
        $errors = [];
206
 
207
        // Phone number validation.
208
        if (!empty($data["phonenumber"]) && empty(helper::is_valid_phonenumber($data["phonenumber"]))) {
209
            $errors['phonenumber'] = get_string('error:wrongphonenumber', 'factor_sms');
210
 
211
        } else if (!empty($this->get_phonenumber())) {
212
            // Code validation.
213
            if (empty($data["verificationcode"])) {
214
                $errors['verificationcode'] = get_string('error:emptyverification', 'factor_sms');
215
            } else if ($this->secretmanager->validate_secret($data['verificationcode']) !== $this->secretmanager::VALID) {
216
                $errors['verificationcode'] = get_string('error:wrongverification', 'factor_sms');
217
            }
218
        }
219
 
220
        return $errors;
221
    }
222
 
223
    /**
224
     * Reset values of the session data of the given factor.
225
     *
226
     * @param int $factorid
227
     * @return void
228
     */
229
    public function setup_factor_form_is_cancelled(int $factorid): void {
230
        global $SESSION;
231
        if (!empty($SESSION->tool_mfa_sms_number)) {
232
            unset($SESSION->tool_mfa_sms_number);
233
        }
234
        // Clean temp secrets code.
1441 ariadna 235
        $secretmanager = new secret_manager('sms');
1 efrain 236
        $secretmanager->cleanup_temp_secrets();
237
    }
238
 
239
    /**
240
     * Setup submit button string in given factor
241
     *
242
     * @return string|null
243
     */
244
    public function setup_factor_form_submit_button_string(): ?string {
245
        global $SESSION;
246
        if (!empty($SESSION->tool_mfa_sms_number)) {
247
            return get_string('setupsubmitcode', 'factor_sms');
248
        }
249
        return get_string('setupsubmitphone', 'factor_sms');
250
    }
251
 
252
    /**
253
     * Adds an instance of the factor for a user, from form data.
254
     *
255
     * @param stdClass $data
256
     * @return stdClass|null the factor record, or null.
257
     */
258
    public function setup_user_factor(stdClass $data): ?stdClass {
259
        global $DB, $SESSION, $USER;
260
 
261
        // Handle phone number submission.
262
        if (empty($SESSION->tool_mfa_sms_number)) {
263
            $SESSION->tool_mfa_sms_number = !empty($data->phonenumber) ? $data->phonenumber : '';
264
 
265
            $addurl = new \moodle_url('/admin/tool/mfa/action.php', [
266
                'action' => 'setup',
267
                'factor' => 'sms',
268
            ]);
269
            redirect($addurl);
270
        }
271
 
272
        // If the user somehow gets here through form resubmission.
273
        // We dont want two phones active.
274
        if ($DB->record_exists('tool_mfa', ['userid' => $USER->id, 'factor' => $this->name, 'revoked' => 0])) {
275
            return null;
276
        }
277
 
278
        $time = time();
279
        $label = $this->get_phonenumber();
280
 
281
        $row = new \stdClass();
282
        $row->userid = $USER->id;
283
        $row->factor = $this->name;
284
        $row->secret = '';
285
        $row->label = $label;
286
        $row->timecreated = $time;
287
        $row->createdfromip = $USER->lastip;
288
        $row->timemodified = $time;
289
        $row->lastverified = $time;
290
        $row->revoked = 0;
291
 
292
        $id = $DB->insert_record('tool_mfa', $row);
293
        $record = $DB->get_record('tool_mfa', ['id' => $id]);
294
        $this->create_event_after_factor_setup($USER);
295
 
296
        // Remove session phone number.
297
        unset($SESSION->tool_mfa_sms_number);
298
 
299
        return $record;
300
    }
301
 
302
    /**
303
     * Returns an array of all user factors of given type.
304
     *
305
     * @param stdClass $user the user to check against.
306
     * @return array
307
     */
308
    public function get_all_user_factors(stdClass $user): array {
309
        global $DB;
310
 
311
        $sql = 'SELECT *
312
                  FROM {tool_mfa}
313
                 WHERE userid = ?
314
                   AND factor = ?
315
                   AND label IS NOT NULL
316
                   AND revoked = 0';
317
 
318
        return $DB->get_records_sql($sql, [$user->id, $this->name]);
319
    }
320
 
321
    /**
322
     * Returns the information about factor availability.
323
     *
324
     * @return bool
325
     */
326
    public function is_enabled(): bool {
327
        return parent::is_enabled();
328
    }
329
 
330
    /**
331
     * Decides if a factor requires input from the user to verify.
332
     *
333
     * @return bool
334
     */
335
    public function has_input(): bool {
336
        return true;
337
    }
338
 
339
    /**
340
     * Decides if factor needs to be setup by user and has setup_form.
341
     *
342
     * @return bool
343
     */
344
    public function has_setup(): bool {
345
        return true;
346
    }
347
 
348
    /**
349
     * Decides if the setup buttons should be shown on the preferences page.
350
     *
351
     * @return bool
352
     */
353
    public function show_setup_buttons(): bool {
1441 ariadna 354
        if (get_config('factor_sms', 'smsgateway') > 0) {
355
            return true;
356
        }
357
        return false;
1 efrain 358
    }
359
 
360
    /**
361
     * Returns true if factor class has factor records that might be revoked.
362
     * It means that user can revoke factor record from their profile.
363
     *
364
     * @return bool
365
     */
366
    public function has_revoke(): bool {
367
        return true;
368
    }
369
 
370
    /**
371
     * Generates and sms' the code for login to the user, stores codes in DB.
372
     *
373
     * @return int|null the instance ID being used.
374
     */
375
    private function generate_and_sms_code(): ?int {
376
        global $DB, $USER;
377
 
378
        $duration = get_config('factor_sms', 'duration');
379
        $instance = $DB->get_record('tool_mfa', ['factor' => $this->name, 'userid' => $USER->id, 'revoked' => 0]);
380
        if (empty($instance)) {
381
            return null;
382
        }
383
        $secret = $this->secretmanager->create_secret($duration, false);
384
        // There is a new code that needs to be sent.
385
        if (!empty($secret)) {
386
            // Grab the singleton SMS record.
387
            $this->sms_verification_code($secret, $instance->label);
388
        }
389
        return $instance->id;
390
    }
391
 
392
    /**
393
     * This function sends an SMS code to the user based on the phonenumber provided.
394
     *
395
     * @param int $secret the secret to send.
396
     * @param string|null $phonenumber the phonenumber to send the verification code to.
397
     * @return void
398
     */
399
    private function sms_verification_code(int $secret, ?string $phonenumber): void {
400
        global $CFG, $SITE;
401
 
402
        // Here we should get the information, then construct the message.
403
        $url = new moodle_url($CFG->wwwroot);
404
        $content = [
405
            'fullname' => $SITE->fullname,
406
            'url' => $url->get_host(),
407
            'code' => $secret,
408
        ];
409
        $message = get_string('smsstring', 'factor_sms', $content);
410
 
1441 ariadna 411
        $manager = \core\di::get(\core_sms\manager::class);
412
        $manager->send(
413
            recipientnumber: $phonenumber,
414
            content: $message,
415
            component: 'factor_sms',
416
            messagetype: 'mfa',
417
            recipientuserid: null,
418
            issensitive: true,
419
            async: false,
420
            gatewayid: get_config('factor_sms', 'smsgateway'),
421
        );
1 efrain 422
    }
423
 
424
    /**
425
     * Verifies entered code against stored DB record.
426
     *
427
     * @param string $enteredcode
428
     * @return bool
429
     */
430
    private function check_verification_code(string $enteredcode): bool {
1441 ariadna 431
        return $this->secretmanager->validate_secret($enteredcode) === secret_manager::VALID;
1 efrain 432
    }
433
 
434
    /**
435
     * Returns all possible states for a user.
436
     *
437
     * @param \stdClass $user
438
     */
439
    public function possible_states(\stdClass $user): array {
440
        return [
441
            \tool_mfa\plugininfo\factor::STATE_PASS,
442
            \tool_mfa\plugininfo\factor::STATE_NEUTRAL,
443
            \tool_mfa\plugininfo\factor::STATE_FAIL,
444
            \tool_mfa\plugininfo\factor::STATE_UNKNOWN,
445
        ];
446
    }
447
 
448
    /**
449
     * Get the login description associated with this factor.
450
     * Override for factors that have a user input.
451
     *
452
     * @return string The login option.
453
     */
454
    public function get_login_desc(): string {
455
 
456
        $phonenumber = $this->get_phonenumber();
457
 
458
        if (empty($phonenumber)) {
459
            return get_string('errorsmssent', 'factor_sms');
460
        }
1441 ariadna 461
 
462
        return get_string('logindesc', 'factor_' . $this->name, $phonenumber);
1 efrain 463
    }
464
}