* @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:----jk/6oXpr4Ark/INzGAwlWRW973jph3IKEt9hPYypcirsoAAoXzbxezq+h+6MBegoRCXSiPHBVNVbj8LgIL4BS7oGJjzLIO77wSEcIJ0m+lh7gjWe+Kxb71lhOC4eXjud5js+N3P1AfIFqvSnXgkN18sWD/WmleM8DP3/DQa1SAL6BpkIxae9ghSNOl4eqNd5wQssMT9YGmElN5TQdMNFp+ilQnO/MIzpiXwxMsUTqvpgEzhPmhkWAygOtK6dHAzdAxggUMQqN279x446N/kauM5KJFsnqQERS+3dkIhxTFzDzCvtbu8q58cdU2uEfY6qEoIRFjPOurZC2hH1pcHWacTCD6mw1HNtkxXc6UbRgiz8MLrQixjBswVJPoHZKt73CMJhziRryH2G4E5/nmjsvEl+n7C35xLZvVauIrcJcQ+WExCmD51umTydpIEhk6DZ4vJsdwz/vWUH2DnAj/H+DDHP54EDOev8VM3fmlOoTdYbIYgVEhmttDskWdbhtVBLH1Uyd30qX11IiSvrm6qz1pMn/3fH3m/4+thaqKJYDutWKoe3i/6cRJzg9e7KMEtaHoXgYIqT5Grcb0YmhXJ9RejkDOjbwHEIMrCcw53Y8BNH9qC7ZN77je4y6YjtBJmHm58GNRIgMtvR72v3ftVspqcUw7nUJYEtQH9VPhdQzu8=----ATTACHMENT:----MTE3NzU2ODgyMjMxMTcxNiA2NjI4MTM4NzAwOTY0MjU0IDg5OTczNDk3Nzk4MTYzMQ==