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