1<?php
2
3/**
4 * This example shows how to do Basic authentication.
5 * *
6 * @copyright Copyright (C) 2009-2015 fruux GmbH (https://fruux.com/).
7 * @author Evert Pot (http://evertpot.com/)
8 * @license http://sabre.io/license/ Modified BSD License
9 */
10$userList = [
11    "user1" => "password",
12    "user2" => "password",
13];
14
15use Sabre\HTTP\Sapi;
16use Sabre\HTTP\Response;
17use Sabre\HTTP\Auth;
18
19// Find the autoloader
20$paths = [
21    __DIR__ . '/../vendor/autoload.php',
22    __DIR__ . '/../../../autoload.php',
23    __DIR__ . '/vendor/autoload.php',
24
25];
26
27foreach ($paths as $path) {
28    if (file_exists($path)) {
29        include $path;
30        break;
31    }
32}
33
34$request = Sapi::getRequest();
35$response = new Response();
36
37$basicAuth = new Auth\Basic("Locked down area", $request, $response);
38if (!$userPass = $basicAuth->getCredentials()) {
39
40    // No username or password given
41    $basicAuth->requireLogin();
42
43} elseif (!isset($userList[$userPass[0]]) || $userList[$userPass[0]] !== $userPass[1]) {
44
45    // Username or password are incorrect
46    $basicAuth->requireLogin();
47} else {
48
49    // Success !
50    $response->setBody('You are logged in!');
51
52}
53
54// Sending the response
55Sapi::sendResponse($response);
56