2009-11-06 03:23:30 +01:00
|
|
|
<?php
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Security\Tests;
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
2017-08-14 17:50:17 +02:00
|
|
|
use SilverStripe\Control\NullHTTPRequest;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
2017-05-07 21:11:00 +02:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
2017-06-15 04:20:12 +02:00
|
|
|
use SilverStripe\ORM\ValidationResult;
|
2017-05-30 09:42:00 +02:00
|
|
|
use SilverStripe\Security\Authenticator;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Security\DefaultAdminService;
|
|
|
|
use SilverStripe\Security\IdentityStore;
|
2017-05-09 22:24:15 +02:00
|
|
|
use SilverStripe\Security\LoginAttempt;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Security\Member;
|
2017-05-30 09:42:00 +02:00
|
|
|
use SilverStripe\Security\MemberAuthenticator\CMSMemberAuthenticator;
|
|
|
|
use SilverStripe\Security\MemberAuthenticator\CMSMemberLoginForm;
|
|
|
|
use SilverStripe\Security\MemberAuthenticator\MemberAuthenticator;
|
|
|
|
use SilverStripe\Security\MemberAuthenticator\MemberLoginForm;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Security\Security;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2017-07-03 02:21:27 +02:00
|
|
|
/**
|
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
class MemberAuthenticatorTest extends SapphireTest
|
|
|
|
{
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
protected $usesDatabase = true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
protected $defaultUsername = null;
|
|
|
|
protected $defaultPassword = null;
|
2014-10-06 05:01:33 +02:00
|
|
|
|
2017-03-24 04:00:54 +01:00
|
|
|
protected function setUp()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2014-10-06 05:01:33 +02:00
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
if (DefaultAdminService::hasDefaultAdmin()) {
|
|
|
|
$this->defaultUsername = DefaultAdminService::getDefaultAdminUsername();
|
|
|
|
$this->defaultPassword = DefaultAdminService::getDefaultAdminPassword();
|
|
|
|
DefaultAdminService::clearDefaultAdmin();
|
|
|
|
} else {
|
|
|
|
$this->defaultUsername = null;
|
|
|
|
$this->defaultPassword = null;
|
|
|
|
}
|
2017-06-13 11:04:43 +02:00
|
|
|
DefaultAdminService::setDefaultAdmin('admin', 'password');
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2014-10-06 05:01:33 +02:00
|
|
|
|
2017-03-24 04:00:54 +01:00
|
|
|
protected function tearDown()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2017-06-13 11:04:43 +02:00
|
|
|
DefaultAdminService::clearDefaultAdmin();
|
2017-06-15 04:20:12 +02:00
|
|
|
if ($this->defaultUsername) {
|
|
|
|
DefaultAdminService::setDefaultAdmin($this->defaultUsername, $this->defaultPassword);
|
|
|
|
}
|
2016-12-16 05:34:21 +01:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testCustomIdentifierField()
|
|
|
|
{
|
2017-06-15 04:20:12 +02:00
|
|
|
Member::config()->set('unique_identifier_field', 'Username');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
$label = Member::singleton()
|
|
|
|
->fieldLabel(Member::config()->get('unique_identifier_field'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertEquals($label, 'Username');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGenerateLoginForm()
|
|
|
|
{
|
2017-05-30 09:42:00 +02:00
|
|
|
$authenticator = new MemberAuthenticator();
|
2017-04-23 05:30:33 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$controller = new Security();
|
|
|
|
|
|
|
|
// Create basic login form
|
2017-04-23 05:30:33 +02:00
|
|
|
$frontendResponse = $authenticator
|
|
|
|
->getLoginHandler($controller->link())
|
2017-06-22 12:50:45 +02:00
|
|
|
->handleRequest(Controller::curr()->getRequest());
|
2017-04-23 05:30:33 +02:00
|
|
|
|
|
|
|
$this->assertTrue(is_array($frontendResponse));
|
|
|
|
$this->assertTrue(isset($frontendResponse['Form']));
|
2017-05-30 09:42:00 +02:00
|
|
|
$this->assertTrue($frontendResponse['Form'] instanceof MemberLoginForm);
|
2017-04-23 05:30:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGenerateCMSLoginForm()
|
|
|
|
{
|
2017-05-30 09:42:00 +02:00
|
|
|
/** @var CMSMemberAuthenticator $authenticator */
|
|
|
|
$authenticator = new CMSMemberAuthenticator();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Supports cms login form
|
2017-05-30 09:42:00 +02:00
|
|
|
$this->assertGreaterThan(0, ($authenticator->supportedServices() & Authenticator::CMS_LOGIN));
|
|
|
|
$cmsHandler = $authenticator->getLoginHandler('/');
|
|
|
|
$cmsForm = $cmsHandler->loginForm();
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue($cmsForm instanceof CMSMemberLoginForm);
|
|
|
|
}
|
2017-04-23 05:30:33 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a member can be authenticated via their temp id
|
|
|
|
*/
|
|
|
|
public function testAuthenticateByTempID()
|
|
|
|
{
|
2017-05-30 09:42:00 +02:00
|
|
|
$authenticator = new CMSMemberAuthenticator();
|
2017-04-23 05:30:33 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$member = new Member();
|
|
|
|
$member->Email = 'test1@test.com';
|
|
|
|
$member->PasswordEncryption = "sha1";
|
|
|
|
$member->Password = "mypassword";
|
|
|
|
$member->write();
|
|
|
|
|
|
|
|
// If the user has never logged in, then the tempid should be empty
|
|
|
|
$tempID = $member->TempIDHash;
|
|
|
|
$this->assertEmpty($tempID);
|
|
|
|
|
|
|
|
// If the user logs in then they have a temp id
|
2017-05-30 09:42:00 +02:00
|
|
|
Injector::inst()->get(IdentityStore::class)->logIn($member, true);
|
2016-12-16 05:34:21 +01:00
|
|
|
$tempID = $member->TempIDHash;
|
|
|
|
$this->assertNotEmpty($tempID);
|
|
|
|
|
|
|
|
// Test correct login
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var ValidationResult $message */
|
2017-04-23 05:30:33 +02:00
|
|
|
$result = $authenticator->authenticate(
|
2017-06-22 12:50:45 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'tempid' => $tempID,
|
|
|
|
'Password' => 'mypassword'
|
2017-06-22 12:50:45 +02:00
|
|
|
],
|
|
|
|
Controller::curr()->getRequest(),
|
2017-04-23 05:30:33 +02:00
|
|
|
$message
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2017-04-23 05:30:33 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertNotEmpty($result);
|
|
|
|
$this->assertEquals($result->ID, $member->ID);
|
2017-05-30 09:42:00 +02:00
|
|
|
$this->assertTrue($message->isValid());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Test incorrect login
|
2017-04-23 05:30:33 +02:00
|
|
|
$result = $authenticator->authenticate(
|
2017-06-22 12:50:45 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'tempid' => $tempID,
|
|
|
|
'Password' => 'notmypassword'
|
2017-06-22 12:50:45 +02:00
|
|
|
],
|
|
|
|
Controller::curr()->getRequest(),
|
2017-04-23 05:30:33 +02:00
|
|
|
$message
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2017-04-23 05:30:33 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEmpty($result);
|
2017-05-30 09:42:00 +02:00
|
|
|
$messages = $message->getMessages();
|
2017-01-25 04:35:13 +01:00
|
|
|
$this->assertEquals(
|
2017-04-20 03:15:24 +02:00
|
|
|
_t('SilverStripe\\Security\\Member.ERRORWRONGCRED', 'The provided details don\'t seem to be correct. Please try again.'),
|
2017-05-30 09:42:00 +02:00
|
|
|
$messages[0]['message']
|
2017-01-25 04:35:13 +01:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
2017-08-14 17:50:17 +02:00
|
|
|
public function testExpiredTempID()
|
|
|
|
{
|
|
|
|
DefaultAdminService::clearDefaultAdmin();
|
|
|
|
|
|
|
|
$authenticator = new CMSMemberAuthenticator();
|
|
|
|
|
|
|
|
// Make member with expired TempID
|
|
|
|
$member = new Member();
|
|
|
|
$member->Email = 'test1@test.com';
|
|
|
|
$member->PasswordEncryption = "sha1";
|
|
|
|
$member->Password = "mypassword";
|
|
|
|
$member->TempIDExpired = '2016-05-22 00:00:00';
|
|
|
|
$member->write();
|
|
|
|
Injector::inst()->get(IdentityStore::class)->logIn($member, true);
|
|
|
|
|
|
|
|
$tempID = $member->TempIDHash;
|
|
|
|
|
|
|
|
DBDatetime::set_mock_now('2016-05-29 00:00:00');
|
|
|
|
|
|
|
|
$this->assertNotEmpty($tempID);
|
|
|
|
$this->assertFalse(DefaultAdminService::hasDefaultAdmin());
|
|
|
|
|
|
|
|
$result = $authenticator->authenticate(array(
|
|
|
|
'tempid' => $tempID,
|
|
|
|
'Password' => 'notmypassword'
|
|
|
|
), Controller::curr()->getRequest(), $validationResult);
|
|
|
|
|
|
|
|
$this->assertNull($result);
|
|
|
|
$this->assertFalse($validationResult->isValid());
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* Test that the default admin can be authenticated
|
|
|
|
*/
|
|
|
|
public function testDefaultAdmin()
|
|
|
|
{
|
2017-05-30 09:42:00 +02:00
|
|
|
$authenticator = new MemberAuthenticator();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Test correct login
|
2017-06-15 04:20:12 +02:00
|
|
|
/** @var ValidationResult $message */
|
2017-04-23 05:30:33 +02:00
|
|
|
$result = $authenticator->authenticate(
|
2017-06-22 12:50:45 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Email' => 'admin',
|
|
|
|
'Password' => 'password'
|
2017-06-22 12:50:45 +02:00
|
|
|
],
|
|
|
|
Controller::curr()->getRequest(),
|
2017-04-23 05:30:33 +02:00
|
|
|
$message
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertNotEmpty($result);
|
2017-06-13 11:04:43 +02:00
|
|
|
$this->assertEquals($result->Email, DefaultAdminService::getDefaultAdminUsername());
|
2017-05-30 09:42:00 +02:00
|
|
|
$this->assertTrue($message->isValid());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Test incorrect login
|
2017-04-23 05:30:33 +02:00
|
|
|
$result = $authenticator->authenticate(
|
2017-06-22 12:50:45 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Email' => 'admin',
|
|
|
|
'Password' => 'notmypassword'
|
2017-06-22 12:50:45 +02:00
|
|
|
],
|
|
|
|
Controller::curr()->getRequest(),
|
2017-04-23 05:30:33 +02:00
|
|
|
$message
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2017-05-30 09:42:00 +02:00
|
|
|
$messages = $message->getMessages();
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEmpty($result);
|
|
|
|
$this->assertEquals(
|
|
|
|
'The provided details don\'t seem to be correct. Please try again.',
|
2017-05-30 09:42:00 +02:00
|
|
|
$messages[0]['message']
|
2016-11-23 06:09:10 +01:00
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testDefaultAdminLockOut()
|
|
|
|
{
|
2017-05-30 09:42:00 +02:00
|
|
|
$authenticator = new MemberAuthenticator();
|
2017-04-23 05:30:33 +02:00
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
Config::modify()->set(Member::class, 'lock_out_after_incorrect_logins', 1);
|
|
|
|
Config::modify()->set(Member::class, 'lock_out_delay_mins', 10);
|
2016-12-16 05:34:21 +01:00
|
|
|
DBDatetime::set_mock_now('2016-04-18 00:00:00');
|
|
|
|
|
|
|
|
// Test correct login
|
2017-04-23 05:30:33 +02:00
|
|
|
$authenticator->authenticate(
|
2017-01-26 05:20:08 +01:00
|
|
|
[
|
|
|
|
'Email' => 'admin',
|
|
|
|
'Password' => 'wrongpassword'
|
2017-06-22 12:50:45 +02:00
|
|
|
],
|
|
|
|
Controller::curr()->getRequest()
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
2017-06-15 04:20:12 +02:00
|
|
|
$defaultAdmin = DefaultAdminService::singleton()->findOrCreateDefaultAdmin();
|
|
|
|
$this->assertNotNull($defaultAdmin);
|
|
|
|
$this->assertFalse($defaultAdmin->canLogin());
|
|
|
|
$this->assertEquals('2016-04-18 00:10:00', $defaultAdmin->LockedOutUntil);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2017-05-09 22:24:15 +02:00
|
|
|
|
|
|
|
public function testNonExistantMemberGetsLoginAttemptRecorded()
|
|
|
|
{
|
|
|
|
Security::config()->set('login_recording', true);
|
|
|
|
Member::config()
|
|
|
|
->set('lock_out_after_incorrect_logins', 1)
|
|
|
|
->set('lock_out_delay_mins', 10);
|
|
|
|
|
|
|
|
$email = 'notreal@example.com';
|
|
|
|
$this->assertFalse(Member::get()->filter(array('Email' => $email))->exists());
|
|
|
|
$this->assertCount(0, LoginAttempt::get());
|
|
|
|
$authenticator = new MemberAuthenticator();
|
|
|
|
$result = new ValidationResult();
|
|
|
|
$member = $authenticator->authenticate(
|
|
|
|
[
|
|
|
|
'Email' => $email,
|
|
|
|
'Password' => 'password',
|
|
|
|
],
|
|
|
|
Controller::curr()->getRequest(),
|
|
|
|
$result
|
|
|
|
);
|
|
|
|
$this->assertFalse($result->isValid());
|
|
|
|
$this->assertNull($member);
|
|
|
|
$this->assertCount(1, LoginAttempt::get());
|
|
|
|
$attempt = LoginAttempt::get()->first();
|
|
|
|
$this->assertEquals($email, $attempt->Email);
|
|
|
|
$this->assertEquals(LoginAttempt::FAILURE, $attempt->Status);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNonExistantMemberGetsLockedOut()
|
|
|
|
{
|
|
|
|
Security::config()->set('login_recording', true);
|
|
|
|
Member::config()
|
|
|
|
->set('lock_out_after_incorrect_logins', 1)
|
|
|
|
->set('lock_out_delay_mins', 10);
|
|
|
|
|
|
|
|
$email = 'notreal@example.com';
|
|
|
|
$this->assertFalse(Member::get()->filter(array('Email' => $email))->exists());
|
|
|
|
|
|
|
|
$authenticator = new MemberAuthenticator();
|
|
|
|
$result = new ValidationResult();
|
|
|
|
$member = $authenticator->authenticate(
|
|
|
|
[
|
|
|
|
'Email' => $email,
|
|
|
|
'Password' => 'password',
|
|
|
|
],
|
|
|
|
Controller::curr()->getRequest(),
|
|
|
|
$result
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertNull($member);
|
|
|
|
$this->assertFalse($result->isValid());
|
|
|
|
$member = new Member();
|
|
|
|
$member->Email = $email;
|
|
|
|
|
|
|
|
$this->assertTrue($member->isLockedOut());
|
|
|
|
$this->assertFalse($member->canLogIn());
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|