This repository has been archived by the owner on Dec 21, 2022. It is now read-only.
/
LDAPLoginForm.php
222 lines (201 loc) · 8.79 KB
/
LDAPLoginForm.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
<?php
/**
* Class LDAPLoginForm
*
* This not very interesting in itself. It's pretty much boiler-plate code to access the authenticator.
*/
class LDAPLoginForm extends MemberLoginForm
{
/**
* This field is used in the "You are logged in as %s" message
* @var string
*/
public $loggedInAsField = 'FirstName';
/**
* @var string
*/
protected $authenticator_class = 'LDAPAuthenticator';
/**
* @var LDAPSecurityController
*/
protected $ldapSecController = null;
/**
* Time in seconds that we use to ensure consistent repsonse times
*
* @var int
*/
const RESPONSE_TIME = 2;
/**
* Enables consistent handling times of password resets
* @config
*/
private static $consistent_password_times = false;
/**
* Constructor.
*
* @param Controller $controller
* @param string $name method on the $controller
* @param FieldList $fields
* @param FieldList $actions
* @param bool $checkCurrentUser - show logout button if logged in
*/
public function __construct($controller, $name, $fields = null, $actions = null, $checkCurrentUser = true)
{
parent::__construct($controller, $name, $fields, $actions, $checkCurrentUser);
// will be used to get correct Link()
$this->ldapSecController = Injector::inst()->create('LDAPSecurityController');
if (Config::inst()->get('LDAPAuthenticator', 'allow_email_login')==='yes') {
$loginField = new TextField('Login', _t('LDAPLoginForm.USERNAMEOREMAIL', 'Username or email'), null, null, $this);
} else {
$loginField = new TextField('Login', _t('LDAPLoginForm.USERNAME', 'Username'), null, null, $this);
}
$this->Fields()->replaceField('Email', $loginField);
$this->setValidator(new RequiredFields('Login', 'Password'));
if (Security::config()->remember_username) {
$loginField->setValue(Session::get('SessionForms.MemberLoginForm.Email'));
} else {
// Some browsers won't respect this attribute unless it's added to the form
$this->setAttribute('autocomplete', 'off');
$loginField->setAttribute('autocomplete', 'off');
}
// Users can't change passwords unless appropriate a LDAP user with write permissions is
// configured the LDAP connection binding
$this->Actions()->remove($this->Actions()->fieldByName('forgotPassword'));
$allowPasswordChange = Config::inst()->get('LDAPService', 'allow_password_change');
if ($allowPasswordChange && $name != 'LostPasswordForm' && !Member::currentUser()) {
$forgotPasswordLink = sprintf('<p id="ForgotPassword"><a href="%s">%s</a></p>',
$this->ldapSecController->Link('lostpassword'),
_t('Member.BUTTONLOSTPASSWORD', "I've lost my password")
);
$forgotPassword = new LiteralField('forgotPassword', $forgotPasswordLink);
$this->Actions()->add($forgotPassword);
}
// Focus on the Username field when the page is loaded
Requirements::block('MemberLoginFormFieldFocus');
$js = <<<JS
(function() {
var el = document.getElementById("Login");
if(el && el.focus && (typeof jQuery == 'undefined' || jQuery(el).is(':visible'))) el.focus();
})();
JS;
Requirements::customScript($js, 'LDAPLoginFormFieldFocus');
}
/**
* Forgot password form handler method.
*
* Called when the user clicks on "I've lost my password".
*
* Extensions can use the 'forgotPassword' method to veto executing
* the logic, by returning FALSE. In this case, the user will be redirected back
* to the form without further action. It is recommended to set a message
* in the form detailing why the action was denied.
*
* Overridden because we need to generate a link to the LDAPSecurityController
* instead of the SecurityController
*
* @param array $data Submitted data
* @return SS_HTTPResponse
*/
public function forgotPassword($data)
{
// Passing true returns a float rather than a string
$startTime = microtime(true);
// No need to protect against injections, LDAPService will ensure that this is safe
$login = trim($data['Login']);
$service = Injector::inst()->get('LDAPService');
if (Email::validEmailAddress($login)) {
if (Config::inst()->get('LDAPAuthenticator', 'allow_email_login')!='yes') {
$this->sessionMessage(
_t(
'LDAPLoginForm.USERNAMEINSTEADOFEMAIL',
'Please enter your username instead of your email to get a password reset link.'
),
'bad'
);
$this->consistentResponseTime($startTime);
$this->controller->redirect($this->controller->Link('lostpassword'));
return;
}
$userData = $service->getUserByEmail($login);
} else {
$userData = $service->getUserByUsername($login);
}
// Avoid information disclosure by displaying the same status,
// regardless whether the email address actually exists
if (!isset($userData['objectguid'])) {
$this->consistentResponseTime($startTime);
return $this->controller->redirect($this->controller->Link('passwordsent/')
. urlencode($data['Login']));
}
$member = Member::get()->filter('GUID', $userData['objectguid'])->limit(1)->first();
// User haven't been imported yet so do that now
if (!($member && $member->exists())) {
$member = new Member();
$member->GUID = $userData['objectguid'];
}
// Update the users from LDAP so we are sure that the email is correct.
// This will also write the Member record.
$service->updateMemberFromLDAP($member);
// Allow vetoing forgot password requests
$results = $this->extend('forgotPassword', $member);
if ($results && is_array($results) && in_array(false, $results, true)) {
$this->consistentResponseTime($startTime);
return $this->controller->redirect($this->ldapSecController->Link('lostpassword'));
}
if ($member) {
$token = $member->generateAutologinTokenAndStoreHash();
$e = Member_ForgotPasswordEmail::create();
$e->populateTemplate($member);
$e->populateTemplate([
'PasswordResetLink' => LDAPSecurityController::getPasswordResetLink($member, $token)
]);
$e->setTo($member->Email);
$e->send();
$this->consistentResponseTime($startTime);
$this->controller->redirect($this->controller->Link('passwordsent/') . urlencode($data['Login']));
} elseif ($data['Login']) {
// Avoid information disclosure by displaying the same status,
// regardless whether the email address actually exists
$this->consistentResponseTime($startTime);
$this->controller->redirect($this->controller->Link('passwordsent/') . urlencode($data['Login']));
} else {
if (Config::inst()->get('LDAPAuthenticator', 'allow_email_login')==='yes') {
$this->sessionMessage(
_t(
'LDAPLoginForm.ENTERUSERNAMEOREMAIL',
'Please enter your username or your email address to get a password reset link.'
),
'bad'
);
} else {
$this->sessionMessage(
_t(
'LDAPLoginForm.ENTERUSERNAME',
'Please enter your username to get a password reset link.'
),
'bad'
);
}
$this->consistentResponseTime($startTime);
$this->controller->redirect($this->controller->Link('lostpassword'));
}
}
/**
* Ensures response times are the same across all scenarios i.e. email exists or doesn't
* this helps to avoid issues where malicious users could find if an email is legitimate based on response time
*
* @param float $startTime
*/
protected function consistentResponseTime($startTime)
{
if (!Config::inst()->get('LDAPLoginForm', 'consistent_password_times')) {
return;
}
$timeTaken = microtime(true) - $startTime;
if ($timeTaken < self::RESPONSE_TIME) {
$sleepTime = self::RESPONSE_TIME - $timeTaken;
// usleep takes microseconds, so we times our sleep period by 1mil (1mil ms = 1s)
usleep($sleepTime * 1000000);
}
}
}