forked from otatarintseva/moodle-auth_samlidp
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauth.php
279 lines (252 loc) · 9.38 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
<?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: SAML IdP
*
* Allows Moodle to act as an IdP. Reqiures standalone SimpleSAMLphp properly configured
*
* @package auth_samlidp
* @copyright 2017 Enovation Solutions (http://enovation.ie)
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/authlib.php');
class auth_plugin_samlidp extends auth_plugin_base {
const COMPONENT_NAME = 'auth_samlidp';
const AUTH_COOKIE_DEFAULT = 'MoodleSAMLIDPSessionID';
private $simplesamlAutoloadPhp;
/**
* Constructor.
*/
public function __construct() {
global $CFG;
$this->authtype = 'samlidp';
$this->config = get_config(self::COMPONENT_NAME);
$this->simplesamlAutoloadPhp = $this->config->simplesaml_coderoot.'/lib/_autoload.php';
}
/**
* Saves $_GET{'ReturnTo'}
*
* @param void
* @return void
*/
public function loginpage_hook () {
$returnto = optional_param('ReturnTo', '', PARAM_URL);
if ($returnto) {
if (isloggedin() and !isguestuser()) {
# this is a consequent return to Moodle via SAML
# it needs to be handled a usual SAML way - create a cookie and redirect to the return address
global $USER;
$this->set_cookie($USER);
header('Location: '. $returnto);
exit();
} else {
global $SESSION;
$SESSION->samlurl = $returnto; # record $returnto as it will not be available later
}
}
}
/**
* Sets a module-specific cookie to send a user ID to SimpleSAMLphp
* called from loginpage_hook() and user_authenticated_hook()
*
* @param stdClass $user
* @return void
*/
private function set_cookie ($user) {
if (file_exists($this->simplesamlAutoloadPhp)) {
require_once($this->simplesamlAutoloadPhp);
$sspConfig = \SimpleSAML\Configuration::getInstance();
$sspAuthsources = \SimpleSAML\Configuration::getConfig('authsources.php');
$cookieName = $sspAuthsources->getValue($this->config->simplesaml_authsource);
$uid = $user->id;
if ($cookieName && isset($cookieName['cookie_name']) && $cookieName['cookie_name']) {
$salt = $sspConfig->getValue('secretsalt');
setcookie($cookieName['cookie_name'], hash_hmac('sha1', $salt.$uid, $salt).':'.$uid, 0, $sspConfig->getValue('session.cookie.path'));
} else {
$this->report_misconfigured_authsouces();
}
} else {
$this->report_missing_autoload();
}
}
/**
* If configured properly, sets an authentication cookie, encrypts a user ID into the cookie value
*
* @param object $user user object, later used for $USER
* @param string $username (with system magic quotes)
* @param string $password plain text password (with system magic quotes)
* @return void
*/
public function user_authenticated_hook (&$user, $username, $password) {
global $SESSION, $USER;
$this->set_cookie($user);
if (isset($SESSION->samlurl) && $SESSION->samlurl) {
$samlurl = $SESSION->samlurl;
unset($SESSION->samlurl);
complete_user_login($user); # need to run it here otherwise the moodle user is not really logged in
header('Location: '. $samlurl);
exit(); # need to exit as otherwise moodle takes control and redirects to own /
}
}
/**
* If configured properly, destroys own auth cookie, optionally redirects to the ReturnTo URL
*
* @param stdClass $user clone of USER object before the user session was terminated
* @return void
*/
public function postlogout_hook($user) {
global $OUTPUT;
if (file_exists($this->simplesamlAutoloadPhp)) {
$returnto = optional_param('ReturnTo', '', PARAM_URL);
require_once($this->simplesamlAutoloadPhp);
$sspConfig = \SimpleSAML\Configuration::getInstance();
$sspAuthsources = \SimpleSAML\Configuration::getConfig('authsources.php');
$cookieName = $sspAuthsources->getValue($this->config->simplesaml_authsource);
if ($cookieName && isset($cookieName['cookie_name']) && $cookieName['cookie_name']) {
setcookie($cookieName['cookie_name'], '', time() - 3600, $sspConfig->getValue('session.cookie.path'));
if (ini_get('session.use_cookies')) {
$params = session_get_cookie_params();
setcookie(
session_name(),
'',
time() - 42000,
$params['path'], $params['domain'],
$params['secure'], $params['httponly']
);
}
if ($returnto) {
header('Location: '.$returnto);
exit();
}
} else {
$this->report_misconfigured_authsouces();
}
} else {
$this->report_missing_autoload();
}
}
/**
* Reports a configuration error into http server error.log
*
* @return void
*/
private function report_misconfigured_authsouces () {
$msg = sprintf("Misconfigured SimpleSAMLphp IdP (missing configuration block for '%s' in authsources.php, or 'cookie_name' entry in the block)
or incorrect SAML IdP Moodle module configuration (wrong simplesaml_authsource)", $this->config->simplesaml_authsource);
trigger_error($msg, E_USER_WARNING);
}
/**
* Reports a configuration error into http server error.log
*
* @return void
*/
private function report_missing_autoload () {
trigger_error("Misconfigured SAML IDP plugin: cannot find a path to SimpleSAMLphp _autoload.php. The current path is incorrect: ".
$this->simplesamlAutoloadPhp, E_USER_WARNING);
}
/**
* Returns true if the username and password work and false if they are
* wrong or don't exist.
*
* @param string $username The username
* @param string $password The password
* @return bool Authentication success or failure.
*/
public function user_login($username, $password) {
return false;
}
/**
* Called when the user record is updated.
* Modifies user in external database. It takes olduser (before changes) and newuser (after changes)
* compares information saved modified information to external db.
*
* @param stdClass $olduser Userobject before modifications
* @param stdClass $newuser Userobject new modified userobject
* @return boolean result
*
*/
public function user_update($olduser, $newuser) {
return false;
}
/**
* A chance to validate form data, and last chance to
* do stuff before it is inserted in config_plugin
*
* @param stfdClass $form
* @param array $err errors
* @return void
*/
public function validate_form($form, &$err) {
}
/**
* 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;
}
/**
* Returns false if this plugin is enabled but not configured.
*
* @return bool
*/
public function is_configured() {
if (!empty($this->config->simplesaml_authsource) && !empty($this->config->simplesaml_coderoot)) {
return true;
}
return false;
}
/**
* Indicates if moodle should automatically update internal user
* records with data from external sources using the information
* from auth_plugin_base::get_userinfo().
*
* @return bool true means automatically copy data from ext to user table
*/
public function is_synchronised_with_external() {
return false;
}
/**
* Returns true if this authentication plugin can change the user's
* password.
*
* @return bool
*/
public function can_change_password() {
return false;
}
/**
* Returns the URL for changing the user's pw, or empty if the default can
* be used.
*
* @return moodle_url
*/
public function change_password_url() {
return null;
}
/**
* Returns true if plugin allows resetting of internal password.
*
* @return bool
*/
public function can_reset_password() {
return false;
}
}