]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/lib/Sabre/DAV/Auth/Backend/AbstractBasic.php
Initial Release of the calendar plugin
[friendica-addons.git] / dav / SabreDAV / lib / Sabre / DAV / Auth / Backend / AbstractBasic.php
1 <?php
2 /**
3  * HTTP Basic authentication backend class
4  *
5  * This class can be used by authentication objects wishing to use HTTP Basic
6  * Most of the digest logic is handled, implementors just need to worry about
7  * the validateUserPass method.
8  *
9  * @package Sabre
10  * @subpackage DAV
11  * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
12  * @author James David Low (http://jameslow.com/)
13  * @author Evert Pot (http://www.rooftopsolutions.nl/)
14  * @license http://code.google.com/p/sabredav/wiki/License Modified BSD License
15  */
16 abstract class Sabre_DAV_Auth_Backend_AbstractBasic implements Sabre_DAV_Auth_IBackend {
17
18     /**
19      * This variable holds the currently logged in username.
20      *
21      * @var string|null
22      */
23     protected $currentUser;
24
25     /**
26      * Validates a username and password
27      *
28      * This method should return true or false depending on if login
29      * succeeded.
30      *
31      * @param string $username
32      * @param string $password
33      * @return bool
34      */
35     abstract protected function validateUserPass($username, $password);
36
37     /**
38      * Returns information about the currently logged in username.
39      *
40      * If nobody is currently logged in, this method should return null.
41      *
42      * @return string|null
43      */
44     public function getCurrentUser() {
45         return $this->currentUser;
46     }
47
48
49     /**
50      * Authenticates the user based on the current request.
51      *
52      * If authentication is successful, true must be returned.
53      * If authentication fails, an exception must be thrown.
54      *
55      * @param Sabre_DAV_Server $server
56      * @param string $realm
57      * @throws Sabre_DAV_Exception_NotAuthenticated
58      * @return bool
59      */
60     public function authenticate(Sabre_DAV_Server $server, $realm) {
61
62         $auth = new Sabre_HTTP_BasicAuth();
63         $auth->setHTTPRequest($server->httpRequest);
64         $auth->setHTTPResponse($server->httpResponse);
65         $auth->setRealm($realm);
66         $userpass = $auth->getUserPass();
67         if (!$userpass) {
68             $auth->requireLogin();
69             throw new Sabre_DAV_Exception_NotAuthenticated('No basic authentication headers were found');
70         }
71
72         // Authenticates the user
73         if (!$this->validateUserPass($userpass[0],$userpass[1])) {
74             $auth->requireLogin();
75             throw new Sabre_DAV_Exception_NotAuthenticated('Username or password does not match');
76         }
77         $this->currentUser = $userpass[0];
78         return true;
79     }
80
81
82 }
83