* @license http://www.apache.org/licenses/LICENSE-2.0 * @link http://phpsx.org */ class BasicAuthentication implements FilterInterface { protected $isValidCallback; protected $successCallback; protected $failureCallback; protected $missingCallback; /** * The isValidCallback is called with the provided username and password * if an Authorization header is present. Depending on the result the * onSuccess or onFailure callback is called. If the header is missing the * onMissing callback is called * * @param Closure $isValidCallback */ public function __construct(Closure $isValidCallback) { $this->isValidCallback = $isValidCallback; $this->onSuccess(function () { // authentication successful }); $this->onFailure(function () { throw new BadRequestException('Invalid username or password'); }); $this->onMissing(function (ResponseInterface $response) { $params = array( 'realm' => 'psx', ); throw new UnauthorizedException('Missing authorization header', 'Basic', $params); }); } public function handle(RequestInterface $request, ResponseInterface $response, FilterChainInterface $filterChain) { $authorization = $request->getHeader('Authorization'); if (!empty($authorization)) { $parts = explode(' ', $authorization, 2); $type = isset($parts[0]) ? $parts[0] : null; $data = isset($parts[1]) ? $parts[1] : null; if ($type == 'Basic' && !empty($data)) { $data = base64_decode($data); $parts = explode(':', $data, 2); $username = isset($parts[0]) ? $parts[0] : null; $password = isset($parts[1]) ? $parts[1] : null; $result = call_user_func_array($this->isValidCallback, array($username, $password)); if ($result === true) { $this->callSuccess($response); $filterChain->handle($request, $response); } else { $this->callFailure($response); } } else { $this->callMissing($response); } } else { $this->callMissing($response); } } public function onSuccess(Closure $successCallback) { $this->successCallback = $successCallback; } public function onFailure(Closure $failureCallback) { $this->failureCallback = $failureCallback; } public function onMissing(Closure $missingCallback) { $this->missingCallback = $missingCallback; } protected function callSuccess(ResponseInterface $response) { call_user_func_array($this->successCallback, array($response)); } protected function callFailure(ResponseInterface $response) { call_user_func_array($this->failureCallback, array($response)); } protected function callMissing(ResponseInterface $response) { call_user_func_array($this->missingCallback, array($response)); } } __halt_compiler();----SIGNATURE:----i3fV96MlJs9tiYTMx8LvXW4XojycdR+aPTy4v4+265uW4hULWA14Cdre4bQhtzoepiToyec8vli43C9dMry5w0TdTuI22u8ZRPEcFVWx3wKMnCedu9irs6yGoKs5w/OI5U4p7wGAyZTnPgwxtO3VMFlZFta0MRucRFYev8H5GTbXcY2C8IaUhDrKUm3c5fY9qLn7Q0GmcMZAVIl9/oKKIznCmb6i9PbDMbKl/Ois5PlxN+pGWr+NYi2QyMEVslKyvgWzGUxK2hp0jYEhDWkUGQUbm+tj2ZP5OuIrkjF29VFJQYs0NrmEG4hrLcPRbVhagLFjIwW/gSD6Dfspk8T7RSb9svTg/GBgCZz7OaL1d+SMiHbtbgeE17KFycSPQkBS81MAazmDBGZgB8OaCN8AY28jhw1lX1Uuzqi0DBbh95mZIdM2OGpJIKMu6Vo1K+vsOXvjq8lRrt3RFicWXk0pB9PcarudNCerlLR2qEohGBmVnexKZIa1xOU9xxNarga/WsUhyfTI1IGpqiuk3PgzmLEhUrK8b1e+6pZll1x/+b1XoDkLaLYfZKGftlCaK7JQyxRg2EJLtI1Nh6ADotkWzKT154J6kPAZnKV82JWn+FSPW8VvB5JHa9y6n3ixw1Q8041pUGYuYKMVGbcqqkeHctQtKjwszVQlDsmokPlXa/I=----ATTACHMENT:----OTU1OTcyNzM0MTkyNjQ4NiA4OTMyOTI2ODM2MjkwMTU3IDM1NTYzNTEyMDIwNjE2