1<?php 2 3/** 4 * This example shows how to do Digest authentication. 5 * * 6 * @copyright Copyright (C) 2009-2015 fruux GmbH (https://fruux.com/). 7 * @author Markus Staab 8 * @license http://sabre.io/license/ Modified BSD License 9 */ 10$userList = [ 11 "user1" => "password", 12 "user2" => "password", 13]; 14 15use Sabre\HTTP\Auth; 16use Sabre\HTTP\Response; 17use Sabre\HTTP\Sapi; 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$digestAuth = new Auth\Digest("Locked down area", $request, $response); 38$digestAuth->init(); 39if (!$userName = $digestAuth->getUsername()) { 40 41 // No username given 42 $digestAuth->requireLogin(); 43 44} elseif (!isset($userList[$userName]) || !$digestAuth->validatePassword($userList[$userName])) { 45 46 // Username or password are incorrect 47 $digestAuth->requireLogin(); 48} else { 49 50 // Success ! 51 $response->setBody('You are logged in!'); 52 53} 54 55// Sending the response 56Sapi::sendResponse($response); 57