1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
|
<?php
namespace Sabre\DAV\Auth;
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;
use Sabre\HTTP\URLUtil;
use Sabre\DAV\Exception\NotAuthenticated;
use Sabre\DAV\Server;
use Sabre\DAV\ServerPlugin;
/**
* This plugin provides Authentication for a WebDAV server.
*
* It works by providing a Auth\Backend class. Several examples of these
* classes can be found in the Backend directory.
*
* It's possible to provide more than one backend to this plugin. If more than
* one backend was provided, each backend will attempt to authenticate. Only if
* all backends fail, we throw a 401.
*
* @copyright Copyright (C) fruux GmbH (https://fruux.com/)
* @author Evert Pot (http://evertpot.com/)
* @license http://sabre.io/license/ Modified BSD License
*/
class Plugin extends ServerPlugin {
/**
* authentication backends
*/
protected $backends;
/**
* The currently logged in principal. Will be `null` if nobody is currently
* logged in.
*
* @var string|null
*/
protected $currentPrincipal;
/**
* Creates the authentication plugin
*
* @param Backend\BackendInterface $authBackend
*/
function __construct(Backend\BackendInterface $authBackend = null) {
if (!is_null($authBackend)) {
$this->addBackend($authBackend);
}
}
/**
* Adds an authentication backend to the plugin.
*
* @param Backend\BackendInterface $authBackend
* @return void
*/
function addBackend(Backend\BackendInterface $authBackend) {
$this->backends[] = $authBackend;
}
/**
* Initializes the plugin. This function is automatically called by the server
*
* @param Server $server
* @return void
*/
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
*/
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
*/
function getCurrentPrincipal() {
return $this->currentPrincipal;
}
/**
* Returns the current username.
*
* This method is deprecated and is only kept for backwards compatibility
* purposes. Please switch to getCurrentPrincipal().
*
* @deprecated Will be removed in a future version!
* @return string|null
*/
function getCurrentUser() {
// We just do a 'basename' on the principal to give back a sane value
// here.
list(, $userName) = URLUtil::splitPath(
$this->getCurrentPrincipal()
);
return $userName;
}
/**
* This method is called before any HTTP method and forces users to be authenticated
*
* @param RequestInterface $request
* @param ResponseInterface $response
* @return bool
*/
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;
}
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) || count($result) !== 2 || !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;
}
$reasons[] = $result[1];
}
// If we got here, it means that no authentication backend was
// successful in authenticating the user.
$this->currentPrincipal = null;
foreach ($this->backends as $backend) {
$backend->challenge($request, $response);
}
throw new NotAuthenticated(implode(', ', $reasons));
}
/**
* 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
*/
function getPluginInfo() {
return [
'name' => $this->getPluginName(),
'description' => 'Generic authentication plugin',
'link' => 'http://sabre.io/dav/authentication/',
];
}
}
|