2008-04-26 08:31:52 +02:00
|
|
|
<?php
|
2015-08-30 07:02:55 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Security\Tests;
|
2016-06-15 06:03:16 +02:00
|
|
|
|
2017-08-03 05:52:31 +02:00
|
|
|
use Page;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Control\Director;
|
2017-08-03 05:52:31 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Control\HTTPResponse;
|
2017-08-03 05:52:31 +02:00
|
|
|
use SilverStripe\Control\Session;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Core\Config\Config;
|
|
|
|
use SilverStripe\Core\Convert;
|
2019-04-05 00:22:27 +02:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
|
|
|
use SilverStripe\i18n\i18n;
|
2016-06-15 06:03:16 +02:00
|
|
|
use SilverStripe\ORM\DataObject;
|
|
|
|
use SilverStripe\ORM\DB;
|
2018-10-08 06:07:50 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBEnum;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\ORM\FieldType\DBDatetime;
|
2017-12-14 02:49:58 +01:00
|
|
|
use SilverStripe\ORM\FieldType\DBField;
|
2016-11-23 06:09:10 +01:00
|
|
|
use SilverStripe\ORM\ValidationResult;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Security\LoginAttempt;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\Member;
|
2017-05-30 09:42:00 +02:00
|
|
|
use SilverStripe\Security\MemberAuthenticator\MemberAuthenticator;
|
2018-12-03 00:04:34 +01:00
|
|
|
use SilverStripe\Security\PasswordValidator;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\Security;
|
2017-06-15 12:54:41 +02:00
|
|
|
use SilverStripe\Security\SecurityToken;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2008-04-26 08:31:52 +02:00
|
|
|
/**
|
|
|
|
* Test the security class, including log-in form, change password form, etc
|
2017-07-03 02:21:27 +02:00
|
|
|
*
|
|
|
|
* @skipUpgrade
|
2008-04-26 08:31:52 +02:00
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
class SecurityTest extends FunctionalTest
|
|
|
|
{
|
|
|
|
protected static $fixture_file = 'MemberTest.yml';
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
protected $autoFollowRedirection = false;
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2017-03-24 12:17:26 +01:00
|
|
|
protected static $extra_controllers = [
|
2016-12-16 05:34:21 +01:00
|
|
|
SecurityTest\NullController::class,
|
|
|
|
SecurityTest\SecuredController::class,
|
|
|
|
];
|
|
|
|
|
2017-03-24 04:00:54 +01:00
|
|
|
protected function setUp()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
2017-04-17 05:07:28 +02:00
|
|
|
// Set to an empty array of authenticators to enable the default
|
2017-05-30 09:42:00 +02:00
|
|
|
Config::modify()->set(MemberAuthenticator::class, 'authenticators', []);
|
|
|
|
Config::modify()->set(MemberAuthenticator::class, 'default_authenticator', MemberAuthenticator::class);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2019-04-05 00:22:27 +02:00
|
|
|
// Set the default authenticator to use for these tests
|
|
|
|
Injector::inst()->load([
|
|
|
|
Security::class => [
|
|
|
|
'properties' => [
|
|
|
|
'Authenticators' => [
|
|
|
|
'default' => '%$' . MemberAuthenticator::class,
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
]);
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
2017-04-17 05:07:28 +02:00
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
2017-06-22 12:50:45 +02:00
|
|
|
Member::config()->set('unique_identifier_field', 'Email');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2018-12-03 00:04:34 +01:00
|
|
|
PasswordValidator::config()
|
|
|
|
->remove('min_length')
|
|
|
|
->remove('historic_count')
|
|
|
|
->remove('min_test_score');
|
|
|
|
|
|
|
|
Member::set_password_validator(null);
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
parent::setUp();
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
Director::config()->set('alternate_base_url', '/');
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testAccessingAuthenticatedPageRedirectsToLoginForm()
|
|
|
|
{
|
|
|
|
$this->autoFollowRedirection = false;
|
|
|
|
|
|
|
|
$response = $this->get('SecurityTest_SecuredController');
|
|
|
|
$this->assertEquals(302, $response->getStatusCode());
|
|
|
|
$this->assertContains(
|
|
|
|
Config::inst()->get(Security::class, 'login_url'),
|
|
|
|
$response->getHeader('Location')
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->logInWithPermission('ADMIN');
|
|
|
|
$response = $this->get('SecurityTest_SecuredController');
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
|
|
$this->assertContains('Success', $response->getBody());
|
|
|
|
|
|
|
|
$this->autoFollowRedirection = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPermissionFailureSetsCorrectFormMessages()
|
|
|
|
{
|
|
|
|
// Controller that doesn't attempt redirections
|
|
|
|
$controller = new SecurityTest\NullController();
|
2017-06-22 12:50:45 +02:00
|
|
|
$controller->setRequest(Controller::curr()->getRequest());
|
2016-12-16 05:34:21 +01:00
|
|
|
$controller->setResponse(new HTTPResponse());
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
$session = Controller::curr()->getRequest()->getSession();
|
2020-04-20 19:58:09 +02:00
|
|
|
Security::permissionFailure($controller, ['default' => 'Oops, not allowed']);
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->assertEquals('Oops, not allowed', $session->get('Security.Message.message'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Test that config values are used correctly
|
2017-06-22 12:50:45 +02:00
|
|
|
Config::modify()->set(Security::class, 'default_message_set', 'stringvalue');
|
2016-12-16 05:34:21 +01:00
|
|
|
Security::permissionFailure($controller);
|
|
|
|
$this->assertEquals(
|
|
|
|
'stringvalue',
|
2017-06-22 12:50:45 +02:00
|
|
|
$session->get('Security.Message.message'),
|
2016-12-16 05:34:21 +01:00
|
|
|
'Default permission failure message value was not present'
|
|
|
|
);
|
|
|
|
|
2017-04-17 05:07:28 +02:00
|
|
|
Config::modify()->remove(Security::class, 'default_message_set');
|
2020-04-20 19:58:09 +02:00
|
|
|
Config::modify()->merge(Security::class, 'default_message_set', ['default' => 'arrayvalue']);
|
2016-12-16 05:34:21 +01:00
|
|
|
Security::permissionFailure($controller);
|
|
|
|
$this->assertEquals(
|
|
|
|
'arrayvalue',
|
2017-06-22 12:50:45 +02:00
|
|
|
$session->get('Security.Message.message'),
|
2016-12-16 05:34:21 +01:00
|
|
|
'Default permission failure message value was not present'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test that non-default messages work.
|
|
|
|
// NOTE: we inspect the response body here as the session message has already
|
|
|
|
// been fetched and output as part of it, so has been removed from the session
|
|
|
|
$this->logInWithPermission('EDITOR');
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
Config::modify()->set(
|
2016-12-16 05:34:21 +01:00
|
|
|
Security::class,
|
|
|
|
'default_message_set',
|
2020-04-20 19:58:09 +02:00
|
|
|
['default' => 'default', 'alreadyLoggedIn' => 'You are already logged in!']
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
Security::permissionFailure($controller);
|
|
|
|
$this->assertContains(
|
|
|
|
'You are already logged in!',
|
|
|
|
$controller->getResponse()->getBody(),
|
|
|
|
'Custom permission failure message was ignored'
|
|
|
|
);
|
|
|
|
|
|
|
|
Security::permissionFailure(
|
|
|
|
$controller,
|
2020-04-20 19:58:09 +02:00
|
|
|
['default' => 'default', 'alreadyLoggedIn' => 'One-off failure message']
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
'One-off failure message',
|
|
|
|
$controller->getResponse()->getBody(),
|
|
|
|
"Message set passed to Security::permissionFailure() didn't override Config values"
|
|
|
|
);
|
2017-12-14 02:49:58 +01:00
|
|
|
|
|
|
|
// Test DBField cast messages work
|
|
|
|
Security::permissionFailure(
|
|
|
|
$controller,
|
|
|
|
DBField::create_field('HTMLFragment', '<p>Custom HTML & Message</p>')
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
'<p>Custom HTML & Message</p>',
|
|
|
|
$controller->getResponse()->getBody()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Plain text DBText
|
|
|
|
Security::permissionFailure(
|
|
|
|
$controller,
|
|
|
|
DBField::create_field('Text', 'Safely escaped & message')
|
|
|
|
);
|
|
|
|
$this->assertContains(
|
|
|
|
'Safely escaped & message',
|
|
|
|
$controller->getResponse()->getBody()
|
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Follow all redirects recursively
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @param int $limit Max number of requests
|
|
|
|
* @return HTTPResponse
|
|
|
|
*/
|
|
|
|
protected function getRecursive($url, $limit = 10)
|
|
|
|
{
|
|
|
|
$this->cssParser = null;
|
|
|
|
$response = $this->mainSession->get($url);
|
|
|
|
while (--$limit > 0 && $response instanceof HTTPResponse && $response->getHeader('Location')) {
|
|
|
|
$response = $this->mainSession->followRedirection();
|
|
|
|
}
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAutomaticRedirectionOnLogin()
|
|
|
|
{
|
|
|
|
// BackURL with permission error (not authenticated) should not redirect
|
2017-05-20 06:32:25 +02:00
|
|
|
if ($member = Security::getCurrentUser()) {
|
|
|
|
Security::setCurrentUser(null);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
$response = $this->getRecursive('SecurityTest_SecuredController');
|
|
|
|
$this->assertContains(Convert::raw2xml("That page is secured."), $response->getBody());
|
2017-04-23 05:30:33 +02:00
|
|
|
$this->assertContains('<input type="submit" name="action_doLogin"', $response->getBody());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Non-logged in user should not be redirected, but instead shown the login form
|
|
|
|
// No message/context is available as the user has not attempted to view the secured controller
|
|
|
|
$response = $this->getRecursive('Security/login?BackURL=SecurityTest_SecuredController/');
|
|
|
|
$this->assertNotContains(Convert::raw2xml("That page is secured."), $response->getBody());
|
|
|
|
$this->assertNotContains(Convert::raw2xml("You don't have access to this page"), $response->getBody());
|
2017-04-23 05:30:33 +02:00
|
|
|
$this->assertContains('<input type="submit" name="action_doLogin"', $response->getBody());
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// BackURL with permission error (wrong permissions) should not redirect
|
|
|
|
$this->logInAs('grouplessmember');
|
|
|
|
$response = $this->getRecursive('SecurityTest_SecuredController');
|
|
|
|
$this->assertContains(Convert::raw2xml("You don't have access to this page"), $response->getBody());
|
|
|
|
$this->assertContains(
|
|
|
|
'<input type="submit" name="action_logout" value="Log in as someone else"',
|
|
|
|
$response->getBody()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Directly accessing this page should attempt to follow the BackURL, but stop when it encounters the error
|
|
|
|
$response = $this->getRecursive('Security/login?BackURL=SecurityTest_SecuredController/');
|
|
|
|
$this->assertContains(Convert::raw2xml("You don't have access to this page"), $response->getBody());
|
|
|
|
$this->assertContains(
|
|
|
|
'<input type="submit" name="action_logout" value="Log in as someone else"',
|
|
|
|
$response->getBody()
|
|
|
|
);
|
|
|
|
|
|
|
|
// Check correctly logged in admin doesn't generate the same errors
|
|
|
|
$this->logInAs('admin');
|
|
|
|
$response = $this->getRecursive('SecurityTest_SecuredController');
|
|
|
|
$this->assertContains(Convert::raw2xml("Success"), $response->getBody());
|
|
|
|
|
|
|
|
// Directly accessing this page should attempt to follow the BackURL and succeed
|
|
|
|
$response = $this->getRecursive('Security/login?BackURL=SecurityTest_SecuredController/');
|
|
|
|
$this->assertContains(Convert::raw2xml("Success"), $response->getBody());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testLogInAsSomeoneElse()
|
|
|
|
{
|
|
|
|
$member = DataObject::get_one(Member::class);
|
|
|
|
|
|
|
|
/* Log in with any user that we can find */
|
2017-05-20 06:32:25 +02:00
|
|
|
Security::setCurrentUser($member);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
/* View the Security/login page */
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->get(Config::inst()->get(Security::class, 'login_url'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-30 09:42:00 +02:00
|
|
|
$items = $this->cssParser()->getBySelector('#MemberLoginForm_LoginForm input.action');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
/* We have only 1 input, one to allow the user to log in as someone else */
|
|
|
|
$this->assertEquals(count($items), 1, 'There is 1 input, allowing the user to log in as someone else.');
|
|
|
|
|
|
|
|
/* Submit the form, using only the logout action and a hidden field for the authenticator */
|
|
|
|
$response = $this->submitForm(
|
2017-05-30 09:42:00 +02:00
|
|
|
'MemberLoginForm_LoginForm',
|
2016-12-16 05:34:21 +01:00
|
|
|
null,
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-04-23 05:30:33 +02:00
|
|
|
'action_logout' => 1,
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
/* We get a good response */
|
2017-06-26 12:39:46 +02:00
|
|
|
$this->assertEquals($response->getStatusCode(), 302, 'We have a redirection response');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
/* Log the user out */
|
2017-05-20 06:32:25 +02:00
|
|
|
Security::setCurrentUser(null);
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testMemberIDInSessionDoesntExistInDatabaseHasToLogin()
|
|
|
|
{
|
2021-08-12 00:56:29 +02:00
|
|
|
// Attempt to fake a log in with a Member ID that doesn't exist in the DB
|
|
|
|
// Note: attempting $this->logInAs(500) will throw a TypeError in RequestAuthenticationHandler::logIn()
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->session()->set('loggedInAs', 500);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->autoFollowRedirection = true;
|
|
|
|
|
|
|
|
/* Attempt to get into the admin section */
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->get(Config::inst()->get(Security::class, 'login_url'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-30 09:42:00 +02:00
|
|
|
$items = $this->cssParser()->getBySelector('#MemberLoginForm_LoginForm input.text');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
/* We have 2 text inputs - one for email, and another for the password */
|
|
|
|
$this->assertEquals(count($items), 2, 'There are 2 inputs - one for email, another for password');
|
|
|
|
|
|
|
|
$this->autoFollowRedirection = false;
|
|
|
|
|
|
|
|
/* Log the user out */
|
2021-08-12 00:56:29 +02:00
|
|
|
$this->logOut();
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testLoginUsernamePersists()
|
|
|
|
{
|
|
|
|
// Test that username does not persist
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->session()->set('SessionForms.MemberLoginForm.Email', 'myuser@silverstripe.com');
|
|
|
|
Security::config()->set('remember_username', false);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->get(Config::inst()->get(Security::class, 'login_url'));
|
|
|
|
$items = $this
|
|
|
|
->cssParser()
|
2017-05-30 09:42:00 +02:00
|
|
|
->getBySelector('#MemberLoginForm_LoginForm #MemberLoginForm_LoginForm_Email');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(1, count($items));
|
|
|
|
$this->assertEmpty((string)$items[0]->attributes()->value);
|
|
|
|
$this->assertEquals('off', (string)$items[0]->attributes()->autocomplete);
|
2017-05-30 09:42:00 +02:00
|
|
|
$form = $this->cssParser()->getBySelector('#MemberLoginForm_LoginForm');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(1, count($form));
|
|
|
|
$this->assertEquals('off', (string)$form[0]->attributes()->autocomplete);
|
|
|
|
|
|
|
|
// Test that username does persist when necessary
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->session()->set('SessionForms.MemberLoginForm.Email', 'myuser@silverstripe.com');
|
|
|
|
Security::config()->set('remember_username', true);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->get(Config::inst()->get(Security::class, 'login_url'));
|
|
|
|
$items = $this
|
|
|
|
->cssParser()
|
2017-05-30 09:42:00 +02:00
|
|
|
->getBySelector('#MemberLoginForm_LoginForm #MemberLoginForm_LoginForm_Email');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(1, count($items));
|
|
|
|
$this->assertEquals('myuser@silverstripe.com', (string)$items[0]->attributes()->value);
|
|
|
|
$this->assertNotEquals('off', (string)$items[0]->attributes()->autocomplete);
|
2017-05-30 09:42:00 +02:00
|
|
|
$form = $this->cssParser()->getBySelector('#MemberLoginForm_LoginForm');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(1, count($form));
|
|
|
|
$this->assertNotEquals('off', (string)$form[0]->attributes()->autocomplete);
|
|
|
|
}
|
|
|
|
|
2017-06-15 12:54:41 +02:00
|
|
|
public function testLogout()
|
|
|
|
{
|
|
|
|
/* Enable SecurityToken */
|
|
|
|
$securityTokenWasEnabled = SecurityToken::is_enabled();
|
|
|
|
SecurityToken::enable();
|
|
|
|
|
|
|
|
$member = DataObject::get_one(Member::class);
|
|
|
|
|
|
|
|
/* Log in with any user that we can find */
|
|
|
|
Security::setCurrentUser($member);
|
|
|
|
|
|
|
|
/* Visit the Security/logout page with a test referer, but without a security token */
|
2017-05-09 22:24:15 +02:00
|
|
|
$this->get(
|
2017-06-15 12:54:41 +02:00
|
|
|
Config::inst()->get(Security::class, 'logout_url'),
|
|
|
|
null,
|
|
|
|
['Referer' => Director::absoluteBaseURL() . 'testpage']
|
|
|
|
);
|
|
|
|
|
|
|
|
/* Make sure the user is still logged in */
|
|
|
|
$this->assertNotNull(Security::getCurrentUser(), 'User is still logged in.');
|
|
|
|
|
|
|
|
$token = $this->cssParser()->getBySelector('#LogoutForm_Form #LogoutForm_Form_SecurityID');
|
|
|
|
$actions = $this->cssParser()->getBySelector('#LogoutForm_Form input.action');
|
|
|
|
|
|
|
|
/* We have a security token, and an action to allow the user to log out */
|
|
|
|
$this->assertCount(1, $token, 'There is a hidden field containing a security token.');
|
|
|
|
$this->assertCount(1, $actions, 'There is 1 action, allowing the user to log out.');
|
|
|
|
|
|
|
|
/* Submit the form, using the logout action */
|
|
|
|
$response = $this->submitForm(
|
|
|
|
'LogoutForm_Form',
|
|
|
|
null,
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2017-06-15 12:54:41 +02:00
|
|
|
'action_doLogout' => 1,
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2017-06-15 12:54:41 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
/* We get a good response */
|
|
|
|
$this->assertEquals(302, $response->getStatusCode());
|
|
|
|
$this->assertRegExp(
|
|
|
|
'/testpage/',
|
|
|
|
$response->getHeader('Location'),
|
|
|
|
"Logout form redirects to back to referer."
|
|
|
|
);
|
|
|
|
|
|
|
|
/* User is logged out successfully */
|
|
|
|
$this->assertNull(Security::getCurrentUser(), 'User is logged out.');
|
|
|
|
|
|
|
|
/* Re-disable SecurityToken */
|
|
|
|
if (!$securityTokenWasEnabled) {
|
|
|
|
SecurityToken::disable();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testExternalBackUrlRedirectionDisallowed()
|
|
|
|
{
|
|
|
|
// Test internal relative redirect
|
|
|
|
$response = $this->doTestLoginForm('noexpiry@silverstripe.com', '1nitialPassword', 'testpage');
|
|
|
|
$this->assertEquals(302, $response->getStatusCode());
|
|
|
|
$this->assertRegExp(
|
|
|
|
'/testpage/',
|
|
|
|
$response->getHeader('Location'),
|
|
|
|
"Internal relative BackURLs work when passed through to login form"
|
|
|
|
);
|
|
|
|
// Log the user out
|
2021-08-12 00:56:29 +02:00
|
|
|
$this->logOut();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Test internal absolute redirect
|
|
|
|
$response = $this->doTestLoginForm(
|
|
|
|
'noexpiry@silverstripe.com',
|
|
|
|
'1nitialPassword',
|
|
|
|
Director::absoluteBaseURL() . 'testpage'
|
|
|
|
);
|
|
|
|
// for some reason the redirect happens to a relative URL
|
|
|
|
$this->assertRegExp(
|
|
|
|
'/^' . preg_quote(Director::absoluteBaseURL(), '/') . 'testpage/',
|
|
|
|
$response->getHeader('Location'),
|
|
|
|
"Internal absolute BackURLs work when passed through to login form"
|
|
|
|
);
|
|
|
|
// Log the user out
|
2021-08-12 00:56:29 +02:00
|
|
|
$this->logOut();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Test external redirect
|
|
|
|
$response = $this->doTestLoginForm('noexpiry@silverstripe.com', '1nitialPassword', 'http://myspoofedhost.com');
|
|
|
|
$this->assertNotRegExp(
|
|
|
|
'/^' . preg_quote('http://myspoofedhost.com', '/') . '/',
|
|
|
|
(string)$response->getHeader('Location'),
|
|
|
|
"Redirection to external links in login form BackURL gets prevented as a measure against spoofing attacks"
|
|
|
|
);
|
|
|
|
|
|
|
|
// Test external redirection on ChangePasswordForm
|
|
|
|
$this->get('Security/changepassword?BackURL=http://myspoofedhost.com');
|
2018-12-03 00:04:34 +01:00
|
|
|
$changedResponse = $this->doTestChangepasswordForm('1nitialPassword', 'changedPassword#123');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertNotRegExp(
|
|
|
|
'/^' . preg_quote('http://myspoofedhost.com', '/') . '/',
|
|
|
|
(string)$changedResponse->getHeader('Location'),
|
|
|
|
"Redirection to external links in change password form BackURL gets prevented to stop spoofing attacks"
|
|
|
|
);
|
|
|
|
|
|
|
|
// Log the user out
|
2021-08-12 00:56:29 +02:00
|
|
|
$this->logOut();
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test that the login form redirects to the change password form after logging in with an expired password
|
|
|
|
*/
|
|
|
|
public function testExpiredPassword()
|
|
|
|
{
|
|
|
|
/* BAD PASSWORDS ARE LOCKED OUT */
|
|
|
|
$badResponse = $this->doTestLoginForm('testuser@example.com', 'badpassword');
|
|
|
|
$this->assertEquals(302, $badResponse->getStatusCode());
|
|
|
|
$this->assertRegExp('/Security\/login/', $badResponse->getHeader('Location'));
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->assertNull($this->session()->get('loggedInAs'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
/* UNEXPIRED PASSWORD GO THROUGH WITHOUT A HITCH */
|
|
|
|
$goodResponse = $this->doTestLoginForm('testuser@example.com', '1nitialPassword');
|
|
|
|
$this->assertEquals(302, $goodResponse->getStatusCode());
|
|
|
|
$this->assertEquals(
|
|
|
|
Controller::join_links(Director::absoluteBaseURL(), 'test/link'),
|
|
|
|
$goodResponse->getHeader('Location')
|
|
|
|
);
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->assertEquals($this->idFromFixture(Member::class, 'test'), $this->session()->get('loggedInAs'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-05-07 21:11:00 +02:00
|
|
|
$this->logOut();
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/* EXPIRED PASSWORDS ARE SENT TO THE CHANGE PASSWORD FORM */
|
|
|
|
$expiredResponse = $this->doTestLoginForm('expired@silverstripe.com', '1nitialPassword');
|
|
|
|
$this->assertEquals(302, $expiredResponse->getStatusCode());
|
|
|
|
$this->assertEquals(
|
2018-01-16 19:39:30 +01:00
|
|
|
Director::absoluteURL('Security/changepassword') . '?BackURL=test%2Flink',
|
2017-03-02 03:24:38 +01:00
|
|
|
Director::absoluteURL($expiredResponse->getHeader('Location'))
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertEquals(
|
|
|
|
$this->idFromFixture(Member::class, 'expiredpassword'),
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->session()->get('loggedInAs')
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
|
|
|
|
// Make sure it redirects correctly after the password has been changed
|
|
|
|
$this->mainSession->followRedirection();
|
2018-12-03 00:04:34 +01:00
|
|
|
$changedResponse = $this->doTestChangepasswordForm('1nitialPassword', 'changedPassword#123');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(302, $changedResponse->getStatusCode());
|
|
|
|
$this->assertEquals(
|
|
|
|
Controller::join_links(Director::absoluteBaseURL(), 'test/link'),
|
|
|
|
$changedResponse->getHeader('Location')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testChangePasswordForLoggedInUsers()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->doTestLoginForm('testuser@example.com', '1nitialPassword');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Change the password
|
|
|
|
$this->get('Security/changepassword?BackURL=test/back');
|
2018-12-03 00:04:34 +01:00
|
|
|
$changedResponse = $this->doTestChangepasswordForm('1nitialPassword', 'changedPassword#123');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(302, $changedResponse->getStatusCode());
|
|
|
|
$this->assertEquals(
|
|
|
|
Controller::join_links(Director::absoluteBaseURL(), 'test/back'),
|
|
|
|
$changedResponse->getHeader('Location')
|
|
|
|
);
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->assertEquals($this->idFromFixture(Member::class, 'test'), $this->session()->get('loggedInAs'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Check if we can login with the new password
|
2017-05-07 21:11:00 +02:00
|
|
|
$this->logOut();
|
2018-12-03 00:04:34 +01:00
|
|
|
$goodResponse = $this->doTestLoginForm('testuser@example.com', 'changedPassword#123');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(302, $goodResponse->getStatusCode());
|
|
|
|
$this->assertEquals(
|
|
|
|
Controller::join_links(Director::absoluteBaseURL(), 'test/link'),
|
|
|
|
$goodResponse->getHeader('Location')
|
|
|
|
);
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->assertEquals($this->idFromFixture(Member::class, 'test'), $this->session()->get('loggedInAs'));
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testChangePasswordFromLostPassword()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
/** @var Member $admin */
|
2016-12-16 05:34:21 +01:00
|
|
|
$admin = $this->objFromFixture(Member::class, 'test');
|
|
|
|
$admin->FailedLoginCount = 99;
|
2017-01-26 05:20:08 +01:00
|
|
|
$admin->LockedOutUntil = DBDatetime::now()->getValue();
|
2016-12-16 05:34:21 +01:00
|
|
|
$admin->write();
|
|
|
|
|
|
|
|
$this->assertNull($admin->AutoLoginHash, 'Hash is empty before lost password');
|
|
|
|
|
|
|
|
// Request new password by email
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->get('Security/lostpassword');
|
2020-04-20 19:58:09 +02:00
|
|
|
$this->post('Security/lostpassword/LostPasswordForm', ['Email' => 'testuser@example.com']);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$this->assertEmailSent('testuser@example.com');
|
|
|
|
|
|
|
|
// Load password link from email
|
|
|
|
$admin = DataObject::get_by_id(Member::class, $admin->ID);
|
|
|
|
$this->assertNotNull($admin->AutoLoginHash, 'Hash has been written after lost password');
|
|
|
|
|
|
|
|
// We don't have access to the token - generate a new token and hash pair.
|
|
|
|
$token = $admin->generateAutologinTokenAndStoreHash();
|
|
|
|
|
|
|
|
// Check.
|
2018-01-16 19:39:30 +01:00
|
|
|
$response = $this->get('Security/changepassword/?m=' . $admin->ID . '&t=' . $token);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(302, $response->getStatusCode());
|
2017-03-02 03:24:38 +01:00
|
|
|
$this->assertEquals(
|
|
|
|
Director::absoluteURL('Security/changepassword'),
|
|
|
|
Director::absoluteURL($response->getHeader('Location'))
|
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Follow redirection to form without hash in GET parameter
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->get('Security/changepassword');
|
2018-12-03 00:04:34 +01:00
|
|
|
$this->doTestChangepasswordForm('1nitialPassword', 'changedPassword#123');
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->assertEquals($this->idFromFixture(Member::class, 'test'), $this->session()->get('loggedInAs'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Check if we can login with the new password
|
2017-05-07 21:11:00 +02:00
|
|
|
$this->logOut();
|
2018-12-03 00:04:34 +01:00
|
|
|
$goodResponse = $this->doTestLoginForm('testuser@example.com', 'changedPassword#123');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(302, $goodResponse->getStatusCode());
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->assertEquals($this->idFromFixture(Member::class, 'test'), $this->session()->get('loggedInAs'));
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
$admin = DataObject::get_by_id(Member::class, $admin->ID, false);
|
|
|
|
$this->assertNull($admin->LockedOutUntil);
|
|
|
|
$this->assertEquals(0, $admin->FailedLoginCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRepeatedLoginAttemptsLockingPeopleOut()
|
|
|
|
{
|
|
|
|
i18n::set_locale('en_US');
|
2017-06-22 12:50:45 +02:00
|
|
|
Member::config()->set('lock_out_after_incorrect_logins', 5);
|
|
|
|
Member::config()->set('lock_out_delay_mins', 15);
|
2017-05-09 22:24:15 +02:00
|
|
|
DBDatetime::set_mock_now('2017-05-22 00:00:00');
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Login with a wrong password for more than the defined threshold
|
2017-05-09 22:24:15 +02:00
|
|
|
/** @var Member $member */
|
|
|
|
$member = null;
|
2017-06-22 12:50:45 +02:00
|
|
|
for ($i = 1; $i <= 6; $i++) {
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->doTestLoginForm('testuser@example.com', 'incorrectpassword');
|
2017-06-22 12:50:45 +02:00
|
|
|
/** @var Member $member */
|
2016-12-16 05:34:21 +01:00
|
|
|
$member = DataObject::get_by_id(Member::class, $this->idFromFixture(Member::class, 'test'));
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
if ($i < 5) {
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertNull(
|
|
|
|
$member->LockedOutUntil,
|
|
|
|
'User does not have a lockout time set if under threshold for failed attempts'
|
|
|
|
);
|
|
|
|
$this->assertHasMessage(
|
|
|
|
_t(
|
2017-04-20 03:15:24 +02:00
|
|
|
'SilverStripe\\Security\\Member.ERRORWRONGCRED',
|
2016-12-16 05:34:21 +01:00
|
|
|
'The provided details don\'t seem to be correct. Please try again.'
|
|
|
|
)
|
|
|
|
);
|
|
|
|
} else {
|
2017-05-09 22:24:15 +02:00
|
|
|
// Lockout should be exactly 15 minutes from now
|
|
|
|
/** @var DBDatetime $lockedOutUntilObj */
|
|
|
|
$lockedOutUntilObj = $member->dbObject('LockedOutUntil');
|
|
|
|
$this->assertEquals(
|
|
|
|
DBDatetime::now()->getTimestamp() + (15 * 60),
|
|
|
|
$lockedOutUntilObj->getTimestamp(),
|
2016-12-16 05:34:21 +01:00
|
|
|
'User has a lockout time set after too many failed attempts'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
2017-05-09 22:24:15 +02:00
|
|
|
$msg = _t(
|
|
|
|
'SilverStripe\\Security\\Member.ERRORLOCKEDOUT2',
|
2018-01-16 19:39:30 +01:00
|
|
|
'Your account has been temporarily disabled because of too many failed attempts at ' . 'logging in. Please try again in {count} minutes.',
|
2017-05-09 22:24:15 +02:00
|
|
|
null,
|
2020-04-20 19:58:09 +02:00
|
|
|
['count' => 15]
|
2017-05-09 22:24:15 +02:00
|
|
|
);
|
|
|
|
$this->assertHasMessage($msg);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->doTestLoginForm('testuser@example.com', '1nitialPassword');
|
|
|
|
$this->assertNull(
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->session()->get('loggedInAs'),
|
2016-12-16 05:34:21 +01:00
|
|
|
'The user can\'t log in after being locked out, even with the right password'
|
|
|
|
);
|
|
|
|
|
2017-05-09 22:24:15 +02:00
|
|
|
// Move into the future so we can login again
|
|
|
|
DBDatetime::set_mock_now('2017-06-22 00:00:00');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->doTestLoginForm('testuser@example.com', '1nitialPassword');
|
|
|
|
$this->assertEquals(
|
|
|
|
$member->ID,
|
2017-05-09 22:24:15 +02:00
|
|
|
$this->session()->get('loggedInAs'),
|
2016-12-16 05:34:21 +01:00
|
|
|
'After lockout expires, the user can login again'
|
|
|
|
);
|
|
|
|
|
|
|
|
// Log the user out
|
2017-05-07 21:11:00 +02:00
|
|
|
$this->logOut();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Login again with wrong password, but less attempts than threshold
|
2017-06-22 12:50:45 +02:00
|
|
|
for ($i = 1; $i < 5; $i++) {
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->doTestLoginForm('testuser@example.com', 'incorrectpassword');
|
|
|
|
}
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->assertNull($this->session()->get('loggedInAs'));
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertHasMessage(
|
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.'),
|
2016-12-16 05:34:21 +01:00
|
|
|
'The user can retry with a wrong password after the lockout expires'
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->doTestLoginForm('testuser@example.com', '1nitialPassword');
|
|
|
|
$this->assertEquals(
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->session()->get('loggedInAs'),
|
2016-12-16 05:34:21 +01:00
|
|
|
$member->ID,
|
|
|
|
'The user can login successfully after lockout expires, if staying below the threshold'
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAlternatingRepeatedLoginAttempts()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
Member::config()->set('lock_out_after_incorrect_logins', 3);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// ATTEMPTING LOG-IN TWICE WITH ONE ACCOUNT AND TWICE WITH ANOTHER SHOULDN'T LOCK ANYBODY OUT
|
|
|
|
|
|
|
|
$this->doTestLoginForm('testuser@example.com', 'incorrectpassword');
|
|
|
|
$this->doTestLoginForm('testuser@example.com', 'incorrectpassword');
|
|
|
|
|
|
|
|
$this->doTestLoginForm('noexpiry@silverstripe.com', 'incorrectpassword');
|
|
|
|
$this->doTestLoginForm('noexpiry@silverstripe.com', 'incorrectpassword');
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
/** @var Member $member1 */
|
2016-12-16 05:34:21 +01:00
|
|
|
$member1 = DataObject::get_by_id(Member::class, $this->idFromFixture(Member::class, 'test'));
|
2017-06-22 12:50:45 +02:00
|
|
|
/** @var Member $member2 */
|
2016-12-16 05:34:21 +01:00
|
|
|
$member2 = DataObject::get_by_id(Member::class, $this->idFromFixture(Member::class, 'noexpiry'));
|
|
|
|
|
|
|
|
$this->assertNull($member1->LockedOutUntil);
|
|
|
|
$this->assertNull($member2->LockedOutUntil);
|
|
|
|
|
|
|
|
// BUT, DOING AN ADDITIONAL LOG-IN WITH EITHER OF THEM WILL LOCK OUT, SINCE THAT IS THE 3RD FAILURE IN
|
|
|
|
// THIS SESSION
|
|
|
|
|
|
|
|
$this->doTestLoginForm('testuser@example.com', 'incorrectpassword');
|
|
|
|
$member1 = DataObject::get_by_id(Member::class, $this->idFromFixture(Member::class, 'test'));
|
|
|
|
$this->assertNotNull($member1->LockedOutUntil);
|
|
|
|
|
|
|
|
$this->doTestLoginForm('noexpiry@silverstripe.com', 'incorrectpassword');
|
|
|
|
$member2 = DataObject::get_by_id(Member::class, $this->idFromFixture(Member::class, 'noexpiry'));
|
|
|
|
$this->assertNotNull($member2->LockedOutUntil);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUnsuccessfulLoginAttempts()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
Security::config()->set('login_recording', true);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
/* UNSUCCESSFUL ATTEMPTS WITH WRONG PASSWORD FOR EXISTING USER ARE LOGGED */
|
|
|
|
$this->doTestLoginForm('testuser@example.com', 'wrongpassword');
|
2017-06-22 12:50:45 +02:00
|
|
|
/** @var LoginAttempt $attempt */
|
2017-11-30 04:56:18 +01:00
|
|
|
$attempt = LoginAttempt::getByEmail('testuser@example.com')->first();
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->assertInstanceOf(LoginAttempt::class, $attempt);
|
2017-11-30 04:56:18 +01:00
|
|
|
$member = Member::get()->filter('Email', 'testuser@example.com')->first();
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($attempt->Status, 'Failure');
|
2017-11-30 04:56:18 +01:00
|
|
|
$this->assertEmpty($attempt->Email); // Doesn't store potentially sensitive data
|
|
|
|
$this->assertEquals($attempt->EmailHashed, sha1('testuser@example.com'));
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($attempt->Member()->toMap(), $member->toMap());
|
|
|
|
|
|
|
|
/* UNSUCCESSFUL ATTEMPTS WITH NONEXISTING USER ARE LOGGED */
|
|
|
|
$this->doTestLoginForm('wronguser@silverstripe.com', 'wrongpassword');
|
2017-11-30 04:56:18 +01:00
|
|
|
$attempt = LoginAttempt::getByEmail('wronguser@silverstripe.com')->first();
|
|
|
|
$this->assertInstanceOf(LoginAttempt::class, $attempt);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($attempt->Status, 'Failure');
|
2017-11-30 04:56:18 +01:00
|
|
|
$this->assertEmpty($attempt->Email); // Doesn't store potentially sensitive data
|
|
|
|
$this->assertEquals($attempt->EmailHashed, sha1('wronguser@silverstripe.com'));
|
2016-11-23 06:09:10 +01:00
|
|
|
$this->assertNotEmpty($this->getValidationResult()->getMessages(), 'An invalid email returns a message.');
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSuccessfulLoginAttempts()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
Security::config()->set('login_recording', true);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
/* SUCCESSFUL ATTEMPTS ARE LOGGED */
|
|
|
|
$this->doTestLoginForm('testuser@example.com', '1nitialPassword');
|
2017-06-22 12:50:45 +02:00
|
|
|
/** @var LoginAttempt $attempt */
|
2017-11-30 04:56:18 +01:00
|
|
|
$attempt = LoginAttempt::getByEmail('testuser@example.com')->first();
|
|
|
|
$member = Member::get()->filter('Email', 'testuser@example.com')->first();
|
|
|
|
$this->assertInstanceOf(LoginAttempt::class, $attempt);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($attempt->Status, 'Success');
|
2017-11-30 04:56:18 +01:00
|
|
|
$this->assertEmpty($attempt->Email); // Doesn't store potentially sensitive data
|
|
|
|
$this->assertEquals($attempt->EmailHashed, sha1('testuser@example.com'));
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals($attempt->Member()->toMap(), $member->toMap());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDatabaseIsReadyWithInsufficientMemberColumns()
|
|
|
|
{
|
2017-04-23 05:30:33 +02:00
|
|
|
Security::clear_database_is_ready();
|
2018-10-08 06:07:50 +02:00
|
|
|
DBEnum::flushCache();
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
// Assumption: The database has been built correctly by the test runner,
|
|
|
|
// and has all columns present in the ORM
|
|
|
|
/**
|
2017-03-24 12:17:26 +01:00
|
|
|
* @skipUpgrade
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
DB::get_schema()->renameField('Member', 'Email', 'Email_renamed');
|
|
|
|
|
|
|
|
// Email column is now missing, which means we're not ready to do permission checks
|
|
|
|
$this->assertFalse(Security::database_is_ready());
|
|
|
|
|
|
|
|
// Rebuild the database (which re-adds the Email column), and try again
|
2017-03-24 12:17:26 +01:00
|
|
|
static::resetDBSchema(true);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue(Security::database_is_ready());
|
|
|
|
}
|
|
|
|
|
2017-01-17 13:23:41 +01:00
|
|
|
public function testSecurityControllerSendsRobotsTagHeader()
|
|
|
|
{
|
|
|
|
$response = $this->get(Config::inst()->get(Security::class, 'login_url'));
|
|
|
|
$robotsHeader = $response->getHeader('X-Robots-Tag');
|
|
|
|
$this->assertNotNull($robotsHeader);
|
|
|
|
$this->assertContains('noindex', $robotsHeader);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDoNotSendEmptyRobotsHeaderIfNotDefined()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
Config::modify()->remove(Security::class, 'robots_tag');
|
2017-01-17 13:23:41 +01:00
|
|
|
$response = $this->get(Config::inst()->get(Security::class, 'login_url'));
|
|
|
|
$robotsHeader = $response->getHeader('X-Robots-Tag');
|
|
|
|
$this->assertNull($robotsHeader);
|
|
|
|
}
|
|
|
|
|
2017-08-03 05:52:31 +02:00
|
|
|
public function testGetResponseController()
|
|
|
|
{
|
|
|
|
if (!class_exists(Page::class)) {
|
|
|
|
$this->markTestSkipped("This test requires CMS module");
|
|
|
|
}
|
|
|
|
|
|
|
|
$request = new HTTPRequest('GET', '/');
|
|
|
|
$request->setSession(new Session([]));
|
|
|
|
$security = new Security();
|
|
|
|
$security->setRequest($request);
|
|
|
|
$reflection = new \ReflectionClass($security);
|
|
|
|
$method = $reflection->getMethod('getResponseController');
|
|
|
|
$method->setAccessible(true);
|
|
|
|
$result = $method->invoke($security, 'Page');
|
|
|
|
|
|
|
|
// Ensure page shares the same controller as security
|
2017-09-16 01:27:16 +02:00
|
|
|
$securityClass = Config::inst()->get(Security::class, 'page_class');
|
2017-11-30 04:56:18 +01:00
|
|
|
/** @var Page $securityPage */
|
2017-09-16 01:27:16 +02:00
|
|
|
$securityPage = new $securityClass();
|
|
|
|
$this->assertInstanceOf($securityPage->getControllerName(), $result);
|
2017-08-03 05:52:31 +02:00
|
|
|
$this->assertEquals($request, $result->getRequest());
|
|
|
|
}
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
/**
|
|
|
|
* Execute a log-in form using Director::test().
|
|
|
|
* Helper method for the tests above
|
2017-06-22 12:50:45 +02:00
|
|
|
*
|
|
|
|
* @param string $email
|
|
|
|
* @param string $password
|
|
|
|
* @param string $backURL
|
|
|
|
* @return HTTPResponse
|
2016-12-16 05:34:21 +01:00
|
|
|
*/
|
|
|
|
public function doTestLoginForm($email, $password, $backURL = 'test/link')
|
|
|
|
{
|
|
|
|
$this->get(Config::inst()->get(Security::class, 'logout_url'));
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->session()->set('BackURL', $backURL);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->get(Config::inst()->get(Security::class, 'login_url'));
|
|
|
|
|
|
|
|
return $this->submitForm(
|
2017-05-30 09:42:00 +02:00
|
|
|
"MemberLoginForm_LoginForm",
|
2016-12-16 05:34:21 +01:00
|
|
|
null,
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'Email' => $email,
|
|
|
|
'Password' => $password,
|
|
|
|
'AuthenticationMethod' => MemberAuthenticator::class,
|
2017-04-23 05:30:33 +02:00
|
|
|
'action_doLogin' => 1,
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper method to execute a change password form
|
2017-06-22 12:50:45 +02:00
|
|
|
*
|
|
|
|
* @param string $oldPassword
|
|
|
|
* @param string $newPassword
|
|
|
|
* @return HTTPResponse
|
2016-12-16 05:34:21 +01:00
|
|
|
*/
|
|
|
|
public function doTestChangepasswordForm($oldPassword, $newPassword)
|
|
|
|
{
|
|
|
|
return $this->submitForm(
|
|
|
|
"ChangePasswordForm_ChangePasswordForm",
|
|
|
|
null,
|
2020-04-20 19:58:09 +02:00
|
|
|
[
|
2016-12-16 05:34:21 +01:00
|
|
|
'OldPassword' => $oldPassword,
|
|
|
|
'NewPassword1' => $newPassword,
|
|
|
|
'NewPassword2' => $newPassword,
|
|
|
|
'action_doChangePassword' => 1,
|
2020-04-20 19:58:09 +02:00
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-11-23 06:09:10 +01:00
|
|
|
/**
|
|
|
|
* Assert this message is in the current login form errors
|
|
|
|
*
|
|
|
|
* @param string $expected
|
|
|
|
* @param string $errorMessage
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
protected function assertHasMessage($expected, $errorMessage = null)
|
|
|
|
{
|
2016-11-23 06:09:10 +01:00
|
|
|
$messages = [];
|
|
|
|
$result = $this->getValidationResult();
|
2016-12-16 05:34:21 +01:00
|
|
|
if ($result) {
|
|
|
|
foreach ($result->getMessages() as $message) {
|
2016-11-23 06:09:10 +01:00
|
|
|
$messages[] = $message['message'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertContains($expected, $messages, $errorMessage);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get validation result from last login form submission
|
|
|
|
*
|
|
|
|
* @return ValidationResult
|
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
protected function getValidationResult()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
$result = $this->session()->get('FormInfo.MemberLoginForm_LoginForm.result');
|
2016-12-16 05:34:21 +01:00
|
|
|
if ($result) {
|
2016-11-23 06:09:10 +01:00
|
|
|
return unserialize($result);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2008-08-11 02:14:48 +02:00
|
|
|
}
|