silverstripe-framework/src/Security/AuthenticationMiddleware.php
Damian Mooyman d20ab50f9d API Stronger Injector service unregistration
BUG Fix up test regressions
FIX director references to request object
API Move all middlewares to common namespace
API Implement RequestHandlerMiddlewareAdapter
ENHANCEMENT Improve IP address parsing
Fix up PHPDoc / psr2 linting
BUG Fix property parsing in TrustedProxyMiddleware
BUG Fix Director::is_https()
2017-06-27 13:32:39 +12:00

63 lines
1.5 KiB
PHP

<?php
namespace SilverStripe\Security;
use SilverStripe\Control\HTTPRequest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\Middleware\HTTPMiddleware;
use SilverStripe\Core\Config\Configurable;
use SilverStripe\ORM\ValidationException;
class AuthenticationMiddleware implements HTTPMiddleware
{
use Configurable;
/**
* @var AuthenticationHandler
*/
protected $authenticationHandler;
/**
* @return AuthenticationHandler
*/
public function getAuthenticationHandler()
{
return $this->authenticationHandler;
}
/**
* @param AuthenticationHandler $authenticationHandler
* @return $this
*/
public function setAuthenticationHandler(AuthenticationHandler $authenticationHandler)
{
$this->authenticationHandler = $authenticationHandler;
return $this;
}
/**
* Identify the current user from the request
*
* @param HTTPRequest $request
* @param callable $delegate
* @return HTTPResponse
*/
public function process(HTTPRequest $request, callable $delegate)
{
if (Security::database_is_ready()) {
try {
$this
->getAuthenticationHandler()
->authenticateRequest($request);
} catch (ValidationException $e) {
return new HTTPResponse(
"Bad log-in details: " . $e->getMessage(),
400
);
}
}
return $delegate($request);
}
}