1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SilverStripe\LDAP\Tests\Authenticators; |
4
|
|
|
|
5
|
|
|
use SilverStripe\Control\HTTPRequest; |
6
|
|
|
use SilverStripe\Control\Session; |
7
|
|
|
use SilverStripe\Core\Config\Config; |
8
|
|
|
use SilverStripe\Core\Injector\Injector; |
9
|
|
|
use SilverStripe\Dev\SapphireTest; |
10
|
|
|
use SilverStripe\LDAP\Authenticators\LDAPAuthenticator; |
11
|
|
|
use SilverStripe\LDAP\Model\LDAPGateway; |
12
|
|
|
use SilverStripe\LDAP\Services\LDAPService; |
13
|
|
|
use SilverStripe\LDAP\Tests\Model\LDAPFakeGateway; |
14
|
|
|
use SilverStripe\ORM\ValidationResult; |
15
|
|
|
use SilverStripe\Security\Member; |
16
|
|
|
|
17
|
|
|
class LDAPAuthenticatorTest extends SapphireTest |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var LDAPAuthenticator |
21
|
|
|
*/ |
22
|
|
|
protected $authenticator; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var HTTPRequest |
26
|
|
|
*/ |
27
|
|
|
private $request; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var ValidationResult |
31
|
|
|
*/ |
32
|
|
|
private $result; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var array |
36
|
|
|
*/ |
37
|
|
|
private $data; |
38
|
|
|
|
39
|
|
|
protected static $fixture_file = 'LDAPAuthenticatorTest.yml'; |
40
|
|
|
|
41
|
|
|
protected function setUp() |
42
|
|
|
{ |
43
|
|
|
parent::setUp(); |
44
|
|
|
|
45
|
|
|
$gateway = new LDAPFakeGateway(); |
46
|
|
|
Injector::inst()->registerService($gateway, LDAPGateway::class); |
47
|
|
|
|
48
|
|
|
$service = Injector::inst()->get(LDAPService::class); |
49
|
|
|
$service->setGateway($gateway); |
50
|
|
|
|
51
|
|
|
$this->service = $service; |
|
|
|
|
52
|
|
|
|
53
|
|
|
$this->authenticator = Injector::inst()->create(LDAPAuthenticator::class); |
54
|
|
|
|
55
|
|
|
Config::modify()->set(LDAPAuthenticator::class, 'allow_email_login', 'yes'); |
56
|
|
|
|
57
|
|
|
$this->request = new HTTPRequest('get', '/'); |
58
|
|
|
$this->request->setSession(new Session([])); |
59
|
|
|
$this->result = new ValidationResult(); |
60
|
|
|
$this->data = [ |
61
|
|
|
'Login' => null, |
62
|
|
|
'Password' => null |
63
|
|
|
]; |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
public function testDisallowedEmailLogin() |
67
|
|
|
{ |
68
|
|
|
Config::modify()->set(LDAPAuthenticator::class, 'allow_email_login', 'no'); |
69
|
|
|
$this->data['Login'] = '[email protected]'; |
70
|
|
|
$this->data['Password'] = 'test'; |
71
|
|
|
$this->callAuthMethod(); |
72
|
|
|
$this->assertFalse($this->result->isValid()); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Tests whether a validator error results if User not found at gateway and no fallback member found |
77
|
|
|
*/ |
78
|
|
|
public function testEmailNotFoundAtGateWay() |
79
|
|
|
{ |
80
|
|
|
$invalidGatewayAndLocalEmail = '[email protected]'; |
81
|
|
|
$this->data = ['Login' => $invalidGatewayAndLocalEmail, 'Password' => 'test']; |
82
|
|
|
$this->callAuthMethod(); |
83
|
|
|
$this->assertFalse($this->result->isValid()); |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Tests whether fallback authenticator returns a member if enabled |
88
|
|
|
*/ |
89
|
|
|
public function testFallbackAuthenticator() |
90
|
|
|
{ |
91
|
|
|
Config::modify()->set(LDAPAuthenticator::class, 'fallback_authenticator', 'yes'); |
92
|
|
|
$member = $this->objFromFixture(Member::class, 'dbOnlyMember'); |
93
|
|
|
$this->data = ['Login' => $member->Email, 'Email' => $member->Email, 'Password' => 'password']; |
94
|
|
|
$result = $this->callAuthMethod(); |
95
|
|
|
$this->assertInstanceOf(Member::class, $result); |
96
|
|
|
$this->assertEquals($member->Email, $result->Email); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* Tests for Invalid Credentials upon LDAP authentication failure |
101
|
|
|
*/ |
102
|
|
|
public function testLDAPAuthenticationFailure() |
103
|
|
|
{ |
104
|
|
|
$this->data = ['Login' => 'usernotfound', 'Password' => 'passwordnotfound']; |
105
|
|
|
$this->callAuthMethod(); |
106
|
|
|
$this->assertFalse($this->result->isValid()); |
107
|
|
|
$this->assertContains('Username not found', $this->result->getMessages()[0]['message']); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Tests whether a new member is created in SS if it was found in LDAP but doesn't |
112
|
|
|
* exist in SS |
113
|
|
|
*/ |
114
|
|
|
public function testAuthenticateCreatesNewMemberIfNotFound() |
115
|
|
|
{ |
116
|
|
|
$this->data = ['Login' => '[email protected]', 'Password' => 'mockPassword']; |
117
|
|
|
$member = $this->callAuthMethod(); |
118
|
|
|
$this->assertTrue($this->result->isValid()); |
119
|
|
|
$this->assertInstanceOf(Member::class, $member); |
120
|
|
|
$this->assertEquals(123, $member->GUID); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
private function callAuthMethod() |
124
|
|
|
{ |
125
|
|
|
$result = $this->authenticator->authenticate( |
126
|
|
|
$this->data, |
127
|
|
|
$this->request, |
128
|
|
|
$this->result |
129
|
|
|
); |
130
|
|
|
|
131
|
|
|
return $result; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
|