1
0

PublicAuth.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. <?php
  2. /**
  3. * @author Thomas Müller <thomas.mueller@tmit.eu>
  4. *
  5. * @copyright Copyright (c) 2016, ownCloud, Inc.
  6. * @license AGPL-3.0
  7. *
  8. * This code is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Affero General Public License, version 3,
  10. * as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License, version 3,
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>
  19. *
  20. */
  21. namespace OCA\DAV\DAV;
  22. use Sabre\DAV\Auth\Backend\BackendInterface;
  23. use Sabre\HTTP\RequestInterface;
  24. use Sabre\HTTP\ResponseInterface;
  25. class PublicAuth implements BackendInterface {
  26. /** @var string[] */
  27. private $publicURLs;
  28. public function __construct() {
  29. $this->publicURLs = [
  30. 'public-calendars',
  31. 'principals/system/public'
  32. ];
  33. }
  34. /**
  35. * When this method is called, the backend must check if authentication was
  36. * successful.
  37. *
  38. * The returned value must be one of the following
  39. *
  40. * [true, "principals/username"]
  41. * [false, "reason for failure"]
  42. *
  43. * If authentication was successful, it's expected that the authentication
  44. * backend returns a so-called principal url.
  45. *
  46. * Examples of a principal url:
  47. *
  48. * principals/admin
  49. * principals/user1
  50. * principals/users/joe
  51. * principals/uid/123457
  52. *
  53. * If you don't use WebDAV ACL (RFC3744) we recommend that you simply
  54. * return a string such as:
  55. *
  56. * principals/users/[username]
  57. *
  58. * @param RequestInterface $request
  59. * @param ResponseInterface $response
  60. * @return array
  61. */
  62. function check(RequestInterface $request, ResponseInterface $response) {
  63. if ($this->isRequestPublic($request)) {
  64. return [true, "principals/system/public"];
  65. }
  66. return [false, "No public access to this resource."];
  67. }
  68. /**
  69. * @inheritdoc
  70. */
  71. function challenge(RequestInterface $request, ResponseInterface $response) {
  72. }
  73. /**
  74. * @param RequestInterface $request
  75. * @return bool
  76. */
  77. private function isRequestPublic(RequestInterface $request) {
  78. $url = $request->getPath();
  79. $matchingUrls = array_filter($this->publicURLs, function ($publicUrl) use ($url) {
  80. return strpos($url, $publicUrl, 0) === 0;
  81. });
  82. return !empty($matchingUrls);
  83. }
  84. }