-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.php
151 lines (137 loc) · 5.07 KB
/
auth.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* Twili OTP authentication plugin auth.
*
* @package auth_twiliootp
* @author Erudisiya <contact.erudisiya@gmail.com>
* @copyright 2024 Erudisiya Team(https://erudisiya.com)
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
defined('MOODLE_INTERNAL') || die();
global $CFG;
require_once($CFG->libdir . "/formslib.php");
require_once($CFG->libdir . '/authlib.php');
/**
* Phone OTP authentication plugin.
*
* @see self::user_login()
* @see self::get_user_field()
* @package auth_otp
* @copyright 2021 Brain Station 23 ltd
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class auth_plugin_twiliootp extends auth_plugin_base {
/**
* Default mapping field.
*/
const DEFAULT_MAPPING_FIELD = 'twiliootp';
const COMPONENT_NAME = 'auth_twiliootp';
const LEGACY_COMPONENT_NAME = 'auth/twiliootp';
/**
* User key manager.
*
* @var userkey_manager_interface
*/
//protected $userkeymanager;
/**
* Defaults for config form.
*
* @var array
*/
/**
* Constructor.
*/
public function __construct() {
$this->authtype = 'twiliootp';
$this->config = get_config('auth_twiliootp');
}
function can_signup() {
return true;
}
function loginpage_hook() {
global $PAGE, $CFG;
$PAGE->requires->jquery();
$PAGE->requires->js_init_code("buttonsAddMethod = 'auto';");
$content = str_replace(array("\n", "\r"), array("\\\n", "\\\r",), $this->get_buttons_string());
$PAGE->requires->js_init_code("buttons = '$content';");
$PAGE->requires->js(new moodle_url($CFG->wwwroot . "/auth/twiliootp/script.js"));
}
private function get_buttons_string() {
global $CFG;
$link = $CFG->wwwroot.'/auth/twiliootp/signup.php';
$content = '<div class="login-divider"></div>
<div class="login-instructions mb-3">
<h2 class="login-heading">Is this your first time here?</h2>
For full access to this site, you first need to create an account.
<div class="createnewlink">
<a class="btn btn-secondary"
href="'.$link.'" >'.get_string("createnewbutton", "auth_twiliootp") .'
</a><br>
</div>
</div>
';
return $content;
}
function user_signup($user, $notify=true) {
// Standard signup, without custom confirmatinurl.
return $this->user_signup_with_confirmation($user, $notify);
}
public function user_signup_with_confirmation($user, $notify=true, $confirmationurl = null) {
global $CFG, $DB, $SESSION;
require_once($CFG->dirroot.'/user/profile/lib.php');
require_once($CFG->dirroot.'/user/lib.php');
$plainpassword = $user->password;
$user->password = hash_internal_user_password($user->password);
if (empty($user->calendartype)) {
$user->calendartype = $CFG->calendartype;
}
$user->id = user_create_user($user, false, false);
user_add_password_history($user->id, $plainpassword);
// Save any custom profile field information.
profile_save_data($user);
}
function user_confirm($username, $confirmsecret) { //echo 'heee';die;
global $DB, $SESSION;
$user = get_complete_user_data('username', $username);
if (!empty($user)) {
if ($user->confirmed) {
return AUTH_CONFIRM_ALREADY;
} else {
$DB->set_field("user", "confirmed", 1, array("id"=>$user->id));
return AUTH_CONFIRM_OK;
}
} else {
return AUTH_CONFIRM_ERROR;
}
}
public function user_login($username, $password) {
global $CFG, $DB, $USER;
if (!$user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
return false;
}
if (!validate_internal_user_password($user, $password)) {
return false;
}
if ($password === 'changeme') {
// force the change - this is deprecated and it makes sense only for manual auth,
// because most other plugins can not change password easily or
// passwords are always specified by users
set_user_preference('auth_forcepasswordchange', true, $user->id);
}
return true;
}
}