1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
use Mockery\MockInterface; |
4
|
|
|
|
5
|
|
|
class LDAPAuthenticatorTest extends SapphireTest |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var MockInterface |
9
|
|
|
*/ |
10
|
|
|
protected $service; |
11
|
|
|
|
12
|
|
|
protected $usesDatabase = true; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* For mocking fallback authenticator return value. |
16
|
|
|
* |
17
|
|
|
* @var Member |
18
|
|
|
*/ |
19
|
|
|
private static $fallback_auth_value; |
20
|
|
|
|
21
|
|
|
public function setUp() |
22
|
|
|
{ |
23
|
|
|
parent::setUp(); |
24
|
|
|
|
25
|
|
|
$this->service = Mockery::mock(LDAPService::class); |
26
|
|
|
$this->service->shouldReceive('setGateway'); |
27
|
|
|
Injector::inst()->registerService($this->service, 'LDAPService'); |
|
|
|
|
28
|
|
|
|
29
|
|
|
Config::inst()->nest(); |
30
|
|
|
Config::inst()->remove('Member', 'extensions'); |
31
|
|
|
Config::inst()->remove('Group', 'extensions'); |
32
|
|
|
Config::inst()->update('Member', 'update_ldap_from_local', false); |
33
|
|
|
Config::inst()->update('Member', 'create_users_in_ldap', false); |
34
|
|
|
Config::inst()->update('Group', 'extensions', ['LDAPGroupExtension']); |
35
|
|
|
Config::inst()->update('Member', 'extensions', ['LDAPMemberExtension']); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function tearDown() |
39
|
|
|
{ |
40
|
|
|
self::$fallback_auth_value = null; |
41
|
|
|
parent::tearDown(); |
42
|
|
|
|
43
|
|
|
Config::inst()->unnest(); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function testLoginSuccess() |
47
|
|
|
{ |
48
|
|
|
Session::set('BackURL', '/redirect-here-after-login'); |
49
|
|
|
$data = ['Login' => 'denvercoder9', 'Password' => 'Correct Horse Battery Staple']; |
50
|
|
|
$form = \Form::create(Controller::create(), 'name', new FieldList(), new FieldList()); |
51
|
|
|
$this->service->shouldReceive('authenticate')->andReturn([ |
52
|
|
|
'success' => true, |
53
|
|
|
'identity' => 'denvercoder9', |
54
|
|
|
]); |
55
|
|
|
$this->service->shouldReceive('getUserByUsername')->andReturn(['objectguid' => '123456']); |
56
|
|
|
$this->service->shouldReceive('updateMemberFromLDAP')->andReturn(true); |
57
|
|
|
|
58
|
|
|
$res = LDAPAuthenticator::authenticate($data, $form); |
59
|
|
|
|
60
|
|
|
$this->assertTrue($res instanceof Member); |
|
|
|
|
61
|
|
|
$this->assertNull(Session::get('BackURL')); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testLoginWithEmailSuccess() |
65
|
|
|
{ |
66
|
|
|
Config::inst()->update('LDAPAuthenticator', 'allow_email_login', 'yes'); |
67
|
|
|
|
68
|
|
|
$data = ['Login' => '[email protected]', 'Password' => 'Correct Horse Battery Staple']; |
69
|
|
|
$form = \Form::create(Controller::create(), 'name', new FieldList(), new FieldList()); |
70
|
|
|
|
71
|
|
|
$this->service->shouldReceive('getUsernameByEmail')->andReturn('denvercoder9'); |
72
|
|
|
|
73
|
|
|
$this->service->shouldReceive('authenticate')->andReturn([ |
74
|
|
|
'success' => true, |
75
|
|
|
'identity' => 'denvercoder9', |
76
|
|
|
]); |
77
|
|
|
$this->service->shouldReceive('getUserByUsername')->andReturn(['objectguid' => '123456']); |
78
|
|
|
$this->service->shouldReceive('updateMemberFromLDAP')->andReturn(true); |
79
|
|
|
|
80
|
|
|
$res = LDAPAuthenticator::authenticate($data, $form); |
81
|
|
|
|
82
|
|
|
$this->assertTrue($res instanceof Member); |
|
|
|
|
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
public function testUsernameOnly() |
86
|
|
|
{ |
87
|
|
|
Config::inst()->update('LDAPAuthenticator', 'allow_email_login', 'no'); |
88
|
|
|
$data = ['Login' => '[email protected]', 'Password' => 'Correct Horse Battery Staple']; |
89
|
|
|
$form = \Form::create(Controller::create(), 'name', new FieldList(), new FieldList()); |
90
|
|
|
$this->service->shouldReceive('getUsernameByEmail')->andReturn('denvercoder9'); |
91
|
|
|
|
92
|
|
|
LDAPAuthenticator::authenticate($data, $form); |
93
|
|
|
|
94
|
|
|
$this->assertEquals('Please enter your username instead of your email to log in.', $form->Message()); |
|
|
|
|
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function testEmailNoUsernameNoFallbackAuth() |
98
|
|
|
{ |
99
|
|
|
Session::set('BackURL', '/redirect-here-after-login'); |
100
|
|
|
Config::inst()->update('LDAPAuthenticator', 'allow_email_login', 'yes'); |
101
|
|
|
Config::inst()->update('LDAPAuthenticator', 'fallback_authenticator', 'no'); |
102
|
|
|
$data = ['Login' => '[email protected]', 'Password' => 'Correct Horse Battery Staple']; |
103
|
|
|
$form = \Form::create(Controller::create(), 'name', new FieldList(), new FieldList()); |
104
|
|
|
$this->service->shouldReceive('getUsernameByEmail')->andReturn(false); |
105
|
|
|
|
106
|
|
|
LDAPAuthenticator::authenticate($data, $form); |
107
|
|
|
|
108
|
|
|
$this->assertEquals('Invalid credentials', $form->Message()); |
|
|
|
|
109
|
|
|
$this->assertEquals('/redirect-here-after-login', Session::get('BackURL')); |
|
|
|
|
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function testEmailLoginToFallbackAuthFailed() |
113
|
|
|
{ |
114
|
|
|
Config::inst()->update('LDAPAuthenticator', 'allow_email_login', 'yes'); |
115
|
|
|
Config::inst()->update('LDAPAuthenticator', 'fallback_authenticator', 'yes'); |
116
|
|
|
$data = ['Login' => '[email protected]', 'Password' => 'Correct Horse Battery Staple']; |
117
|
|
|
$form = \Form::create(Controller::create(), 'name', new FieldList(), new FieldList()); |
118
|
|
|
$this->service->shouldReceive('getUsernameByEmail')->andReturn(false); |
119
|
|
|
|
120
|
|
|
LDAPAuthenticator::authenticate($data, $form); |
121
|
|
|
|
122
|
|
|
$this->assertEquals('Invalid credentials', $form->Message()); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
public function testEmailLoginToFallbackAuthSuccess() |
126
|
|
|
{ |
127
|
|
|
// prevent LDAPMemberExtension from messing with Member:write() |
128
|
|
|
Config::inst()->update('Member', 'extensions', null); |
129
|
|
|
Config::inst()->update('LDAPAuthenticator', 'allow_email_login', 'yes'); |
130
|
|
|
Config::inst()->update('LDAPAuthenticator', 'fallback_authenticator', 'yes'); |
131
|
|
|
$user = Member::create(['Email' => '[email protected]']); |
132
|
|
|
$user->changePassword('Correct Horse Battery Staple'); |
133
|
|
|
$data = ['Login' => $user->Email, 'Password' => 'Correct Horse Battery Staple']; |
134
|
|
|
$form = \Form::create(Controller::create(), 'name', new FieldList(), new FieldList()); |
135
|
|
|
$this->service->shouldReceive('getUsernameByEmail')->andReturn(false); |
136
|
|
|
|
137
|
|
|
$res = LDAPAuthenticator::authenticate($data, $form); |
138
|
|
|
|
139
|
|
|
$this->assertTrue(0 != $res->ID); |
|
|
|
|
140
|
|
|
$this->assertEquals($res->ID, $user->ID); |
|
|
|
|
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function testUsernameLoginToFallbackAuthSuccess() |
144
|
|
|
{ |
145
|
|
|
// prevent LDAPMemberExtension from messing with Member:write() |
146
|
|
|
Config::inst()->update('LDAPAuthenticator', 'allow_email_login', 'yes'); |
147
|
|
|
Config::inst()->update('LDAPAuthenticator', 'fallback_authenticator', 'yes'); |
148
|
|
|
$user = Member::create(['Email' => '[email protected]']); |
149
|
|
|
Config::inst()->update('LDAPAuthenticator', 'fallback_authenticator_class', self::class); |
150
|
|
|
self::set_fallback_authenticator_value($user); |
151
|
|
|
$data = ['Login' => 'denvercoder', 'Password' => 'Correct Horse Battery Staple']; |
152
|
|
|
$form = \Form::create(Controller::create(), 'name', new FieldList(), new FieldList()); |
153
|
|
|
$this->service->shouldReceive('authenticate')->andReturn(['success' => false]); |
154
|
|
|
|
155
|
|
|
$res = LDAPAuthenticator::authenticate($data, $form); |
156
|
|
|
|
157
|
|
|
$this->assertEquals($res->Email, $user->Email); |
|
|
|
|
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
public function testUsernameLoginToFallbackAuthFailure() |
161
|
|
|
{ |
162
|
|
|
Config::inst()->update('LDAPAuthenticator', 'allow_email_login', 'yes'); |
163
|
|
|
Config::inst()->update('LDAPAuthenticator', 'fallback_authenticator', 'yes'); |
164
|
|
|
Config::inst()->update('LDAPAuthenticator', 'fallback_authenticator_class', self::class); |
165
|
|
|
self::set_fallback_authenticator_value(false); |
166
|
|
|
$data = ['Login' => 'denvercoder', 'Password' => 'Correct Horse Battery Staple']; |
167
|
|
|
$form = \Form::create(Controller::create(), 'name', new FieldList(), new FieldList()); |
168
|
|
|
$this->service->shouldReceive('authenticate')->andReturn(['success' => false, 'message' => 'oh noe']); |
169
|
|
|
$res = LDAPAuthenticator::authenticate($data, $form); |
170
|
|
|
$this->assertNull($res); |
|
|
|
|
171
|
|
|
$this->assertEquals('oh noe', $form->Message()); |
|
|
|
|
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
public function testCannotFetchUserData() |
175
|
|
|
{ |
176
|
|
|
$data = ['Login' => 'denvercoder9', 'Password' => 'Correct Horse Battery Staple']; |
177
|
|
|
$form = \Form::create(Controller::create(), 'name', new FieldList(), new FieldList()); |
178
|
|
|
$this->service->shouldReceive('authenticate')->andReturn([ |
179
|
|
|
'success' => true, |
180
|
|
|
'identity' => 'denvercoder9', |
181
|
|
|
]); |
182
|
|
|
$this->service->shouldReceive('getUserByUsername')->andReturn([]); |
183
|
|
|
|
184
|
|
|
$res = LDAPAuthenticator::authenticate($data, $form); |
185
|
|
|
$this->assertNull($res); |
|
|
|
|
186
|
|
|
$this->assertEquals('There was a problem retrieving your user data', $form->Message()); |
|
|
|
|
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
// self shunting by setting Config::inst()->update('LDAPAuthenticator', 'fallback_authenticator_class', 'LDAPAuthenticatorTest'); |
190
|
|
|
public static function authenticate($data, Form $form = null) |
|
|
|
|
191
|
|
|
{ |
192
|
|
|
return self::$fallback_auth_value; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
private function set_fallback_authenticator_value($user) |
196
|
|
|
{ |
197
|
|
|
self::$fallback_auth_value = $user; |
198
|
|
|
|
199
|
|
|
} |
200
|
|
|
} |
201
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: