ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
Plugin.php
Go to the documentation of this file.
1 <?php
2 
3 namespace Sabre\DAV\Auth;
4 
10 
25 class Plugin extends ServerPlugin {
26 
39  public $autoRequireLogin = true;
40 
44  protected $backends;
45 
52  protected $currentPrincipal;
53 
59  function __construct(Backend\BackendInterface $authBackend = null) {
60 
61  if (!is_null($authBackend)) {
62  $this->addBackend($authBackend);
63  }
64 
65  }
66 
73  function addBackend(Backend\BackendInterface $authBackend) {
74 
75  $this->backends[] = $authBackend;
76 
77  }
78 
86 
87  $server->on('beforeMethod', [$this, 'beforeMethod'], 10);
88 
89  }
90 
99  function getPluginName() {
100 
101  return 'auth';
102 
103  }
104 
117  function getCurrentPrincipal() {
118 
120 
121  }
122 
131 
132  if ($this->currentPrincipal) {
133 
134  // We already have authentication information. This means that the
135  // event has already fired earlier, and is now likely fired for a
136  // sub-request.
137  //
138  // We don't want to authenticate users twice, so we simply don't do
139  // anything here. See Issue #700 for additional reasoning.
140  //
141  // This is not a perfect solution, but will be fixed once the
142  // "currently authenticated principal" is information that's not
143  // not associated with the plugin, but rather per-request.
144  //
145  // See issue #580 for more information about that.
146  return;
147 
148  }
149 
150  $authResult = $this->check($request, $response);
151 
152  if ($authResult[0]) {
153  // Auth was successful
154  $this->currentPrincipal = $authResult[1];
155  $this->loginFailedReasons = null;
156  return;
157  }
158 
159 
160 
161  // If we got here, it means that no authentication backend was
162  // successful in authenticating the user.
163  $this->currentPrincipal = null;
164  $this->loginFailedReasons = $authResult[1];
165 
166  if ($this->autoRequireLogin) {
167  $this->challenge($request, $response);
168  throw new NotAuthenticated(implode(', ', $authResult[1]));
169  }
170 
171  }
172 
192 
193  if (!$this->backends) {
194  throw new \Sabre\DAV\Exception('No authentication backends were configured on this server.');
195  }
196  $reasons = [];
197  foreach ($this->backends as $backend) {
198 
199  $result = $backend->check(
200  $request,
201  $response
202  );
203 
204  if (!is_array($result) || count($result) !== 2 || !is_bool($result[0]) || !is_string($result[1])) {
205  throw new \Sabre\DAV\Exception('The authentication backend did not return a correct value from the check() method.');
206  }
207 
208  if ($result[0]) {
209  $this->currentPrincipal = $result[1];
210  // Exit early
211  return [true, $result[1]];
212  }
213  $reasons[] = $result[1];
214 
215  }
216 
217  return [false, $reasons];
218 
219  }
220 
233 
234  foreach ($this->backends as $backend) {
235  $backend->challenge($request, $response);
236  }
237 
238  }
239 
246 
259 
261 
262  }
263 
275  function getPluginInfo() {
276 
277  return [
278  'name' => $this->getPluginName(),
279  'description' => 'Generic authentication plugin',
280  'link' => 'http://sabre.io/dav/authentication/',
281  ];
282 
283  }
284 
285 }
check(RequestInterface $request, ResponseInterface $response)
Checks authentication credentials, and logs the user in if possible.
Definition: Plugin.php:191
This interface represents a HTTP response.
$backends
authentication backends
Definition: Plugin.php:44
The RequestInterface represents a HTTP request.
getPluginName()
Returns a plugin name.
Definition: Plugin.php:99
The baseclass for all server plugins.
on($eventName, callable $callBack, $priority=100)
Subscribe to an event.
This plugin provides Authentication for a WebDAV server.
Definition: Plugin.php:25
$result
foreach($paths as $path) $request
Definition: asyncclient.php:32
__construct(Backend\BackendInterface $authBackend=null)
Creates the authentication plugin.
Definition: Plugin.php:59
$server
Definition: sabredav.php:48
initialize(Server $server)
Initializes the plugin.
Definition: Plugin.php:85
challenge(RequestInterface $request, ResponseInterface $response)
This method sends authentication challenges to the user.
Definition: Plugin.php:232
addBackend(Backend\BackendInterface $authBackend)
Adds an authentication backend to the plugin.
Definition: Plugin.php:73
$autoRequireLogin
By default this plugin will require that the user is authenticated, and refuse any access if the user...
Definition: Plugin.php:39
Main DAV server class.
Definition: Server.php:23
getLoginFailedReasons()
Returns a list of reasons why login was unsuccessful.
Definition: Plugin.php:258
$authBackend
getCurrentPrincipal()
Returns the currently logged-in principal.
Definition: Plugin.php:117
$response
getPluginInfo()
Returns a bunch of meta-data about the plugin.
Definition: Plugin.php:275
beforeMethod(RequestInterface $request, ResponseInterface $response)
This method is called before any HTTP method and forces users to be authenticated.
Definition: Plugin.php:130