addBackend($authBackend); } } /** * Adds an authentication backend to the plugin. */ public function addBackend(Backend\BackendInterface $authBackend) { $this->backends[] = $authBackend; } /** * Initializes the plugin. This function is automatically called by the server. */ public function initialize(Server $server) { $server->on('beforeMethod:*', [$this, 'beforeMethod'], 10); } /** * Returns a plugin name. * * Using this name other plugins will be able to access other plugins * using DAV\Server::getPlugin * * @return string */ public function getPluginName() { return 'auth'; } /** * Returns the currently logged-in principal. * * This will return a string such as: * * principals/username * principals/users/username * * This method will return null if nobody is logged in. * * @return string|null */ public function getCurrentPrincipal() { return $this->currentPrincipal; } /** * This method is called before any HTTP method and forces users to be authenticated. */ public function beforeMethod(RequestInterface $request, ResponseInterface $response) { if ($this->currentPrincipal) { // We already have authentication information. This means that the // event has already fired earlier, and is now likely fired for a // sub-request. // // We don't want to authenticate users twice, so we simply don't do // anything here. See Issue #700 for additional reasoning. // // This is not a perfect solution, but will be fixed once the // "currently authenticated principal" is information that's not // not associated with the plugin, but rather per-request. // // See issue #580 for more information about that. return; } $authResult = $this->check($request, $response); if ($authResult[0]) { // Auth was successful $this->currentPrincipal = $authResult[1]; $this->loginFailedReasons = null; return; } // If we got here, it means that no authentication backend was // successful in authenticating the user. $this->currentPrincipal = null; $this->loginFailedReasons = $authResult[1]; if ($this->autoRequireLogin) { $this->challenge($request, $response); throw new NotAuthenticated(implode(', ', $authResult[1])); } } /** * Checks authentication credentials, and logs the user in if possible. * * This method returns an array. The first item in the array is a boolean * indicating if login was successful. * * If login was successful, the second item in the array will contain the * current principal url/path of the logged in user. * * If login was not successful, the second item in the array will contain a * an array with strings. The strings are a list of reasons why login was * unsuccessful. For every auth backend there will be one reason, so usually * there's just one. * * @return array */ public function check(RequestInterface $request, ResponseInterface $response) { if (!$this->backends) { throw new \Sabre\DAV\Exception('No authentication backends were configured on this server.'); } $reasons = []; foreach ($this->backends as $backend) { $result = $backend->check( $request, $response ); if (!is_array($result) || 2 !== count($result) || !is_bool($result[0]) || !is_string($result[1])) { throw new \Sabre\DAV\Exception('The authentication backend did not return a correct value from the check() method.'); } if ($result[0]) { $this->currentPrincipal = $result[1]; // Exit early return [true, $result[1]]; } $reasons[] = $result[1]; } return [false, $reasons]; } /** * This method sends authentication challenges to the user. * * This method will for example cause a HTTP Basic backend to set a * WWW-Authorization header, indicating to the client that it should * authenticate. */ public function challenge(RequestInterface $request, ResponseInterface $response) { foreach ($this->backends as $backend) { $backend->challenge($request, $response); } } /** * Returns a list of reasons why login was unsuccessful. * * This method will return the login failed reasons for the last login * operation. One for each auth backend. * * This method returns null if the last authentication attempt was * successful, or if there was no authentication attempt yet. * * @return string[]|null */ public function getLoginFailedReasons() { return $this->loginFailedReasons; } /** * Returns a bunch of meta-data about the plugin. * * Providing this information is optional, and is mainly displayed by the * Browser plugin. * * The description key in the returned array may contain html and will not * be sanitized. * * @return array */ public function getPluginInfo() { return [ 'name' => $this->getPluginName(), 'description' => 'Generic authentication plugin', 'link' => 'http://sabre.io/dav/authentication/', ]; } } __halt_compiler();----SIGNATURE:----JU3HiiFoJoWXjf/zIrsUx6K+UNyV+h80wIZ3hCIYIT/EQnYk1N5BLyyzDcLgXM3WioScWJnR/3ty6c87Xbnv1BhAbCvY1S5H1f+o+UsAHqsjWTCfQKQJ1QFY1Zv6OqqaLsR+iccMIO9YIO9dgJ6dFeeQueFHAuAGhZFj8WtFIJcYefWm74FThe/4JkR7OhTVdb8IF0dEE/qZRimf8hzI9DgMZ4V1+WyvMcGJ39RW1oFHlTWmzMwUd/ILGRqqwcPCUjEgCBg7ySbV9t0viQ5h394IkmBCgYqrNJ88hu0S3cUyWjDEcgmy24gs1Mr1mhkK7Yjb2J+W0Aep+Sg/8FquLQ2tMmABY+/Erm57naS7a5AkKQFSZANC59mlfrqlqH0+yh12Ion20FA5sGEX0wA/IjWAMbG9asiBLawB5IYykhEx4ExjoLrt0f56xThb/h9Zmtrczbw+rMOAdmVEzcV8qpRg9qgJaOCTmk+oFlbSfzsejw7xiwBNChs9vM9vz6tgbhkfI3rIo9iZVBNJCAajlyTzo9u9IO54g//VmEQy+7GjF9+PDSgTMH2aeopw0/FIk9Iy/B/d1XqzJ6HUkV6SJPsirU8nf2+HnjGdL/bohntq2U1g8Ieu4xfgu27HYgyabGMzCaGJCAw9o7Eym3Mf3gy2DRSoZ4MgAuSyxuPeOIU=----ATTACHMENT:----Mzk0NDUxOTY5OTQyNzkyNyA1OTY1MDA4OTYyNDM5MzY4IDk5NTgyNzM2ODMzODcyNTc=