2009-11-06 03:23:30 +01:00
|
|
|
<?php
|
|
|
|
/**
|
2012-04-12 08:02:46 +02:00
|
|
|
* @package framework
|
2009-11-06 03:23:30 +01:00
|
|
|
* @subpackage tests
|
|
|
|
*/
|
|
|
|
class MemberAuthenticatorTest extends SapphireTest {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-30 02:56:05 +01:00
|
|
|
protected $usesDatabase = true;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-10-06 05:01:33 +02:00
|
|
|
protected $defaultUsername = null;
|
|
|
|
protected $defaultPassword = null;
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->defaultUsername = Security::default_admin_username();
|
|
|
|
$this->defaultPassword = Security::default_admin_password();
|
|
|
|
Security::clear_default_admin();
|
|
|
|
Security::setDefaultAdmin('admin', 'password');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
Security::setDefaultAdmin($this->defaultUsername, $this->defaultPassword);
|
|
|
|
parent::tearDown();
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testLegacyPasswordHashMigrationUponLogin() {
|
2009-11-06 03:23:30 +01:00
|
|
|
$member = new Member();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
$field=Member::config()->unique_identifier_field;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-05-25 06:24:53 +02:00
|
|
|
$member->$field = 'test1@test.com';
|
2009-11-06 03:23:30 +01:00
|
|
|
$member->PasswordEncryption = "sha1";
|
|
|
|
$member->Password = "mypassword";
|
|
|
|
$member->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-06 03:23:30 +01:00
|
|
|
$data = array(
|
2010-04-12 07:01:31 +02:00
|
|
|
'Email' => $member->$field,
|
2009-11-06 03:23:30 +01:00
|
|
|
'Password' => 'mypassword'
|
|
|
|
);
|
|
|
|
MemberAuthenticator::authenticate($data);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-06 03:23:30 +01:00
|
|
|
$member = DataObject::get_by_id('Member', $member->ID);
|
|
|
|
$this->assertEquals($member->PasswordEncryption, "sha1_v2.4");
|
2010-02-23 05:05:34 +01:00
|
|
|
$result = $member->checkPassword('mypassword');
|
|
|
|
$this->assertTrue($result->valid());
|
2009-11-06 03:23:30 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testNoLegacyPasswordHashMigrationOnIncompatibleAlgorithm() {
|
2012-09-26 23:34:00 +02:00
|
|
|
Config::inst()->update('PasswordEncryptor', 'encryptors',
|
|
|
|
array('crc32'=>array('PasswordEncryptor_PHPHash'=>'crc32')));
|
2013-03-21 19:48:54 +01:00
|
|
|
$field=Member::config()->unique_identifier_field;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-06 03:23:30 +01:00
|
|
|
$member = new Member();
|
2010-05-25 06:24:53 +02:00
|
|
|
$member->$field = 'test2@test.com';
|
2009-11-06 03:23:30 +01:00
|
|
|
$member->PasswordEncryption = "crc32";
|
|
|
|
$member->Password = "mypassword";
|
|
|
|
$member->write();
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-06 03:23:30 +01:00
|
|
|
$data = array(
|
2010-04-12 07:01:31 +02:00
|
|
|
'Email' => $member->$field,
|
2009-11-06 03:23:30 +01:00
|
|
|
'Password' => 'mypassword'
|
|
|
|
);
|
|
|
|
MemberAuthenticator::authenticate($data);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2009-11-06 03:23:30 +01:00
|
|
|
$member = DataObject::get_by_id('Member', $member->ID);
|
|
|
|
$this->assertEquals($member->PasswordEncryption, "crc32");
|
2010-02-23 05:05:34 +01:00
|
|
|
$result = $member->checkPassword('mypassword');
|
|
|
|
$this->assertTrue($result->valid());
|
2009-11-06 03:23:30 +01:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2012-09-19 12:07:39 +02:00
|
|
|
public function testCustomIdentifierField(){
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
$origField = Member::config()->unique_identifier_field;
|
|
|
|
Member::config()->unique_identifier_field = 'Username';
|
2010-04-12 07:01:48 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
$label=singleton('Member')->fieldLabel(Member::config()->unique_identifier_field);
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2010-04-12 07:01:31 +02:00
|
|
|
$this->assertEquals($label, 'Username');
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-03-21 19:48:54 +01:00
|
|
|
Member::config()->unique_identifier_field = $origField;
|
2010-04-12 07:01:31 +02:00
|
|
|
}
|
2014-10-06 05:01:33 +02:00
|
|
|
|
|
|
|
public function testGenerateLoginForm() {
|
|
|
|
$controller = new Security();
|
|
|
|
|
|
|
|
// Create basic login form
|
|
|
|
$frontendForm = MemberAuthenticator::get_login_form($controller);
|
|
|
|
$this->assertTrue($frontendForm instanceof MemberLoginForm);
|
|
|
|
|
|
|
|
// Supports cms login form
|
|
|
|
$this->assertTrue(MemberAuthenticator::supports_cms());
|
|
|
|
$cmsForm = MemberAuthenticator::get_cms_login_form($controller);
|
|
|
|
$this->assertTrue($cmsForm instanceof CMSMemberLoginForm);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that a member can be authenticated via their temp id
|
|
|
|
*/
|
|
|
|
public function testAuthenticateByTempID() {
|
|
|
|
$member = new Member();
|
|
|
|
$member->Email = 'test1@test.com';
|
|
|
|
$member->PasswordEncryption = "sha1";
|
|
|
|
$member->Password = "mypassword";
|
|
|
|
$member->write();
|
|
|
|
|
|
|
|
// Make form
|
|
|
|
$controller = new Security();
|
|
|
|
$form = new Form($controller, 'Form', new FieldList(), new FieldList());
|
|
|
|
|
|
|
|
// 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
|
|
|
|
$member->logIn(true);
|
|
|
|
$tempID = $member->TempIDHash;
|
|
|
|
$this->assertNotEmpty($tempID);
|
|
|
|
|
|
|
|
// Test correct login
|
|
|
|
$result = MemberAuthenticator::authenticate(array(
|
|
|
|
'tempid' => $tempID,
|
|
|
|
'Password' => 'mypassword'
|
|
|
|
), $form);
|
|
|
|
$this->assertNotEmpty($result);
|
|
|
|
$this->assertEquals($result->ID, $member->ID);
|
|
|
|
$this->assertEmpty($form->Message());
|
|
|
|
|
|
|
|
// Test incorrect login
|
|
|
|
$form->clearMessage();
|
|
|
|
$result = MemberAuthenticator::authenticate(array(
|
|
|
|
'tempid' => $tempID,
|
|
|
|
'Password' => 'notmypassword'
|
|
|
|
), $form);
|
|
|
|
$this->assertEmpty($result);
|
|
|
|
$this->assertEquals('The provided details don't seem to be correct. Please try again.', $form->Message());
|
|
|
|
$this->assertEquals('bad', $form->MessageType());
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the default admin can be authenticated
|
|
|
|
*/
|
|
|
|
public function testDefaultAdmin() {
|
|
|
|
// Make form
|
|
|
|
$controller = new Security();
|
|
|
|
$form = new Form($controller, 'Form', new FieldList(), new FieldList());
|
|
|
|
|
|
|
|
// Test correct login
|
|
|
|
$result = MemberAuthenticator::authenticate(array(
|
|
|
|
'Email' => 'admin',
|
|
|
|
'Password' => 'password'
|
|
|
|
), $form);
|
|
|
|
$this->assertNotEmpty($result);
|
|
|
|
$this->assertEquals($result->Email, Security::default_admin_username());
|
|
|
|
$this->assertEmpty($form->Message());
|
|
|
|
|
|
|
|
// Test incorrect login
|
|
|
|
$form->clearMessage();
|
|
|
|
$result = MemberAuthenticator::authenticate(array(
|
|
|
|
'Email' => 'admin',
|
|
|
|
'Password' => 'notmypassword'
|
|
|
|
), $form);
|
|
|
|
$this->assertEmpty($result);
|
|
|
|
$this->assertEquals('The provided details don't seem to be correct. Please try again.', $form->Message());
|
|
|
|
$this->assertEquals('bad', $form->MessageType());
|
|
|
|
}
|
2016-04-18 19:35:14 +02:00
|
|
|
|
|
|
|
public function testDefaultAdminLockOut()
|
|
|
|
{
|
|
|
|
Config::inst()->update('Member', 'lock_out_after_incorrect_logins', 1);
|
|
|
|
Config::inst()->update('Member', 'lock_out_delay_mins', 10);
|
|
|
|
SS_Datetime::set_mock_now('2016-04-18 00:00:00');
|
|
|
|
$controller = new Security();
|
|
|
|
$form = new Form($controller, 'Form', new FieldList(), new FieldList());
|
|
|
|
|
|
|
|
// Test correct login
|
|
|
|
MemberAuthenticator::authenticate(array(
|
|
|
|
'Email' => 'admin',
|
|
|
|
'Password' => 'wrongpassword'
|
|
|
|
), $form);
|
|
|
|
|
|
|
|
$this->assertTrue(Member::default_admin()->isLockedOut());
|
2017-05-09 22:24:15 +02:00
|
|
|
$this->assertEquals('2016-04-18 00:10:00', Member::default_admin()->LockedOutUntil);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNonExistantMemberGetsLoginAttemptRecorded()
|
|
|
|
{
|
|
|
|
Config::inst()->update('Member', 'lock_out_after_incorrect_logins', 1);
|
|
|
|
$email = 'notreal@example.com';
|
|
|
|
$this->assertFalse(Member::get()->filter(array('Email' => $email))->exists());
|
|
|
|
$this->assertCount(0, LoginAttempt::get());
|
|
|
|
$response = MemberAuthenticator::authenticate(array(
|
|
|
|
'Email' => $email,
|
|
|
|
'Password' => 'password',
|
|
|
|
));
|
|
|
|
$this->assertNull($response);
|
|
|
|
$this->assertCount(1, LoginAttempt::get());
|
|
|
|
$attempt = LoginAttempt::get()->first();
|
2017-11-30 03:50:36 +01:00
|
|
|
$this->assertEmpty($attempt->Email); // Doesn't store potentially sensitive data
|
|
|
|
$this->assertEquals(sha1($email), $attempt->EmailHashed);
|
2017-05-09 22:24:15 +02:00
|
|
|
$this->assertEquals('Failure', $attempt->Status);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNonExistantMemberGetsLockedOut()
|
|
|
|
{
|
|
|
|
Config::inst()->update('Member', 'lock_out_after_incorrect_logins', 1);
|
|
|
|
Config::inst()->update('Member', 'lock_out_delay_mins', 10);
|
|
|
|
$email = 'notreal@example.com';
|
|
|
|
|
|
|
|
$this->assertFalse(Member::get()->filter(array('Email' => $email))->exists());
|
|
|
|
|
|
|
|
$response = MemberAuthenticator::authenticate(array(
|
|
|
|
'Email' => $email,
|
|
|
|
'Password' => 'password'
|
|
|
|
));
|
|
|
|
|
|
|
|
$this->assertNull($response);
|
|
|
|
$member = new Member();
|
|
|
|
$member->Email = $email;
|
|
|
|
|
|
|
|
$this->assertTrue($member->isLockedOut());
|
|
|
|
$this->assertFalse($member->canLogIn()->valid());
|
2016-04-18 19:35:14 +02:00
|
|
|
}
|
2017-09-12 16:57:03 +02:00
|
|
|
|
|
|
|
public function testDefaultAuthenticatorWontReturnIfDisabled()
|
|
|
|
{
|
|
|
|
Authenticator::unregister('MemberAuthenticator');
|
|
|
|
$this->assertNotEquals('MemberAuthenticator', Authenticator::get_default_authenticator());
|
|
|
|
}
|
2012-03-24 04:04:52 +01:00
|
|
|
}
|