@@ -18,155 +18,155 @@ |
||
18 | 18 | */ |
19 | 19 | class PDO extends AbstractBackend |
20 | 20 | { |
21 | - /** |
|
22 | - * The PDO tablename this backend uses. |
|
23 | - * |
|
24 | - * @var string |
|
25 | - */ |
|
26 | - public $tableName = 'locks'; |
|
27 | - |
|
28 | - /** |
|
29 | - * The PDO connection object. |
|
30 | - * |
|
31 | - * @var pdo |
|
32 | - */ |
|
33 | - protected $pdo; |
|
34 | - |
|
35 | - /** |
|
36 | - * Constructor. |
|
37 | - */ |
|
38 | - public function __construct(\PDO $pdo) |
|
39 | - { |
|
40 | - $this->pdo = $pdo; |
|
41 | - } |
|
42 | - |
|
43 | - /** |
|
44 | - * Returns a list of Sabre\DAV\Locks\LockInfo objects. |
|
45 | - * |
|
46 | - * This method should return all the locks for a particular uri, including |
|
47 | - * locks that might be set on a parent uri. |
|
48 | - * |
|
49 | - * If returnChildLocks is set to true, this method should also look for |
|
50 | - * any locks in the subtree of the uri for locks. |
|
51 | - * |
|
52 | - * @param string $uri |
|
53 | - * @param bool $returnChildLocks |
|
54 | - * |
|
55 | - * @return array |
|
56 | - */ |
|
57 | - public function getLocks($uri, $returnChildLocks) |
|
58 | - { |
|
59 | - // NOTE: the following 10 lines or so could be easily replaced by |
|
60 | - // pure sql. MySQL's non-standard string concatenation prevents us |
|
61 | - // from doing this though. |
|
62 | - $query = 'SELECT owner, token, timeout, created, scope, depth, uri FROM '.$this->tableName.' WHERE (created > (? - timeout)) AND ((uri = ?)'; |
|
63 | - $params = [time(), $uri]; |
|
64 | - |
|
65 | - // We need to check locks for every part in the uri. |
|
66 | - $uriParts = explode('/', $uri); |
|
67 | - |
|
68 | - // We already covered the last part of the uri |
|
69 | - array_pop($uriParts); |
|
70 | - |
|
71 | - $currentPath = ''; |
|
72 | - |
|
73 | - foreach ($uriParts as $part) { |
|
74 | - if ($currentPath) { |
|
75 | - $currentPath .= '/'; |
|
76 | - } |
|
77 | - $currentPath .= $part; |
|
78 | - |
|
79 | - $query .= ' OR (depth!=0 AND uri = ?)'; |
|
80 | - $params[] = $currentPath; |
|
81 | - } |
|
82 | - |
|
83 | - if ($returnChildLocks) { |
|
84 | - $query .= ' OR (uri LIKE ?)'; |
|
85 | - $params[] = $uri.'/%'; |
|
86 | - } |
|
87 | - $query .= ')'; |
|
88 | - |
|
89 | - $stmt = $this->pdo->prepare($query); |
|
90 | - $stmt->execute($params); |
|
91 | - $result = $stmt->fetchAll(); |
|
92 | - |
|
93 | - $lockList = []; |
|
94 | - foreach ($result as $row) { |
|
95 | - $lockInfo = new LockInfo(); |
|
96 | - $lockInfo->owner = $row['owner']; |
|
97 | - $lockInfo->token = $row['token']; |
|
98 | - $lockInfo->timeout = $row['timeout']; |
|
99 | - $lockInfo->created = $row['created']; |
|
100 | - $lockInfo->scope = $row['scope']; |
|
101 | - $lockInfo->depth = $row['depth']; |
|
102 | - $lockInfo->uri = $row['uri']; |
|
103 | - $lockList[] = $lockInfo; |
|
104 | - } |
|
105 | - |
|
106 | - return $lockList; |
|
107 | - } |
|
108 | - |
|
109 | - /** |
|
110 | - * Locks a uri. |
|
111 | - * |
|
112 | - * @param string $uri |
|
113 | - * |
|
114 | - * @return bool |
|
115 | - */ |
|
116 | - public function lock($uri, LockInfo $lockInfo) |
|
117 | - { |
|
118 | - // We're making the lock timeout 30 minutes |
|
119 | - $lockInfo->timeout = 30 * 60; |
|
120 | - $lockInfo->created = time(); |
|
121 | - $lockInfo->uri = $uri; |
|
122 | - |
|
123 | - $locks = $this->getLocks($uri, false); |
|
124 | - $exists = false; |
|
125 | - foreach ($locks as $lock) { |
|
126 | - if ($lock->token == $lockInfo->token) { |
|
127 | - $exists = true; |
|
128 | - } |
|
129 | - } |
|
130 | - |
|
131 | - if ($exists) { |
|
132 | - $stmt = $this->pdo->prepare('UPDATE '.$this->tableName.' SET owner = ?, timeout = ?, scope = ?, depth = ?, uri = ?, created = ? WHERE token = ?'); |
|
133 | - $stmt->execute([ |
|
134 | - $lockInfo->owner, |
|
135 | - $lockInfo->timeout, |
|
136 | - $lockInfo->scope, |
|
137 | - $lockInfo->depth, |
|
138 | - $uri, |
|
139 | - $lockInfo->created, |
|
140 | - $lockInfo->token, |
|
141 | - ]); |
|
142 | - } else { |
|
143 | - $stmt = $this->pdo->prepare('INSERT INTO '.$this->tableName.' (owner,timeout,scope,depth,uri,created,token) VALUES (?,?,?,?,?,?,?)'); |
|
144 | - $stmt->execute([ |
|
145 | - $lockInfo->owner, |
|
146 | - $lockInfo->timeout, |
|
147 | - $lockInfo->scope, |
|
148 | - $lockInfo->depth, |
|
149 | - $uri, |
|
150 | - $lockInfo->created, |
|
151 | - $lockInfo->token, |
|
152 | - ]); |
|
153 | - } |
|
154 | - |
|
155 | - return true; |
|
156 | - } |
|
157 | - |
|
158 | - /** |
|
159 | - * Removes a lock from a uri. |
|
160 | - * |
|
161 | - * @param string $uri |
|
162 | - * |
|
163 | - * @return bool |
|
164 | - */ |
|
165 | - public function unlock($uri, LockInfo $lockInfo) |
|
166 | - { |
|
167 | - $stmt = $this->pdo->prepare('DELETE FROM '.$this->tableName.' WHERE uri = ? AND token = ?'); |
|
168 | - $stmt->execute([$uri, $lockInfo->token]); |
|
169 | - |
|
170 | - return 1 === $stmt->rowCount(); |
|
171 | - } |
|
21 | + /** |
|
22 | + * The PDO tablename this backend uses. |
|
23 | + * |
|
24 | + * @var string |
|
25 | + */ |
|
26 | + public $tableName = 'locks'; |
|
27 | + |
|
28 | + /** |
|
29 | + * The PDO connection object. |
|
30 | + * |
|
31 | + * @var pdo |
|
32 | + */ |
|
33 | + protected $pdo; |
|
34 | + |
|
35 | + /** |
|
36 | + * Constructor. |
|
37 | + */ |
|
38 | + public function __construct(\PDO $pdo) |
|
39 | + { |
|
40 | + $this->pdo = $pdo; |
|
41 | + } |
|
42 | + |
|
43 | + /** |
|
44 | + * Returns a list of Sabre\DAV\Locks\LockInfo objects. |
|
45 | + * |
|
46 | + * This method should return all the locks for a particular uri, including |
|
47 | + * locks that might be set on a parent uri. |
|
48 | + * |
|
49 | + * If returnChildLocks is set to true, this method should also look for |
|
50 | + * any locks in the subtree of the uri for locks. |
|
51 | + * |
|
52 | + * @param string $uri |
|
53 | + * @param bool $returnChildLocks |
|
54 | + * |
|
55 | + * @return array |
|
56 | + */ |
|
57 | + public function getLocks($uri, $returnChildLocks) |
|
58 | + { |
|
59 | + // NOTE: the following 10 lines or so could be easily replaced by |
|
60 | + // pure sql. MySQL's non-standard string concatenation prevents us |
|
61 | + // from doing this though. |
|
62 | + $query = 'SELECT owner, token, timeout, created, scope, depth, uri FROM '.$this->tableName.' WHERE (created > (? - timeout)) AND ((uri = ?)'; |
|
63 | + $params = [time(), $uri]; |
|
64 | + |
|
65 | + // We need to check locks for every part in the uri. |
|
66 | + $uriParts = explode('/', $uri); |
|
67 | + |
|
68 | + // We already covered the last part of the uri |
|
69 | + array_pop($uriParts); |
|
70 | + |
|
71 | + $currentPath = ''; |
|
72 | + |
|
73 | + foreach ($uriParts as $part) { |
|
74 | + if ($currentPath) { |
|
75 | + $currentPath .= '/'; |
|
76 | + } |
|
77 | + $currentPath .= $part; |
|
78 | + |
|
79 | + $query .= ' OR (depth!=0 AND uri = ?)'; |
|
80 | + $params[] = $currentPath; |
|
81 | + } |
|
82 | + |
|
83 | + if ($returnChildLocks) { |
|
84 | + $query .= ' OR (uri LIKE ?)'; |
|
85 | + $params[] = $uri.'/%'; |
|
86 | + } |
|
87 | + $query .= ')'; |
|
88 | + |
|
89 | + $stmt = $this->pdo->prepare($query); |
|
90 | + $stmt->execute($params); |
|
91 | + $result = $stmt->fetchAll(); |
|
92 | + |
|
93 | + $lockList = []; |
|
94 | + foreach ($result as $row) { |
|
95 | + $lockInfo = new LockInfo(); |
|
96 | + $lockInfo->owner = $row['owner']; |
|
97 | + $lockInfo->token = $row['token']; |
|
98 | + $lockInfo->timeout = $row['timeout']; |
|
99 | + $lockInfo->created = $row['created']; |
|
100 | + $lockInfo->scope = $row['scope']; |
|
101 | + $lockInfo->depth = $row['depth']; |
|
102 | + $lockInfo->uri = $row['uri']; |
|
103 | + $lockList[] = $lockInfo; |
|
104 | + } |
|
105 | + |
|
106 | + return $lockList; |
|
107 | + } |
|
108 | + |
|
109 | + /** |
|
110 | + * Locks a uri. |
|
111 | + * |
|
112 | + * @param string $uri |
|
113 | + * |
|
114 | + * @return bool |
|
115 | + */ |
|
116 | + public function lock($uri, LockInfo $lockInfo) |
|
117 | + { |
|
118 | + // We're making the lock timeout 30 minutes |
|
119 | + $lockInfo->timeout = 30 * 60; |
|
120 | + $lockInfo->created = time(); |
|
121 | + $lockInfo->uri = $uri; |
|
122 | + |
|
123 | + $locks = $this->getLocks($uri, false); |
|
124 | + $exists = false; |
|
125 | + foreach ($locks as $lock) { |
|
126 | + if ($lock->token == $lockInfo->token) { |
|
127 | + $exists = true; |
|
128 | + } |
|
129 | + } |
|
130 | + |
|
131 | + if ($exists) { |
|
132 | + $stmt = $this->pdo->prepare('UPDATE '.$this->tableName.' SET owner = ?, timeout = ?, scope = ?, depth = ?, uri = ?, created = ? WHERE token = ?'); |
|
133 | + $stmt->execute([ |
|
134 | + $lockInfo->owner, |
|
135 | + $lockInfo->timeout, |
|
136 | + $lockInfo->scope, |
|
137 | + $lockInfo->depth, |
|
138 | + $uri, |
|
139 | + $lockInfo->created, |
|
140 | + $lockInfo->token, |
|
141 | + ]); |
|
142 | + } else { |
|
143 | + $stmt = $this->pdo->prepare('INSERT INTO '.$this->tableName.' (owner,timeout,scope,depth,uri,created,token) VALUES (?,?,?,?,?,?,?)'); |
|
144 | + $stmt->execute([ |
|
145 | + $lockInfo->owner, |
|
146 | + $lockInfo->timeout, |
|
147 | + $lockInfo->scope, |
|
148 | + $lockInfo->depth, |
|
149 | + $uri, |
|
150 | + $lockInfo->created, |
|
151 | + $lockInfo->token, |
|
152 | + ]); |
|
153 | + } |
|
154 | + |
|
155 | + return true; |
|
156 | + } |
|
157 | + |
|
158 | + /** |
|
159 | + * Removes a lock from a uri. |
|
160 | + * |
|
161 | + * @param string $uri |
|
162 | + * |
|
163 | + * @return bool |
|
164 | + */ |
|
165 | + public function unlock($uri, LockInfo $lockInfo) |
|
166 | + { |
|
167 | + $stmt = $this->pdo->prepare('DELETE FROM '.$this->tableName.' WHERE uri = ? AND token = ?'); |
|
168 | + $stmt->execute([$uri, $lockInfo->token]); |
|
169 | + |
|
170 | + return 1 === $stmt->rowCount(); |
|
171 | + } |
|
172 | 172 | } |
@@ -16,67 +16,67 @@ |
||
16 | 16 | */ |
17 | 17 | class LockInfo |
18 | 18 | { |
19 | - /** |
|
20 | - * A shared lock. |
|
21 | - */ |
|
22 | - const SHARED = 1; |
|
19 | + /** |
|
20 | + * A shared lock. |
|
21 | + */ |
|
22 | + const SHARED = 1; |
|
23 | 23 | |
24 | - /** |
|
25 | - * An exclusive lock. |
|
26 | - */ |
|
27 | - const EXCLUSIVE = 2; |
|
24 | + /** |
|
25 | + * An exclusive lock. |
|
26 | + */ |
|
27 | + const EXCLUSIVE = 2; |
|
28 | 28 | |
29 | - /** |
|
30 | - * A never expiring timeout. |
|
31 | - */ |
|
32 | - const TIMEOUT_INFINITE = -1; |
|
29 | + /** |
|
30 | + * A never expiring timeout. |
|
31 | + */ |
|
32 | + const TIMEOUT_INFINITE = -1; |
|
33 | 33 | |
34 | - /** |
|
35 | - * The owner of the lock. |
|
36 | - * |
|
37 | - * @var string |
|
38 | - */ |
|
39 | - public $owner; |
|
34 | + /** |
|
35 | + * The owner of the lock. |
|
36 | + * |
|
37 | + * @var string |
|
38 | + */ |
|
39 | + public $owner; |
|
40 | 40 | |
41 | - /** |
|
42 | - * The locktoken. |
|
43 | - * |
|
44 | - * @var string |
|
45 | - */ |
|
46 | - public $token; |
|
41 | + /** |
|
42 | + * The locktoken. |
|
43 | + * |
|
44 | + * @var string |
|
45 | + */ |
|
46 | + public $token; |
|
47 | 47 | |
48 | - /** |
|
49 | - * How long till the lock is expiring. |
|
50 | - * |
|
51 | - * @var int |
|
52 | - */ |
|
53 | - public $timeout; |
|
48 | + /** |
|
49 | + * How long till the lock is expiring. |
|
50 | + * |
|
51 | + * @var int |
|
52 | + */ |
|
53 | + public $timeout; |
|
54 | 54 | |
55 | - /** |
|
56 | - * UNIX Timestamp of when this lock was created. |
|
57 | - * |
|
58 | - * @var int |
|
59 | - */ |
|
60 | - public $created; |
|
55 | + /** |
|
56 | + * UNIX Timestamp of when this lock was created. |
|
57 | + * |
|
58 | + * @var int |
|
59 | + */ |
|
60 | + public $created; |
|
61 | 61 | |
62 | - /** |
|
63 | - * Exclusive or shared lock. |
|
64 | - * |
|
65 | - * @var int |
|
66 | - */ |
|
67 | - public $scope = self::EXCLUSIVE; |
|
62 | + /** |
|
63 | + * Exclusive or shared lock. |
|
64 | + * |
|
65 | + * @var int |
|
66 | + */ |
|
67 | + public $scope = self::EXCLUSIVE; |
|
68 | 68 | |
69 | - /** |
|
70 | - * Depth of lock, can be 0 or Sabre\DAV\Server::DEPTH_INFINITY. |
|
71 | - */ |
|
72 | - public $depth = 0; |
|
69 | + /** |
|
70 | + * Depth of lock, can be 0 or Sabre\DAV\Server::DEPTH_INFINITY. |
|
71 | + */ |
|
72 | + public $depth = 0; |
|
73 | 73 | |
74 | - /** |
|
75 | - * The uri this lock locks. |
|
76 | - * |
|
77 | - * TODO: This value is not always set |
|
78 | - * |
|
79 | - * @var mixed |
|
80 | - */ |
|
81 | - public $uri; |
|
74 | + /** |
|
75 | + * The uri this lock locks. |
|
76 | + * |
|
77 | + * TODO: This value is not always set |
|
78 | + * |
|
79 | + * @var mixed |
|
80 | + */ |
|
81 | + public $uri; |
|
82 | 82 | } |
@@ -15,32 +15,32 @@ |
||
15 | 15 | */ |
16 | 16 | interface IProperties extends INode |
17 | 17 | { |
18 | - /** |
|
19 | - * Updates properties on this node. |
|
20 | - * |
|
21 | - * This method received a PropPatch object, which contains all the |
|
22 | - * information about the update. |
|
23 | - * |
|
24 | - * To update specific properties, call the 'handle' method on this object. |
|
25 | - * Read the PropPatch documentation for more information. |
|
26 | - */ |
|
27 | - public function propPatch(PropPatch $propPatch); |
|
18 | + /** |
|
19 | + * Updates properties on this node. |
|
20 | + * |
|
21 | + * This method received a PropPatch object, which contains all the |
|
22 | + * information about the update. |
|
23 | + * |
|
24 | + * To update specific properties, call the 'handle' method on this object. |
|
25 | + * Read the PropPatch documentation for more information. |
|
26 | + */ |
|
27 | + public function propPatch(PropPatch $propPatch); |
|
28 | 28 | |
29 | - /** |
|
30 | - * Returns a list of properties for this nodes. |
|
31 | - * |
|
32 | - * The properties list is a list of propertynames the client requested, |
|
33 | - * encoded in clark-notation {xmlnamespace}tagname |
|
34 | - * |
|
35 | - * If the array is empty, it means 'all properties' were requested. |
|
36 | - * |
|
37 | - * Note that it's fine to liberally give properties back, instead of |
|
38 | - * conforming to the list of requested properties. |
|
39 | - * The Server class will filter out the extra. |
|
40 | - * |
|
41 | - * @param array $properties |
|
42 | - * |
|
43 | - * @return array |
|
44 | - */ |
|
45 | - public function getProperties($properties); |
|
29 | + /** |
|
30 | + * Returns a list of properties for this nodes. |
|
31 | + * |
|
32 | + * The properties list is a list of propertynames the client requested, |
|
33 | + * encoded in clark-notation {xmlnamespace}tagname |
|
34 | + * |
|
35 | + * If the array is empty, it means 'all properties' were requested. |
|
36 | + * |
|
37 | + * Note that it's fine to liberally give properties back, instead of |
|
38 | + * conforming to the list of requested properties. |
|
39 | + * The Server class will filter out the extra. |
|
40 | + * |
|
41 | + * @param array $properties |
|
42 | + * |
|
43 | + * @return array |
|
44 | + */ |
|
45 | + public function getProperties($properties); |
|
46 | 46 | } |
@@ -26,230 +26,230 @@ |
||
26 | 26 | */ |
27 | 27 | class Plugin extends ServerPlugin |
28 | 28 | { |
29 | - /** |
|
30 | - * By default this plugin will require that the user is authenticated, |
|
31 | - * and refuse any access if the user is not authenticated. |
|
32 | - * |
|
33 | - * If this setting is set to false, we let the user through, whether they |
|
34 | - * are authenticated or not. |
|
35 | - * |
|
36 | - * This is useful if you want to allow both authenticated and |
|
37 | - * unauthenticated access to your server. |
|
38 | - * |
|
39 | - * @param bool |
|
40 | - */ |
|
41 | - public $autoRequireLogin = true; |
|
42 | - |
|
43 | - /** |
|
44 | - * authentication backends. |
|
45 | - */ |
|
46 | - protected $backends; |
|
47 | - |
|
48 | - /** |
|
49 | - * The currently logged in principal. Will be `null` if nobody is currently |
|
50 | - * logged in. |
|
51 | - * |
|
52 | - * @var string|null |
|
53 | - */ |
|
54 | - protected $currentPrincipal; |
|
55 | - |
|
56 | - /** |
|
57 | - * Creates the authentication plugin. |
|
58 | - * |
|
59 | - * @param Backend\BackendInterface $authBackend |
|
60 | - */ |
|
61 | - public function __construct(Backend\BackendInterface $authBackend = null) |
|
62 | - { |
|
63 | - if (!is_null($authBackend)) { |
|
64 | - $this->addBackend($authBackend); |
|
65 | - } |
|
66 | - } |
|
67 | - |
|
68 | - /** |
|
69 | - * Adds an authentication backend to the plugin. |
|
70 | - */ |
|
71 | - public function addBackend(Backend\BackendInterface $authBackend) |
|
72 | - { |
|
73 | - $this->backends[] = $authBackend; |
|
74 | - } |
|
75 | - |
|
76 | - /** |
|
77 | - * Initializes the plugin. This function is automatically called by the server. |
|
78 | - */ |
|
79 | - public function initialize(Server $server) |
|
80 | - { |
|
81 | - $server->on('beforeMethod:*', [$this, 'beforeMethod'], 10); |
|
82 | - } |
|
83 | - |
|
84 | - /** |
|
85 | - * Returns a plugin name. |
|
86 | - * |
|
87 | - * Using this name other plugins will be able to access other plugins |
|
88 | - * using DAV\Server::getPlugin |
|
89 | - * |
|
90 | - * @return string |
|
91 | - */ |
|
92 | - public function getPluginName() |
|
93 | - { |
|
94 | - return 'auth'; |
|
95 | - } |
|
96 | - |
|
97 | - /** |
|
98 | - * Returns the currently logged-in principal. |
|
99 | - * |
|
100 | - * This will return a string such as: |
|
101 | - * |
|
102 | - * principals/username |
|
103 | - * principals/users/username |
|
104 | - * |
|
105 | - * This method will return null if nobody is logged in. |
|
106 | - * |
|
107 | - * @return string|null |
|
108 | - */ |
|
109 | - public function getCurrentPrincipal() |
|
110 | - { |
|
111 | - return $this->currentPrincipal; |
|
112 | - } |
|
113 | - |
|
114 | - /** |
|
115 | - * This method is called before any HTTP method and forces users to be authenticated. |
|
116 | - */ |
|
117 | - public function beforeMethod(RequestInterface $request, ResponseInterface $response) |
|
118 | - { |
|
119 | - if ($this->currentPrincipal) { |
|
120 | - // We already have authentication information. This means that the |
|
121 | - // event has already fired earlier, and is now likely fired for a |
|
122 | - // sub-request. |
|
123 | - // |
|
124 | - // We don't want to authenticate users twice, so we simply don't do |
|
125 | - // anything here. See Issue #700 for additional reasoning. |
|
126 | - // |
|
127 | - // This is not a perfect solution, but will be fixed once the |
|
128 | - // "currently authenticated principal" is information that's not |
|
129 | - // not associated with the plugin, but rather per-request. |
|
130 | - // |
|
131 | - // See issue #580 for more information about that. |
|
132 | - return; |
|
133 | - } |
|
134 | - |
|
135 | - $authResult = $this->check($request, $response); |
|
136 | - |
|
137 | - if ($authResult[0]) { |
|
138 | - // Auth was successful |
|
139 | - $this->currentPrincipal = $authResult[1]; |
|
140 | - $this->loginFailedReasons = null; |
|
141 | - |
|
142 | - return; |
|
143 | - } |
|
144 | - |
|
145 | - // If we got here, it means that no authentication backend was |
|
146 | - // successful in authenticating the user. |
|
147 | - $this->currentPrincipal = null; |
|
148 | - $this->loginFailedReasons = $authResult[1]; |
|
149 | - |
|
150 | - if ($this->autoRequireLogin) { |
|
151 | - $this->challenge($request, $response); |
|
152 | - throw new NotAuthenticated(implode(', ', $authResult[1])); |
|
153 | - } |
|
154 | - } |
|
155 | - |
|
156 | - /** |
|
157 | - * Checks authentication credentials, and logs the user in if possible. |
|
158 | - * |
|
159 | - * This method returns an array. The first item in the array is a boolean |
|
160 | - * indicating if login was successful. |
|
161 | - * |
|
162 | - * If login was successful, the second item in the array will contain the |
|
163 | - * current principal url/path of the logged in user. |
|
164 | - * |
|
165 | - * If login was not successful, the second item in the array will contain a |
|
166 | - * an array with strings. The strings are a list of reasons why login was |
|
167 | - * unsuccessful. For every auth backend there will be one reason, so usually |
|
168 | - * there's just one. |
|
169 | - * |
|
170 | - * @return array |
|
171 | - */ |
|
172 | - public function check(RequestInterface $request, ResponseInterface $response) |
|
173 | - { |
|
174 | - if (!$this->backends) { |
|
175 | - throw new \Sabre\DAV\Exception('No authentication backends were configured on this server.'); |
|
176 | - } |
|
177 | - $reasons = []; |
|
178 | - foreach ($this->backends as $backend) { |
|
179 | - $result = $backend->check( |
|
180 | - $request, |
|
181 | - $response |
|
182 | - ); |
|
183 | - |
|
184 | - if (!is_array($result) || 2 !== count($result) || !is_bool($result[0]) || !is_string($result[1])) { |
|
185 | - throw new \Sabre\DAV\Exception('The authentication backend did not return a correct value from the check() method.'); |
|
186 | - } |
|
187 | - |
|
188 | - if ($result[0]) { |
|
189 | - $this->currentPrincipal = $result[1]; |
|
190 | - // Exit early |
|
191 | - return [true, $result[1]]; |
|
192 | - } |
|
193 | - $reasons[] = $result[1]; |
|
194 | - } |
|
195 | - |
|
196 | - return [false, $reasons]; |
|
197 | - } |
|
198 | - |
|
199 | - /** |
|
200 | - * This method sends authentication challenges to the user. |
|
201 | - * |
|
202 | - * This method will for example cause a HTTP Basic backend to set a |
|
203 | - * WWW-Authorization header, indicating to the client that it should |
|
204 | - * authenticate. |
|
205 | - */ |
|
206 | - public function challenge(RequestInterface $request, ResponseInterface $response) |
|
207 | - { |
|
208 | - foreach ($this->backends as $backend) { |
|
209 | - $backend->challenge($request, $response); |
|
210 | - } |
|
211 | - } |
|
212 | - |
|
213 | - /** |
|
214 | - * List of reasons why login failed for the last login operation. |
|
215 | - * |
|
216 | - * @var string[]|null |
|
217 | - */ |
|
218 | - protected $loginFailedReasons; |
|
219 | - |
|
220 | - /** |
|
221 | - * Returns a list of reasons why login was unsuccessful. |
|
222 | - * |
|
223 | - * This method will return the login failed reasons for the last login |
|
224 | - * operation. One for each auth backend. |
|
225 | - * |
|
226 | - * This method returns null if the last authentication attempt was |
|
227 | - * successful, or if there was no authentication attempt yet. |
|
228 | - * |
|
229 | - * @return string[]|null |
|
230 | - */ |
|
231 | - public function getLoginFailedReasons() |
|
232 | - { |
|
233 | - return $this->loginFailedReasons; |
|
234 | - } |
|
235 | - |
|
236 | - /** |
|
237 | - * Returns a bunch of meta-data about the plugin. |
|
238 | - * |
|
239 | - * Providing this information is optional, and is mainly displayed by the |
|
240 | - * Browser plugin. |
|
241 | - * |
|
242 | - * The description key in the returned array may contain html and will not |
|
243 | - * be sanitized. |
|
244 | - * |
|
245 | - * @return array |
|
246 | - */ |
|
247 | - public function getPluginInfo() |
|
248 | - { |
|
249 | - return [ |
|
250 | - 'name' => $this->getPluginName(), |
|
251 | - 'description' => 'Generic authentication plugin', |
|
252 | - 'link' => 'http://sabre.io/dav/authentication/', |
|
253 | - ]; |
|
254 | - } |
|
29 | + /** |
|
30 | + * By default this plugin will require that the user is authenticated, |
|
31 | + * and refuse any access if the user is not authenticated. |
|
32 | + * |
|
33 | + * If this setting is set to false, we let the user through, whether they |
|
34 | + * are authenticated or not. |
|
35 | + * |
|
36 | + * This is useful if you want to allow both authenticated and |
|
37 | + * unauthenticated access to your server. |
|
38 | + * |
|
39 | + * @param bool |
|
40 | + */ |
|
41 | + public $autoRequireLogin = true; |
|
42 | + |
|
43 | + /** |
|
44 | + * authentication backends. |
|
45 | + */ |
|
46 | + protected $backends; |
|
47 | + |
|
48 | + /** |
|
49 | + * The currently logged in principal. Will be `null` if nobody is currently |
|
50 | + * logged in. |
|
51 | + * |
|
52 | + * @var string|null |
|
53 | + */ |
|
54 | + protected $currentPrincipal; |
|
55 | + |
|
56 | + /** |
|
57 | + * Creates the authentication plugin. |
|
58 | + * |
|
59 | + * @param Backend\BackendInterface $authBackend |
|
60 | + */ |
|
61 | + public function __construct(Backend\BackendInterface $authBackend = null) |
|
62 | + { |
|
63 | + if (!is_null($authBackend)) { |
|
64 | + $this->addBackend($authBackend); |
|
65 | + } |
|
66 | + } |
|
67 | + |
|
68 | + /** |
|
69 | + * Adds an authentication backend to the plugin. |
|
70 | + */ |
|
71 | + public function addBackend(Backend\BackendInterface $authBackend) |
|
72 | + { |
|
73 | + $this->backends[] = $authBackend; |
|
74 | + } |
|
75 | + |
|
76 | + /** |
|
77 | + * Initializes the plugin. This function is automatically called by the server. |
|
78 | + */ |
|
79 | + public function initialize(Server $server) |
|
80 | + { |
|
81 | + $server->on('beforeMethod:*', [$this, 'beforeMethod'], 10); |
|
82 | + } |
|
83 | + |
|
84 | + /** |
|
85 | + * Returns a plugin name. |
|
86 | + * |
|
87 | + * Using this name other plugins will be able to access other plugins |
|
88 | + * using DAV\Server::getPlugin |
|
89 | + * |
|
90 | + * @return string |
|
91 | + */ |
|
92 | + public function getPluginName() |
|
93 | + { |
|
94 | + return 'auth'; |
|
95 | + } |
|
96 | + |
|
97 | + /** |
|
98 | + * Returns the currently logged-in principal. |
|
99 | + * |
|
100 | + * This will return a string such as: |
|
101 | + * |
|
102 | + * principals/username |
|
103 | + * principals/users/username |
|
104 | + * |
|
105 | + * This method will return null if nobody is logged in. |
|
106 | + * |
|
107 | + * @return string|null |
|
108 | + */ |
|
109 | + public function getCurrentPrincipal() |
|
110 | + { |
|
111 | + return $this->currentPrincipal; |
|
112 | + } |
|
113 | + |
|
114 | + /** |
|
115 | + * This method is called before any HTTP method and forces users to be authenticated. |
|
116 | + */ |
|
117 | + public function beforeMethod(RequestInterface $request, ResponseInterface $response) |
|
118 | + { |
|
119 | + if ($this->currentPrincipal) { |
|
120 | + // We already have authentication information. This means that the |
|
121 | + // event has already fired earlier, and is now likely fired for a |
|
122 | + // sub-request. |
|
123 | + // |
|
124 | + // We don't want to authenticate users twice, so we simply don't do |
|
125 | + // anything here. See Issue #700 for additional reasoning. |
|
126 | + // |
|
127 | + // This is not a perfect solution, but will be fixed once the |
|
128 | + // "currently authenticated principal" is information that's not |
|
129 | + // not associated with the plugin, but rather per-request. |
|
130 | + // |
|
131 | + // See issue #580 for more information about that. |
|
132 | + return; |
|
133 | + } |
|
134 | + |
|
135 | + $authResult = $this->check($request, $response); |
|
136 | + |
|
137 | + if ($authResult[0]) { |
|
138 | + // Auth was successful |
|
139 | + $this->currentPrincipal = $authResult[1]; |
|
140 | + $this->loginFailedReasons = null; |
|
141 | + |
|
142 | + return; |
|
143 | + } |
|
144 | + |
|
145 | + // If we got here, it means that no authentication backend was |
|
146 | + // successful in authenticating the user. |
|
147 | + $this->currentPrincipal = null; |
|
148 | + $this->loginFailedReasons = $authResult[1]; |
|
149 | + |
|
150 | + if ($this->autoRequireLogin) { |
|
151 | + $this->challenge($request, $response); |
|
152 | + throw new NotAuthenticated(implode(', ', $authResult[1])); |
|
153 | + } |
|
154 | + } |
|
155 | + |
|
156 | + /** |
|
157 | + * Checks authentication credentials, and logs the user in if possible. |
|
158 | + * |
|
159 | + * This method returns an array. The first item in the array is a boolean |
|
160 | + * indicating if login was successful. |
|
161 | + * |
|
162 | + * If login was successful, the second item in the array will contain the |
|
163 | + * current principal url/path of the logged in user. |
|
164 | + * |
|
165 | + * If login was not successful, the second item in the array will contain a |
|
166 | + * an array with strings. The strings are a list of reasons why login was |
|
167 | + * unsuccessful. For every auth backend there will be one reason, so usually |
|
168 | + * there's just one. |
|
169 | + * |
|
170 | + * @return array |
|
171 | + */ |
|
172 | + public function check(RequestInterface $request, ResponseInterface $response) |
|
173 | + { |
|
174 | + if (!$this->backends) { |
|
175 | + throw new \Sabre\DAV\Exception('No authentication backends were configured on this server.'); |
|
176 | + } |
|
177 | + $reasons = []; |
|
178 | + foreach ($this->backends as $backend) { |
|
179 | + $result = $backend->check( |
|
180 | + $request, |
|
181 | + $response |
|
182 | + ); |
|
183 | + |
|
184 | + if (!is_array($result) || 2 !== count($result) || !is_bool($result[0]) || !is_string($result[1])) { |
|
185 | + throw new \Sabre\DAV\Exception('The authentication backend did not return a correct value from the check() method.'); |
|
186 | + } |
|
187 | + |
|
188 | + if ($result[0]) { |
|
189 | + $this->currentPrincipal = $result[1]; |
|
190 | + // Exit early |
|
191 | + return [true, $result[1]]; |
|
192 | + } |
|
193 | + $reasons[] = $result[1]; |
|
194 | + } |
|
195 | + |
|
196 | + return [false, $reasons]; |
|
197 | + } |
|
198 | + |
|
199 | + /** |
|
200 | + * This method sends authentication challenges to the user. |
|
201 | + * |
|
202 | + * This method will for example cause a HTTP Basic backend to set a |
|
203 | + * WWW-Authorization header, indicating to the client that it should |
|
204 | + * authenticate. |
|
205 | + */ |
|
206 | + public function challenge(RequestInterface $request, ResponseInterface $response) |
|
207 | + { |
|
208 | + foreach ($this->backends as $backend) { |
|
209 | + $backend->challenge($request, $response); |
|
210 | + } |
|
211 | + } |
|
212 | + |
|
213 | + /** |
|
214 | + * List of reasons why login failed for the last login operation. |
|
215 | + * |
|
216 | + * @var string[]|null |
|
217 | + */ |
|
218 | + protected $loginFailedReasons; |
|
219 | + |
|
220 | + /** |
|
221 | + * Returns a list of reasons why login was unsuccessful. |
|
222 | + * |
|
223 | + * This method will return the login failed reasons for the last login |
|
224 | + * operation. One for each auth backend. |
|
225 | + * |
|
226 | + * This method returns null if the last authentication attempt was |
|
227 | + * successful, or if there was no authentication attempt yet. |
|
228 | + * |
|
229 | + * @return string[]|null |
|
230 | + */ |
|
231 | + public function getLoginFailedReasons() |
|
232 | + { |
|
233 | + return $this->loginFailedReasons; |
|
234 | + } |
|
235 | + |
|
236 | + /** |
|
237 | + * Returns a bunch of meta-data about the plugin. |
|
238 | + * |
|
239 | + * Providing this information is optional, and is mainly displayed by the |
|
240 | + * Browser plugin. |
|
241 | + * |
|
242 | + * The description key in the returned array may contain html and will not |
|
243 | + * be sanitized. |
|
244 | + * |
|
245 | + * @return array |
|
246 | + */ |
|
247 | + public function getPluginInfo() |
|
248 | + { |
|
249 | + return [ |
|
250 | + 'name' => $this->getPluginName(), |
|
251 | + 'description' => 'Generic authentication plugin', |
|
252 | + 'link' => 'http://sabre.io/dav/authentication/', |
|
253 | + ]; |
|
254 | + } |
|
255 | 255 | } |
@@ -21,73 +21,73 @@ |
||
21 | 21 | */ |
22 | 22 | class Apache implements BackendInterface |
23 | 23 | { |
24 | - /** |
|
25 | - * This is the prefix that will be used to generate principal urls. |
|
26 | - * |
|
27 | - * @var string |
|
28 | - */ |
|
29 | - protected $principalPrefix = 'principals/'; |
|
24 | + /** |
|
25 | + * This is the prefix that will be used to generate principal urls. |
|
26 | + * |
|
27 | + * @var string |
|
28 | + */ |
|
29 | + protected $principalPrefix = 'principals/'; |
|
30 | 30 | |
31 | - /** |
|
32 | - * When this method is called, the backend must check if authentication was |
|
33 | - * successful. |
|
34 | - * |
|
35 | - * The returned value must be one of the following |
|
36 | - * |
|
37 | - * [true, "principals/username"] |
|
38 | - * [false, "reason for failure"] |
|
39 | - * |
|
40 | - * If authentication was successful, it's expected that the authentication |
|
41 | - * backend returns a so-called principal url. |
|
42 | - * |
|
43 | - * Examples of a principal url: |
|
44 | - * |
|
45 | - * principals/admin |
|
46 | - * principals/user1 |
|
47 | - * principals/users/joe |
|
48 | - * principals/uid/123457 |
|
49 | - * |
|
50 | - * If you don't use WebDAV ACL (RFC3744) we recommend that you simply |
|
51 | - * return a string such as: |
|
52 | - * |
|
53 | - * principals/users/[username] |
|
54 | - * |
|
55 | - * @return array |
|
56 | - */ |
|
57 | - public function check(RequestInterface $request, ResponseInterface $response) |
|
58 | - { |
|
59 | - $remoteUser = $request->getRawServerValue('REMOTE_USER'); |
|
60 | - if (is_null($remoteUser)) { |
|
61 | - $remoteUser = $request->getRawServerValue('REDIRECT_REMOTE_USER'); |
|
62 | - } |
|
63 | - if (is_null($remoteUser)) { |
|
64 | - $remoteUser = $request->getRawServerValue('PHP_AUTH_USER'); |
|
65 | - } |
|
66 | - if (is_null($remoteUser)) { |
|
67 | - return [false, 'No REMOTE_USER, REDIRECT_REMOTE_USER, or PHP_AUTH_USER property was found in the PHP $_SERVER super-global. This likely means your server is not configured correctly']; |
|
68 | - } |
|
31 | + /** |
|
32 | + * When this method is called, the backend must check if authentication was |
|
33 | + * successful. |
|
34 | + * |
|
35 | + * The returned value must be one of the following |
|
36 | + * |
|
37 | + * [true, "principals/username"] |
|
38 | + * [false, "reason for failure"] |
|
39 | + * |
|
40 | + * If authentication was successful, it's expected that the authentication |
|
41 | + * backend returns a so-called principal url. |
|
42 | + * |
|
43 | + * Examples of a principal url: |
|
44 | + * |
|
45 | + * principals/admin |
|
46 | + * principals/user1 |
|
47 | + * principals/users/joe |
|
48 | + * principals/uid/123457 |
|
49 | + * |
|
50 | + * If you don't use WebDAV ACL (RFC3744) we recommend that you simply |
|
51 | + * return a string such as: |
|
52 | + * |
|
53 | + * principals/users/[username] |
|
54 | + * |
|
55 | + * @return array |
|
56 | + */ |
|
57 | + public function check(RequestInterface $request, ResponseInterface $response) |
|
58 | + { |
|
59 | + $remoteUser = $request->getRawServerValue('REMOTE_USER'); |
|
60 | + if (is_null($remoteUser)) { |
|
61 | + $remoteUser = $request->getRawServerValue('REDIRECT_REMOTE_USER'); |
|
62 | + } |
|
63 | + if (is_null($remoteUser)) { |
|
64 | + $remoteUser = $request->getRawServerValue('PHP_AUTH_USER'); |
|
65 | + } |
|
66 | + if (is_null($remoteUser)) { |
|
67 | + return [false, 'No REMOTE_USER, REDIRECT_REMOTE_USER, or PHP_AUTH_USER property was found in the PHP $_SERVER super-global. This likely means your server is not configured correctly']; |
|
68 | + } |
|
69 | 69 | |
70 | - return [true, $this->principalPrefix.$remoteUser]; |
|
71 | - } |
|
70 | + return [true, $this->principalPrefix.$remoteUser]; |
|
71 | + } |
|
72 | 72 | |
73 | - /** |
|
74 | - * This method is called when a user could not be authenticated, and |
|
75 | - * authentication was required for the current request. |
|
76 | - * |
|
77 | - * This gives you the opportunity to set authentication headers. The 401 |
|
78 | - * status code will already be set. |
|
79 | - * |
|
80 | - * In this case of Basic Auth, this would for example mean that the |
|
81 | - * following header needs to be set: |
|
82 | - * |
|
83 | - * $response->addHeader('WWW-Authenticate', 'Basic realm=SabreDAV'); |
|
84 | - * |
|
85 | - * Keep in mind that in the case of multiple authentication backends, other |
|
86 | - * WWW-Authenticate headers may already have been set, and you'll want to |
|
87 | - * append your own WWW-Authenticate header instead of overwriting the |
|
88 | - * existing one. |
|
89 | - */ |
|
90 | - public function challenge(RequestInterface $request, ResponseInterface $response) |
|
91 | - { |
|
92 | - } |
|
73 | + /** |
|
74 | + * This method is called when a user could not be authenticated, and |
|
75 | + * authentication was required for the current request. |
|
76 | + * |
|
77 | + * This gives you the opportunity to set authentication headers. The 401 |
|
78 | + * status code will already be set. |
|
79 | + * |
|
80 | + * In this case of Basic Auth, this would for example mean that the |
|
81 | + * following header needs to be set: |
|
82 | + * |
|
83 | + * $response->addHeader('WWW-Authenticate', 'Basic realm=SabreDAV'); |
|
84 | + * |
|
85 | + * Keep in mind that in the case of multiple authentication backends, other |
|
86 | + * WWW-Authenticate headers may already have been set, and you'll want to |
|
87 | + * append your own WWW-Authenticate header instead of overwriting the |
|
88 | + * existing one. |
|
89 | + */ |
|
90 | + public function challenge(RequestInterface $request, ResponseInterface $response) |
|
91 | + { |
|
92 | + } |
|
93 | 93 | } |
@@ -16,50 +16,50 @@ |
||
16 | 16 | */ |
17 | 17 | interface BackendInterface |
18 | 18 | { |
19 | - /** |
|
20 | - * When this method is called, the backend must check if authentication was |
|
21 | - * successful. |
|
22 | - * |
|
23 | - * The returned value must be one of the following |
|
24 | - * |
|
25 | - * [true, "principals/username"] |
|
26 | - * [false, "reason for failure"] |
|
27 | - * |
|
28 | - * If authentication was successful, it's expected that the authentication |
|
29 | - * backend returns a so-called principal url. |
|
30 | - * |
|
31 | - * Examples of a principal url: |
|
32 | - * |
|
33 | - * principals/admin |
|
34 | - * principals/user1 |
|
35 | - * principals/users/joe |
|
36 | - * principals/uid/123457 |
|
37 | - * |
|
38 | - * If you don't use WebDAV ACL (RFC3744) we recommend that you simply |
|
39 | - * return a string such as: |
|
40 | - * |
|
41 | - * principals/users/[username] |
|
42 | - * |
|
43 | - * @return array |
|
44 | - */ |
|
45 | - public function check(RequestInterface $request, ResponseInterface $response); |
|
19 | + /** |
|
20 | + * When this method is called, the backend must check if authentication was |
|
21 | + * successful. |
|
22 | + * |
|
23 | + * The returned value must be one of the following |
|
24 | + * |
|
25 | + * [true, "principals/username"] |
|
26 | + * [false, "reason for failure"] |
|
27 | + * |
|
28 | + * If authentication was successful, it's expected that the authentication |
|
29 | + * backend returns a so-called principal url. |
|
30 | + * |
|
31 | + * Examples of a principal url: |
|
32 | + * |
|
33 | + * principals/admin |
|
34 | + * principals/user1 |
|
35 | + * principals/users/joe |
|
36 | + * principals/uid/123457 |
|
37 | + * |
|
38 | + * If you don't use WebDAV ACL (RFC3744) we recommend that you simply |
|
39 | + * return a string such as: |
|
40 | + * |
|
41 | + * principals/users/[username] |
|
42 | + * |
|
43 | + * @return array |
|
44 | + */ |
|
45 | + public function check(RequestInterface $request, ResponseInterface $response); |
|
46 | 46 | |
47 | - /** |
|
48 | - * This method is called when a user could not be authenticated, and |
|
49 | - * authentication was required for the current request. |
|
50 | - * |
|
51 | - * This gives you the opportunity to set authentication headers. The 401 |
|
52 | - * status code will already be set. |
|
53 | - * |
|
54 | - * In this case of Basic Auth, this would for example mean that the |
|
55 | - * following header needs to be set: |
|
56 | - * |
|
57 | - * $response->addHeader('WWW-Authenticate', 'Basic realm=SabreDAV'); |
|
58 | - * |
|
59 | - * Keep in mind that in the case of multiple authentication backends, other |
|
60 | - * WWW-Authenticate headers may already have been set, and you'll want to |
|
61 | - * append your own WWW-Authenticate header instead of overwriting the |
|
62 | - * existing one. |
|
63 | - */ |
|
64 | - public function challenge(RequestInterface $request, ResponseInterface $response); |
|
47 | + /** |
|
48 | + * This method is called when a user could not be authenticated, and |
|
49 | + * authentication was required for the current request. |
|
50 | + * |
|
51 | + * This gives you the opportunity to set authentication headers. The 401 |
|
52 | + * status code will already be set. |
|
53 | + * |
|
54 | + * In this case of Basic Auth, this would for example mean that the |
|
55 | + * following header needs to be set: |
|
56 | + * |
|
57 | + * $response->addHeader('WWW-Authenticate', 'Basic realm=SabreDAV'); |
|
58 | + * |
|
59 | + * Keep in mind that in the case of multiple authentication backends, other |
|
60 | + * WWW-Authenticate headers may already have been set, and you'll want to |
|
61 | + * append your own WWW-Authenticate header instead of overwriting the |
|
62 | + * existing one. |
|
63 | + */ |
|
64 | + public function challenge(RequestInterface $request, ResponseInterface $response); |
|
65 | 65 | } |
@@ -18,39 +18,39 @@ |
||
18 | 18 | */ |
19 | 19 | class BasicCallBack extends AbstractBasic |
20 | 20 | { |
21 | - /** |
|
22 | - * Callback. |
|
23 | - * |
|
24 | - * @var callable |
|
25 | - */ |
|
26 | - protected $callBack; |
|
21 | + /** |
|
22 | + * Callback. |
|
23 | + * |
|
24 | + * @var callable |
|
25 | + */ |
|
26 | + protected $callBack; |
|
27 | 27 | |
28 | - /** |
|
29 | - * Creates the backend. |
|
30 | - * |
|
31 | - * A callback must be provided to handle checking the username and |
|
32 | - * password. |
|
33 | - */ |
|
34 | - public function __construct(callable $callBack) |
|
35 | - { |
|
36 | - $this->callBack = $callBack; |
|
37 | - } |
|
28 | + /** |
|
29 | + * Creates the backend. |
|
30 | + * |
|
31 | + * A callback must be provided to handle checking the username and |
|
32 | + * password. |
|
33 | + */ |
|
34 | + public function __construct(callable $callBack) |
|
35 | + { |
|
36 | + $this->callBack = $callBack; |
|
37 | + } |
|
38 | 38 | |
39 | - /** |
|
40 | - * Validates a username and password. |
|
41 | - * |
|
42 | - * This method should return true or false depending on if login |
|
43 | - * succeeded. |
|
44 | - * |
|
45 | - * @param string $username |
|
46 | - * @param string $password |
|
47 | - * |
|
48 | - * @return bool |
|
49 | - */ |
|
50 | - protected function validateUserPass($username, $password) |
|
51 | - { |
|
52 | - $cb = $this->callBack; |
|
39 | + /** |
|
40 | + * Validates a username and password. |
|
41 | + * |
|
42 | + * This method should return true or false depending on if login |
|
43 | + * succeeded. |
|
44 | + * |
|
45 | + * @param string $username |
|
46 | + * @param string $password |
|
47 | + * |
|
48 | + * @return bool |
|
49 | + */ |
|
50 | + protected function validateUserPass($username, $password) |
|
51 | + { |
|
52 | + $cb = $this->callBack; |
|
53 | 53 | |
54 | - return $cb($username, $password); |
|
55 | - } |
|
54 | + return $cb($username, $password); |
|
55 | + } |
|
56 | 56 | } |
@@ -22,115 +22,115 @@ |
||
22 | 22 | */ |
23 | 23 | abstract class AbstractBasic implements BackendInterface |
24 | 24 | { |
25 | - /** |
|
26 | - * Authentication Realm. |
|
27 | - * |
|
28 | - * The realm is often displayed by browser clients when showing the |
|
29 | - * authentication dialog. |
|
30 | - * |
|
31 | - * @var string |
|
32 | - */ |
|
33 | - protected $realm = 'sabre/dav'; |
|
25 | + /** |
|
26 | + * Authentication Realm. |
|
27 | + * |
|
28 | + * The realm is often displayed by browser clients when showing the |
|
29 | + * authentication dialog. |
|
30 | + * |
|
31 | + * @var string |
|
32 | + */ |
|
33 | + protected $realm = 'sabre/dav'; |
|
34 | 34 | |
35 | - /** |
|
36 | - * This is the prefix that will be used to generate principal urls. |
|
37 | - * |
|
38 | - * @var string |
|
39 | - */ |
|
40 | - protected $principalPrefix = 'principals/'; |
|
35 | + /** |
|
36 | + * This is the prefix that will be used to generate principal urls. |
|
37 | + * |
|
38 | + * @var string |
|
39 | + */ |
|
40 | + protected $principalPrefix = 'principals/'; |
|
41 | 41 | |
42 | - /** |
|
43 | - * Validates a username and password. |
|
44 | - * |
|
45 | - * This method should return true or false depending on if login |
|
46 | - * succeeded. |
|
47 | - * |
|
48 | - * @param string $username |
|
49 | - * @param string $password |
|
50 | - * |
|
51 | - * @return bool |
|
52 | - */ |
|
53 | - abstract protected function validateUserPass($username, $password); |
|
42 | + /** |
|
43 | + * Validates a username and password. |
|
44 | + * |
|
45 | + * This method should return true or false depending on if login |
|
46 | + * succeeded. |
|
47 | + * |
|
48 | + * @param string $username |
|
49 | + * @param string $password |
|
50 | + * |
|
51 | + * @return bool |
|
52 | + */ |
|
53 | + abstract protected function validateUserPass($username, $password); |
|
54 | 54 | |
55 | - /** |
|
56 | - * Sets the authentication realm for this backend. |
|
57 | - * |
|
58 | - * @param string $realm |
|
59 | - */ |
|
60 | - public function setRealm($realm) |
|
61 | - { |
|
62 | - $this->realm = $realm; |
|
63 | - } |
|
55 | + /** |
|
56 | + * Sets the authentication realm for this backend. |
|
57 | + * |
|
58 | + * @param string $realm |
|
59 | + */ |
|
60 | + public function setRealm($realm) |
|
61 | + { |
|
62 | + $this->realm = $realm; |
|
63 | + } |
|
64 | 64 | |
65 | - /** |
|
66 | - * When this method is called, the backend must check if authentication was |
|
67 | - * successful. |
|
68 | - * |
|
69 | - * The returned value must be one of the following |
|
70 | - * |
|
71 | - * [true, "principals/username"] |
|
72 | - * [false, "reason for failure"] |
|
73 | - * |
|
74 | - * If authentication was successful, it's expected that the authentication |
|
75 | - * backend returns a so-called principal url. |
|
76 | - * |
|
77 | - * Examples of a principal url: |
|
78 | - * |
|
79 | - * principals/admin |
|
80 | - * principals/user1 |
|
81 | - * principals/users/joe |
|
82 | - * principals/uid/123457 |
|
83 | - * |
|
84 | - * If you don't use WebDAV ACL (RFC3744) we recommend that you simply |
|
85 | - * return a string such as: |
|
86 | - * |
|
87 | - * principals/users/[username] |
|
88 | - * |
|
89 | - * @return array |
|
90 | - */ |
|
91 | - public function check(RequestInterface $request, ResponseInterface $response) |
|
92 | - { |
|
93 | - $auth = new HTTP\Auth\Basic( |
|
94 | - $this->realm, |
|
95 | - $request, |
|
96 | - $response |
|
97 | - ); |
|
65 | + /** |
|
66 | + * When this method is called, the backend must check if authentication was |
|
67 | + * successful. |
|
68 | + * |
|
69 | + * The returned value must be one of the following |
|
70 | + * |
|
71 | + * [true, "principals/username"] |
|
72 | + * [false, "reason for failure"] |
|
73 | + * |
|
74 | + * If authentication was successful, it's expected that the authentication |
|
75 | + * backend returns a so-called principal url. |
|
76 | + * |
|
77 | + * Examples of a principal url: |
|
78 | + * |
|
79 | + * principals/admin |
|
80 | + * principals/user1 |
|
81 | + * principals/users/joe |
|
82 | + * principals/uid/123457 |
|
83 | + * |
|
84 | + * If you don't use WebDAV ACL (RFC3744) we recommend that you simply |
|
85 | + * return a string such as: |
|
86 | + * |
|
87 | + * principals/users/[username] |
|
88 | + * |
|
89 | + * @return array |
|
90 | + */ |
|
91 | + public function check(RequestInterface $request, ResponseInterface $response) |
|
92 | + { |
|
93 | + $auth = new HTTP\Auth\Basic( |
|
94 | + $this->realm, |
|
95 | + $request, |
|
96 | + $response |
|
97 | + ); |
|
98 | 98 | |
99 | - $userpass = $auth->getCredentials(); |
|
100 | - if (!$userpass) { |
|
101 | - return [false, "No 'Authorization: Basic' header found. Either the client didn't send one, or the server is misconfigured"]; |
|
102 | - } |
|
103 | - if (!$this->validateUserPass($userpass[0], $userpass[1])) { |
|
104 | - return [false, 'Username or password was incorrect']; |
|
105 | - } |
|
99 | + $userpass = $auth->getCredentials(); |
|
100 | + if (!$userpass) { |
|
101 | + return [false, "No 'Authorization: Basic' header found. Either the client didn't send one, or the server is misconfigured"]; |
|
102 | + } |
|
103 | + if (!$this->validateUserPass($userpass[0], $userpass[1])) { |
|
104 | + return [false, 'Username or password was incorrect']; |
|
105 | + } |
|
106 | 106 | |
107 | - return [true, $this->principalPrefix.$userpass[0]]; |
|
108 | - } |
|
107 | + return [true, $this->principalPrefix.$userpass[0]]; |
|
108 | + } |
|
109 | 109 | |
110 | - /** |
|
111 | - * This method is called when a user could not be authenticated, and |
|
112 | - * authentication was required for the current request. |
|
113 | - * |
|
114 | - * This gives you the opportunity to set authentication headers. The 401 |
|
115 | - * status code will already be set. |
|
116 | - * |
|
117 | - * In this case of Basic Auth, this would for example mean that the |
|
118 | - * following header needs to be set: |
|
119 | - * |
|
120 | - * $response->addHeader('WWW-Authenticate', 'Basic realm=SabreDAV'); |
|
121 | - * |
|
122 | - * Keep in mind that in the case of multiple authentication backends, other |
|
123 | - * WWW-Authenticate headers may already have been set, and you'll want to |
|
124 | - * append your own WWW-Authenticate header instead of overwriting the |
|
125 | - * existing one. |
|
126 | - */ |
|
127 | - public function challenge(RequestInterface $request, ResponseInterface $response) |
|
128 | - { |
|
129 | - $auth = new HTTP\Auth\Basic( |
|
130 | - $this->realm, |
|
131 | - $request, |
|
132 | - $response |
|
133 | - ); |
|
134 | - $auth->requireLogin(); |
|
135 | - } |
|
110 | + /** |
|
111 | + * This method is called when a user could not be authenticated, and |
|
112 | + * authentication was required for the current request. |
|
113 | + * |
|
114 | + * This gives you the opportunity to set authentication headers. The 401 |
|
115 | + * status code will already be set. |
|
116 | + * |
|
117 | + * In this case of Basic Auth, this would for example mean that the |
|
118 | + * following header needs to be set: |
|
119 | + * |
|
120 | + * $response->addHeader('WWW-Authenticate', 'Basic realm=SabreDAV'); |
|
121 | + * |
|
122 | + * Keep in mind that in the case of multiple authentication backends, other |
|
123 | + * WWW-Authenticate headers may already have been set, and you'll want to |
|
124 | + * append your own WWW-Authenticate header instead of overwriting the |
|
125 | + * existing one. |
|
126 | + */ |
|
127 | + public function challenge(RequestInterface $request, ResponseInterface $response) |
|
128 | + { |
|
129 | + $auth = new HTTP\Auth\Basic( |
|
130 | + $this->realm, |
|
131 | + $request, |
|
132 | + $response |
|
133 | + ); |
|
134 | + $auth->requireLogin(); |
|
135 | + } |
|
136 | 136 | } |
@@ -22,139 +22,139 @@ |
||
22 | 22 | */ |
23 | 23 | abstract class AbstractDigest implements BackendInterface |
24 | 24 | { |
25 | - /** |
|
26 | - * Authentication Realm. |
|
27 | - * |
|
28 | - * The realm is often displayed by browser clients when showing the |
|
29 | - * authentication dialog. |
|
30 | - * |
|
31 | - * @var string |
|
32 | - */ |
|
33 | - protected $realm = 'SabreDAV'; |
|
34 | - |
|
35 | - /** |
|
36 | - * This is the prefix that will be used to generate principal urls. |
|
37 | - * |
|
38 | - * @var string |
|
39 | - */ |
|
40 | - protected $principalPrefix = 'principals/'; |
|
41 | - |
|
42 | - /** |
|
43 | - * Sets the authentication realm for this backend. |
|
44 | - * |
|
45 | - * Be aware that for Digest authentication, the realm influences the digest |
|
46 | - * hash. Choose the realm wisely, because if you change it later, all the |
|
47 | - * existing hashes will break and nobody can authenticate. |
|
48 | - * |
|
49 | - * @param string $realm |
|
50 | - */ |
|
51 | - public function setRealm($realm) |
|
52 | - { |
|
53 | - $this->realm = $realm; |
|
54 | - } |
|
55 | - |
|
56 | - /** |
|
57 | - * Returns a users digest hash based on the username and realm. |
|
58 | - * |
|
59 | - * If the user was not known, null must be returned. |
|
60 | - * |
|
61 | - * @param string $realm |
|
62 | - * @param string $username |
|
63 | - * |
|
64 | - * @return string|null |
|
65 | - */ |
|
66 | - abstract public function getDigestHash($realm, $username); |
|
67 | - |
|
68 | - /** |
|
69 | - * When this method is called, the backend must check if authentication was |
|
70 | - * successful. |
|
71 | - * |
|
72 | - * The returned value must be one of the following |
|
73 | - * |
|
74 | - * [true, "principals/username"] |
|
75 | - * [false, "reason for failure"] |
|
76 | - * |
|
77 | - * If authentication was successful, it's expected that the authentication |
|
78 | - * backend returns a so-called principal url. |
|
79 | - * |
|
80 | - * Examples of a principal url: |
|
81 | - * |
|
82 | - * principals/admin |
|
83 | - * principals/user1 |
|
84 | - * principals/users/joe |
|
85 | - * principals/uid/123457 |
|
86 | - * |
|
87 | - * If you don't use WebDAV ACL (RFC3744) we recommend that you simply |
|
88 | - * return a string such as: |
|
89 | - * |
|
90 | - * principals/users/[username] |
|
91 | - * |
|
92 | - * @return array |
|
93 | - */ |
|
94 | - public function check(RequestInterface $request, ResponseInterface $response) |
|
95 | - { |
|
96 | - $digest = new HTTP\Auth\Digest( |
|
97 | - $this->realm, |
|
98 | - $request, |
|
99 | - $response |
|
100 | - ); |
|
101 | - $digest->init(); |
|
102 | - |
|
103 | - $username = $digest->getUsername(); |
|
104 | - |
|
105 | - // No username was given |
|
106 | - if (!$username) { |
|
107 | - return [false, "No 'Authorization: Digest' header found. Either the client didn't send one, or the server is misconfigured"]; |
|
108 | - } |
|
109 | - |
|
110 | - $hash = $this->getDigestHash($this->realm, $username); |
|
111 | - // If this was false, the user account didn't exist |
|
112 | - if (false === $hash || is_null($hash)) { |
|
113 | - return [false, 'Username or password was incorrect']; |
|
114 | - } |
|
115 | - if (!is_string($hash)) { |
|
116 | - throw new DAV\Exception('The returned value from getDigestHash must be a string or null'); |
|
117 | - } |
|
118 | - |
|
119 | - // If this was false, the password or part of the hash was incorrect. |
|
120 | - if (!$digest->validateA1($hash)) { |
|
121 | - return [false, 'Username or password was incorrect']; |
|
122 | - } |
|
123 | - |
|
124 | - return [true, $this->principalPrefix.$username]; |
|
125 | - } |
|
126 | - |
|
127 | - /** |
|
128 | - * This method is called when a user could not be authenticated, and |
|
129 | - * authentication was required for the current request. |
|
130 | - * |
|
131 | - * This gives you the opportunity to set authentication headers. The 401 |
|
132 | - * status code will already be set. |
|
133 | - * |
|
134 | - * In this case of Basic Auth, this would for example mean that the |
|
135 | - * following header needs to be set: |
|
136 | - * |
|
137 | - * $response->addHeader('WWW-Authenticate', 'Basic realm=SabreDAV'); |
|
138 | - * |
|
139 | - * Keep in mind that in the case of multiple authentication backends, other |
|
140 | - * WWW-Authenticate headers may already have been set, and you'll want to |
|
141 | - * append your own WWW-Authenticate header instead of overwriting the |
|
142 | - * existing one. |
|
143 | - */ |
|
144 | - public function challenge(RequestInterface $request, ResponseInterface $response) |
|
145 | - { |
|
146 | - $auth = new HTTP\Auth\Digest( |
|
147 | - $this->realm, |
|
148 | - $request, |
|
149 | - $response |
|
150 | - ); |
|
151 | - $auth->init(); |
|
152 | - |
|
153 | - $oldStatus = $response->getStatus() ?: 200; |
|
154 | - $auth->requireLogin(); |
|
155 | - |
|
156 | - // Preventing the digest utility from modifying the http status code, |
|
157 | - // this should be handled by the main plugin. |
|
158 | - $response->setStatus($oldStatus); |
|
159 | - } |
|
25 | + /** |
|
26 | + * Authentication Realm. |
|
27 | + * |
|
28 | + * The realm is often displayed by browser clients when showing the |
|
29 | + * authentication dialog. |
|
30 | + * |
|
31 | + * @var string |
|
32 | + */ |
|
33 | + protected $realm = 'SabreDAV'; |
|
34 | + |
|
35 | + /** |
|
36 | + * This is the prefix that will be used to generate principal urls. |
|
37 | + * |
|
38 | + * @var string |
|
39 | + */ |
|
40 | + protected $principalPrefix = 'principals/'; |
|
41 | + |
|
42 | + /** |
|
43 | + * Sets the authentication realm for this backend. |
|
44 | + * |
|
45 | + * Be aware that for Digest authentication, the realm influences the digest |
|
46 | + * hash. Choose the realm wisely, because if you change it later, all the |
|
47 | + * existing hashes will break and nobody can authenticate. |
|
48 | + * |
|
49 | + * @param string $realm |
|
50 | + */ |
|
51 | + public function setRealm($realm) |
|
52 | + { |
|
53 | + $this->realm = $realm; |
|
54 | + } |
|
55 | + |
|
56 | + /** |
|
57 | + * Returns a users digest hash based on the username and realm. |
|
58 | + * |
|
59 | + * If the user was not known, null must be returned. |
|
60 | + * |
|
61 | + * @param string $realm |
|
62 | + * @param string $username |
|
63 | + * |
|
64 | + * @return string|null |
|
65 | + */ |
|
66 | + abstract public function getDigestHash($realm, $username); |
|
67 | + |
|
68 | + /** |
|
69 | + * When this method is called, the backend must check if authentication was |
|
70 | + * successful. |
|
71 | + * |
|
72 | + * The returned value must be one of the following |
|
73 | + * |
|
74 | + * [true, "principals/username"] |
|
75 | + * [false, "reason for failure"] |
|
76 | + * |
|
77 | + * If authentication was successful, it's expected that the authentication |
|
78 | + * backend returns a so-called principal url. |
|
79 | + * |
|
80 | + * Examples of a principal url: |
|
81 | + * |
|
82 | + * principals/admin |
|
83 | + * principals/user1 |
|
84 | + * principals/users/joe |
|
85 | + * principals/uid/123457 |
|
86 | + * |
|
87 | + * If you don't use WebDAV ACL (RFC3744) we recommend that you simply |
|
88 | + * return a string such as: |
|
89 | + * |
|
90 | + * principals/users/[username] |
|
91 | + * |
|
92 | + * @return array |
|
93 | + */ |
|
94 | + public function check(RequestInterface $request, ResponseInterface $response) |
|
95 | + { |
|
96 | + $digest = new HTTP\Auth\Digest( |
|
97 | + $this->realm, |
|
98 | + $request, |
|
99 | + $response |
|
100 | + ); |
|
101 | + $digest->init(); |
|
102 | + |
|
103 | + $username = $digest->getUsername(); |
|
104 | + |
|
105 | + // No username was given |
|
106 | + if (!$username) { |
|
107 | + return [false, "No 'Authorization: Digest' header found. Either the client didn't send one, or the server is misconfigured"]; |
|
108 | + } |
|
109 | + |
|
110 | + $hash = $this->getDigestHash($this->realm, $username); |
|
111 | + // If this was false, the user account didn't exist |
|
112 | + if (false === $hash || is_null($hash)) { |
|
113 | + return [false, 'Username or password was incorrect']; |
|
114 | + } |
|
115 | + if (!is_string($hash)) { |
|
116 | + throw new DAV\Exception('The returned value from getDigestHash must be a string or null'); |
|
117 | + } |
|
118 | + |
|
119 | + // If this was false, the password or part of the hash was incorrect. |
|
120 | + if (!$digest->validateA1($hash)) { |
|
121 | + return [false, 'Username or password was incorrect']; |
|
122 | + } |
|
123 | + |
|
124 | + return [true, $this->principalPrefix.$username]; |
|
125 | + } |
|
126 | + |
|
127 | + /** |
|
128 | + * This method is called when a user could not be authenticated, and |
|
129 | + * authentication was required for the current request. |
|
130 | + * |
|
131 | + * This gives you the opportunity to set authentication headers. The 401 |
|
132 | + * status code will already be set. |
|
133 | + * |
|
134 | + * In this case of Basic Auth, this would for example mean that the |
|
135 | + * following header needs to be set: |
|
136 | + * |
|
137 | + * $response->addHeader('WWW-Authenticate', 'Basic realm=SabreDAV'); |
|
138 | + * |
|
139 | + * Keep in mind that in the case of multiple authentication backends, other |
|
140 | + * WWW-Authenticate headers may already have been set, and you'll want to |
|
141 | + * append your own WWW-Authenticate header instead of overwriting the |
|
142 | + * existing one. |
|
143 | + */ |
|
144 | + public function challenge(RequestInterface $request, ResponseInterface $response) |
|
145 | + { |
|
146 | + $auth = new HTTP\Auth\Digest( |
|
147 | + $this->realm, |
|
148 | + $request, |
|
149 | + $response |
|
150 | + ); |
|
151 | + $auth->init(); |
|
152 | + |
|
153 | + $oldStatus = $response->getStatus() ?: 200; |
|
154 | + $auth->requireLogin(); |
|
155 | + |
|
156 | + // Preventing the digest utility from modifying the http status code, |
|
157 | + // this should be handled by the main plugin. |
|
158 | + $response->setStatus($oldStatus); |
|
159 | + } |
|
160 | 160 | } |