-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathauth.php
executable file
·322 lines (291 loc) · 11.6 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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<?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/>.
/**
* Authentication Plugin: CAS Authentication with attribute release
*
* Authentication using CAS (Central Authentication Server) with attributes returned from the CAS server
*
* @package auth_casattras
* @author Adam Franco
* @copyright 2014 Middlebury College {@link http://www.middlebury.edu}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
if (!defined('MOODLE_INTERNAL')) {
die('Direct access to this script is forbidden.'); // It must be included from a Moodle page.
}
require_once($CFG->libdir.'/authlib.php');
require_once($CFG->dirroot.'/auth/cas/CAS/vendor/autoload.php');
require_once($CFG->dirroot.'/auth/cas/CAS/vendor/apereo/phpcas/source/CAS.php');
/**
* CAS-Attras authentication plugin.
*
* @package auth_casattras
* @author Adam Franco
* @copyright 2014 Middlebury College {@link http://www.middlebury.edu}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class auth_plugin_casattras extends auth_plugin_base {
/** @var boolean Flag to ensure that phpCAS only gets initialized once. */
protected static $casinitialized = false;
/**
* Constructor with initialization.
*/
public function __construct() {
$this->authtype = 'casattras';
$this->errorlogtag = '[AUTH CAS-ATTRAS] ';
$this->config = get_config('auth_casattras');
// Verify that the CAS auth plugin is not enabled, disable this plugin (casattras) if so, because they will conflict.
if (is_enabled_auth('cas') && is_enabled_auth('casattras')) {
// This code is modeled on that in moodle/admin/auth.php.
global $CFG;
get_enabled_auth_plugins(true);
if (empty($CFG->auth)) {
$authsenabled = array();
} else {
$authsenabled = explode(',', $CFG->auth);
}
$key = array_search('casattras', $authsenabled);
if ($key !== false) {
unset($authsenabled[$key]);
set_config('auth', implode(',', $authsenabled));
}
if ('casattras' == $CFG->registerauth) {
set_config('registerauth', '');
}
\core\session\manager::gc(); // Remove stale sessions.
$returnurl = new moodle_url('/admin/settings.php', array('section' => 'manageauths'));
print_error('casattras_disabled_by_cas', 'auth_casattras', $returnurl, null,
get_string('casattras_disabled_by_cas', 'auth_casattras'));
}
}
/**
* Return the properly translated human-friendly title of this auth plugin
*
* @todo Document this function
*/
public function get_title() {
$title = parent::get_title();
if (is_enabled_auth('cas')) {
$title .= ' - '.get_string('cas_conflict_warning', 'auth_casattras');
}
return $title;
}
/**
* Returns true if this authentication plugin is "internal".
*
* Internal plugins use password hashes from Moodle user table for authentication.
*
* @return bool
*/
public function is_internal() {
return false;
}
/**
* Initialize phpCAS configuration.
*
*/
protected function init_cas() {
global $CFG;
if (self::$casinitialized) {
return;
}
// See MDL-75479
// Form the base URL of the server with just the protocol and hostname.
$serverurl = new moodle_url("/");
$servicebaseurl = $serverurl->get_scheme() ? $serverurl->get_scheme() . "://" : '';
$servicebaseurl .= $serverurl->get_host();
// Add the port if set.
$servicebaseurl .= $serverurl->get_port() ? ':' . $serverurl->get_port() : '';
// Make sure phpCAS doesn't try to start a new PHP session when connecting to the CAS server.
if ($this->config->proxycas) {
phpCAS::proxy(
constant($this->config->casversion),
$this->config->hostname,
(int) $this->config->port,
$this->config->baseuri,
$servicebaseurl,
false);
} else {
phpCAS::client(
constant($this->config->casversion),
$this->config->hostname,
(int) $this->config->port,
$this->config->baseuri,
$servicebaseurl,
false);
}
self::$casinitialized = true;
// If Moodle is configured to use a proxy, phpCAS needs some curl options set.
if (!empty($CFG->proxyhost) && !is_proxybypass($this->config->hostname)) {
phpCAS::setExtraCurlOption(CURLOPT_PROXY, $CFG->proxyhost);
if (!empty($CFG->proxyport)) {
phpCAS::setExtraCurlOption(CURLOPT_PROXYPORT, $CFG->proxyport);
}
if (!empty($CFG->proxytype)) {
// Only set CURLOPT_PROXYTYPE if it's something other than the curl-default http.
if ($CFG->proxytype == 'SOCKS5') {
phpCAS::setExtraCurlOption(CURLOPT_PROXYTYPE, CURLPROXY_SOCKS5);
}
}
if (!empty($CFG->proxyuser) and !empty($CFG->proxypassword)) {
phpCAS::setExtraCurlOption(CURLOPT_PROXYUSERPWD, $CFG->proxyuser.':'.$CFG->proxypassword);
if (defined('CURLOPT_PROXYAUTH')) {
// Any proxy authentication if PHP 5.1.
phpCAS::setExtraCurlOption(CURLOPT_PROXYAUTH, CURLAUTH_BASIC | CURLAUTH_NTLM);
}
}
}
if ($this->config->certificatecheck && $this->config->certificatepath) {
phpCAS::setCasServerCACert($this->config->certificatepath);
} else {
// Don't try to validate the server SSL credentials.
phpCAS::setNoCasServerValidation();
}
}
/**
* Hook for overriding behaviour of login page.
* This method is called from login/index.php page for all enabled auth plugins.
*/
public function loginpage_hook() {
global $frm; // Can be used to override submitted login form.
global $SESSION;
// Return if CAS enabled and settings are not specified yet.
if (empty($this->config->hostname)) {
return;
}
// Don't do CAS authentication if the username/password form was submitted.
$username = optional_param('username', '', PARAM_RAW);
$ticket = optional_param('ticket', '', PARAM_RAW);
if (!empty($username)) {
if (isset($SESSION->wantsurl) && (strstr($SESSION->wantsurl, 'ticket') ||
strstr($SESSION->wantsurl, 'NOCAS'))) {
unset($SESSION->wantsurl);
}
return;
}
if ($this->config->multiauth) {
// If there is an authentication error, stay on the default authentication page.
if (!empty($SESSION->loginerrormsg)) {
return;
}
$usecas = optional_param('authCASattras', '', PARAM_ALPHA);
if ($usecas != 'CASattras') {
return;
}
}
// Configure phpCAS.
$this->init_cas();
// If already authenticated.
if (phpCAS::checkAuthentication()) {
if (empty($frm)) {
$frm = new stdClass;
}
$frm->username = phpCAS::getUser();
$frm->password = 'passwdCas';
$frm->logintoken = \core\session\manager::get_login_token();
return;
}
// Force CAS authentication (if needed).
if (!phpCAS::isAuthenticated()) {
phpCAS::forceAuthentication();
}
}
/**
* Authenticates user against CAS
* Returns true if the username and password work and false if they are
* wrong or don't exist.
*
* @param string $username The username (with system magic quotes)
* @param string $password The password (with system magic quotes)
* @return bool Authentication success or failure.
*/
public function user_login ($username, $password) {
$this->init_cas();
return phpCAS::isAuthenticated() && (trim(core_text::strtolower(phpCAS::getUser())) == $username);
}
/**
* Returns user attribute mappings between Moodle and the CAS server.
*
* @return array
*/
protected function attributes() {
$moodleattributes = array();
$customfields = $this->get_custom_user_profile_fields();
if (!empty($customfields) && !empty($this->userfields)) {
$userfields = array_merge($this->userfields, $customfields);
} else {
$userfields = $this->userfields;
}
foreach ($userfields as $field) {
if (!empty($this->config->{"field_map_$field"})) {
$moodleattributes[$field] = $this->config->{"field_map_$field"};
}
}
return $moodleattributes;
}
/**
* Read user information from cas server and returns it as array().
* Function should return all information available. If you are saving
* this information to moodle user-table you should honour synchronisation flags
*
* @param string $username username
*
* @return mixed array with no magic quotes or false on error
*/
public function get_userinfo($username) {
if (!phpCAS::isAuthenticated() || trim(core_text::strtolower(phpCAS::getUser())) != $username) {
return array();
}
$casattras = phpCAS::getAttributes();
$moodleattras = array();
foreach ($this->attributes() as $key => $field) {
$moodleattras[$key] = $casattras[$field];
}
return $moodleattras;
}
/**
* Logout from the CAS
*
*/
public function prelogout_hook() {
global $CFG;
if (!empty($this->config->logoutcas)) {
$backurl = $CFG->wwwroot;
$this->init_cas();
phpCAS::logoutWithURL($backurl);
}
}
/**
* Return a list of identity providers to display on the login page.
*
* @param string|moodle_url $wantsurl The requested URL.
* @return array List of arrays with keys url, iconurl and name.
*/
public function loginpage_idp_list($wantsurl) {
global $CFG;
$config = get_config('auth_casattras');
$params = ["authCASattras" => "CASattras"];
$url = new moodle_url(get_login_url(), $params);
$iconurl = moodle_url::make_pluginfile_url(context_system::instance()->id,
'auth_casattras',
'logo',
null,
'/',
$config->auth_logo);
$result[] = ['url' => $url, 'iconurl' => $iconurl, 'name' => $config->auth_name];
return $result;
}
}