forked from tonyjbutler/moodle-auth_collabcosso
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.php
193 lines (153 loc) · 6.66 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
<?php
/*****************************************************************************************************************************************/
/* ____ ___ _ _ _ ____ ____ ___
/* / ___/ _ \| | | | / \ | __ ) / ___/ _ \
/* | | | | | | | | | / _ \ | _ \| | | | | |
/* | |__| |_| | |___| |___ / ___ \| |_) | |__| |_| |
/* \____\___/|_____|_____/_/ \_\____/ \____\___/
/*
/*****************************************************************************************************************************************/
/* Author: Collabco Software (Oli Newsham)
/* Support: [email protected]
/* Website: Collabco.co.uk
/* Twitter: @collabco
/*****************************************************************************************************************************************/
/*
/* This source code must retain the above copyright notice, this list of conditions and the following disclaimer.
/*
/* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT
/* NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
/* THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
/* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
/* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
/* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
/*
/*****************************************************************************************************************************************/
if (!defined('MOODLE_INTERNAL'))
{
die('Direct access to this script is forbidden.');
}
@date_default_timezone_set('UTC');
require_once($CFG->libdir.'/authlib.php');
class auth_plugin_collabcosso extends auth_plugin_base {
function auth_plugin_collabcosso() {
$this->authtype = 'collabcosso';
$this->config = get_config('auth/collabcosco');
}
function user_login($username, $password) {
return false;
}
function can_reset_password() {
return false;
}
function can_signup() {
return false;
}
function can_confirm() {
return false;
}
function can_change_password() {
return false;
}
function is_internal() {
return true;
}
function loginpage_hook()
{
$plugin_loggingname = "Collabco SSO";
global $CFG, $USER, $SESSION;
if ($_SERVER['REQUEST_METHOD'] === 'GET' && !empty($_GET['u']) && !empty($_GET['t']) && !empty($_GET['h']) && !empty($_GET['r']))
{
$username = $_GET['u'];
$time = $_GET['t'];
$hash = $_GET['h'];
$redirect = urldecode($_GET['r']);
//Ensure we don't generate the hash on an escaped backslash
$usernameH = str_replace("\\\\","\\",$username);
if (strlen($CFG->auth_collabcosso_salt) == 0)
{
add_to_log(SITEID, $plugin_loggingname, 'Error', 'auth.php', "Salt is not set." );
return false;
}
else if (strlen($CFG->auth_collabcosso_salt) <= 10)
{
add_to_log(SITEID, $plugin_loggingname, 'Error', 'auth.php', "Salt is an insufficient length." );
return false;
}
$strToHash = $usernameH . $time . $redirect . $CFG->auth_collabcosso_salt;
$expectedHash = md5($strToHash);
if (strcmp($hash,$expectedHash) !== 0)
{
add_to_log(SITEID, $plugin_loggingname, 'Error', 'auth.php', "Bad hash: " . addslashes($username));
return false;
}
if (isloggedin() && !isguestuser())
{
add_to_log(SITEID, $plugin_loggingname, 'Warning', 'auth.php', "User is already loggedin: " . addslashes($username));
echo "<script language='Javascript'>";
echo " alert ('this single sign on link has expired')";
echo " window.location.replace('" . $CFG->wwwroot . "/" . $redirect . "')";
echo "</script>";
//redirect($CFG->wwwroot . "/" . $redirect);
return false;
}
$dbits = explode("-",$time);
$incomingDateString = $dbits[2].'-'.$dbits[1].'-'.$dbits[0].' '.$dbits[3].':'.$dbits[4].':'.$dbits[5];
$timestamp = strtotime($incomingDateString);
$now = time();
if ($now - $timestamp <= 120)
{
//Moodle doesn't contain user principals so strip the domain
if (strpos($username,"\\\\") !== false)
{
$usernameBits = explode("\\\\", $username);
$username = $usernameBits[1];
}
if ($user)
{
add_to_log(SITEID, $plugin_loggingname, 'sso login event', "auth.php" , "SSO login: " . addslashes($username), 0, $USER->id);
$auth = empty($USER->auth) ? 'manual' : $USER->auth; // use manual if auth not set
if (!empty($USER->suspended))
{
add_to_log(SITEID, $plugin_loggingname, 'Error', 'auth.php', "Suspended login: " . addslashes($username), 0, $USER->id);
return false;
}
if ($auth=='nologin' or !is_enabled_auth($auth))
{
add_to_log(SITEID, $plugin_loggingname, 'Error', 'auth.php', "Disabled login: " . addslashes($username), 0, $USER->id);
return false;
}
complete_user_login($user);
if (user_not_fully_set_up($USER))
{
$urltogo = $CFG->wwwroot . "/user/edit.php";
}
else
{
$urltogo = $CFG->wwwroot . "/" . $redirect;
}
add_to_log(SITEID, $plugin_loggingname, 'Sign-In', 'auth.php', "Successfully signed in: " . addslashes($username), 0, $USER->id);
redirect($urltogo);
}
else
{
add_to_log(SITEID, $plugin_loggingname, 'Error', 'auth.php', "Unknown user: " . addslashes($username));
return false;
}
}
else
{
echo "<script language='Javascript'>";
echo "alert ('this single sign on link has expired')";
echo "</script>";
}
}
}
function config_form($config, $err, $user_fields) {
include "config.html";
}
function process_config($config) {
return true;
}
}
?>