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-12-13 01:38:55 +01:00
|
|
|
use SilverStripe\Core\Injector\Injector;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Security\BasicAuth;
|
2017-12-13 01:38:55 +01:00
|
|
|
use SilverStripe\Security\BasicAuthMiddleware;
|
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;
|
2017-12-13 01:38:55 +01:00
|
|
|
use SilverStripe\Security\Tests\BasicAuthTest\ControllerNotSecured;
|
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
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class BasicAuthTest extends FunctionalTest
|
|
|
|
{
|
|
|
|
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-12-13 01:38:55 +01:00
|
|
|
ControllerNotSecured::class,
|
2016-12-16 05:34:21 +01:00
|
|
|
];
|
|
|
|
|
2021-10-27 04:39:47 +02:00
|
|
|
protected function setUp(): void
|
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
|
|
|
|
2017-12-13 01:38:55 +01:00
|
|
|
// Set route-specific permissions
|
|
|
|
/** @var BasicAuthMiddleware $middleware */
|
|
|
|
$middleware = Injector::inst()->get(BasicAuthMiddleware::class);
|
|
|
|
$middleware->setURLPatterns([
|
|
|
|
'/^BasicAuthTest_ControllerSecuredWithPermission$/' => 'MYCODE',
|
|
|
|
'/^BasicAuthTest_ControllerSecuredWithoutPermission$/' => true,
|
|
|
|
]);
|
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 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);
|
|
|
|
}
|
2017-12-13 01:38:55 +01:00
|
|
|
|
|
|
|
public function testProtectEntireSite()
|
|
|
|
{
|
|
|
|
// Unsecured controller allows access
|
|
|
|
$response = Director::test('BasicAuthTest_ControllerNotSecured');
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
|
|
|
|
|
|
// Globally enable basic auth
|
|
|
|
BasicAuth::protect_entire_site();
|
|
|
|
$response = Director::test('BasicAuthTest_ControllerNotSecured');
|
|
|
|
$this->assertEquals(401, $response->getStatusCode());
|
|
|
|
$this->assertNotEmpty($response->getHeader('WWW-Authenticate'));
|
|
|
|
|
|
|
|
// Can be excluded via rule
|
|
|
|
/** @var BasicAuthMiddleware $middleware */
|
|
|
|
$middleware = Injector::inst()->get(BasicAuthMiddleware::class);
|
|
|
|
$middleware->setURLPatterns([
|
|
|
|
'/^BasicAuthTest_ControllerNotSecured$/' => false,
|
|
|
|
]);
|
|
|
|
$response = Director::test('BasicAuthTest_ControllerNotSecured');
|
|
|
|
$this->assertEquals(200, $response->getStatusCode());
|
|
|
|
}
|
2010-10-19 00:29:35 +02:00
|
|
|
}
|