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