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-05-07 21:11:00 +02:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2017-05-30 09:42:00 +02:00
|
|
|
use SilverStripe\ORM\DataModel;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
2017-05-30 09:42:00 +02:00
|
|
|
use SilverStripe\Security\Authenticator;
|
|
|
|
use SilverStripe\Security\MemberAuthenticator\CMSMemberAuthenticator;
|
|
|
|
use SilverStripe\Security\MemberAuthenticator\CMSMemberLoginForm;
|
|
|
|
use SilverStripe\Security\MemberAuthenticator\MemberAuthenticator;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\Security;
|
|
|
|
use SilverStripe\Security\Member;
|
2017-05-30 09:42:00 +02:00
|
|
|
use SilverStripe\Security\MemberAuthenticator\MemberLoginForm;
|
2017-05-07 21:11:00 +02:00
|
|
|
use SilverStripe\Security\IdentityStore;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2017-04-23 05:30:33 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
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
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->defaultUsername = Security::default_admin_username();
|
|
|
|
$this->defaultPassword = Security::default_admin_password();
|
|
|
|
Security::clear_default_admin();
|
|
|
|
Security::setDefaultAdmin('admin', 'password');
|
|
|
|
}
|
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
|
|
|
{
|
|
|
|
Security::setDefaultAdmin($this->defaultUsername, $this->defaultPassword);
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testCustomIdentifierField()
|
|
|
|
{
|
|
|
|
|
|
|
|
$origField = Member::config()->unique_identifier_field;
|
|
|
|
Member::config()->unique_identifier_field = 'Username';
|
|
|
|
|
|
|
|
$label=singleton(Member::class)->fieldLabel(Member::config()->unique_identifier_field);
|
|
|
|
|
|
|
|
$this->assertEquals($label, 'Username');
|
|
|
|
|
|
|
|
Member::config()->unique_identifier_field = $origField;
|
|
|
|
}
|
|
|
|
|
|
|
|
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-05-30 09:42:00 +02:00
|
|
|
->handleRequest(new HTTPRequest('get', '/'), DataModel::inst());
|
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-04-23 05:30:33 +02:00
|
|
|
$result = $authenticator->authenticate(
|
2016-12-16 05:34:21 +01:00
|
|
|
array(
|
|
|
|
'tempid' => $tempID,
|
|
|
|
'Password' => 'mypassword'
|
|
|
|
),
|
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(
|
2016-12-16 05:34:21 +01:00
|
|
|
array(
|
|
|
|
'tempid' => $tempID,
|
|
|
|
'Password' => 'notmypassword'
|
|
|
|
),
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-04-23 05:30:33 +02:00
|
|
|
$result = $authenticator->authenticate(
|
2016-12-16 05:34:21 +01:00
|
|
|
array(
|
|
|
|
'Email' => 'admin',
|
|
|
|
'Password' => 'password'
|
|
|
|
),
|
2017-04-23 05:30:33 +02:00
|
|
|
$message
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertNotEmpty($result);
|
|
|
|
$this->assertEquals($result->Email, Security::default_admin_username());
|
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(
|
2016-12-16 05:34:21 +01:00
|
|
|
array(
|
|
|
|
'Email' => 'admin',
|
|
|
|
'Password' => 'notmypassword'
|
|
|
|
),
|
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
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
Config::inst()->update(Member::class, 'lock_out_after_incorrect_logins', 1);
|
|
|
|
Config::inst()->update(Member::class, 'lock_out_delay_mins', 10);
|
|
|
|
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-05-30 09:42:00 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
2017-04-30 05:17:26 +02:00
|
|
|
$this->assertFalse(Member::default_admin()->canLogin()->isValid());
|
2017-01-26 05:20:08 +01:00
|
|
|
$this->assertEquals('2016-04-18 00:10:00', Member::default_admin()->LockedOutUntil);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|