PublicShareController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCP\AppFramework;
  25. use OCP\IRequest;
  26. use OCP\ISession;
  27. /**
  28. * Base controller for public shares
  29. *
  30. * It will verify if the user is properly authenticated to the share. If not a 404
  31. * is thrown by the PublicShareMiddleware.
  32. *
  33. * Use this for example for a controller that is not to be called via a webbrowser
  34. * directly. For example a PublicPreviewController. As this is not meant to be
  35. * called by a user direclty.
  36. *
  37. * To show an auth page extend the AuthPublicShareController
  38. *
  39. * @since 14.0.0
  40. */
  41. abstract class PublicShareController extends Controller {
  42. /** @var ISession */
  43. protected $session;
  44. /** @var string */
  45. private $token;
  46. /**
  47. * @since 14.0.0
  48. */
  49. public function __construct(string $appName,
  50. IRequest $request,
  51. ISession $session) {
  52. parent::__construct($appName, $request);
  53. $this->session = $session;
  54. }
  55. /**
  56. * Middleware set the token for the request
  57. *
  58. * @since 14.0.0
  59. */
  60. final public function setToken(string $token) {
  61. $this->token = $token;
  62. }
  63. /**
  64. * Get the token for this request
  65. *
  66. * @since 14.0.0
  67. */
  68. final public function getToken(): string {
  69. return $this->token;
  70. }
  71. /**
  72. * Get a hash of the password for this share
  73. *
  74. * To ensure access is blocked when the password to a share is changed we store
  75. * a hash of the password for this token.
  76. *
  77. * @since 14.0.0
  78. */
  79. abstract protected function getPasswordHash(): string;
  80. /**
  81. * Is the provided token a valid token
  82. *
  83. * This function is already called from the middleware directly after setting the token.
  84. *
  85. * @since 14.0.0
  86. */
  87. abstract public function isValidToken(): bool;
  88. /**
  89. * Is a share with this token password protected
  90. *
  91. * @since 14.0.0
  92. */
  93. abstract protected function isPasswordProtected(): bool;
  94. /**
  95. * Check if a share is authenticated or not
  96. *
  97. * @since 14.0.0
  98. */
  99. public function isAuthenticated(): bool {
  100. // Always authenticated against non password protected shares
  101. if (!$this->isPasswordProtected()) {
  102. return true;
  103. }
  104. // If we are authenticated properly
  105. if ($this->session->get('public_link_authenticated_token') === $this->getToken() &&
  106. $this->session->get('public_link_authenticated_password_hash') === $this->getPasswordHash()) {
  107. return true;
  108. }
  109. // Fail by default if nothing matches
  110. return false;
  111. }
  112. /**
  113. * Function called if the share is not found.
  114. *
  115. * You can use this to do some logging for example
  116. *
  117. * @since 14.0.0
  118. */
  119. public function shareNotFound() {
  120. }
  121. }