AuthPublicShareController.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. <?php
  2. /**
  3. * @copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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. declare(strict_types=1);
  24. namespace OCP\AppFramework;
  25. use OCP\AppFramework\Http\RedirectResponse;
  26. use OCP\AppFramework\Http\TemplateResponse;
  27. use OCP\IRequest;
  28. use OCP\ISession;
  29. use OCP\IURLGenerator;
  30. /**
  31. * Base controller for interactive public shares
  32. *
  33. * It will verify if the user is properly authenticated to the share. If not the
  34. * user will be redirected to an authentication page.
  35. *
  36. * Use this for a controller that is to be called directly by a user. So the
  37. * normal public share page for files/calendars etc.
  38. *
  39. * @since 14.0.0
  40. */
  41. abstract class AuthPublicShareController extends PublicShareController {
  42. /** @var IURLGenerator */
  43. protected $urlGenerator;
  44. /**
  45. * @since 14.0.0
  46. */
  47. public function __construct(string $appName,
  48. IRequest $request,
  49. ISession $session,
  50. IURLGenerator $urlGenerator) {
  51. parent::__construct($appName, $request, $session);
  52. $this->urlGenerator = $urlGenerator;
  53. }
  54. /**
  55. * @PublicPage
  56. * @NoCSRFRequired
  57. *
  58. * Show the authentication page
  59. * The form has to submit to the authenticate method route
  60. *
  61. * @since 14.0.0
  62. */
  63. public function showAuthenticate(): TemplateResponse {
  64. return new TemplateResponse('core', 'publicshareauth', [], 'guest');
  65. }
  66. /**
  67. * The template to show when authentication failed
  68. *
  69. * @since 14.0.0
  70. */
  71. protected function showAuthFailed(): TemplateResponse {
  72. return new TemplateResponse('core', 'publicshareauth', ['wrongpw' => true], 'guest');
  73. }
  74. /**
  75. * Verify the password
  76. *
  77. * @since 14.0.0
  78. */
  79. abstract protected function verifyPassword(string $password): bool;
  80. /**
  81. * Function called after failed authentication
  82. *
  83. * You can use this to do some logging for example
  84. *
  85. * @since 14.0.0
  86. */
  87. protected function authFailed() {
  88. }
  89. /**
  90. * Function called after successfull authentication
  91. *
  92. * You can use this to do some logging for example
  93. *
  94. * @since 14.0.0
  95. */
  96. protected function authSucceeded() {
  97. }
  98. /**
  99. * @UseSession
  100. * @PublicPage
  101. * @BruteForceProtection(action=publicLinkAuth)
  102. *
  103. * Authenticate the share
  104. *
  105. * @since 14.0.0
  106. */
  107. final public function authenticate(string $password = '') {
  108. // Already authenticated
  109. if ($this->isAuthenticated()) {
  110. return $this->getRedirect();
  111. }
  112. if (!$this->verifyPassword($password)) {
  113. $this->authFailed();
  114. $response = $this->showAuthFailed();
  115. $response->throttle();
  116. return $response;
  117. }
  118. $this->session->regenerateId(true, true);
  119. $response = $this->getRedirect();
  120. $this->session->set('public_link_authenticated_token', $this->getToken());
  121. $this->session->set('public_link_authenticated_password_hash', $this->getPasswordHash());
  122. $this->authSucceeded();
  123. return $response;
  124. }
  125. /**
  126. * Default landing page
  127. *
  128. * @since 14.0.0
  129. */
  130. abstract public function showShare(): TemplateResponse;
  131. /**
  132. * @since 14.0.0
  133. */
  134. final public function getAuthenticationRedirect(string $redirect): RedirectResponse {
  135. return new RedirectResponse(
  136. $this->urlGenerator->linkToRoute($this->getRoute('showAuthenticate'), ['token' => $this->getToken(), 'redirect' => $redirect])
  137. );
  138. }
  139. /**
  140. * @since 14.0.0
  141. */
  142. private function getRoute(string $function): string {
  143. $app = strtolower($this->appName);
  144. $class = strtolower((new \ReflectionClass($this))->getShortName());
  145. return $app . '.' . $class . '.' . $function;
  146. }
  147. /**
  148. * @since 14.0.0
  149. */
  150. private function getRedirect(): RedirectResponse {
  151. //Get all the stored redirect parameters:
  152. $params = $this->session->get('public_link_authenticate_redirect');
  153. $route = $this->getRoute('showShare');
  154. if ($params === null) {
  155. $params = [
  156. 'token' => $this->getToken(),
  157. ];
  158. } else {
  159. $params = json_decode($params, true);
  160. if (isset($params['_route'])) {
  161. $route = $params['_route'];
  162. unset($params['_route']);
  163. }
  164. }
  165. return new RedirectResponse($this->urlGenerator->linkToRoute($route, $params));
  166. }
  167. }