You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

BearerAuth.php 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2017 Lukas Reschke <lukas@statuscode.ch>
  4. *
  5. * @author Lukas Reschke <lukas@statuscode.ch>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OCA\DAV\Connector\Sabre;
  24. use OCP\IRequest;
  25. use OCP\ISession;
  26. use OCP\IUserSession;
  27. use Sabre\DAV\Auth\Backend\AbstractBearer;
  28. use Sabre\HTTP\RequestInterface;
  29. use Sabre\HTTP\ResponseInterface;
  30. class BearerAuth extends AbstractBearer {
  31. /** @var IUserSession */
  32. private $userSession;
  33. /** @var ISession */
  34. private $session;
  35. /** @var IRequest */
  36. private $request;
  37. /** @var string */
  38. private $principalPrefix;
  39. /**
  40. * @param IUserSession $userSession
  41. * @param ISession $session
  42. * @param string $principalPrefix
  43. * @param IRequest $request
  44. */
  45. public function __construct(IUserSession $userSession,
  46. ISession $session,
  47. IRequest $request,
  48. $principalPrefix = 'principals/users/') {
  49. $this->userSession = $userSession;
  50. $this->session = $session;
  51. $this->request = $request;
  52. $this->principalPrefix = $principalPrefix;
  53. // setup realm
  54. $defaults = new \OCP\Defaults();
  55. $this->realm = $defaults->getName();
  56. }
  57. private function setupUserFs($userId) {
  58. \OC_Util::setupFS($userId);
  59. $this->session->close();
  60. return $this->principalPrefix . $userId;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function validateBearerToken($bearerToken) {
  66. \OC_Util::setupFS();
  67. if(!$this->userSession->isLoggedIn()) {
  68. $this->userSession->tryTokenLogin($this->request);
  69. }
  70. if($this->userSession->isLoggedIn()) {
  71. return $this->setupUserFs($this->userSession->getUser()->getUID());
  72. }
  73. return false;
  74. }
  75. /**
  76. * \Sabre\DAV\Auth\Backend\AbstractBearer::challenge sets an WWW-Authenticate
  77. * header which some DAV clients can't handle. Thus we override this function
  78. * and make it simply return a 401.
  79. *
  80. * @param RequestInterface $request
  81. * @param ResponseInterface $response
  82. */
  83. public function challenge(RequestInterface $request, ResponseInterface $response) {
  84. $response->setStatus(401);
  85. }
  86. }