2010-10-19 00:29:35 +02:00
|
|
|
<?php
|
2016-06-23 01:37:22 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Security\Tests;
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Security\BasicAuth;
|
2016-06-23 01:37:22 +02:00
|
|
|
use SilverStripe\Security\Member;
|
|
|
|
use SilverStripe\Security\Security;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\FunctionalTest;
|
|
|
|
use SilverStripe\Control\Director;
|
2016-10-14 03:30:05 +02:00
|
|
|
use SilverStripe\Security\Tests\BasicAuthTest\ControllerSecuredWithoutPermission;
|
|
|
|
use SilverStripe\Security\Tests\BasicAuthTest\ControllerSecuredWithPermission;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2010-10-19 00:29:35 +02:00
|
|
|
/**
|
2016-08-19 00:51:35 +02:00
|
|
|
* @skipUpgrade
|
2010-10-19 00:29:35 +02:00
|
|
|
*/
|
2016-12-16 05:34:21 +01:00
|
|
|
class BasicAuthTest extends FunctionalTest
|
|
|
|
{
|
|
|
|
|
2017-05-20 06:32:25 +02:00
|
|
|
protected static $original_unique_identifier_field;
|
2016-12-16 05:34:21 +01:00
|
|
|
|
|
|
|
protected static $fixture_file = 'BasicAuthTest.yml';
|
|
|
|
|
2017-03-24 12:17:26 +01:00
|
|
|
protected static $extra_controllers = [
|
2016-12-16 05:34:21 +01:00
|
|
|
ControllerSecuredWithPermission::class,
|
|
|
|
ControllerSecuredWithoutPermission::class,
|
|
|
|
];
|
|
|
|
|
2017-03-24 04:00:54 +01:00
|
|
|
protected function setUp()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
// Fixtures assume Email is the field used to identify the log in identity
|
2017-06-22 12:50:45 +02:00
|
|
|
Member::config()->set('unique_identifier_field', 'Email');
|
2017-04-23 05:30:33 +02:00
|
|
|
Security::force_database_is_ready(true); // Prevents Member test subclasses breaking ready test
|
2017-06-22 12:50:45 +02:00
|
|
|
Member::config()->set('lock_out_after_incorrect_logins', 10);
|
|
|
|
|
|
|
|
// Temp disable is_cli() exemption for tests
|
|
|
|
BasicAuth::config()->set('ignore_cli', false);
|
2017-11-02 22:45:58 +01:00
|
|
|
|
|
|
|
// Reset statics
|
|
|
|
BasicAuthTest\ControllerSecuredWithPermission::$index_called = false;
|
|
|
|
BasicAuthTest\ControllerSecuredWithPermission::$post_init_called = false;
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testBasicAuthEnabledWithoutLogin()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
$response = Director::test('BasicAuthTest_ControllerSecuredWithPermission');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(401, $response->getStatusCode());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBasicAuthDoesntCallActionOrFurtherInitOnAuthFailure()
|
|
|
|
{
|
2017-11-02 22:45:58 +01:00
|
|
|
Director::test('BasicAuthTest_ControllerSecuredWithPermission');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertFalse(BasicAuthTest\ControllerSecuredWithPermission::$index_called);
|
|
|
|
$this->assertFalse(BasicAuthTest\ControllerSecuredWithPermission::$post_init_called);
|
|
|
|
|
2017-11-02 22:45:58 +01:00
|
|
|
$headers = [
|
|
|
|
'PHP_AUTH_USER' => 'user-in-mygroup@test.com',
|
|
|
|
'PHP_AUTH_PW' => 'test',
|
|
|
|
];
|
|
|
|
Director::test('BasicAuthTest_ControllerSecuredWithPermission', [], [], null, null, $headers);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertTrue(BasicAuthTest\ControllerSecuredWithPermission::$index_called);
|
|
|
|
$this->assertTrue(BasicAuthTest\ControllerSecuredWithPermission::$post_init_called);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBasicAuthEnabledWithPermission()
|
|
|
|
{
|
2017-11-02 22:45:58 +01:00
|
|
|
$headers = [
|
|
|
|
'PHP_AUTH_USER' => 'user-in-mygroup@test.com',
|
|
|
|
'PHP_AUTH_PW' => 'wrongpassword',
|
|
|
|
];
|
|
|
|
$response = Director::test('BasicAuthTest_ControllerSecuredWithPermission', [], [], null, null, $headers);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(401, $response->getStatusCode(), 'Invalid users dont have access');
|
|
|
|
|
2017-11-02 22:45:58 +01:00
|
|
|
$headers = [
|
|
|
|
'PHP_AUTH_USER' => 'user-without-groups@test.com',
|
|
|
|
'PHP_AUTH_PW' => 'test',
|
|
|
|
];
|
|
|
|
$response = Director::test('BasicAuthTest_ControllerSecuredWithPermission', [], [], null, null, $headers);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(401, $response->getStatusCode(), 'Valid user without required permission has no access');
|
|
|
|
|
2017-11-02 22:45:58 +01:00
|
|
|
$headers = [
|
|
|
|
'PHP_AUTH_USER' => 'user-in-mygroup@test.com',
|
|
|
|
'PHP_AUTH_PW' => 'test',
|
|
|
|
];
|
|
|
|
$response = Director::test('BasicAuthTest_ControllerSecuredWithPermission', [], [], null, null, $headers);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(200, $response->getStatusCode(), 'Valid user with required permission has access');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBasicAuthEnabledWithoutPermission()
|
|
|
|
{
|
2017-11-02 22:45:58 +01:00
|
|
|
$headers = [
|
|
|
|
'PHP_AUTH_USER' => 'user-without-groups@test.com',
|
|
|
|
'PHP_AUTH_PW' => 'wrongpassword',
|
|
|
|
];
|
|
|
|
$response = Director::test('BasicAuthTest_ControllerSecuredWithoutPermission', [], [], null, null, $headers);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(401, $response->getStatusCode(), 'Invalid users dont have access');
|
|
|
|
|
2017-11-02 22:45:58 +01:00
|
|
|
$headers = [
|
|
|
|
'PHP_AUTH_USER' => 'user-without-groups@test.com',
|
|
|
|
'PHP_AUTH_PW' => 'test',
|
|
|
|
];
|
|
|
|
$response = Director::test('BasicAuthTest_ControllerSecuredWithoutPermission', [], [], null, null, $headers);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(200, $response->getStatusCode(), 'All valid users have access');
|
|
|
|
|
2017-11-02 22:45:58 +01:00
|
|
|
$headers = [
|
|
|
|
'PHP_AUTH_USER' => 'user-in-mygroup@test.com',
|
|
|
|
'PHP_AUTH_PW' => 'test',
|
|
|
|
];
|
|
|
|
$response = Director::test('BasicAuthTest_ControllerSecuredWithoutPermission', [], [], null, null, $headers);
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertEquals(200, $response->getStatusCode(), 'All valid users have access');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testBasicAuthFailureIncreasesFailedLoginCount()
|
|
|
|
{
|
|
|
|
// Prior to login
|
|
|
|
$check = Member::get()->filter('Email', 'failedlogin@test.com')->first();
|
|
|
|
$this->assertEquals(0, $check->FailedLoginCount);
|
|
|
|
|
|
|
|
// First failed attempt
|
2017-11-02 22:45:58 +01:00
|
|
|
$headers = [
|
|
|
|
'PHP_AUTH_USER' => 'failedlogin@test.com',
|
|
|
|
'PHP_AUTH_PW' => 'test',
|
|
|
|
];
|
|
|
|
Director::test('BasicAuthTest_ControllerSecuredWithoutPermission', [], [], null, null, $headers);
|
2016-12-16 05:34:21 +01:00
|
|
|
$check = Member::get()->filter('Email', 'failedlogin@test.com')->first();
|
|
|
|
$this->assertEquals(1, $check->FailedLoginCount);
|
|
|
|
|
|
|
|
// Second failed attempt
|
2017-11-02 22:45:58 +01:00
|
|
|
$headers['PHP_AUTH_PW'] = 'testwrong';
|
|
|
|
Director::test('BasicAuthTest_ControllerSecuredWithoutPermission', [], [], null, null, $headers);
|
2016-12-16 05:34:21 +01:00
|
|
|
$check = Member::get()->filter('Email', 'failedlogin@test.com')->first();
|
|
|
|
$this->assertEquals(2, $check->FailedLoginCount);
|
|
|
|
|
|
|
|
// successful basic auth should reset failed login count
|
2017-11-02 22:45:58 +01:00
|
|
|
$headers['PHP_AUTH_PW'] = 'Password';
|
|
|
|
Director::test('BasicAuthTest_ControllerSecuredWithoutPermission', [], [], null, null, $headers);
|
2016-12-16 05:34:21 +01:00
|
|
|
$check = Member::get()->filter('Email', 'failedlogin@test.com')->first();
|
|
|
|
$this->assertEquals(0, $check->FailedLoginCount);
|
|
|
|
}
|
2010-10-19 00:29:35 +02:00
|
|
|
}
|