@@ -43,164 +43,164 @@ |
||
43 | 43 | */ |
44 | 44 | abstract class AuthPublicShareController extends PublicShareController { |
45 | 45 | |
46 | - /** @var IURLGenerator */ |
|
47 | - protected $urlGenerator; |
|
48 | - |
|
49 | - /** |
|
50 | - * @since 14.0.0 |
|
51 | - */ |
|
52 | - public function __construct(string $appName, |
|
53 | - IRequest $request, |
|
54 | - ISession $session, |
|
55 | - IURLGenerator $urlGenerator) { |
|
56 | - parent::__construct($appName, $request, $session); |
|
57 | - |
|
58 | - $this->urlGenerator = $urlGenerator; |
|
59 | - } |
|
60 | - |
|
61 | - /** |
|
62 | - * @PublicPage |
|
63 | - * @NoCSRFRequired |
|
64 | - * |
|
65 | - * Show the authentication page |
|
66 | - * The form has to submit to the authenticate method route |
|
67 | - * |
|
68 | - * @since 14.0.0 |
|
69 | - */ |
|
70 | - public function showAuthenticate(): TemplateResponse { |
|
71 | - return new TemplateResponse('core', 'publicshareauth', [], 'guest'); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * The template to show when authentication failed |
|
76 | - * |
|
77 | - * @since 14.0.0 |
|
78 | - */ |
|
79 | - protected function showAuthFailed(): TemplateResponse { |
|
80 | - return new TemplateResponse('core', 'publicshareauth', ['wrongpw' => true], 'guest'); |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * Verify the password |
|
85 | - * |
|
86 | - * @since 14.0.0 |
|
87 | - */ |
|
88 | - abstract protected function verifyPassword(string $password): bool; |
|
89 | - |
|
90 | - /** |
|
91 | - * Function called after failed authentication |
|
92 | - * |
|
93 | - * You can use this to do some logging for example |
|
94 | - * |
|
95 | - * @since 14.0.0 |
|
96 | - */ |
|
97 | - protected function authFailed() { |
|
98 | - } |
|
99 | - |
|
100 | - /** |
|
101 | - * Function called after successfull authentication |
|
102 | - * |
|
103 | - * You can use this to do some logging for example |
|
104 | - * |
|
105 | - * @since 14.0.0 |
|
106 | - */ |
|
107 | - protected function authSucceeded() { |
|
108 | - } |
|
109 | - |
|
110 | - /** |
|
111 | - * @UseSession |
|
112 | - * @PublicPage |
|
113 | - * @BruteForceProtection(action=publicLinkAuth) |
|
114 | - * |
|
115 | - * Authenticate the share |
|
116 | - * |
|
117 | - * @since 14.0.0 |
|
118 | - */ |
|
119 | - final public function authenticate(string $password = '') { |
|
120 | - // Already authenticated |
|
121 | - if ($this->isAuthenticated()) { |
|
122 | - return $this->getRedirect(); |
|
123 | - } |
|
124 | - |
|
125 | - if (!$this->verifyPassword($password)) { |
|
126 | - $this->authFailed(); |
|
127 | - $response = $this->showAuthFailed(); |
|
128 | - $response->throttle(); |
|
129 | - return $response; |
|
130 | - } |
|
131 | - |
|
132 | - $this->session->regenerateId(true, true); |
|
133 | - $response = $this->getRedirect(); |
|
134 | - |
|
135 | - $this->session->set('public_link_authenticated_token', $this->getToken()); |
|
136 | - $this->session->set('public_link_authenticated_password_hash', $this->getPasswordHash()); |
|
137 | - |
|
138 | - $this->authSucceeded(); |
|
139 | - |
|
140 | - return $response; |
|
141 | - } |
|
142 | - |
|
143 | - /** |
|
144 | - * Default landing page |
|
145 | - * |
|
146 | - * @since 14.0.0 |
|
147 | - */ |
|
148 | - abstract public function showShare(): TemplateResponse; |
|
149 | - |
|
150 | - /** |
|
151 | - * @since 14.0.0 |
|
152 | - */ |
|
153 | - final public function getAuthenticationRedirect(string $redirect): RedirectResponse { |
|
154 | - return new RedirectResponse( |
|
155 | - $this->urlGenerator->linkToRoute($this->getRoute('showAuthenticate'), ['token' => $this->getToken(), 'redirect' => $redirect]) |
|
156 | - ); |
|
157 | - } |
|
158 | - |
|
159 | - /** |
|
160 | - * @since 14.0.0 |
|
161 | - */ |
|
162 | - private function getRoute(string $function): string { |
|
163 | - $app = strtolower($this->appName); |
|
164 | - $class = strtolower((new \ReflectionClass($this))->getShortName()); |
|
165 | - |
|
166 | - return $app . '.' . $class . '.' . $function; |
|
167 | - } |
|
168 | - |
|
169 | - /** |
|
170 | - * @since 14.0.0 |
|
171 | - */ |
|
172 | - private function getRedirect(): RedirectResponse { |
|
173 | - //Get all the stored redirect parameters: |
|
174 | - $params = $this->session->get('public_link_authenticate_redirect'); |
|
175 | - |
|
176 | - $route = $this->getRoute('showShare'); |
|
177 | - |
|
178 | - if ($params === null) { |
|
179 | - $params = [ |
|
180 | - 'token' => $this->getToken(), |
|
181 | - ]; |
|
182 | - } else { |
|
183 | - $params = json_decode($params, true); |
|
184 | - if (isset($params['_route'])) { |
|
185 | - $route = $params['_route']; |
|
186 | - unset($params['_route']); |
|
187 | - } |
|
188 | - |
|
189 | - // If the token doesn't match the rest of the arguments can't be trusted either |
|
190 | - if (isset($params['token']) && $params['token'] !== $this->getToken()) { |
|
191 | - $params = [ |
|
192 | - 'token' => $this->getToken(), |
|
193 | - ]; |
|
194 | - } |
|
195 | - |
|
196 | - // We need a token |
|
197 | - if (!isset($params['token'])) { |
|
198 | - $params = [ |
|
199 | - 'token' => $this->getToken(), |
|
200 | - ]; |
|
201 | - } |
|
202 | - } |
|
203 | - |
|
204 | - return new RedirectResponse($this->urlGenerator->linkToRoute($route, $params)); |
|
205 | - } |
|
46 | + /** @var IURLGenerator */ |
|
47 | + protected $urlGenerator; |
|
48 | + |
|
49 | + /** |
|
50 | + * @since 14.0.0 |
|
51 | + */ |
|
52 | + public function __construct(string $appName, |
|
53 | + IRequest $request, |
|
54 | + ISession $session, |
|
55 | + IURLGenerator $urlGenerator) { |
|
56 | + parent::__construct($appName, $request, $session); |
|
57 | + |
|
58 | + $this->urlGenerator = $urlGenerator; |
|
59 | + } |
|
60 | + |
|
61 | + /** |
|
62 | + * @PublicPage |
|
63 | + * @NoCSRFRequired |
|
64 | + * |
|
65 | + * Show the authentication page |
|
66 | + * The form has to submit to the authenticate method route |
|
67 | + * |
|
68 | + * @since 14.0.0 |
|
69 | + */ |
|
70 | + public function showAuthenticate(): TemplateResponse { |
|
71 | + return new TemplateResponse('core', 'publicshareauth', [], 'guest'); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * The template to show when authentication failed |
|
76 | + * |
|
77 | + * @since 14.0.0 |
|
78 | + */ |
|
79 | + protected function showAuthFailed(): TemplateResponse { |
|
80 | + return new TemplateResponse('core', 'publicshareauth', ['wrongpw' => true], 'guest'); |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * Verify the password |
|
85 | + * |
|
86 | + * @since 14.0.0 |
|
87 | + */ |
|
88 | + abstract protected function verifyPassword(string $password): bool; |
|
89 | + |
|
90 | + /** |
|
91 | + * Function called after failed authentication |
|
92 | + * |
|
93 | + * You can use this to do some logging for example |
|
94 | + * |
|
95 | + * @since 14.0.0 |
|
96 | + */ |
|
97 | + protected function authFailed() { |
|
98 | + } |
|
99 | + |
|
100 | + /** |
|
101 | + * Function called after successfull authentication |
|
102 | + * |
|
103 | + * You can use this to do some logging for example |
|
104 | + * |
|
105 | + * @since 14.0.0 |
|
106 | + */ |
|
107 | + protected function authSucceeded() { |
|
108 | + } |
|
109 | + |
|
110 | + /** |
|
111 | + * @UseSession |
|
112 | + * @PublicPage |
|
113 | + * @BruteForceProtection(action=publicLinkAuth) |
|
114 | + * |
|
115 | + * Authenticate the share |
|
116 | + * |
|
117 | + * @since 14.0.0 |
|
118 | + */ |
|
119 | + final public function authenticate(string $password = '') { |
|
120 | + // Already authenticated |
|
121 | + if ($this->isAuthenticated()) { |
|
122 | + return $this->getRedirect(); |
|
123 | + } |
|
124 | + |
|
125 | + if (!$this->verifyPassword($password)) { |
|
126 | + $this->authFailed(); |
|
127 | + $response = $this->showAuthFailed(); |
|
128 | + $response->throttle(); |
|
129 | + return $response; |
|
130 | + } |
|
131 | + |
|
132 | + $this->session->regenerateId(true, true); |
|
133 | + $response = $this->getRedirect(); |
|
134 | + |
|
135 | + $this->session->set('public_link_authenticated_token', $this->getToken()); |
|
136 | + $this->session->set('public_link_authenticated_password_hash', $this->getPasswordHash()); |
|
137 | + |
|
138 | + $this->authSucceeded(); |
|
139 | + |
|
140 | + return $response; |
|
141 | + } |
|
142 | + |
|
143 | + /** |
|
144 | + * Default landing page |
|
145 | + * |
|
146 | + * @since 14.0.0 |
|
147 | + */ |
|
148 | + abstract public function showShare(): TemplateResponse; |
|
149 | + |
|
150 | + /** |
|
151 | + * @since 14.0.0 |
|
152 | + */ |
|
153 | + final public function getAuthenticationRedirect(string $redirect): RedirectResponse { |
|
154 | + return new RedirectResponse( |
|
155 | + $this->urlGenerator->linkToRoute($this->getRoute('showAuthenticate'), ['token' => $this->getToken(), 'redirect' => $redirect]) |
|
156 | + ); |
|
157 | + } |
|
158 | + |
|
159 | + /** |
|
160 | + * @since 14.0.0 |
|
161 | + */ |
|
162 | + private function getRoute(string $function): string { |
|
163 | + $app = strtolower($this->appName); |
|
164 | + $class = strtolower((new \ReflectionClass($this))->getShortName()); |
|
165 | + |
|
166 | + return $app . '.' . $class . '.' . $function; |
|
167 | + } |
|
168 | + |
|
169 | + /** |
|
170 | + * @since 14.0.0 |
|
171 | + */ |
|
172 | + private function getRedirect(): RedirectResponse { |
|
173 | + //Get all the stored redirect parameters: |
|
174 | + $params = $this->session->get('public_link_authenticate_redirect'); |
|
175 | + |
|
176 | + $route = $this->getRoute('showShare'); |
|
177 | + |
|
178 | + if ($params === null) { |
|
179 | + $params = [ |
|
180 | + 'token' => $this->getToken(), |
|
181 | + ]; |
|
182 | + } else { |
|
183 | + $params = json_decode($params, true); |
|
184 | + if (isset($params['_route'])) { |
|
185 | + $route = $params['_route']; |
|
186 | + unset($params['_route']); |
|
187 | + } |
|
188 | + |
|
189 | + // If the token doesn't match the rest of the arguments can't be trusted either |
|
190 | + if (isset($params['token']) && $params['token'] !== $this->getToken()) { |
|
191 | + $params = [ |
|
192 | + 'token' => $this->getToken(), |
|
193 | + ]; |
|
194 | + } |
|
195 | + |
|
196 | + // We need a token |
|
197 | + if (!isset($params['token'])) { |
|
198 | + $params = [ |
|
199 | + 'token' => $this->getToken(), |
|
200 | + ]; |
|
201 | + } |
|
202 | + } |
|
203 | + |
|
204 | + return new RedirectResponse($this->urlGenerator->linkToRoute($route, $params)); |
|
205 | + } |
|
206 | 206 | } |