-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauth.php
172 lines (146 loc) · 4.41 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
<?php
/**
* HTTP Basic Authentication.
* @package auth_http
* @copyright "Valentin Popov" <info@valentineus.link>
* @license MIT License (https://opensource.org/licenses/MIT)
*/
defined('MOODLE_INTERNAL') || die();
require_once($CFG->libdir.'/authlib.php');
/**
* Plugin for no authentication.
*/
class auth_plugin_http extends auth_plugin_base {
/**
* Constructor.
*/
public function __construct() {
$this->authtype = 'http';
}
/**
* Old syntax of class constructor. Deprecated in PHP7.
* @deprecated since Moodle 3.1
*/
public function auth_plugin_http() {
debugging('Use of class name as constructor is deprecated', DEBUG_DEVELOPER);
self::__construct();
}
/**
* Returns true if the username and password work or don't exist and false
* if the user exists and the password is wrong.
* @param string $username The username
* @param string $password The password
* @return bool Authentication success or failure.
*/
function user_login($username, $password) {
global $CFG, $DB;
if ($user = $DB->get_record('user', array('username'=>$username, 'mnethostid'=>$CFG->mnet_localhost_id))) {
return validate_internal_user_password($user, $password);
}
return true;
}
/**
* No password updates.
*/
function user_update_password($user, $newpassword) {
return false;
}
function prevent_local_passwords() {
// just in case, we do not want to loose the passwords
return false;
}
/**
* Returns true if this authentication plugin is 'internal'.
* @return bool
*/
function is_internal() {
//we do not know if it was internal or external originally
return true;
}
/**
* No changing of password.
*/
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
*/
function change_password_url() {
return null;
}
/**
* No password resetting.
*/
function can_reset_password() {
return true;
}
/**
* Returns true if plugin can be manually set.
* @return bool
*/
function can_be_manually_set() {
return true;
}
/**
* Hook for overriding behaviour before going to the login page.
*/
function pre_loginpage_hook() {
$this->loginpage_hook();
}
/**
* Hook for overriding behaviour of login page.
*/
function loginpage_hook() {
global $DB;
if (!isloggedin()) {
if (isset($_SERVER['PHP_AUTH_USER']) &&
isset($_SERVER['PHP_AUTH_PW'])) {
$username = htmlspecialchars($_SERVER['PHP_AUTH_USER']);
$password = htmlspecialchars($_SERVER['PHP_AUTH_PW']);
// User existence check
if ($user = $DB->get_record( 'user', array( 'username' => $username) )) {
// Verification of authorization data
if (validate_internal_user_password($user, $password)) {
complete_user_login($user);
$this->redirect_user();
} else {
// Authentication data verification error
$this->authorization_window();
}
} else {
// User search failed
$this->authorization_window();
}
} else {
// Authorization data is missing
$this->authorization_window();
}
}
}
/**
* Call authorization window.
*/
function authorization_window() {
global $SITE;
header('WWW-Authenticate: Basic realm="'. $SITE->shortname .'"');
header('HTTP/1.0 401 Unauthorized');
die(print_string('auth_httperror', 'auth_http'));
}
/**
* Redirect client to the original target.
*/
function redirect_user() {
global $CFG, $SESSION;
if (isset($SESSION->wantsurl)) {
$redirect = $SESSION->wantsurl;
} elseif (isset($_GET['wantsurl'])) {
$redirect = htmlspecialchars($_GET['wantsurl']);
} else {
$redirect = $CFG->wwwroot;
}
redirect($redirect);
}
}