2017-05-08 07:11:00 +12:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace SilverStripe\Security;
|
|
|
|
|
|
|
|
use SilverStripe\Control\HTTPRequest;
|
|
|
|
use SilverStripe\Control\HTTPResponse;
|
2017-06-09 15:07:35 +12:00
|
|
|
use SilverStripe\Control\HTTPResponse_Exception;
|
2017-06-23 12:32:43 +12:00
|
|
|
use SilverStripe\Control\HTTPMiddleware;
|
2017-05-08 07:11:00 +12:00
|
|
|
use SilverStripe\Core\Config\Configurable;
|
2017-05-30 19:42:00 +12:00
|
|
|
use SilverStripe\ORM\ValidationException;
|
2017-05-08 07:11:00 +12:00
|
|
|
|
2017-06-23 12:32:43 +12:00
|
|
|
class AuthenticationMiddleware implements HTTPMiddleware
|
2017-05-08 07:11:00 +12:00
|
|
|
{
|
|
|
|
use Configurable;
|
|
|
|
|
2017-05-20 16:32:25 +12:00
|
|
|
/**
|
2017-06-09 15:07:35 +12:00
|
|
|
* @var AuthenticationHandler
|
2017-05-30 19:42:00 +12:00
|
|
|
*/
|
2017-06-09 15:07:35 +12:00
|
|
|
protected $authenticationHandler;
|
2017-05-30 19:42:00 +12:00
|
|
|
|
|
|
|
/**
|
2017-06-09 15:07:35 +12:00
|
|
|
* @return AuthenticationHandler
|
2017-05-20 16:32:25 +12:00
|
|
|
*/
|
2017-06-09 15:07:35 +12:00
|
|
|
public function getAuthenticationHandler()
|
2017-05-08 07:11:00 +12:00
|
|
|
{
|
2017-06-09 15:07:35 +12:00
|
|
|
return $this->authenticationHandler;
|
2017-05-08 07:11:00 +12:00
|
|
|
}
|
|
|
|
|
2017-05-30 19:42:00 +12:00
|
|
|
/**
|
2017-06-09 15:07:35 +12:00
|
|
|
* @param AuthenticationHandler $authenticationHandler
|
|
|
|
* @return $this
|
2017-05-30 19:42:00 +12:00
|
|
|
*/
|
2017-06-09 15:07:35 +12:00
|
|
|
public function setAuthenticationHandler(AuthenticationHandler $authenticationHandler)
|
2017-05-30 19:42:00 +12:00
|
|
|
{
|
2017-06-09 15:07:35 +12:00
|
|
|
$this->authenticationHandler = $authenticationHandler;
|
|
|
|
return $this;
|
2017-05-30 19:42:00 +12:00
|
|
|
}
|
|
|
|
|
2017-05-08 07:11:00 +12:00
|
|
|
/**
|
|
|
|
* Identify the current user from the request
|
2017-05-30 19:42:00 +12:00
|
|
|
*
|
|
|
|
* @param HTTPRequest $request
|
|
|
|
* @return bool|void
|
|
|
|
* @throws HTTPResponse_Exception
|
2017-05-08 07:11:00 +12:00
|
|
|
*/
|
2017-06-23 12:32:43 +12:00
|
|
|
public function process(HTTPRequest $request, callable $delegate)
|
2017-05-08 07:11:00 +12:00
|
|
|
{
|
2017-06-23 12:32:43 +12:00
|
|
|
if (Security::database_is_ready()) {
|
|
|
|
try {
|
|
|
|
$this
|
|
|
|
->getAuthenticationHandler()
|
|
|
|
->authenticateRequest($request);
|
|
|
|
} catch (ValidationException $e) {
|
|
|
|
return new HTTPResponse(
|
|
|
|
"Bad log-in details: " . $e->getMessage(),
|
|
|
|
400
|
|
|
|
);
|
|
|
|
}
|
2017-06-22 22:50:45 +12:00
|
|
|
}
|
|
|
|
|
2017-06-23 12:32:43 +12:00
|
|
|
return $delegate($request);
|
2017-05-08 07:11:00 +12:00
|
|
|
}
|
|
|
|
}
|