basicauth.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. <?php
  2. /**
  3. * This example shows how to do Basic authentication.
  4. * *.
  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. use Sabre\HTTP\Auth;
  15. use Sabre\HTTP\Response;
  16. use Sabre\HTTP\Sapi;
  17. // Find the autoloader
  18. $paths = [
  19. __DIR__.'/../vendor/autoload.php',
  20. __DIR__.'/../../../autoload.php',
  21. __DIR__.'/vendor/autoload.php',
  22. ];
  23. foreach ($paths as $path) {
  24. if (file_exists($path)) {
  25. include $path;
  26. break;
  27. }
  28. }
  29. $request = Sapi::getRequest();
  30. $response = new Response();
  31. $basicAuth = new Auth\Basic('Locked down area', $request, $response);
  32. if (!$userPass = $basicAuth->getCredentials()) {
  33. // No username or password given
  34. $basicAuth->requireLogin();
  35. } elseif (!isset($userList[$userPass[0]]) || $userList[$userPass[0]] !== $userPass[1]) {
  36. // Username or password are incorrect
  37. $basicAuth->requireLogin();
  38. } else {
  39. // Success !
  40. $response->setBody('You are logged in!');
  41. }
  42. // Sending the response
  43. Sapi::sendResponse($response);