]> git.mxchange.org Git - friendica-addons.git/blob - dav/SabreDAV/lib/Sabre/DAV/Auth/Backend/AbstractDigest.php
9833928b9769c33257b46cb217f46f837d835e79
[friendica-addons.git] / dav / SabreDAV / lib / Sabre / DAV / Auth / Backend / AbstractDigest.php
1 <?php
2
3 /**
4  * HTTP Digest authentication backend class
5  *
6  * This class can be used by authentication objects wishing to use HTTP Digest
7  * Most of the digest logic is handled, implementors just need to worry about
8  * the getDigestHash method
9  *
10  * @package Sabre
11  * @subpackage DAV
12  * @copyright Copyright (C) 2007-2012 Rooftop Solutions. All rights reserved.
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_AbstractDigest implements Sabre_DAV_Auth_IBackend {
17
18     /**
19      * This variable holds the currently logged in username.
20      *
21      * @var array|null
22      */
23     protected $currentUser;
24
25     /**
26      * Returns a users digest hash based on the username and realm.
27      *
28      * If the user was not known, null must be returned.
29      *
30      * @param string $realm
31      * @param string $username
32      * @return string|null
33      */
34     abstract public function getDigestHash($realm, $username);
35
36     /**
37      * Authenticates the user based on the current request.
38      *
39      * If authentication is successful, true must be returned.
40      * If authentication fails, an exception must be thrown.
41      *
42      * @param Sabre_DAV_Server $server
43      * @param string $realm
44      * @throws Sabre_DAV_Exception_NotAuthenticated
45      * @return bool
46      */
47     public function authenticate(Sabre_DAV_Server $server, $realm) {
48
49         $digest = new Sabre_HTTP_DigestAuth();
50
51         // Hooking up request and response objects
52         $digest->setHTTPRequest($server->httpRequest);
53         $digest->setHTTPResponse($server->httpResponse);
54
55         $digest->setRealm($realm);
56         $digest->init();
57
58         $username = $digest->getUsername();
59
60         // No username was given
61         if (!$username) {
62             $digest->requireLogin();
63             throw new Sabre_DAV_Exception_NotAuthenticated('No digest authentication headers were found');
64         }
65
66         $hash = $this->getDigestHash($realm, $username);
67         // If this was false, the user account didn't exist
68         if ($hash===false || is_null($hash)) {
69             $digest->requireLogin();
70             throw new Sabre_DAV_Exception_NotAuthenticated('The supplied username was not on file');
71         }
72         if (!is_string($hash)) {
73             throw new Sabre_DAV_Exception('The returned value from getDigestHash must be a string or null');
74         }
75
76         // If this was false, the password or part of the hash was incorrect.
77         if (!$digest->validateA1($hash)) {
78             $digest->requireLogin();
79             throw new Sabre_DAV_Exception_NotAuthenticated('Incorrect username');
80         }
81
82         $this->currentUser = $username;
83         return true;
84
85     }
86
87     /**
88      * Returns the currently logged in username.
89      *
90      * @return string|null
91      */
92     public function getCurrentUser() {
93
94         return $this->currentUser;
95
96     }
97
98 }