-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.php
288 lines (244 loc) · 9.26 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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
defined('MOODLE_INTERNAL') || die();
require_once $CFG->dirroot . '/auth/eklaseoauth2/lib/EklaseOAuth.php';
require_once $CFG->dirroot . '/auth/eklaseoauth2/locallib.php';
require_once $CFG->libdir . '/authlib.php';
require_once $CFG->dirroot . '/cohort/lib.php';
class auth_plugin_eklaseoauth2 extends auth_plugin_base
{
/** @var EklaseOAuth */
private $eklaseoauth;
const ROLE_STUDENT = 'Student';
const ROLE_TEACHER = 'Teacher';
const TEACHER = 1;
const STUDENT = 2;
public function __construct()
{
$this->authtype = 'eklaseoauth2';
$this->config = get_config('auth/' . $this->authtype);
}
public function loginpage_hook()
{
global $CFG, $DB, $SESSION;
$provider = optional_param('provider', '', PARAM_TEXT);
if (empty($provider) || $provider !== $this->authtype) {
return;
}
$code = optional_param('code', '', PARAM_TEXT);
if (empty($code)) {
if ($this->config->debug) {
error_log('[Eklase OAuth2] Missing authorization code.');
}
print_error('error_missingcode', 'auth_' . $this->authtype, null);
}
$this->eklaseoauth = new EklaseOAuth($this->config->client_id, $this->config->client_secret, $this->config->redirect_url);
$token = $this->get_access_token($code);
$userinfo = $this->get_user_info($token);
$username = $this->get_username($userinfo);
$user_role = $this->get_role($userinfo);
$user = $DB->get_record('user', array('username' => $username, 'mnethostid' => $CFG->mnet_localhost_id, 'idnumber' => $userinfo['person_code']));
if (!$user) {
$user = create_user_record($username, null, $this->authtype);
}
$SESSION->username = $username;
if (!authenticate_user_login($username, null)) {
print_error('error_failedauth', 'auth_' . $this->authtype);
}
$user->firstname = $userinfo['firstname'];
$user->lastname = $userinfo['lastname'];
$user->idnumber = $userinfo['person_code'];
$user->email = (!empty($userinfo['emailaddress'])) ? $userinfo['emailaddress'] : $user->email;
$user->school = $userinfo['school'];
$user->school_code = $userinfo['schoolviisid'];
$user->role = $userinfo['persontype'];
if (!empty($userinfo['classlevel']) && !empty($userinfo['classname'])) {
$user->classlevel = $userinfo['classlevel'];
$user->classname = $userinfo['classname'];
}
try {
$DB->update_record('user', $user);
} catch (moodle_exception $e) {
if ($this->config->debug) {
error_log('[Eklase OAuth2] Failed to update user.' . $e->getMessage() . '\n Serialized user data: (' . serialize($user) . ')');
}
print_error('error_failedupdateuser', 'auth_' . $this->authtype);
}
complete_user_login($user);
$goto = $CFG->wwwroot . '/';
if ((isset($this->config->forcecomplete) && $this->config->forcecomplete)/* && !$completed*/) {
$goto = $CFG->wwwroot . '/local/eu/complete.php';
} else {
if (user_not_fully_set_up($user)) {
$goto = $CFG->wwwroot . '/user/edit.php';
} else if (isset($SESSION->wantsurl) && (strpos($SESSION->wantsurl, $CFG->wwwroot) === 0)) {
$goto = $SESSION->wantsurl;
unset($SESSION->wantsurl);
}
}
redirect($goto);
}
/**
* @param string $username
* @param null $password
* @return bool
*/
public function user_login($username, $password)
{
global $CFG, $DB, $SESSION;
if (isset($SESSION->username) && $SESSION->username == $username) {
if ($user = $DB->get_record("user", array('username' => $username, 'auth' => $this->authtype, 'mnethostid' => $CFG->mnet_localhost_id))) {
unset($SESSION->username);
return true;
}
}
return false;
}
/**
* @param object $config
* @param object $err
* @param array $user_fields
*/
public function config_form($config, $err, $user_fields)
{
$config->client_id = (isset($config->client_id) ? $config->client_id : '');
$config->client_secret = (isset($config->client_secret) ? $config->client_secret : '');
$config->redirect_url = (isset($config->redirect_url) ? $config->redirect_url : '');
$config->usernameprefix = (isset($config->usernameprefix) ? $config->usernameprefix : 'ek_');
$config->debug = (isset($config->debug) ? $config->debug : '0');
include 'config.html';
}
/**
* Saglabā autentifikācijas moduļa konfigurāciju. Ja konfigurācijas atribūta vērtība netika norādīta,
* tad šim atribūtam saglabā sākumu vērtību.
*
* @param $config
* @return bool
*/
public function process_config($config)
{
set_config('client_id', $config->client_id, "auth/{$this->authtype}");
if ($config->client_secret !== 'password') {
set_config('client_secret', $config->client_secret, "auth/{$this->authtype}");
}
set_config('redirect_url', $config->redirect_url, "auth/{$this->authtype}");
set_config('usernameprefix', (!empty($config->usernameprefix) ? $config->usernameprefix : 'ek_'), "auth/{$this->authtype}");
set_config('debug', (isset($config->debug) ? 1 : 0), "auth/{$this->authtype}");
return true;
}
/**
* Ja autentifikācijas modulis aktivizēts, tad ārējais provaiders pieejams uz lappuses ar login formu.
*
* @param string $wantsurl
* @return array
*/
public function loginpage_idp_list($wantsurl)
{
$config = $this->config;
if (empty($config->client_id) || empty($config->redirect_url)) {
if ($config->debug) {
error_log('[Eklase OAuth2] Auth plugin not configured.');
}
return parent::loginpage_idp_list($wantsurl);
}
$idpurl = EklaseOAuth::getAuthorizeUrl($config->client_id, $config->redirect_url);
return array(
array(
'url' => new eklase_moodle_url($idpurl),
'icon' => new pix_icon('eklase', 'Eklase', 'auth_' . $this->authtype, array("class" => "eklaseoauth2"))
)
);
}
public function can_signup()
{
return false;
}
public function can_confirm()
{
return false;
}
public function can_edit_profile()
{
return true;
}
public function can_change_password()
{
return false;
}
public function can_reset_password()
{
return false;
}
public function is_internal()
{
return false;
}
public function prevent_local_passwords()
{
return true;
}
public function is_synchronised_with_external()
{
return false;
}
private function get_access_token($code)
{
$token = $this->eklaseoauth->getAccessToken($code);
if (!$token) {
if ($this->config->debug) {
error_log('[Eklase OAuth2] Get access token request failed');
}
print_error('error_failedgettoken', 'auth_' . $this->authtype);
}
return $token;
}
private function get_user_info($access_token)
{
$userinfo = array();
$userinfo['person_code'] = '';
$response = $this->eklaseoauth->getResource("https://my.e-klase.lv/Auth/OAuth/API/Me", $access_token);
if (!$xmldata = simplexml_load_string($response)) {
if ($this->config->debug) {
error_log('[auth/eklase] Can\'t connect to authentication API');
}
print_error(get_string('auth_internalerror', 'auth_eklase'));
}
if (!empty($xmldata->AdditionalData)) {
$dataitem = $xmldata->xpath("//DataItem[@id='2']");
$pattern = '/^(\d{6})-(\d{5})$/';
if (!empty($dataitem)) {
$person_code = (string)$dataitem[0];
if (!empty($person_code) && preg_match($pattern, $person_code))
$userinfo['person_code'] = $person_code;
unset($xmldata->AdditionalData);
}
}
foreach ((array)$xmldata as $field => $value) {
$field = strtolower($field);
$userinfo[$field] = empty($value) ? '' : $value;
}
return $userinfo;
}
private function get_username($userinfo)
{
if (!isset($userinfo['person_code']) || empty($userinfo['person_code'])) {
if ($this->config->debug) {
error_log('[Eklase OAuth2] User info not contain person_code.');
}
print_error('error_missinguserperson_code', 'auth_' . $this->authtype);
}
return $this->config->usernameprefix . $userinfo['person_code'];
}
private function get_role($userinfo)
{
$result = false;
switch ($userinfo['persontype']) {
case self::ROLE_STUDENT:
$result = self::STUDENT;
break;
case self::ROLE_TEACHER:
$result = self::TEACHER;
break;
}
return $result;
}
}