@@ -99,7 +99,7 @@ |
||
99 | 99 | * @param bool $isLoggedIn |
100 | 100 | * @param bool $isAdminUser |
101 | 101 | * @param ContentSecurityPolicyManager $contentSecurityPolicyManager |
102 | - * @param CSRFTokenManager $csrfTokenManager |
|
102 | + * @param CsrfTokenManager $csrfTokenManager |
|
103 | 103 | * @param ContentSecurityPolicyNonceManager $cspNonceManager |
104 | 104 | */ |
105 | 105 | public function __construct(IRequest $request, |
@@ -63,207 +63,207 @@ |
||
63 | 63 | * check fails |
64 | 64 | */ |
65 | 65 | class SecurityMiddleware extends Middleware { |
66 | - /** @var INavigationManager */ |
|
67 | - private $navigationManager; |
|
68 | - /** @var IRequest */ |
|
69 | - private $request; |
|
70 | - /** @var ControllerMethodReflector */ |
|
71 | - private $reflector; |
|
72 | - /** @var string */ |
|
73 | - private $appName; |
|
74 | - /** @var IURLGenerator */ |
|
75 | - private $urlGenerator; |
|
76 | - /** @var ILogger */ |
|
77 | - private $logger; |
|
78 | - /** @var ISession */ |
|
79 | - private $session; |
|
80 | - /** @var bool */ |
|
81 | - private $isLoggedIn; |
|
82 | - /** @var bool */ |
|
83 | - private $isAdminUser; |
|
84 | - /** @var ContentSecurityPolicyManager */ |
|
85 | - private $contentSecurityPolicyManager; |
|
86 | - /** @var CsrfTokenManager */ |
|
87 | - private $csrfTokenManager; |
|
88 | - /** @var ContentSecurityPolicyNonceManager */ |
|
89 | - private $cspNonceManager; |
|
66 | + /** @var INavigationManager */ |
|
67 | + private $navigationManager; |
|
68 | + /** @var IRequest */ |
|
69 | + private $request; |
|
70 | + /** @var ControllerMethodReflector */ |
|
71 | + private $reflector; |
|
72 | + /** @var string */ |
|
73 | + private $appName; |
|
74 | + /** @var IURLGenerator */ |
|
75 | + private $urlGenerator; |
|
76 | + /** @var ILogger */ |
|
77 | + private $logger; |
|
78 | + /** @var ISession */ |
|
79 | + private $session; |
|
80 | + /** @var bool */ |
|
81 | + private $isLoggedIn; |
|
82 | + /** @var bool */ |
|
83 | + private $isAdminUser; |
|
84 | + /** @var ContentSecurityPolicyManager */ |
|
85 | + private $contentSecurityPolicyManager; |
|
86 | + /** @var CsrfTokenManager */ |
|
87 | + private $csrfTokenManager; |
|
88 | + /** @var ContentSecurityPolicyNonceManager */ |
|
89 | + private $cspNonceManager; |
|
90 | 90 | |
91 | - /** |
|
92 | - * @param IRequest $request |
|
93 | - * @param ControllerMethodReflector $reflector |
|
94 | - * @param INavigationManager $navigationManager |
|
95 | - * @param IURLGenerator $urlGenerator |
|
96 | - * @param ILogger $logger |
|
97 | - * @param ISession $session |
|
98 | - * @param string $appName |
|
99 | - * @param bool $isLoggedIn |
|
100 | - * @param bool $isAdminUser |
|
101 | - * @param ContentSecurityPolicyManager $contentSecurityPolicyManager |
|
102 | - * @param CSRFTokenManager $csrfTokenManager |
|
103 | - * @param ContentSecurityPolicyNonceManager $cspNonceManager |
|
104 | - */ |
|
105 | - public function __construct(IRequest $request, |
|
106 | - ControllerMethodReflector $reflector, |
|
107 | - INavigationManager $navigationManager, |
|
108 | - IURLGenerator $urlGenerator, |
|
109 | - ILogger $logger, |
|
110 | - ISession $session, |
|
111 | - $appName, |
|
112 | - $isLoggedIn, |
|
113 | - $isAdminUser, |
|
114 | - ContentSecurityPolicyManager $contentSecurityPolicyManager, |
|
115 | - CsrfTokenManager $csrfTokenManager, |
|
116 | - ContentSecurityPolicyNonceManager $cspNonceManager) { |
|
117 | - $this->navigationManager = $navigationManager; |
|
118 | - $this->request = $request; |
|
119 | - $this->reflector = $reflector; |
|
120 | - $this->appName = $appName; |
|
121 | - $this->urlGenerator = $urlGenerator; |
|
122 | - $this->logger = $logger; |
|
123 | - $this->session = $session; |
|
124 | - $this->isLoggedIn = $isLoggedIn; |
|
125 | - $this->isAdminUser = $isAdminUser; |
|
126 | - $this->contentSecurityPolicyManager = $contentSecurityPolicyManager; |
|
127 | - $this->csrfTokenManager = $csrfTokenManager; |
|
128 | - $this->cspNonceManager = $cspNonceManager; |
|
129 | - } |
|
91 | + /** |
|
92 | + * @param IRequest $request |
|
93 | + * @param ControllerMethodReflector $reflector |
|
94 | + * @param INavigationManager $navigationManager |
|
95 | + * @param IURLGenerator $urlGenerator |
|
96 | + * @param ILogger $logger |
|
97 | + * @param ISession $session |
|
98 | + * @param string $appName |
|
99 | + * @param bool $isLoggedIn |
|
100 | + * @param bool $isAdminUser |
|
101 | + * @param ContentSecurityPolicyManager $contentSecurityPolicyManager |
|
102 | + * @param CSRFTokenManager $csrfTokenManager |
|
103 | + * @param ContentSecurityPolicyNonceManager $cspNonceManager |
|
104 | + */ |
|
105 | + public function __construct(IRequest $request, |
|
106 | + ControllerMethodReflector $reflector, |
|
107 | + INavigationManager $navigationManager, |
|
108 | + IURLGenerator $urlGenerator, |
|
109 | + ILogger $logger, |
|
110 | + ISession $session, |
|
111 | + $appName, |
|
112 | + $isLoggedIn, |
|
113 | + $isAdminUser, |
|
114 | + ContentSecurityPolicyManager $contentSecurityPolicyManager, |
|
115 | + CsrfTokenManager $csrfTokenManager, |
|
116 | + ContentSecurityPolicyNonceManager $cspNonceManager) { |
|
117 | + $this->navigationManager = $navigationManager; |
|
118 | + $this->request = $request; |
|
119 | + $this->reflector = $reflector; |
|
120 | + $this->appName = $appName; |
|
121 | + $this->urlGenerator = $urlGenerator; |
|
122 | + $this->logger = $logger; |
|
123 | + $this->session = $session; |
|
124 | + $this->isLoggedIn = $isLoggedIn; |
|
125 | + $this->isAdminUser = $isAdminUser; |
|
126 | + $this->contentSecurityPolicyManager = $contentSecurityPolicyManager; |
|
127 | + $this->csrfTokenManager = $csrfTokenManager; |
|
128 | + $this->cspNonceManager = $cspNonceManager; |
|
129 | + } |
|
130 | 130 | |
131 | - /** |
|
132 | - * This runs all the security checks before a method call. The |
|
133 | - * security checks are determined by inspecting the controller method |
|
134 | - * annotations |
|
135 | - * @param Controller $controller the controller |
|
136 | - * @param string $methodName the name of the method |
|
137 | - * @throws SecurityException when a security check fails |
|
138 | - */ |
|
139 | - public function beforeController($controller, $methodName) { |
|
131 | + /** |
|
132 | + * This runs all the security checks before a method call. The |
|
133 | + * security checks are determined by inspecting the controller method |
|
134 | + * annotations |
|
135 | + * @param Controller $controller the controller |
|
136 | + * @param string $methodName the name of the method |
|
137 | + * @throws SecurityException when a security check fails |
|
138 | + */ |
|
139 | + public function beforeController($controller, $methodName) { |
|
140 | 140 | |
141 | - // this will set the current navigation entry of the app, use this only |
|
142 | - // for normal HTML requests and not for AJAX requests |
|
143 | - $this->navigationManager->setActiveEntry($this->appName); |
|
141 | + // this will set the current navigation entry of the app, use this only |
|
142 | + // for normal HTML requests and not for AJAX requests |
|
143 | + $this->navigationManager->setActiveEntry($this->appName); |
|
144 | 144 | |
145 | - // security checks |
|
146 | - $isPublicPage = $this->reflector->hasAnnotation('PublicPage'); |
|
147 | - if(!$isPublicPage) { |
|
148 | - if(!$this->isLoggedIn) { |
|
149 | - throw new NotLoggedInException(); |
|
150 | - } |
|
145 | + // security checks |
|
146 | + $isPublicPage = $this->reflector->hasAnnotation('PublicPage'); |
|
147 | + if(!$isPublicPage) { |
|
148 | + if(!$this->isLoggedIn) { |
|
149 | + throw new NotLoggedInException(); |
|
150 | + } |
|
151 | 151 | |
152 | - if(!$this->reflector->hasAnnotation('NoAdminRequired')) { |
|
153 | - if(!$this->isAdminUser) { |
|
154 | - throw new NotAdminException(); |
|
155 | - } |
|
156 | - } |
|
157 | - } |
|
152 | + if(!$this->reflector->hasAnnotation('NoAdminRequired')) { |
|
153 | + if(!$this->isAdminUser) { |
|
154 | + throw new NotAdminException(); |
|
155 | + } |
|
156 | + } |
|
157 | + } |
|
158 | 158 | |
159 | - if ($this->reflector->hasAnnotation('PasswordConfirmationRequired')) { |
|
160 | - $lastConfirm = (int) $this->session->get('last-password-confirm'); |
|
161 | - if ($lastConfirm < (time() - (30 * 60 + 15))) { // allow 15 seconds delay |
|
162 | - throw new NotConfirmedException(); |
|
163 | - } |
|
164 | - } |
|
159 | + if ($this->reflector->hasAnnotation('PasswordConfirmationRequired')) { |
|
160 | + $lastConfirm = (int) $this->session->get('last-password-confirm'); |
|
161 | + if ($lastConfirm < (time() - (30 * 60 + 15))) { // allow 15 seconds delay |
|
162 | + throw new NotConfirmedException(); |
|
163 | + } |
|
164 | + } |
|
165 | 165 | |
166 | - // Check for strict cookie requirement |
|
167 | - if($this->reflector->hasAnnotation('StrictCookieRequired') || !$this->reflector->hasAnnotation('NoCSRFRequired')) { |
|
168 | - if(!$this->request->passesStrictCookieCheck()) { |
|
169 | - throw new StrictCookieMissingException(); |
|
170 | - } |
|
171 | - } |
|
172 | - // CSRF check - also registers the CSRF token since the session may be closed later |
|
173 | - Util::callRegister(); |
|
174 | - if(!$this->reflector->hasAnnotation('NoCSRFRequired')) { |
|
175 | - /* |
|
166 | + // Check for strict cookie requirement |
|
167 | + if($this->reflector->hasAnnotation('StrictCookieRequired') || !$this->reflector->hasAnnotation('NoCSRFRequired')) { |
|
168 | + if(!$this->request->passesStrictCookieCheck()) { |
|
169 | + throw new StrictCookieMissingException(); |
|
170 | + } |
|
171 | + } |
|
172 | + // CSRF check - also registers the CSRF token since the session may be closed later |
|
173 | + Util::callRegister(); |
|
174 | + if(!$this->reflector->hasAnnotation('NoCSRFRequired')) { |
|
175 | + /* |
|
176 | 176 | * Only allow the CSRF check to fail on OCS Requests. This kind of |
177 | 177 | * hacks around that we have no full token auth in place yet and we |
178 | 178 | * do want to offer CSRF checks for web requests. |
179 | 179 | */ |
180 | - if(!$this->request->passesCSRFCheck() && !( |
|
181 | - $controller instanceof OCSController && |
|
182 | - $this->request->getHeader('OCS-APIREQUEST') === 'true')) { |
|
183 | - throw new CrossSiteRequestForgeryException(); |
|
184 | - } |
|
185 | - } |
|
180 | + if(!$this->request->passesCSRFCheck() && !( |
|
181 | + $controller instanceof OCSController && |
|
182 | + $this->request->getHeader('OCS-APIREQUEST') === 'true')) { |
|
183 | + throw new CrossSiteRequestForgeryException(); |
|
184 | + } |
|
185 | + } |
|
186 | 186 | |
187 | - /** |
|
188 | - * FIXME: Use DI once available |
|
189 | - * Checks if app is enabled (also includes a check whether user is allowed to access the resource) |
|
190 | - * The getAppPath() check is here since components such as settings also use the AppFramework and |
|
191 | - * therefore won't pass this check. |
|
192 | - */ |
|
193 | - if(\OC_App::getAppPath($this->appName) !== false && !\OC_App::isEnabled($this->appName)) { |
|
194 | - throw new AppNotEnabledException(); |
|
195 | - } |
|
187 | + /** |
|
188 | + * FIXME: Use DI once available |
|
189 | + * Checks if app is enabled (also includes a check whether user is allowed to access the resource) |
|
190 | + * The getAppPath() check is here since components such as settings also use the AppFramework and |
|
191 | + * therefore won't pass this check. |
|
192 | + */ |
|
193 | + if(\OC_App::getAppPath($this->appName) !== false && !\OC_App::isEnabled($this->appName)) { |
|
194 | + throw new AppNotEnabledException(); |
|
195 | + } |
|
196 | 196 | |
197 | - } |
|
197 | + } |
|
198 | 198 | |
199 | - /** |
|
200 | - * Performs the default CSP modifications that may be injected by other |
|
201 | - * applications |
|
202 | - * |
|
203 | - * @param Controller $controller |
|
204 | - * @param string $methodName |
|
205 | - * @param Response $response |
|
206 | - * @return Response |
|
207 | - */ |
|
208 | - public function afterController($controller, $methodName, Response $response) { |
|
209 | - $policy = !is_null($response->getContentSecurityPolicy()) ? $response->getContentSecurityPolicy() : new ContentSecurityPolicy(); |
|
199 | + /** |
|
200 | + * Performs the default CSP modifications that may be injected by other |
|
201 | + * applications |
|
202 | + * |
|
203 | + * @param Controller $controller |
|
204 | + * @param string $methodName |
|
205 | + * @param Response $response |
|
206 | + * @return Response |
|
207 | + */ |
|
208 | + public function afterController($controller, $methodName, Response $response) { |
|
209 | + $policy = !is_null($response->getContentSecurityPolicy()) ? $response->getContentSecurityPolicy() : new ContentSecurityPolicy(); |
|
210 | 210 | |
211 | - if (get_class($policy) === EmptyContentSecurityPolicy::class) { |
|
212 | - return $response; |
|
213 | - } |
|
211 | + if (get_class($policy) === EmptyContentSecurityPolicy::class) { |
|
212 | + return $response; |
|
213 | + } |
|
214 | 214 | |
215 | - $defaultPolicy = $this->contentSecurityPolicyManager->getDefaultPolicy(); |
|
216 | - $defaultPolicy = $this->contentSecurityPolicyManager->mergePolicies($defaultPolicy, $policy); |
|
215 | + $defaultPolicy = $this->contentSecurityPolicyManager->getDefaultPolicy(); |
|
216 | + $defaultPolicy = $this->contentSecurityPolicyManager->mergePolicies($defaultPolicy, $policy); |
|
217 | 217 | |
218 | - if($this->cspNonceManager->browserSupportsCspV3()) { |
|
219 | - $defaultPolicy->useJsNonce($this->csrfTokenManager->getToken()->getEncryptedValue()); |
|
220 | - } |
|
218 | + if($this->cspNonceManager->browserSupportsCspV3()) { |
|
219 | + $defaultPolicy->useJsNonce($this->csrfTokenManager->getToken()->getEncryptedValue()); |
|
220 | + } |
|
221 | 221 | |
222 | - $response->setContentSecurityPolicy($defaultPolicy); |
|
222 | + $response->setContentSecurityPolicy($defaultPolicy); |
|
223 | 223 | |
224 | - return $response; |
|
225 | - } |
|
224 | + return $response; |
|
225 | + } |
|
226 | 226 | |
227 | - /** |
|
228 | - * If an SecurityException is being caught, ajax requests return a JSON error |
|
229 | - * response and non ajax requests redirect to the index |
|
230 | - * @param Controller $controller the controller that is being called |
|
231 | - * @param string $methodName the name of the method that will be called on |
|
232 | - * the controller |
|
233 | - * @param \Exception $exception the thrown exception |
|
234 | - * @throws \Exception the passed in exception if it can't handle it |
|
235 | - * @return Response a Response object or null in case that the exception could not be handled |
|
236 | - */ |
|
237 | - public function afterException($controller, $methodName, \Exception $exception) { |
|
238 | - if($exception instanceof SecurityException) { |
|
239 | - if($exception instanceof StrictCookieMissingException) { |
|
240 | - return new RedirectResponse(\OC::$WEBROOT); |
|
241 | - } |
|
242 | - if (stripos($this->request->getHeader('Accept'),'html') === false) { |
|
243 | - $response = new JSONResponse( |
|
244 | - array('message' => $exception->getMessage()), |
|
245 | - $exception->getCode() |
|
246 | - ); |
|
247 | - } else { |
|
248 | - if($exception instanceof NotLoggedInException) { |
|
249 | - $url = $this->urlGenerator->linkToRoute( |
|
250 | - 'core.login.showLoginForm', |
|
251 | - [ |
|
252 | - 'redirect_url' => $this->request->server['REQUEST_URI'], |
|
253 | - ] |
|
254 | - ); |
|
255 | - $response = new RedirectResponse($url); |
|
256 | - } else { |
|
257 | - $response = new TemplateResponse('core', '403', ['file' => $exception->getMessage()], 'guest'); |
|
258 | - $response->setStatus($exception->getCode()); |
|
259 | - } |
|
260 | - } |
|
227 | + /** |
|
228 | + * If an SecurityException is being caught, ajax requests return a JSON error |
|
229 | + * response and non ajax requests redirect to the index |
|
230 | + * @param Controller $controller the controller that is being called |
|
231 | + * @param string $methodName the name of the method that will be called on |
|
232 | + * the controller |
|
233 | + * @param \Exception $exception the thrown exception |
|
234 | + * @throws \Exception the passed in exception if it can't handle it |
|
235 | + * @return Response a Response object or null in case that the exception could not be handled |
|
236 | + */ |
|
237 | + public function afterException($controller, $methodName, \Exception $exception) { |
|
238 | + if($exception instanceof SecurityException) { |
|
239 | + if($exception instanceof StrictCookieMissingException) { |
|
240 | + return new RedirectResponse(\OC::$WEBROOT); |
|
241 | + } |
|
242 | + if (stripos($this->request->getHeader('Accept'),'html') === false) { |
|
243 | + $response = new JSONResponse( |
|
244 | + array('message' => $exception->getMessage()), |
|
245 | + $exception->getCode() |
|
246 | + ); |
|
247 | + } else { |
|
248 | + if($exception instanceof NotLoggedInException) { |
|
249 | + $url = $this->urlGenerator->linkToRoute( |
|
250 | + 'core.login.showLoginForm', |
|
251 | + [ |
|
252 | + 'redirect_url' => $this->request->server['REQUEST_URI'], |
|
253 | + ] |
|
254 | + ); |
|
255 | + $response = new RedirectResponse($url); |
|
256 | + } else { |
|
257 | + $response = new TemplateResponse('core', '403', ['file' => $exception->getMessage()], 'guest'); |
|
258 | + $response->setStatus($exception->getCode()); |
|
259 | + } |
|
260 | + } |
|
261 | 261 | |
262 | - $this->logger->debug($exception->getMessage()); |
|
263 | - return $response; |
|
264 | - } |
|
262 | + $this->logger->debug($exception->getMessage()); |
|
263 | + return $response; |
|
264 | + } |
|
265 | 265 | |
266 | - throw $exception; |
|
267 | - } |
|
266 | + throw $exception; |
|
267 | + } |
|
268 | 268 | |
269 | 269 | } |
@@ -144,13 +144,13 @@ discard block |
||
144 | 144 | |
145 | 145 | // security checks |
146 | 146 | $isPublicPage = $this->reflector->hasAnnotation('PublicPage'); |
147 | - if(!$isPublicPage) { |
|
148 | - if(!$this->isLoggedIn) { |
|
147 | + if (!$isPublicPage) { |
|
148 | + if (!$this->isLoggedIn) { |
|
149 | 149 | throw new NotLoggedInException(); |
150 | 150 | } |
151 | 151 | |
152 | - if(!$this->reflector->hasAnnotation('NoAdminRequired')) { |
|
153 | - if(!$this->isAdminUser) { |
|
152 | + if (!$this->reflector->hasAnnotation('NoAdminRequired')) { |
|
153 | + if (!$this->isAdminUser) { |
|
154 | 154 | throw new NotAdminException(); |
155 | 155 | } |
156 | 156 | } |
@@ -164,20 +164,20 @@ discard block |
||
164 | 164 | } |
165 | 165 | |
166 | 166 | // Check for strict cookie requirement |
167 | - if($this->reflector->hasAnnotation('StrictCookieRequired') || !$this->reflector->hasAnnotation('NoCSRFRequired')) { |
|
168 | - if(!$this->request->passesStrictCookieCheck()) { |
|
167 | + if ($this->reflector->hasAnnotation('StrictCookieRequired') || !$this->reflector->hasAnnotation('NoCSRFRequired')) { |
|
168 | + if (!$this->request->passesStrictCookieCheck()) { |
|
169 | 169 | throw new StrictCookieMissingException(); |
170 | 170 | } |
171 | 171 | } |
172 | 172 | // CSRF check - also registers the CSRF token since the session may be closed later |
173 | 173 | Util::callRegister(); |
174 | - if(!$this->reflector->hasAnnotation('NoCSRFRequired')) { |
|
174 | + if (!$this->reflector->hasAnnotation('NoCSRFRequired')) { |
|
175 | 175 | /* |
176 | 176 | * Only allow the CSRF check to fail on OCS Requests. This kind of |
177 | 177 | * hacks around that we have no full token auth in place yet and we |
178 | 178 | * do want to offer CSRF checks for web requests. |
179 | 179 | */ |
180 | - if(!$this->request->passesCSRFCheck() && !( |
|
180 | + if (!$this->request->passesCSRFCheck() && !( |
|
181 | 181 | $controller instanceof OCSController && |
182 | 182 | $this->request->getHeader('OCS-APIREQUEST') === 'true')) { |
183 | 183 | throw new CrossSiteRequestForgeryException(); |
@@ -190,7 +190,7 @@ discard block |
||
190 | 190 | * The getAppPath() check is here since components such as settings also use the AppFramework and |
191 | 191 | * therefore won't pass this check. |
192 | 192 | */ |
193 | - if(\OC_App::getAppPath($this->appName) !== false && !\OC_App::isEnabled($this->appName)) { |
|
193 | + if (\OC_App::getAppPath($this->appName) !== false && !\OC_App::isEnabled($this->appName)) { |
|
194 | 194 | throw new AppNotEnabledException(); |
195 | 195 | } |
196 | 196 | |
@@ -215,7 +215,7 @@ discard block |
||
215 | 215 | $defaultPolicy = $this->contentSecurityPolicyManager->getDefaultPolicy(); |
216 | 216 | $defaultPolicy = $this->contentSecurityPolicyManager->mergePolicies($defaultPolicy, $policy); |
217 | 217 | |
218 | - if($this->cspNonceManager->browserSupportsCspV3()) { |
|
218 | + if ($this->cspNonceManager->browserSupportsCspV3()) { |
|
219 | 219 | $defaultPolicy->useJsNonce($this->csrfTokenManager->getToken()->getEncryptedValue()); |
220 | 220 | } |
221 | 221 | |
@@ -235,17 +235,17 @@ discard block |
||
235 | 235 | * @return Response a Response object or null in case that the exception could not be handled |
236 | 236 | */ |
237 | 237 | public function afterException($controller, $methodName, \Exception $exception) { |
238 | - if($exception instanceof SecurityException) { |
|
239 | - if($exception instanceof StrictCookieMissingException) { |
|
238 | + if ($exception instanceof SecurityException) { |
|
239 | + if ($exception instanceof StrictCookieMissingException) { |
|
240 | 240 | return new RedirectResponse(\OC::$WEBROOT); |
241 | 241 | } |
242 | - if (stripos($this->request->getHeader('Accept'),'html') === false) { |
|
242 | + if (stripos($this->request->getHeader('Accept'), 'html') === false) { |
|
243 | 243 | $response = new JSONResponse( |
244 | 244 | array('message' => $exception->getMessage()), |
245 | 245 | $exception->getCode() |
246 | 246 | ); |
247 | 247 | } else { |
248 | - if($exception instanceof NotLoggedInException) { |
|
248 | + if ($exception instanceof NotLoggedInException) { |
|
249 | 249 | $url = $this->urlGenerator->linkToRoute( |
250 | 250 | 'core.login.showLoginForm', |
251 | 251 | [ |
@@ -49,271 +49,271 @@ |
||
49 | 49 | use OC\Hooks\PublicEmitter; |
50 | 50 | |
51 | 51 | class LoginController extends Controller { |
52 | - /** @var IUserManager */ |
|
53 | - private $userManager; |
|
54 | - /** @var IConfig */ |
|
55 | - private $config; |
|
56 | - /** @var ISession */ |
|
57 | - private $session; |
|
58 | - /** @var IUserSession|Session */ |
|
59 | - private $userSession; |
|
60 | - /** @var IURLGenerator */ |
|
61 | - private $urlGenerator; |
|
62 | - /** @var ILogger */ |
|
63 | - private $logger; |
|
64 | - /** @var Manager */ |
|
65 | - private $twoFactorManager; |
|
52 | + /** @var IUserManager */ |
|
53 | + private $userManager; |
|
54 | + /** @var IConfig */ |
|
55 | + private $config; |
|
56 | + /** @var ISession */ |
|
57 | + private $session; |
|
58 | + /** @var IUserSession|Session */ |
|
59 | + private $userSession; |
|
60 | + /** @var IURLGenerator */ |
|
61 | + private $urlGenerator; |
|
62 | + /** @var ILogger */ |
|
63 | + private $logger; |
|
64 | + /** @var Manager */ |
|
65 | + private $twoFactorManager; |
|
66 | 66 | |
67 | - /** |
|
68 | - * @param string $appName |
|
69 | - * @param IRequest $request |
|
70 | - * @param IUserManager $userManager |
|
71 | - * @param IConfig $config |
|
72 | - * @param ISession $session |
|
73 | - * @param IUserSession $userSession |
|
74 | - * @param IURLGenerator $urlGenerator |
|
75 | - * @param ILogger $logger |
|
76 | - * @param Manager $twoFactorManager |
|
77 | - */ |
|
78 | - public function __construct($appName, |
|
79 | - IRequest $request, |
|
80 | - IUserManager $userManager, |
|
81 | - IConfig $config, |
|
82 | - ISession $session, |
|
83 | - IUserSession $userSession, |
|
84 | - IURLGenerator $urlGenerator, |
|
85 | - ILogger $logger, |
|
86 | - Manager $twoFactorManager) { |
|
87 | - parent::__construct($appName, $request); |
|
88 | - $this->userManager = $userManager; |
|
89 | - $this->config = $config; |
|
90 | - $this->session = $session; |
|
91 | - $this->userSession = $userSession; |
|
92 | - $this->urlGenerator = $urlGenerator; |
|
93 | - $this->logger = $logger; |
|
94 | - $this->twoFactorManager = $twoFactorManager; |
|
95 | - } |
|
67 | + /** |
|
68 | + * @param string $appName |
|
69 | + * @param IRequest $request |
|
70 | + * @param IUserManager $userManager |
|
71 | + * @param IConfig $config |
|
72 | + * @param ISession $session |
|
73 | + * @param IUserSession $userSession |
|
74 | + * @param IURLGenerator $urlGenerator |
|
75 | + * @param ILogger $logger |
|
76 | + * @param Manager $twoFactorManager |
|
77 | + */ |
|
78 | + public function __construct($appName, |
|
79 | + IRequest $request, |
|
80 | + IUserManager $userManager, |
|
81 | + IConfig $config, |
|
82 | + ISession $session, |
|
83 | + IUserSession $userSession, |
|
84 | + IURLGenerator $urlGenerator, |
|
85 | + ILogger $logger, |
|
86 | + Manager $twoFactorManager) { |
|
87 | + parent::__construct($appName, $request); |
|
88 | + $this->userManager = $userManager; |
|
89 | + $this->config = $config; |
|
90 | + $this->session = $session; |
|
91 | + $this->userSession = $userSession; |
|
92 | + $this->urlGenerator = $urlGenerator; |
|
93 | + $this->logger = $logger; |
|
94 | + $this->twoFactorManager = $twoFactorManager; |
|
95 | + } |
|
96 | 96 | |
97 | - /** |
|
98 | - * @NoAdminRequired |
|
99 | - * @UseSession |
|
100 | - * |
|
101 | - * @return RedirectResponse |
|
102 | - */ |
|
103 | - public function logout() { |
|
104 | - $loginToken = $this->request->getCookie('nc_token'); |
|
105 | - if (!is_null($loginToken)) { |
|
106 | - $this->config->deleteUserValue($this->userSession->getUser()->getUID(), 'login_token', $loginToken); |
|
107 | - } |
|
108 | - $this->userSession->logout(); |
|
97 | + /** |
|
98 | + * @NoAdminRequired |
|
99 | + * @UseSession |
|
100 | + * |
|
101 | + * @return RedirectResponse |
|
102 | + */ |
|
103 | + public function logout() { |
|
104 | + $loginToken = $this->request->getCookie('nc_token'); |
|
105 | + if (!is_null($loginToken)) { |
|
106 | + $this->config->deleteUserValue($this->userSession->getUser()->getUID(), 'login_token', $loginToken); |
|
107 | + } |
|
108 | + $this->userSession->logout(); |
|
109 | 109 | |
110 | - return new RedirectResponse($this->urlGenerator->linkToRouteAbsolute('core.login.showLoginForm')); |
|
111 | - } |
|
110 | + return new RedirectResponse($this->urlGenerator->linkToRouteAbsolute('core.login.showLoginForm')); |
|
111 | + } |
|
112 | 112 | |
113 | - /** |
|
114 | - * @PublicPage |
|
115 | - * @NoCSRFRequired |
|
116 | - * @UseSession |
|
117 | - * |
|
118 | - * @param string $user |
|
119 | - * @param string $redirect_url |
|
120 | - * @param string $remember_login |
|
121 | - * |
|
122 | - * @return TemplateResponse|RedirectResponse |
|
123 | - */ |
|
124 | - public function showLoginForm($user, $redirect_url, $remember_login) { |
|
125 | - if ($this->userSession->isLoggedIn()) { |
|
126 | - return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
127 | - } |
|
113 | + /** |
|
114 | + * @PublicPage |
|
115 | + * @NoCSRFRequired |
|
116 | + * @UseSession |
|
117 | + * |
|
118 | + * @param string $user |
|
119 | + * @param string $redirect_url |
|
120 | + * @param string $remember_login |
|
121 | + * |
|
122 | + * @return TemplateResponse|RedirectResponse |
|
123 | + */ |
|
124 | + public function showLoginForm($user, $redirect_url, $remember_login) { |
|
125 | + if ($this->userSession->isLoggedIn()) { |
|
126 | + return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
127 | + } |
|
128 | 128 | |
129 | - $parameters = array(); |
|
130 | - $loginMessages = $this->session->get('loginMessages'); |
|
131 | - $errors = []; |
|
132 | - $messages = []; |
|
133 | - if (is_array($loginMessages)) { |
|
134 | - list($errors, $messages) = $loginMessages; |
|
135 | - } |
|
136 | - $this->session->remove('loginMessages'); |
|
137 | - foreach ($errors as $value) { |
|
138 | - $parameters[$value] = true; |
|
139 | - } |
|
129 | + $parameters = array(); |
|
130 | + $loginMessages = $this->session->get('loginMessages'); |
|
131 | + $errors = []; |
|
132 | + $messages = []; |
|
133 | + if (is_array($loginMessages)) { |
|
134 | + list($errors, $messages) = $loginMessages; |
|
135 | + } |
|
136 | + $this->session->remove('loginMessages'); |
|
137 | + foreach ($errors as $value) { |
|
138 | + $parameters[$value] = true; |
|
139 | + } |
|
140 | 140 | |
141 | - $parameters['messages'] = $messages; |
|
142 | - if (!is_null($user) && $user !== '') { |
|
143 | - $parameters['loginName'] = $user; |
|
144 | - $parameters['user_autofocus'] = false; |
|
145 | - } else { |
|
146 | - $parameters['loginName'] = ''; |
|
147 | - $parameters['user_autofocus'] = true; |
|
148 | - } |
|
149 | - if (!empty($redirect_url)) { |
|
150 | - $parameters['redirect_url'] = $redirect_url; |
|
151 | - } |
|
141 | + $parameters['messages'] = $messages; |
|
142 | + if (!is_null($user) && $user !== '') { |
|
143 | + $parameters['loginName'] = $user; |
|
144 | + $parameters['user_autofocus'] = false; |
|
145 | + } else { |
|
146 | + $parameters['loginName'] = ''; |
|
147 | + $parameters['user_autofocus'] = true; |
|
148 | + } |
|
149 | + if (!empty($redirect_url)) { |
|
150 | + $parameters['redirect_url'] = $redirect_url; |
|
151 | + } |
|
152 | 152 | |
153 | - $parameters['canResetPassword'] = true; |
|
154 | - $parameters['resetPasswordLink'] = $this->config->getSystemValue('lost_password_link', ''); |
|
155 | - if (!$parameters['resetPasswordLink']) { |
|
156 | - if (!is_null($user) && $user !== '') { |
|
157 | - $userObj = $this->userManager->get($user); |
|
158 | - if ($userObj instanceof IUser) { |
|
159 | - $parameters['canResetPassword'] = $userObj->canChangePassword(); |
|
160 | - } |
|
161 | - } |
|
162 | - } |
|
153 | + $parameters['canResetPassword'] = true; |
|
154 | + $parameters['resetPasswordLink'] = $this->config->getSystemValue('lost_password_link', ''); |
|
155 | + if (!$parameters['resetPasswordLink']) { |
|
156 | + if (!is_null($user) && $user !== '') { |
|
157 | + $userObj = $this->userManager->get($user); |
|
158 | + if ($userObj instanceof IUser) { |
|
159 | + $parameters['canResetPassword'] = $userObj->canChangePassword(); |
|
160 | + } |
|
161 | + } |
|
162 | + } |
|
163 | 163 | |
164 | - $parameters['alt_login'] = OC_App::getAlternativeLogIns(); |
|
165 | - $parameters['rememberLoginState'] = !empty($remember_login) ? $remember_login : 0; |
|
164 | + $parameters['alt_login'] = OC_App::getAlternativeLogIns(); |
|
165 | + $parameters['rememberLoginState'] = !empty($remember_login) ? $remember_login : 0; |
|
166 | 166 | |
167 | - if (!is_null($user) && $user !== '') { |
|
168 | - $parameters['loginName'] = $user; |
|
169 | - $parameters['user_autofocus'] = false; |
|
170 | - } else { |
|
171 | - $parameters['loginName'] = ''; |
|
172 | - $parameters['user_autofocus'] = true; |
|
173 | - } |
|
167 | + if (!is_null($user) && $user !== '') { |
|
168 | + $parameters['loginName'] = $user; |
|
169 | + $parameters['user_autofocus'] = false; |
|
170 | + } else { |
|
171 | + $parameters['loginName'] = ''; |
|
172 | + $parameters['user_autofocus'] = true; |
|
173 | + } |
|
174 | 174 | |
175 | - return new TemplateResponse( |
|
176 | - $this->appName, 'login', $parameters, 'guest' |
|
177 | - ); |
|
178 | - } |
|
175 | + return new TemplateResponse( |
|
176 | + $this->appName, 'login', $parameters, 'guest' |
|
177 | + ); |
|
178 | + } |
|
179 | 179 | |
180 | - /** |
|
181 | - * @param string $redirectUrl |
|
182 | - * @return RedirectResponse |
|
183 | - */ |
|
184 | - private function generateRedirect($redirectUrl) { |
|
185 | - if (!is_null($redirectUrl) && $this->userSession->isLoggedIn()) { |
|
186 | - $location = $this->urlGenerator->getAbsoluteURL(urldecode($redirectUrl)); |
|
187 | - // Deny the redirect if the URL contains a @ |
|
188 | - // This prevents unvalidated redirects like ?redirect_url=:[email protected] |
|
189 | - if (strpos($location, '@') === false) { |
|
190 | - return new RedirectResponse($location); |
|
191 | - } |
|
192 | - } |
|
193 | - return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
194 | - } |
|
180 | + /** |
|
181 | + * @param string $redirectUrl |
|
182 | + * @return RedirectResponse |
|
183 | + */ |
|
184 | + private function generateRedirect($redirectUrl) { |
|
185 | + if (!is_null($redirectUrl) && $this->userSession->isLoggedIn()) { |
|
186 | + $location = $this->urlGenerator->getAbsoluteURL(urldecode($redirectUrl)); |
|
187 | + // Deny the redirect if the URL contains a @ |
|
188 | + // This prevents unvalidated redirects like ?redirect_url=:[email protected] |
|
189 | + if (strpos($location, '@') === false) { |
|
190 | + return new RedirectResponse($location); |
|
191 | + } |
|
192 | + } |
|
193 | + return new RedirectResponse(OC_Util::getDefaultPageUrl()); |
|
194 | + } |
|
195 | 195 | |
196 | - /** |
|
197 | - * @PublicPage |
|
198 | - * @UseSession |
|
199 | - * @NoCSRFRequired |
|
200 | - * @BruteForceProtection(action=login) |
|
201 | - * |
|
202 | - * @param string $user |
|
203 | - * @param string $password |
|
204 | - * @param string $redirect_url |
|
205 | - * @param boolean $remember_login |
|
206 | - * @param string $timezone |
|
207 | - * @param string $timezone_offset |
|
208 | - * @return RedirectResponse |
|
209 | - */ |
|
210 | - public function tryLogin($user, $password, $redirect_url, $remember_login = false, $timezone = '', $timezone_offset = '') { |
|
211 | - if(!is_string($user)) { |
|
212 | - throw new \InvalidArgumentException('Username must be string'); |
|
213 | - } |
|
196 | + /** |
|
197 | + * @PublicPage |
|
198 | + * @UseSession |
|
199 | + * @NoCSRFRequired |
|
200 | + * @BruteForceProtection(action=login) |
|
201 | + * |
|
202 | + * @param string $user |
|
203 | + * @param string $password |
|
204 | + * @param string $redirect_url |
|
205 | + * @param boolean $remember_login |
|
206 | + * @param string $timezone |
|
207 | + * @param string $timezone_offset |
|
208 | + * @return RedirectResponse |
|
209 | + */ |
|
210 | + public function tryLogin($user, $password, $redirect_url, $remember_login = false, $timezone = '', $timezone_offset = '') { |
|
211 | + if(!is_string($user)) { |
|
212 | + throw new \InvalidArgumentException('Username must be string'); |
|
213 | + } |
|
214 | 214 | |
215 | - // If the user is already logged in and the CSRF check does not pass then |
|
216 | - // simply redirect the user to the correct page as required. This is the |
|
217 | - // case when an user has already logged-in, in another tab. |
|
218 | - if(!$this->request->passesCSRFCheck()) { |
|
219 | - return $this->generateRedirect($redirect_url); |
|
220 | - } |
|
215 | + // If the user is already logged in and the CSRF check does not pass then |
|
216 | + // simply redirect the user to the correct page as required. This is the |
|
217 | + // case when an user has already logged-in, in another tab. |
|
218 | + if(!$this->request->passesCSRFCheck()) { |
|
219 | + return $this->generateRedirect($redirect_url); |
|
220 | + } |
|
221 | 221 | |
222 | - if ($this->userManager instanceof PublicEmitter) { |
|
223 | - $this->userManager->emit('\OC\User', 'preLogin', array($user, $password)); |
|
224 | - } |
|
222 | + if ($this->userManager instanceof PublicEmitter) { |
|
223 | + $this->userManager->emit('\OC\User', 'preLogin', array($user, $password)); |
|
224 | + } |
|
225 | 225 | |
226 | - $originalUser = $user; |
|
227 | - // TODO: Add all the insane error handling |
|
228 | - /* @var $loginResult IUser */ |
|
229 | - $loginResult = $this->userManager->checkPasswordNoLogging($user, $password); |
|
230 | - if ($loginResult === false) { |
|
231 | - $users = $this->userManager->getByEmail($user); |
|
232 | - // we only allow login by email if unique |
|
233 | - if (count($users) === 1) { |
|
234 | - $user = $users[0]->getUID(); |
|
235 | - $loginResult = $this->userManager->checkPassword($user, $password); |
|
236 | - } else { |
|
237 | - $this->logger->warning('Login failed: \''. $user .'\' (Remote IP: \''. $this->request->getRemoteAddress(). '\')', ['app' => 'core']); |
|
238 | - } |
|
239 | - } |
|
240 | - if ($loginResult === false) { |
|
241 | - // Read current user and append if possible - we need to return the unmodified user otherwise we will leak the login name |
|
242 | - $args = !is_null($user) ? ['user' => $originalUser] : []; |
|
243 | - $response = new RedirectResponse($this->urlGenerator->linkToRoute('core.login.showLoginForm', $args)); |
|
244 | - $response->throttle(); |
|
245 | - $this->session->set('loginMessages', [ |
|
246 | - ['invalidpassword'], [] |
|
247 | - ]); |
|
248 | - return $response; |
|
249 | - } |
|
250 | - // TODO: remove password checks from above and let the user session handle failures |
|
251 | - // requires https://github.com/owncloud/core/pull/24616 |
|
252 | - $this->userSession->completeLogin($loginResult, ['loginName' => $user, 'password' => $password]); |
|
253 | - $this->userSession->createSessionToken($this->request, $loginResult->getUID(), $user, $password, (int)$remember_login); |
|
226 | + $originalUser = $user; |
|
227 | + // TODO: Add all the insane error handling |
|
228 | + /* @var $loginResult IUser */ |
|
229 | + $loginResult = $this->userManager->checkPasswordNoLogging($user, $password); |
|
230 | + if ($loginResult === false) { |
|
231 | + $users = $this->userManager->getByEmail($user); |
|
232 | + // we only allow login by email if unique |
|
233 | + if (count($users) === 1) { |
|
234 | + $user = $users[0]->getUID(); |
|
235 | + $loginResult = $this->userManager->checkPassword($user, $password); |
|
236 | + } else { |
|
237 | + $this->logger->warning('Login failed: \''. $user .'\' (Remote IP: \''. $this->request->getRemoteAddress(). '\')', ['app' => 'core']); |
|
238 | + } |
|
239 | + } |
|
240 | + if ($loginResult === false) { |
|
241 | + // Read current user and append if possible - we need to return the unmodified user otherwise we will leak the login name |
|
242 | + $args = !is_null($user) ? ['user' => $originalUser] : []; |
|
243 | + $response = new RedirectResponse($this->urlGenerator->linkToRoute('core.login.showLoginForm', $args)); |
|
244 | + $response->throttle(); |
|
245 | + $this->session->set('loginMessages', [ |
|
246 | + ['invalidpassword'], [] |
|
247 | + ]); |
|
248 | + return $response; |
|
249 | + } |
|
250 | + // TODO: remove password checks from above and let the user session handle failures |
|
251 | + // requires https://github.com/owncloud/core/pull/24616 |
|
252 | + $this->userSession->completeLogin($loginResult, ['loginName' => $user, 'password' => $password]); |
|
253 | + $this->userSession->createSessionToken($this->request, $loginResult->getUID(), $user, $password, (int)$remember_login); |
|
254 | 254 | |
255 | - // User has successfully logged in, now remove the password reset link, when it is available |
|
256 | - $this->config->deleteUserValue($loginResult->getUID(), 'core', 'lostpassword'); |
|
255 | + // User has successfully logged in, now remove the password reset link, when it is available |
|
256 | + $this->config->deleteUserValue($loginResult->getUID(), 'core', 'lostpassword'); |
|
257 | 257 | |
258 | - $this->session->set('last-password-confirm', $loginResult->getLastLogin()); |
|
258 | + $this->session->set('last-password-confirm', $loginResult->getLastLogin()); |
|
259 | 259 | |
260 | - if ($timezone_offset !== '') { |
|
261 | - $this->config->setUserValue($loginResult->getUID(), 'core', 'timezone', $timezone); |
|
262 | - $this->session->set('timezone', $timezone_offset); |
|
263 | - } |
|
260 | + if ($timezone_offset !== '') { |
|
261 | + $this->config->setUserValue($loginResult->getUID(), 'core', 'timezone', $timezone); |
|
262 | + $this->session->set('timezone', $timezone_offset); |
|
263 | + } |
|
264 | 264 | |
265 | - if ($this->twoFactorManager->isTwoFactorAuthenticated($loginResult)) { |
|
266 | - $this->twoFactorManager->prepareTwoFactorLogin($loginResult, $remember_login); |
|
265 | + if ($this->twoFactorManager->isTwoFactorAuthenticated($loginResult)) { |
|
266 | + $this->twoFactorManager->prepareTwoFactorLogin($loginResult, $remember_login); |
|
267 | 267 | |
268 | - $providers = $this->twoFactorManager->getProviders($loginResult); |
|
269 | - if (count($providers) === 1) { |
|
270 | - // Single provider, hence we can redirect to that provider's challenge page directly |
|
271 | - /* @var $provider IProvider */ |
|
272 | - $provider = array_pop($providers); |
|
273 | - $url = 'core.TwoFactorChallenge.showChallenge'; |
|
274 | - $urlParams = [ |
|
275 | - 'challengeProviderId' => $provider->getId(), |
|
276 | - ]; |
|
277 | - } else { |
|
278 | - $url = 'core.TwoFactorChallenge.selectChallenge'; |
|
279 | - $urlParams = []; |
|
280 | - } |
|
268 | + $providers = $this->twoFactorManager->getProviders($loginResult); |
|
269 | + if (count($providers) === 1) { |
|
270 | + // Single provider, hence we can redirect to that provider's challenge page directly |
|
271 | + /* @var $provider IProvider */ |
|
272 | + $provider = array_pop($providers); |
|
273 | + $url = 'core.TwoFactorChallenge.showChallenge'; |
|
274 | + $urlParams = [ |
|
275 | + 'challengeProviderId' => $provider->getId(), |
|
276 | + ]; |
|
277 | + } else { |
|
278 | + $url = 'core.TwoFactorChallenge.selectChallenge'; |
|
279 | + $urlParams = []; |
|
280 | + } |
|
281 | 281 | |
282 | - if (!is_null($redirect_url)) { |
|
283 | - $urlParams['redirect_url'] = $redirect_url; |
|
284 | - } |
|
282 | + if (!is_null($redirect_url)) { |
|
283 | + $urlParams['redirect_url'] = $redirect_url; |
|
284 | + } |
|
285 | 285 | |
286 | - return new RedirectResponse($this->urlGenerator->linkToRoute($url, $urlParams)); |
|
287 | - } |
|
286 | + return new RedirectResponse($this->urlGenerator->linkToRoute($url, $urlParams)); |
|
287 | + } |
|
288 | 288 | |
289 | - if ($remember_login) { |
|
290 | - $this->userSession->createRememberMeToken($loginResult); |
|
291 | - } |
|
289 | + if ($remember_login) { |
|
290 | + $this->userSession->createRememberMeToken($loginResult); |
|
291 | + } |
|
292 | 292 | |
293 | - return $this->generateRedirect($redirect_url); |
|
294 | - } |
|
293 | + return $this->generateRedirect($redirect_url); |
|
294 | + } |
|
295 | 295 | |
296 | - /** |
|
297 | - * @NoAdminRequired |
|
298 | - * @UseSession |
|
299 | - * @BruteForceProtection(action=sudo) |
|
300 | - * |
|
301 | - * @license GNU AGPL version 3 or any later version |
|
302 | - * |
|
303 | - * @param string $password |
|
304 | - * @return DataResponse |
|
305 | - */ |
|
306 | - public function confirmPassword($password) { |
|
307 | - $loginName = $this->userSession->getLoginName(); |
|
308 | - $loginResult = $this->userManager->checkPassword($loginName, $password); |
|
309 | - if ($loginResult === false) { |
|
310 | - $response = new DataResponse([], Http::STATUS_FORBIDDEN); |
|
311 | - $response->throttle(); |
|
312 | - return $response; |
|
313 | - } |
|
296 | + /** |
|
297 | + * @NoAdminRequired |
|
298 | + * @UseSession |
|
299 | + * @BruteForceProtection(action=sudo) |
|
300 | + * |
|
301 | + * @license GNU AGPL version 3 or any later version |
|
302 | + * |
|
303 | + * @param string $password |
|
304 | + * @return DataResponse |
|
305 | + */ |
|
306 | + public function confirmPassword($password) { |
|
307 | + $loginName = $this->userSession->getLoginName(); |
|
308 | + $loginResult = $this->userManager->checkPassword($loginName, $password); |
|
309 | + if ($loginResult === false) { |
|
310 | + $response = new DataResponse([], Http::STATUS_FORBIDDEN); |
|
311 | + $response->throttle(); |
|
312 | + return $response; |
|
313 | + } |
|
314 | 314 | |
315 | - $confirmTimestamp = time(); |
|
316 | - $this->session->set('last-password-confirm', $confirmTimestamp); |
|
317 | - return new DataResponse(['lastLogin' => $confirmTimestamp], Http::STATUS_OK); |
|
318 | - } |
|
315 | + $confirmTimestamp = time(); |
|
316 | + $this->session->set('last-password-confirm', $confirmTimestamp); |
|
317 | + return new DataResponse(['lastLogin' => $confirmTimestamp], Http::STATUS_OK); |
|
318 | + } |
|
319 | 319 | } |
@@ -208,14 +208,14 @@ discard block |
||
208 | 208 | * @return RedirectResponse |
209 | 209 | */ |
210 | 210 | public function tryLogin($user, $password, $redirect_url, $remember_login = false, $timezone = '', $timezone_offset = '') { |
211 | - if(!is_string($user)) { |
|
211 | + if (!is_string($user)) { |
|
212 | 212 | throw new \InvalidArgumentException('Username must be string'); |
213 | 213 | } |
214 | 214 | |
215 | 215 | // If the user is already logged in and the CSRF check does not pass then |
216 | 216 | // simply redirect the user to the correct page as required. This is the |
217 | 217 | // case when an user has already logged-in, in another tab. |
218 | - if(!$this->request->passesCSRFCheck()) { |
|
218 | + if (!$this->request->passesCSRFCheck()) { |
|
219 | 219 | return $this->generateRedirect($redirect_url); |
220 | 220 | } |
221 | 221 | |
@@ -234,7 +234,7 @@ discard block |
||
234 | 234 | $user = $users[0]->getUID(); |
235 | 235 | $loginResult = $this->userManager->checkPassword($user, $password); |
236 | 236 | } else { |
237 | - $this->logger->warning('Login failed: \''. $user .'\' (Remote IP: \''. $this->request->getRemoteAddress(). '\')', ['app' => 'core']); |
|
237 | + $this->logger->warning('Login failed: \''.$user.'\' (Remote IP: \''.$this->request->getRemoteAddress().'\')', ['app' => 'core']); |
|
238 | 238 | } |
239 | 239 | } |
240 | 240 | if ($loginResult === false) { |
@@ -250,7 +250,7 @@ discard block |
||
250 | 250 | // TODO: remove password checks from above and let the user session handle failures |
251 | 251 | // requires https://github.com/owncloud/core/pull/24616 |
252 | 252 | $this->userSession->completeLogin($loginResult, ['loginName' => $user, 'password' => $password]); |
253 | - $this->userSession->createSessionToken($this->request, $loginResult->getUID(), $user, $password, (int)$remember_login); |
|
253 | + $this->userSession->createSessionToken($this->request, $loginResult->getUID(), $user, $password, (int) $remember_login); |
|
254 | 254 | |
255 | 255 | // User has successfully logged in, now remove the password reset link, when it is available |
256 | 256 | $this->config->deleteUserValue($loginResult->getUID(), 'core', 'lostpassword'); |
@@ -35,49 +35,49 @@ |
||
35 | 35 | * @package OC\AppFramework\Middleware\Security |
36 | 36 | */ |
37 | 37 | class BruteForceMiddleware extends Middleware { |
38 | - /** @var ControllerMethodReflector */ |
|
39 | - private $reflector; |
|
40 | - /** @var Throttler */ |
|
41 | - private $throttler; |
|
42 | - /** @var IRequest */ |
|
43 | - private $request; |
|
38 | + /** @var ControllerMethodReflector */ |
|
39 | + private $reflector; |
|
40 | + /** @var Throttler */ |
|
41 | + private $throttler; |
|
42 | + /** @var IRequest */ |
|
43 | + private $request; |
|
44 | 44 | |
45 | - /** |
|
46 | - * @param ControllerMethodReflector $controllerMethodReflector |
|
47 | - * @param Throttler $throttler |
|
48 | - * @param IRequest $request |
|
49 | - */ |
|
50 | - public function __construct(ControllerMethodReflector $controllerMethodReflector, |
|
51 | - Throttler $throttler, |
|
52 | - IRequest $request) { |
|
53 | - $this->reflector = $controllerMethodReflector; |
|
54 | - $this->throttler = $throttler; |
|
55 | - $this->request = $request; |
|
56 | - } |
|
45 | + /** |
|
46 | + * @param ControllerMethodReflector $controllerMethodReflector |
|
47 | + * @param Throttler $throttler |
|
48 | + * @param IRequest $request |
|
49 | + */ |
|
50 | + public function __construct(ControllerMethodReflector $controllerMethodReflector, |
|
51 | + Throttler $throttler, |
|
52 | + IRequest $request) { |
|
53 | + $this->reflector = $controllerMethodReflector; |
|
54 | + $this->throttler = $throttler; |
|
55 | + $this->request = $request; |
|
56 | + } |
|
57 | 57 | |
58 | - /** |
|
59 | - * {@inheritDoc} |
|
60 | - */ |
|
61 | - public function beforeController($controller, $methodName) { |
|
62 | - parent::beforeController($controller, $methodName); |
|
58 | + /** |
|
59 | + * {@inheritDoc} |
|
60 | + */ |
|
61 | + public function beforeController($controller, $methodName) { |
|
62 | + parent::beforeController($controller, $methodName); |
|
63 | 63 | |
64 | - if($this->reflector->hasAnnotation('BruteForceProtection')) { |
|
65 | - $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action'); |
|
66 | - $this->throttler->sleepDelay($this->request->getRemoteAddress(), $action); |
|
67 | - } |
|
68 | - } |
|
64 | + if($this->reflector->hasAnnotation('BruteForceProtection')) { |
|
65 | + $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action'); |
|
66 | + $this->throttler->sleepDelay($this->request->getRemoteAddress(), $action); |
|
67 | + } |
|
68 | + } |
|
69 | 69 | |
70 | - /** |
|
71 | - * {@inheritDoc} |
|
72 | - */ |
|
73 | - public function afterController($controller, $methodName, Response $response) { |
|
74 | - if($this->reflector->hasAnnotation('BruteForceProtection') && $response->isThrottled()) { |
|
75 | - $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action'); |
|
76 | - $ip = $this->request->getRemoteAddress(); |
|
77 | - $this->throttler->sleepDelay($ip, $action); |
|
78 | - $this->throttler->registerAttempt($action, $ip); |
|
79 | - } |
|
70 | + /** |
|
71 | + * {@inheritDoc} |
|
72 | + */ |
|
73 | + public function afterController($controller, $methodName, Response $response) { |
|
74 | + if($this->reflector->hasAnnotation('BruteForceProtection') && $response->isThrottled()) { |
|
75 | + $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action'); |
|
76 | + $ip = $this->request->getRemoteAddress(); |
|
77 | + $this->throttler->sleepDelay($ip, $action); |
|
78 | + $this->throttler->registerAttempt($action, $ip); |
|
79 | + } |
|
80 | 80 | |
81 | - return parent::afterController($controller, $methodName, $response); |
|
82 | - } |
|
81 | + return parent::afterController($controller, $methodName, $response); |
|
82 | + } |
|
83 | 83 | } |
@@ -61,7 +61,7 @@ discard block |
||
61 | 61 | public function beforeController($controller, $methodName) { |
62 | 62 | parent::beforeController($controller, $methodName); |
63 | 63 | |
64 | - if($this->reflector->hasAnnotation('BruteForceProtection')) { |
|
64 | + if ($this->reflector->hasAnnotation('BruteForceProtection')) { |
|
65 | 65 | $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action'); |
66 | 66 | $this->throttler->sleepDelay($this->request->getRemoteAddress(), $action); |
67 | 67 | } |
@@ -71,7 +71,7 @@ discard block |
||
71 | 71 | * {@inheritDoc} |
72 | 72 | */ |
73 | 73 | public function afterController($controller, $methodName, Response $response) { |
74 | - if($this->reflector->hasAnnotation('BruteForceProtection') && $response->isThrottled()) { |
|
74 | + if ($this->reflector->hasAnnotation('BruteForceProtection') && $response->isThrottled()) { |
|
75 | 75 | $action = $this->reflector->getAnnotationParameter('BruteForceProtection', 'action'); |
76 | 76 | $ip = $this->request->getRemoteAddress(); |
77 | 77 | $this->throttler->sleepDelay($ip, $action); |
@@ -62,376 +62,376 @@ |
||
62 | 62 | |
63 | 63 | class DIContainer extends SimpleContainer implements IAppContainer { |
64 | 64 | |
65 | - /** |
|
66 | - * @var array |
|
67 | - */ |
|
68 | - private $middleWares = array(); |
|
69 | - |
|
70 | - /** @var ServerContainer */ |
|
71 | - private $server; |
|
72 | - |
|
73 | - /** |
|
74 | - * Put your class dependencies in here |
|
75 | - * @param string $appName the name of the app |
|
76 | - * @param array $urlParams |
|
77 | - * @param ServerContainer $server |
|
78 | - */ |
|
79 | - public function __construct($appName, $urlParams = array(), ServerContainer $server = null){ |
|
80 | - parent::__construct(); |
|
81 | - $this['AppName'] = $appName; |
|
82 | - $this['urlParams'] = $urlParams; |
|
83 | - |
|
84 | - /** @var \OC\ServerContainer $server */ |
|
85 | - if ($server === null) { |
|
86 | - $server = \OC::$server; |
|
87 | - } |
|
88 | - $this->server = $server; |
|
89 | - $this->server->registerAppContainer($appName, $this); |
|
90 | - |
|
91 | - // aliases |
|
92 | - $this->registerAlias('appName', 'AppName'); |
|
93 | - $this->registerAlias('webRoot', 'WebRoot'); |
|
94 | - $this->registerAlias('userId', 'UserId'); |
|
95 | - |
|
96 | - /** |
|
97 | - * Core services |
|
98 | - */ |
|
99 | - $this->registerService(IOutput::class, function($c){ |
|
100 | - return new Output($this->getServer()->getWebRoot()); |
|
101 | - }); |
|
102 | - |
|
103 | - $this->registerService(Folder::class, function() { |
|
104 | - return $this->getServer()->getUserFolder(); |
|
105 | - }); |
|
106 | - |
|
107 | - $this->registerService(IAppData::class, function (SimpleContainer $c) { |
|
108 | - return $this->getServer()->getAppDataDir($c->query('AppName')); |
|
109 | - }); |
|
110 | - |
|
111 | - $this->registerService(IL10N::class, function($c) { |
|
112 | - return $this->getServer()->getL10N($c->query('AppName')); |
|
113 | - }); |
|
114 | - |
|
115 | - $this->registerAlias(\OCP\AppFramework\Utility\IControllerMethodReflector::class, \OC\AppFramework\Utility\ControllerMethodReflector::class); |
|
116 | - $this->registerAlias('ControllerMethodReflector', \OCP\AppFramework\Utility\IControllerMethodReflector::class); |
|
117 | - |
|
118 | - $this->registerService(IRequest::class, function() { |
|
119 | - return $this->getServer()->query(IRequest::class); |
|
120 | - }); |
|
121 | - $this->registerAlias('Request', IRequest::class); |
|
122 | - |
|
123 | - $this->registerAlias(\OCP\AppFramework\Utility\ITimeFactory::class, \OC\AppFramework\Utility\TimeFactory::class); |
|
124 | - $this->registerAlias('TimeFactory', \OCP\AppFramework\Utility\ITimeFactory::class); |
|
125 | - |
|
126 | - $this->registerAlias(\OC\User\Session::class, \OCP\IUserSession::class); |
|
127 | - |
|
128 | - $this->registerService(IServerContainer::class, function ($c) { |
|
129 | - return $this->getServer(); |
|
130 | - }); |
|
131 | - $this->registerAlias('ServerContainer', IServerContainer::class); |
|
132 | - |
|
133 | - $this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) { |
|
134 | - return $c->query('OCA\WorkflowEngine\Manager'); |
|
135 | - }); |
|
136 | - |
|
137 | - $this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) { |
|
138 | - return $c; |
|
139 | - }); |
|
140 | - |
|
141 | - // commonly used attributes |
|
142 | - $this->registerService('UserId', function ($c) { |
|
143 | - return $c->query('OCP\\IUserSession')->getSession()->get('user_id'); |
|
144 | - }); |
|
145 | - |
|
146 | - $this->registerService('WebRoot', function ($c) { |
|
147 | - return $c->query('ServerContainer')->getWebRoot(); |
|
148 | - }); |
|
149 | - |
|
150 | - $this->registerService('fromMailAddress', function() { |
|
151 | - return Util::getDefaultEmailAddress('no-reply'); |
|
152 | - }); |
|
153 | - |
|
154 | - $this->registerService('OC_Defaults', function ($c) { |
|
155 | - return $c->getServer()->getThemingDefaults(); |
|
156 | - }); |
|
157 | - |
|
158 | - $this->registerService('OCP\Encryption\IManager', function ($c) { |
|
159 | - return $this->getServer()->getEncryptionManager(); |
|
160 | - }); |
|
161 | - |
|
162 | - $this->registerService(IValidator::class, function($c) { |
|
163 | - return $c->query(Validator::class); |
|
164 | - }); |
|
165 | - |
|
166 | - $this->registerService(\OC\Security\IdentityProof\Manager::class, function ($c) { |
|
167 | - return new \OC\Security\IdentityProof\Manager( |
|
168 | - $this->getServer()->getAppDataDir('identityproof'), |
|
169 | - $this->getServer()->getCrypto() |
|
170 | - ); |
|
171 | - }); |
|
172 | - |
|
173 | - /** |
|
174 | - * App Framework APIs |
|
175 | - */ |
|
176 | - $this->registerService('API', function($c){ |
|
177 | - $c->query('OCP\\ILogger')->debug( |
|
178 | - 'Accessing the API class is deprecated! Use the appropriate ' . |
|
179 | - 'services instead!' |
|
180 | - ); |
|
181 | - return new API($c['AppName']); |
|
182 | - }); |
|
183 | - |
|
184 | - $this->registerService('Protocol', function($c){ |
|
185 | - /** @var \OC\Server $server */ |
|
186 | - $server = $c->query('ServerContainer'); |
|
187 | - $protocol = $server->getRequest()->getHttpProtocol(); |
|
188 | - return new Http($_SERVER, $protocol); |
|
189 | - }); |
|
190 | - |
|
191 | - $this->registerService('Dispatcher', function($c) { |
|
192 | - return new Dispatcher( |
|
193 | - $c['Protocol'], |
|
194 | - $c['MiddlewareDispatcher'], |
|
195 | - $c['ControllerMethodReflector'], |
|
196 | - $c['Request'] |
|
197 | - ); |
|
198 | - }); |
|
199 | - |
|
200 | - /** |
|
201 | - * App Framework default arguments |
|
202 | - */ |
|
203 | - $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH'); |
|
204 | - $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept'); |
|
205 | - $this->registerParameter('corsMaxAge', 1728000); |
|
206 | - |
|
207 | - /** |
|
208 | - * Middleware |
|
209 | - */ |
|
210 | - $app = $this; |
|
211 | - $this->registerService('SecurityMiddleware', function($c) use ($app){ |
|
212 | - /** @var \OC\Server $server */ |
|
213 | - $server = $app->getServer(); |
|
214 | - |
|
215 | - return new SecurityMiddleware( |
|
216 | - $c['Request'], |
|
217 | - $c['ControllerMethodReflector'], |
|
218 | - $server->getNavigationManager(), |
|
219 | - $server->getURLGenerator(), |
|
220 | - $server->getLogger(), |
|
221 | - $server->getSession(), |
|
222 | - $c['AppName'], |
|
223 | - $app->isLoggedIn(), |
|
224 | - $app->isAdminUser(), |
|
225 | - $server->getContentSecurityPolicyManager(), |
|
226 | - $server->getCsrfTokenManager(), |
|
227 | - $server->getContentSecurityPolicyNonceManager() |
|
228 | - ); |
|
229 | - |
|
230 | - }); |
|
231 | - |
|
232 | - $this->registerService('BruteForceMiddleware', function($c) use ($app) { |
|
233 | - /** @var \OC\Server $server */ |
|
234 | - $server = $app->getServer(); |
|
235 | - |
|
236 | - return new OC\AppFramework\Middleware\Security\BruteForceMiddleware( |
|
237 | - $c['ControllerMethodReflector'], |
|
238 | - $server->getBruteForceThrottler(), |
|
239 | - $server->getRequest() |
|
240 | - ); |
|
241 | - }); |
|
242 | - |
|
243 | - $this->registerService('RateLimitingMiddleware', function($c) use ($app) { |
|
244 | - /** @var \OC\Server $server */ |
|
245 | - $server = $app->getServer(); |
|
246 | - |
|
247 | - return new RateLimitingMiddleware( |
|
248 | - $server->getRequest(), |
|
249 | - $server->getUserSession(), |
|
250 | - $c['ControllerMethodReflector'], |
|
251 | - $c->query(OC\Security\RateLimiting\Limiter::class) |
|
252 | - ); |
|
253 | - }); |
|
254 | - |
|
255 | - $this->registerService('CORSMiddleware', function($c) { |
|
256 | - return new CORSMiddleware( |
|
257 | - $c['Request'], |
|
258 | - $c['ControllerMethodReflector'], |
|
259 | - $c->query(IUserSession::class), |
|
260 | - $c->getServer()->getBruteForceThrottler() |
|
261 | - ); |
|
262 | - }); |
|
263 | - |
|
264 | - $this->registerService('SessionMiddleware', function($c) use ($app) { |
|
265 | - return new SessionMiddleware( |
|
266 | - $c['Request'], |
|
267 | - $c['ControllerMethodReflector'], |
|
268 | - $app->getServer()->getSession() |
|
269 | - ); |
|
270 | - }); |
|
271 | - |
|
272 | - $this->registerService('TwoFactorMiddleware', function (SimpleContainer $c) use ($app) { |
|
273 | - $twoFactorManager = $c->getServer()->getTwoFactorAuthManager(); |
|
274 | - $userSession = $app->getServer()->getUserSession(); |
|
275 | - $session = $app->getServer()->getSession(); |
|
276 | - $urlGenerator = $app->getServer()->getURLGenerator(); |
|
277 | - $reflector = $c['ControllerMethodReflector']; |
|
278 | - $request = $app->getServer()->getRequest(); |
|
279 | - return new TwoFactorMiddleware($twoFactorManager, $userSession, $session, $urlGenerator, $reflector, $request); |
|
280 | - }); |
|
281 | - |
|
282 | - $this->registerService('OCSMiddleware', function (SimpleContainer $c) { |
|
283 | - return new OCSMiddleware( |
|
284 | - $c['Request'] |
|
285 | - ); |
|
286 | - }); |
|
287 | - |
|
288 | - $middleWares = &$this->middleWares; |
|
289 | - $this->registerService('MiddlewareDispatcher', function($c) use (&$middleWares) { |
|
290 | - $dispatcher = new MiddlewareDispatcher(); |
|
291 | - $dispatcher->registerMiddleware($c['CORSMiddleware']); |
|
292 | - $dispatcher->registerMiddleware($c['OCSMiddleware']); |
|
293 | - $dispatcher->registerMiddleware($c['SecurityMiddleware']); |
|
294 | - $dispatcher->registerMiddleware($c['TwoFactorMiddleware']); |
|
295 | - $dispatcher->registerMiddleware($c['BruteForceMiddleware']); |
|
296 | - $dispatcher->registerMiddleware($c['RateLimitingMiddleware']); |
|
297 | - |
|
298 | - foreach($middleWares as $middleWare) { |
|
299 | - $dispatcher->registerMiddleware($c[$middleWare]); |
|
300 | - } |
|
301 | - |
|
302 | - $dispatcher->registerMiddleware($c['SessionMiddleware']); |
|
303 | - return $dispatcher; |
|
304 | - }); |
|
305 | - |
|
306 | - } |
|
307 | - |
|
308 | - |
|
309 | - /** |
|
310 | - * @deprecated implements only deprecated methods |
|
311 | - * @return IApi |
|
312 | - */ |
|
313 | - function getCoreApi() |
|
314 | - { |
|
315 | - return $this->query('API'); |
|
316 | - } |
|
317 | - |
|
318 | - /** |
|
319 | - * @return \OCP\IServerContainer |
|
320 | - */ |
|
321 | - function getServer() |
|
322 | - { |
|
323 | - return $this->server; |
|
324 | - } |
|
325 | - |
|
326 | - /** |
|
327 | - * @param string $middleWare |
|
328 | - * @return boolean|null |
|
329 | - */ |
|
330 | - function registerMiddleWare($middleWare) { |
|
331 | - array_push($this->middleWares, $middleWare); |
|
332 | - } |
|
333 | - |
|
334 | - /** |
|
335 | - * used to return the appname of the set application |
|
336 | - * @return string the name of your application |
|
337 | - */ |
|
338 | - function getAppName() { |
|
339 | - return $this->query('AppName'); |
|
340 | - } |
|
341 | - |
|
342 | - /** |
|
343 | - * @deprecated use IUserSession->isLoggedIn() |
|
344 | - * @return boolean |
|
345 | - */ |
|
346 | - function isLoggedIn() { |
|
347 | - return \OC::$server->getUserSession()->isLoggedIn(); |
|
348 | - } |
|
349 | - |
|
350 | - /** |
|
351 | - * @deprecated use IGroupManager->isAdmin($userId) |
|
352 | - * @return boolean |
|
353 | - */ |
|
354 | - function isAdminUser() { |
|
355 | - $uid = $this->getUserId(); |
|
356 | - return \OC_User::isAdminUser($uid); |
|
357 | - } |
|
358 | - |
|
359 | - private function getUserId() { |
|
360 | - return $this->getServer()->getSession()->get('user_id'); |
|
361 | - } |
|
362 | - |
|
363 | - /** |
|
364 | - * @deprecated use the ILogger instead |
|
365 | - * @param string $message |
|
366 | - * @param string $level |
|
367 | - * @return mixed |
|
368 | - */ |
|
369 | - function log($message, $level) { |
|
370 | - switch($level){ |
|
371 | - case 'debug': |
|
372 | - $level = \OCP\Util::DEBUG; |
|
373 | - break; |
|
374 | - case 'info': |
|
375 | - $level = \OCP\Util::INFO; |
|
376 | - break; |
|
377 | - case 'warn': |
|
378 | - $level = \OCP\Util::WARN; |
|
379 | - break; |
|
380 | - case 'fatal': |
|
381 | - $level = \OCP\Util::FATAL; |
|
382 | - break; |
|
383 | - default: |
|
384 | - $level = \OCP\Util::ERROR; |
|
385 | - break; |
|
386 | - } |
|
387 | - \OCP\Util::writeLog($this->getAppName(), $message, $level); |
|
388 | - } |
|
389 | - |
|
390 | - /** |
|
391 | - * Register a capability |
|
392 | - * |
|
393 | - * @param string $serviceName e.g. 'OCA\Files\Capabilities' |
|
394 | - */ |
|
395 | - public function registerCapability($serviceName) { |
|
396 | - $this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) { |
|
397 | - return $this->query($serviceName); |
|
398 | - }); |
|
399 | - } |
|
400 | - |
|
401 | - /** |
|
402 | - * @param string $name |
|
403 | - * @return mixed |
|
404 | - * @throws QueryException if the query could not be resolved |
|
405 | - */ |
|
406 | - public function query($name) { |
|
407 | - try { |
|
408 | - return $this->queryNoFallback($name); |
|
409 | - } catch (QueryException $e) { |
|
410 | - return $this->getServer()->query($name); |
|
411 | - } |
|
412 | - } |
|
413 | - |
|
414 | - /** |
|
415 | - * @param string $name |
|
416 | - * @return mixed |
|
417 | - * @throws QueryException if the query could not be resolved |
|
418 | - */ |
|
419 | - public function queryNoFallback($name) { |
|
420 | - $name = $this->sanitizeName($name); |
|
421 | - |
|
422 | - if ($this->offsetExists($name)) { |
|
423 | - return parent::query($name); |
|
424 | - } else { |
|
425 | - if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) { |
|
426 | - return parent::query($name); |
|
427 | - } else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) { |
|
428 | - return parent::query($name); |
|
429 | - } else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) { |
|
430 | - return parent::query($name); |
|
431 | - } |
|
432 | - } |
|
433 | - |
|
434 | - throw new QueryException('Could not resolve ' . $name . '!' . |
|
435 | - ' Class can not be instantiated'); |
|
436 | - } |
|
65 | + /** |
|
66 | + * @var array |
|
67 | + */ |
|
68 | + private $middleWares = array(); |
|
69 | + |
|
70 | + /** @var ServerContainer */ |
|
71 | + private $server; |
|
72 | + |
|
73 | + /** |
|
74 | + * Put your class dependencies in here |
|
75 | + * @param string $appName the name of the app |
|
76 | + * @param array $urlParams |
|
77 | + * @param ServerContainer $server |
|
78 | + */ |
|
79 | + public function __construct($appName, $urlParams = array(), ServerContainer $server = null){ |
|
80 | + parent::__construct(); |
|
81 | + $this['AppName'] = $appName; |
|
82 | + $this['urlParams'] = $urlParams; |
|
83 | + |
|
84 | + /** @var \OC\ServerContainer $server */ |
|
85 | + if ($server === null) { |
|
86 | + $server = \OC::$server; |
|
87 | + } |
|
88 | + $this->server = $server; |
|
89 | + $this->server->registerAppContainer($appName, $this); |
|
90 | + |
|
91 | + // aliases |
|
92 | + $this->registerAlias('appName', 'AppName'); |
|
93 | + $this->registerAlias('webRoot', 'WebRoot'); |
|
94 | + $this->registerAlias('userId', 'UserId'); |
|
95 | + |
|
96 | + /** |
|
97 | + * Core services |
|
98 | + */ |
|
99 | + $this->registerService(IOutput::class, function($c){ |
|
100 | + return new Output($this->getServer()->getWebRoot()); |
|
101 | + }); |
|
102 | + |
|
103 | + $this->registerService(Folder::class, function() { |
|
104 | + return $this->getServer()->getUserFolder(); |
|
105 | + }); |
|
106 | + |
|
107 | + $this->registerService(IAppData::class, function (SimpleContainer $c) { |
|
108 | + return $this->getServer()->getAppDataDir($c->query('AppName')); |
|
109 | + }); |
|
110 | + |
|
111 | + $this->registerService(IL10N::class, function($c) { |
|
112 | + return $this->getServer()->getL10N($c->query('AppName')); |
|
113 | + }); |
|
114 | + |
|
115 | + $this->registerAlias(\OCP\AppFramework\Utility\IControllerMethodReflector::class, \OC\AppFramework\Utility\ControllerMethodReflector::class); |
|
116 | + $this->registerAlias('ControllerMethodReflector', \OCP\AppFramework\Utility\IControllerMethodReflector::class); |
|
117 | + |
|
118 | + $this->registerService(IRequest::class, function() { |
|
119 | + return $this->getServer()->query(IRequest::class); |
|
120 | + }); |
|
121 | + $this->registerAlias('Request', IRequest::class); |
|
122 | + |
|
123 | + $this->registerAlias(\OCP\AppFramework\Utility\ITimeFactory::class, \OC\AppFramework\Utility\TimeFactory::class); |
|
124 | + $this->registerAlias('TimeFactory', \OCP\AppFramework\Utility\ITimeFactory::class); |
|
125 | + |
|
126 | + $this->registerAlias(\OC\User\Session::class, \OCP\IUserSession::class); |
|
127 | + |
|
128 | + $this->registerService(IServerContainer::class, function ($c) { |
|
129 | + return $this->getServer(); |
|
130 | + }); |
|
131 | + $this->registerAlias('ServerContainer', IServerContainer::class); |
|
132 | + |
|
133 | + $this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) { |
|
134 | + return $c->query('OCA\WorkflowEngine\Manager'); |
|
135 | + }); |
|
136 | + |
|
137 | + $this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) { |
|
138 | + return $c; |
|
139 | + }); |
|
140 | + |
|
141 | + // commonly used attributes |
|
142 | + $this->registerService('UserId', function ($c) { |
|
143 | + return $c->query('OCP\\IUserSession')->getSession()->get('user_id'); |
|
144 | + }); |
|
145 | + |
|
146 | + $this->registerService('WebRoot', function ($c) { |
|
147 | + return $c->query('ServerContainer')->getWebRoot(); |
|
148 | + }); |
|
149 | + |
|
150 | + $this->registerService('fromMailAddress', function() { |
|
151 | + return Util::getDefaultEmailAddress('no-reply'); |
|
152 | + }); |
|
153 | + |
|
154 | + $this->registerService('OC_Defaults', function ($c) { |
|
155 | + return $c->getServer()->getThemingDefaults(); |
|
156 | + }); |
|
157 | + |
|
158 | + $this->registerService('OCP\Encryption\IManager', function ($c) { |
|
159 | + return $this->getServer()->getEncryptionManager(); |
|
160 | + }); |
|
161 | + |
|
162 | + $this->registerService(IValidator::class, function($c) { |
|
163 | + return $c->query(Validator::class); |
|
164 | + }); |
|
165 | + |
|
166 | + $this->registerService(\OC\Security\IdentityProof\Manager::class, function ($c) { |
|
167 | + return new \OC\Security\IdentityProof\Manager( |
|
168 | + $this->getServer()->getAppDataDir('identityproof'), |
|
169 | + $this->getServer()->getCrypto() |
|
170 | + ); |
|
171 | + }); |
|
172 | + |
|
173 | + /** |
|
174 | + * App Framework APIs |
|
175 | + */ |
|
176 | + $this->registerService('API', function($c){ |
|
177 | + $c->query('OCP\\ILogger')->debug( |
|
178 | + 'Accessing the API class is deprecated! Use the appropriate ' . |
|
179 | + 'services instead!' |
|
180 | + ); |
|
181 | + return new API($c['AppName']); |
|
182 | + }); |
|
183 | + |
|
184 | + $this->registerService('Protocol', function($c){ |
|
185 | + /** @var \OC\Server $server */ |
|
186 | + $server = $c->query('ServerContainer'); |
|
187 | + $protocol = $server->getRequest()->getHttpProtocol(); |
|
188 | + return new Http($_SERVER, $protocol); |
|
189 | + }); |
|
190 | + |
|
191 | + $this->registerService('Dispatcher', function($c) { |
|
192 | + return new Dispatcher( |
|
193 | + $c['Protocol'], |
|
194 | + $c['MiddlewareDispatcher'], |
|
195 | + $c['ControllerMethodReflector'], |
|
196 | + $c['Request'] |
|
197 | + ); |
|
198 | + }); |
|
199 | + |
|
200 | + /** |
|
201 | + * App Framework default arguments |
|
202 | + */ |
|
203 | + $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH'); |
|
204 | + $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept'); |
|
205 | + $this->registerParameter('corsMaxAge', 1728000); |
|
206 | + |
|
207 | + /** |
|
208 | + * Middleware |
|
209 | + */ |
|
210 | + $app = $this; |
|
211 | + $this->registerService('SecurityMiddleware', function($c) use ($app){ |
|
212 | + /** @var \OC\Server $server */ |
|
213 | + $server = $app->getServer(); |
|
214 | + |
|
215 | + return new SecurityMiddleware( |
|
216 | + $c['Request'], |
|
217 | + $c['ControllerMethodReflector'], |
|
218 | + $server->getNavigationManager(), |
|
219 | + $server->getURLGenerator(), |
|
220 | + $server->getLogger(), |
|
221 | + $server->getSession(), |
|
222 | + $c['AppName'], |
|
223 | + $app->isLoggedIn(), |
|
224 | + $app->isAdminUser(), |
|
225 | + $server->getContentSecurityPolicyManager(), |
|
226 | + $server->getCsrfTokenManager(), |
|
227 | + $server->getContentSecurityPolicyNonceManager() |
|
228 | + ); |
|
229 | + |
|
230 | + }); |
|
231 | + |
|
232 | + $this->registerService('BruteForceMiddleware', function($c) use ($app) { |
|
233 | + /** @var \OC\Server $server */ |
|
234 | + $server = $app->getServer(); |
|
235 | + |
|
236 | + return new OC\AppFramework\Middleware\Security\BruteForceMiddleware( |
|
237 | + $c['ControllerMethodReflector'], |
|
238 | + $server->getBruteForceThrottler(), |
|
239 | + $server->getRequest() |
|
240 | + ); |
|
241 | + }); |
|
242 | + |
|
243 | + $this->registerService('RateLimitingMiddleware', function($c) use ($app) { |
|
244 | + /** @var \OC\Server $server */ |
|
245 | + $server = $app->getServer(); |
|
246 | + |
|
247 | + return new RateLimitingMiddleware( |
|
248 | + $server->getRequest(), |
|
249 | + $server->getUserSession(), |
|
250 | + $c['ControllerMethodReflector'], |
|
251 | + $c->query(OC\Security\RateLimiting\Limiter::class) |
|
252 | + ); |
|
253 | + }); |
|
254 | + |
|
255 | + $this->registerService('CORSMiddleware', function($c) { |
|
256 | + return new CORSMiddleware( |
|
257 | + $c['Request'], |
|
258 | + $c['ControllerMethodReflector'], |
|
259 | + $c->query(IUserSession::class), |
|
260 | + $c->getServer()->getBruteForceThrottler() |
|
261 | + ); |
|
262 | + }); |
|
263 | + |
|
264 | + $this->registerService('SessionMiddleware', function($c) use ($app) { |
|
265 | + return new SessionMiddleware( |
|
266 | + $c['Request'], |
|
267 | + $c['ControllerMethodReflector'], |
|
268 | + $app->getServer()->getSession() |
|
269 | + ); |
|
270 | + }); |
|
271 | + |
|
272 | + $this->registerService('TwoFactorMiddleware', function (SimpleContainer $c) use ($app) { |
|
273 | + $twoFactorManager = $c->getServer()->getTwoFactorAuthManager(); |
|
274 | + $userSession = $app->getServer()->getUserSession(); |
|
275 | + $session = $app->getServer()->getSession(); |
|
276 | + $urlGenerator = $app->getServer()->getURLGenerator(); |
|
277 | + $reflector = $c['ControllerMethodReflector']; |
|
278 | + $request = $app->getServer()->getRequest(); |
|
279 | + return new TwoFactorMiddleware($twoFactorManager, $userSession, $session, $urlGenerator, $reflector, $request); |
|
280 | + }); |
|
281 | + |
|
282 | + $this->registerService('OCSMiddleware', function (SimpleContainer $c) { |
|
283 | + return new OCSMiddleware( |
|
284 | + $c['Request'] |
|
285 | + ); |
|
286 | + }); |
|
287 | + |
|
288 | + $middleWares = &$this->middleWares; |
|
289 | + $this->registerService('MiddlewareDispatcher', function($c) use (&$middleWares) { |
|
290 | + $dispatcher = new MiddlewareDispatcher(); |
|
291 | + $dispatcher->registerMiddleware($c['CORSMiddleware']); |
|
292 | + $dispatcher->registerMiddleware($c['OCSMiddleware']); |
|
293 | + $dispatcher->registerMiddleware($c['SecurityMiddleware']); |
|
294 | + $dispatcher->registerMiddleware($c['TwoFactorMiddleware']); |
|
295 | + $dispatcher->registerMiddleware($c['BruteForceMiddleware']); |
|
296 | + $dispatcher->registerMiddleware($c['RateLimitingMiddleware']); |
|
297 | + |
|
298 | + foreach($middleWares as $middleWare) { |
|
299 | + $dispatcher->registerMiddleware($c[$middleWare]); |
|
300 | + } |
|
301 | + |
|
302 | + $dispatcher->registerMiddleware($c['SessionMiddleware']); |
|
303 | + return $dispatcher; |
|
304 | + }); |
|
305 | + |
|
306 | + } |
|
307 | + |
|
308 | + |
|
309 | + /** |
|
310 | + * @deprecated implements only deprecated methods |
|
311 | + * @return IApi |
|
312 | + */ |
|
313 | + function getCoreApi() |
|
314 | + { |
|
315 | + return $this->query('API'); |
|
316 | + } |
|
317 | + |
|
318 | + /** |
|
319 | + * @return \OCP\IServerContainer |
|
320 | + */ |
|
321 | + function getServer() |
|
322 | + { |
|
323 | + return $this->server; |
|
324 | + } |
|
325 | + |
|
326 | + /** |
|
327 | + * @param string $middleWare |
|
328 | + * @return boolean|null |
|
329 | + */ |
|
330 | + function registerMiddleWare($middleWare) { |
|
331 | + array_push($this->middleWares, $middleWare); |
|
332 | + } |
|
333 | + |
|
334 | + /** |
|
335 | + * used to return the appname of the set application |
|
336 | + * @return string the name of your application |
|
337 | + */ |
|
338 | + function getAppName() { |
|
339 | + return $this->query('AppName'); |
|
340 | + } |
|
341 | + |
|
342 | + /** |
|
343 | + * @deprecated use IUserSession->isLoggedIn() |
|
344 | + * @return boolean |
|
345 | + */ |
|
346 | + function isLoggedIn() { |
|
347 | + return \OC::$server->getUserSession()->isLoggedIn(); |
|
348 | + } |
|
349 | + |
|
350 | + /** |
|
351 | + * @deprecated use IGroupManager->isAdmin($userId) |
|
352 | + * @return boolean |
|
353 | + */ |
|
354 | + function isAdminUser() { |
|
355 | + $uid = $this->getUserId(); |
|
356 | + return \OC_User::isAdminUser($uid); |
|
357 | + } |
|
358 | + |
|
359 | + private function getUserId() { |
|
360 | + return $this->getServer()->getSession()->get('user_id'); |
|
361 | + } |
|
362 | + |
|
363 | + /** |
|
364 | + * @deprecated use the ILogger instead |
|
365 | + * @param string $message |
|
366 | + * @param string $level |
|
367 | + * @return mixed |
|
368 | + */ |
|
369 | + function log($message, $level) { |
|
370 | + switch($level){ |
|
371 | + case 'debug': |
|
372 | + $level = \OCP\Util::DEBUG; |
|
373 | + break; |
|
374 | + case 'info': |
|
375 | + $level = \OCP\Util::INFO; |
|
376 | + break; |
|
377 | + case 'warn': |
|
378 | + $level = \OCP\Util::WARN; |
|
379 | + break; |
|
380 | + case 'fatal': |
|
381 | + $level = \OCP\Util::FATAL; |
|
382 | + break; |
|
383 | + default: |
|
384 | + $level = \OCP\Util::ERROR; |
|
385 | + break; |
|
386 | + } |
|
387 | + \OCP\Util::writeLog($this->getAppName(), $message, $level); |
|
388 | + } |
|
389 | + |
|
390 | + /** |
|
391 | + * Register a capability |
|
392 | + * |
|
393 | + * @param string $serviceName e.g. 'OCA\Files\Capabilities' |
|
394 | + */ |
|
395 | + public function registerCapability($serviceName) { |
|
396 | + $this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) { |
|
397 | + return $this->query($serviceName); |
|
398 | + }); |
|
399 | + } |
|
400 | + |
|
401 | + /** |
|
402 | + * @param string $name |
|
403 | + * @return mixed |
|
404 | + * @throws QueryException if the query could not be resolved |
|
405 | + */ |
|
406 | + public function query($name) { |
|
407 | + try { |
|
408 | + return $this->queryNoFallback($name); |
|
409 | + } catch (QueryException $e) { |
|
410 | + return $this->getServer()->query($name); |
|
411 | + } |
|
412 | + } |
|
413 | + |
|
414 | + /** |
|
415 | + * @param string $name |
|
416 | + * @return mixed |
|
417 | + * @throws QueryException if the query could not be resolved |
|
418 | + */ |
|
419 | + public function queryNoFallback($name) { |
|
420 | + $name = $this->sanitizeName($name); |
|
421 | + |
|
422 | + if ($this->offsetExists($name)) { |
|
423 | + return parent::query($name); |
|
424 | + } else { |
|
425 | + if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) { |
|
426 | + return parent::query($name); |
|
427 | + } else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) { |
|
428 | + return parent::query($name); |
|
429 | + } else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) { |
|
430 | + return parent::query($name); |
|
431 | + } |
|
432 | + } |
|
433 | + |
|
434 | + throw new QueryException('Could not resolve ' . $name . '!' . |
|
435 | + ' Class can not be instantiated'); |
|
436 | + } |
|
437 | 437 | } |
@@ -76,7 +76,7 @@ discard block |
||
76 | 76 | * @param array $urlParams |
77 | 77 | * @param ServerContainer $server |
78 | 78 | */ |
79 | - public function __construct($appName, $urlParams = array(), ServerContainer $server = null){ |
|
79 | + public function __construct($appName, $urlParams = array(), ServerContainer $server = null) { |
|
80 | 80 | parent::__construct(); |
81 | 81 | $this['AppName'] = $appName; |
82 | 82 | $this['urlParams'] = $urlParams; |
@@ -96,7 +96,7 @@ discard block |
||
96 | 96 | /** |
97 | 97 | * Core services |
98 | 98 | */ |
99 | - $this->registerService(IOutput::class, function($c){ |
|
99 | + $this->registerService(IOutput::class, function($c) { |
|
100 | 100 | return new Output($this->getServer()->getWebRoot()); |
101 | 101 | }); |
102 | 102 | |
@@ -104,7 +104,7 @@ discard block |
||
104 | 104 | return $this->getServer()->getUserFolder(); |
105 | 105 | }); |
106 | 106 | |
107 | - $this->registerService(IAppData::class, function (SimpleContainer $c) { |
|
107 | + $this->registerService(IAppData::class, function(SimpleContainer $c) { |
|
108 | 108 | return $this->getServer()->getAppDataDir($c->query('AppName')); |
109 | 109 | }); |
110 | 110 | |
@@ -125,25 +125,25 @@ discard block |
||
125 | 125 | |
126 | 126 | $this->registerAlias(\OC\User\Session::class, \OCP\IUserSession::class); |
127 | 127 | |
128 | - $this->registerService(IServerContainer::class, function ($c) { |
|
128 | + $this->registerService(IServerContainer::class, function($c) { |
|
129 | 129 | return $this->getServer(); |
130 | 130 | }); |
131 | 131 | $this->registerAlias('ServerContainer', IServerContainer::class); |
132 | 132 | |
133 | - $this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) { |
|
133 | + $this->registerService(\OCP\WorkflowEngine\IManager::class, function($c) { |
|
134 | 134 | return $c->query('OCA\WorkflowEngine\Manager'); |
135 | 135 | }); |
136 | 136 | |
137 | - $this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) { |
|
137 | + $this->registerService(\OCP\AppFramework\IAppContainer::class, function($c) { |
|
138 | 138 | return $c; |
139 | 139 | }); |
140 | 140 | |
141 | 141 | // commonly used attributes |
142 | - $this->registerService('UserId', function ($c) { |
|
142 | + $this->registerService('UserId', function($c) { |
|
143 | 143 | return $c->query('OCP\\IUserSession')->getSession()->get('user_id'); |
144 | 144 | }); |
145 | 145 | |
146 | - $this->registerService('WebRoot', function ($c) { |
|
146 | + $this->registerService('WebRoot', function($c) { |
|
147 | 147 | return $c->query('ServerContainer')->getWebRoot(); |
148 | 148 | }); |
149 | 149 | |
@@ -151,11 +151,11 @@ discard block |
||
151 | 151 | return Util::getDefaultEmailAddress('no-reply'); |
152 | 152 | }); |
153 | 153 | |
154 | - $this->registerService('OC_Defaults', function ($c) { |
|
154 | + $this->registerService('OC_Defaults', function($c) { |
|
155 | 155 | return $c->getServer()->getThemingDefaults(); |
156 | 156 | }); |
157 | 157 | |
158 | - $this->registerService('OCP\Encryption\IManager', function ($c) { |
|
158 | + $this->registerService('OCP\Encryption\IManager', function($c) { |
|
159 | 159 | return $this->getServer()->getEncryptionManager(); |
160 | 160 | }); |
161 | 161 | |
@@ -163,7 +163,7 @@ discard block |
||
163 | 163 | return $c->query(Validator::class); |
164 | 164 | }); |
165 | 165 | |
166 | - $this->registerService(\OC\Security\IdentityProof\Manager::class, function ($c) { |
|
166 | + $this->registerService(\OC\Security\IdentityProof\Manager::class, function($c) { |
|
167 | 167 | return new \OC\Security\IdentityProof\Manager( |
168 | 168 | $this->getServer()->getAppDataDir('identityproof'), |
169 | 169 | $this->getServer()->getCrypto() |
@@ -173,15 +173,15 @@ discard block |
||
173 | 173 | /** |
174 | 174 | * App Framework APIs |
175 | 175 | */ |
176 | - $this->registerService('API', function($c){ |
|
176 | + $this->registerService('API', function($c) { |
|
177 | 177 | $c->query('OCP\\ILogger')->debug( |
178 | - 'Accessing the API class is deprecated! Use the appropriate ' . |
|
178 | + 'Accessing the API class is deprecated! Use the appropriate '. |
|
179 | 179 | 'services instead!' |
180 | 180 | ); |
181 | 181 | return new API($c['AppName']); |
182 | 182 | }); |
183 | 183 | |
184 | - $this->registerService('Protocol', function($c){ |
|
184 | + $this->registerService('Protocol', function($c) { |
|
185 | 185 | /** @var \OC\Server $server */ |
186 | 186 | $server = $c->query('ServerContainer'); |
187 | 187 | $protocol = $server->getRequest()->getHttpProtocol(); |
@@ -269,7 +269,7 @@ discard block |
||
269 | 269 | ); |
270 | 270 | }); |
271 | 271 | |
272 | - $this->registerService('TwoFactorMiddleware', function (SimpleContainer $c) use ($app) { |
|
272 | + $this->registerService('TwoFactorMiddleware', function(SimpleContainer $c) use ($app) { |
|
273 | 273 | $twoFactorManager = $c->getServer()->getTwoFactorAuthManager(); |
274 | 274 | $userSession = $app->getServer()->getUserSession(); |
275 | 275 | $session = $app->getServer()->getSession(); |
@@ -279,7 +279,7 @@ discard block |
||
279 | 279 | return new TwoFactorMiddleware($twoFactorManager, $userSession, $session, $urlGenerator, $reflector, $request); |
280 | 280 | }); |
281 | 281 | |
282 | - $this->registerService('OCSMiddleware', function (SimpleContainer $c) { |
|
282 | + $this->registerService('OCSMiddleware', function(SimpleContainer $c) { |
|
283 | 283 | return new OCSMiddleware( |
284 | 284 | $c['Request'] |
285 | 285 | ); |
@@ -295,7 +295,7 @@ discard block |
||
295 | 295 | $dispatcher->registerMiddleware($c['BruteForceMiddleware']); |
296 | 296 | $dispatcher->registerMiddleware($c['RateLimitingMiddleware']); |
297 | 297 | |
298 | - foreach($middleWares as $middleWare) { |
|
298 | + foreach ($middleWares as $middleWare) { |
|
299 | 299 | $dispatcher->registerMiddleware($c[$middleWare]); |
300 | 300 | } |
301 | 301 | |
@@ -367,7 +367,7 @@ discard block |
||
367 | 367 | * @return mixed |
368 | 368 | */ |
369 | 369 | function log($message, $level) { |
370 | - switch($level){ |
|
370 | + switch ($level) { |
|
371 | 371 | case 'debug': |
372 | 372 | $level = \OCP\Util::DEBUG; |
373 | 373 | break; |
@@ -426,12 +426,12 @@ discard block |
||
426 | 426 | return parent::query($name); |
427 | 427 | } else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) { |
428 | 428 | return parent::query($name); |
429 | - } else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) { |
|
429 | + } else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']).'\\') === 0) { |
|
430 | 430 | return parent::query($name); |
431 | 431 | } |
432 | 432 | } |
433 | 433 | |
434 | - throw new QueryException('Could not resolve ' . $name . '!' . |
|
434 | + throw new QueryException('Could not resolve '.$name.'!'. |
|
435 | 435 | ' Class can not be instantiated'); |
436 | 436 | } |
437 | 437 | } |
@@ -42,304 +42,304 @@ |
||
42 | 42 | */ |
43 | 43 | class Response { |
44 | 44 | |
45 | - /** |
|
46 | - * Headers - defaults to ['Cache-Control' => 'no-cache, no-store, must-revalidate'] |
|
47 | - * @var array |
|
48 | - */ |
|
49 | - private $headers = array( |
|
50 | - 'Cache-Control' => 'no-cache, no-store, must-revalidate' |
|
51 | - ); |
|
52 | - |
|
53 | - |
|
54 | - /** |
|
55 | - * Cookies that will be need to be constructed as header |
|
56 | - * @var array |
|
57 | - */ |
|
58 | - private $cookies = array(); |
|
59 | - |
|
60 | - |
|
61 | - /** |
|
62 | - * HTTP status code - defaults to STATUS OK |
|
63 | - * @var int |
|
64 | - */ |
|
65 | - private $status = Http::STATUS_OK; |
|
66 | - |
|
67 | - |
|
68 | - /** |
|
69 | - * Last modified date |
|
70 | - * @var \DateTime |
|
71 | - */ |
|
72 | - private $lastModified; |
|
73 | - |
|
74 | - |
|
75 | - /** |
|
76 | - * ETag |
|
77 | - * @var string |
|
78 | - */ |
|
79 | - private $ETag; |
|
80 | - |
|
81 | - /** @var ContentSecurityPolicy|null Used Content-Security-Policy */ |
|
82 | - private $contentSecurityPolicy = null; |
|
83 | - |
|
84 | - /** @var bool */ |
|
85 | - private $throttled = false; |
|
86 | - |
|
87 | - /** |
|
88 | - * Caches the response |
|
89 | - * @param int $cacheSeconds the amount of seconds that should be cached |
|
90 | - * if 0 then caching will be disabled |
|
91 | - * @return $this |
|
92 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
93 | - */ |
|
94 | - public function cacheFor($cacheSeconds) { |
|
95 | - |
|
96 | - if($cacheSeconds > 0) { |
|
97 | - $this->addHeader('Cache-Control', 'max-age=' . $cacheSeconds . ', must-revalidate'); |
|
98 | - } else { |
|
99 | - $this->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate'); |
|
100 | - } |
|
101 | - |
|
102 | - return $this; |
|
103 | - } |
|
104 | - |
|
105 | - /** |
|
106 | - * Adds a new cookie to the response |
|
107 | - * @param string $name The name of the cookie |
|
108 | - * @param string $value The value of the cookie |
|
109 | - * @param \DateTime|null $expireDate Date on that the cookie should expire, if set |
|
110 | - * to null cookie will be considered as session |
|
111 | - * cookie. |
|
112 | - * @return $this |
|
113 | - * @since 8.0.0 |
|
114 | - */ |
|
115 | - public function addCookie($name, $value, \DateTime $expireDate = null) { |
|
116 | - $this->cookies[$name] = array('value' => $value, 'expireDate' => $expireDate); |
|
117 | - return $this; |
|
118 | - } |
|
119 | - |
|
120 | - |
|
121 | - /** |
|
122 | - * Set the specified cookies |
|
123 | - * @param array $cookies array('foo' => array('value' => 'bar', 'expire' => null)) |
|
124 | - * @return $this |
|
125 | - * @since 8.0.0 |
|
126 | - */ |
|
127 | - public function setCookies(array $cookies) { |
|
128 | - $this->cookies = $cookies; |
|
129 | - return $this; |
|
130 | - } |
|
131 | - |
|
132 | - |
|
133 | - /** |
|
134 | - * Invalidates the specified cookie |
|
135 | - * @param string $name |
|
136 | - * @return $this |
|
137 | - * @since 8.0.0 |
|
138 | - */ |
|
139 | - public function invalidateCookie($name) { |
|
140 | - $this->addCookie($name, 'expired', new \DateTime('1971-01-01 00:00')); |
|
141 | - return $this; |
|
142 | - } |
|
143 | - |
|
144 | - /** |
|
145 | - * Invalidates the specified cookies |
|
146 | - * @param array $cookieNames array('foo', 'bar') |
|
147 | - * @return $this |
|
148 | - * @since 8.0.0 |
|
149 | - */ |
|
150 | - public function invalidateCookies(array $cookieNames) { |
|
151 | - foreach($cookieNames as $cookieName) { |
|
152 | - $this->invalidateCookie($cookieName); |
|
153 | - } |
|
154 | - return $this; |
|
155 | - } |
|
156 | - |
|
157 | - /** |
|
158 | - * Returns the cookies |
|
159 | - * @return array |
|
160 | - * @since 8.0.0 |
|
161 | - */ |
|
162 | - public function getCookies() { |
|
163 | - return $this->cookies; |
|
164 | - } |
|
165 | - |
|
166 | - /** |
|
167 | - * Adds a new header to the response that will be called before the render |
|
168 | - * function |
|
169 | - * @param string $name The name of the HTTP header |
|
170 | - * @param string $value The value, null will delete it |
|
171 | - * @return $this |
|
172 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
173 | - */ |
|
174 | - public function addHeader($name, $value) { |
|
175 | - $name = trim($name); // always remove leading and trailing whitespace |
|
176 | - // to be able to reliably check for security |
|
177 | - // headers |
|
178 | - |
|
179 | - if(is_null($value)) { |
|
180 | - unset($this->headers[$name]); |
|
181 | - } else { |
|
182 | - $this->headers[$name] = $value; |
|
183 | - } |
|
184 | - |
|
185 | - return $this; |
|
186 | - } |
|
187 | - |
|
188 | - |
|
189 | - /** |
|
190 | - * Set the headers |
|
191 | - * @param array $headers value header pairs |
|
192 | - * @return $this |
|
193 | - * @since 8.0.0 |
|
194 | - */ |
|
195 | - public function setHeaders(array $headers) { |
|
196 | - $this->headers = $headers; |
|
197 | - |
|
198 | - return $this; |
|
199 | - } |
|
200 | - |
|
201 | - |
|
202 | - /** |
|
203 | - * Returns the set headers |
|
204 | - * @return array the headers |
|
205 | - * @since 6.0.0 |
|
206 | - */ |
|
207 | - public function getHeaders() { |
|
208 | - $mergeWith = []; |
|
209 | - |
|
210 | - if($this->lastModified) { |
|
211 | - $mergeWith['Last-Modified'] = |
|
212 | - $this->lastModified->format(\DateTime::RFC2822); |
|
213 | - } |
|
214 | - |
|
215 | - // Build Content-Security-Policy and use default if none has been specified |
|
216 | - if(is_null($this->contentSecurityPolicy)) { |
|
217 | - $this->setContentSecurityPolicy(new ContentSecurityPolicy()); |
|
218 | - } |
|
219 | - $this->headers['Content-Security-Policy'] = $this->contentSecurityPolicy->buildPolicy(); |
|
220 | - |
|
221 | - if($this->ETag) { |
|
222 | - $mergeWith['ETag'] = '"' . $this->ETag . '"'; |
|
223 | - } |
|
224 | - |
|
225 | - return array_merge($mergeWith, $this->headers); |
|
226 | - } |
|
227 | - |
|
228 | - |
|
229 | - /** |
|
230 | - * By default renders no output |
|
231 | - * @return null |
|
232 | - * @since 6.0.0 |
|
233 | - */ |
|
234 | - public function render() { |
|
235 | - return null; |
|
236 | - } |
|
237 | - |
|
238 | - |
|
239 | - /** |
|
240 | - * Set response status |
|
241 | - * @param int $status a HTTP status code, see also the STATUS constants |
|
242 | - * @return Response Reference to this object |
|
243 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
244 | - */ |
|
245 | - public function setStatus($status) { |
|
246 | - $this->status = $status; |
|
247 | - |
|
248 | - return $this; |
|
249 | - } |
|
250 | - |
|
251 | - /** |
|
252 | - * Set a Content-Security-Policy |
|
253 | - * @param EmptyContentSecurityPolicy $csp Policy to set for the response object |
|
254 | - * @return $this |
|
255 | - * @since 8.1.0 |
|
256 | - */ |
|
257 | - public function setContentSecurityPolicy(EmptyContentSecurityPolicy $csp) { |
|
258 | - $this->contentSecurityPolicy = $csp; |
|
259 | - return $this; |
|
260 | - } |
|
261 | - |
|
262 | - /** |
|
263 | - * Get the currently used Content-Security-Policy |
|
264 | - * @return EmptyContentSecurityPolicy|null Used Content-Security-Policy or null if |
|
265 | - * none specified. |
|
266 | - * @since 8.1.0 |
|
267 | - */ |
|
268 | - public function getContentSecurityPolicy() { |
|
269 | - return $this->contentSecurityPolicy; |
|
270 | - } |
|
271 | - |
|
272 | - |
|
273 | - /** |
|
274 | - * Get response status |
|
275 | - * @since 6.0.0 |
|
276 | - */ |
|
277 | - public function getStatus() { |
|
278 | - return $this->status; |
|
279 | - } |
|
280 | - |
|
281 | - |
|
282 | - /** |
|
283 | - * Get the ETag |
|
284 | - * @return string the etag |
|
285 | - * @since 6.0.0 |
|
286 | - */ |
|
287 | - public function getETag() { |
|
288 | - return $this->ETag; |
|
289 | - } |
|
290 | - |
|
291 | - |
|
292 | - /** |
|
293 | - * Get "last modified" date |
|
294 | - * @return \DateTime RFC2822 formatted last modified date |
|
295 | - * @since 6.0.0 |
|
296 | - */ |
|
297 | - public function getLastModified() { |
|
298 | - return $this->lastModified; |
|
299 | - } |
|
300 | - |
|
301 | - |
|
302 | - /** |
|
303 | - * Set the ETag |
|
304 | - * @param string $ETag |
|
305 | - * @return Response Reference to this object |
|
306 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
307 | - */ |
|
308 | - public function setETag($ETag) { |
|
309 | - $this->ETag = $ETag; |
|
310 | - |
|
311 | - return $this; |
|
312 | - } |
|
313 | - |
|
314 | - |
|
315 | - /** |
|
316 | - * Set "last modified" date |
|
317 | - * @param \DateTime $lastModified |
|
318 | - * @return Response Reference to this object |
|
319 | - * @since 6.0.0 - return value was added in 7.0.0 |
|
320 | - */ |
|
321 | - public function setLastModified($lastModified) { |
|
322 | - $this->lastModified = $lastModified; |
|
323 | - |
|
324 | - return $this; |
|
325 | - } |
|
326 | - |
|
327 | - /** |
|
328 | - * Marks the response as to throttle. Will be throttled when the |
|
329 | - * @BruteForceProtection annotation is added. |
|
330 | - * |
|
331 | - * @since 12.0.0 |
|
332 | - */ |
|
333 | - public function throttle() { |
|
334 | - $this->throttled = true; |
|
335 | - } |
|
336 | - |
|
337 | - /** |
|
338 | - * Whether the current response is throttled. |
|
339 | - * |
|
340 | - * @since 12.0.0 |
|
341 | - */ |
|
342 | - public function isThrottled() { |
|
343 | - return $this->throttled; |
|
344 | - } |
|
45 | + /** |
|
46 | + * Headers - defaults to ['Cache-Control' => 'no-cache, no-store, must-revalidate'] |
|
47 | + * @var array |
|
48 | + */ |
|
49 | + private $headers = array( |
|
50 | + 'Cache-Control' => 'no-cache, no-store, must-revalidate' |
|
51 | + ); |
|
52 | + |
|
53 | + |
|
54 | + /** |
|
55 | + * Cookies that will be need to be constructed as header |
|
56 | + * @var array |
|
57 | + */ |
|
58 | + private $cookies = array(); |
|
59 | + |
|
60 | + |
|
61 | + /** |
|
62 | + * HTTP status code - defaults to STATUS OK |
|
63 | + * @var int |
|
64 | + */ |
|
65 | + private $status = Http::STATUS_OK; |
|
66 | + |
|
67 | + |
|
68 | + /** |
|
69 | + * Last modified date |
|
70 | + * @var \DateTime |
|
71 | + */ |
|
72 | + private $lastModified; |
|
73 | + |
|
74 | + |
|
75 | + /** |
|
76 | + * ETag |
|
77 | + * @var string |
|
78 | + */ |
|
79 | + private $ETag; |
|
80 | + |
|
81 | + /** @var ContentSecurityPolicy|null Used Content-Security-Policy */ |
|
82 | + private $contentSecurityPolicy = null; |
|
83 | + |
|
84 | + /** @var bool */ |
|
85 | + private $throttled = false; |
|
86 | + |
|
87 | + /** |
|
88 | + * Caches the response |
|
89 | + * @param int $cacheSeconds the amount of seconds that should be cached |
|
90 | + * if 0 then caching will be disabled |
|
91 | + * @return $this |
|
92 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
93 | + */ |
|
94 | + public function cacheFor($cacheSeconds) { |
|
95 | + |
|
96 | + if($cacheSeconds > 0) { |
|
97 | + $this->addHeader('Cache-Control', 'max-age=' . $cacheSeconds . ', must-revalidate'); |
|
98 | + } else { |
|
99 | + $this->addHeader('Cache-Control', 'no-cache, no-store, must-revalidate'); |
|
100 | + } |
|
101 | + |
|
102 | + return $this; |
|
103 | + } |
|
104 | + |
|
105 | + /** |
|
106 | + * Adds a new cookie to the response |
|
107 | + * @param string $name The name of the cookie |
|
108 | + * @param string $value The value of the cookie |
|
109 | + * @param \DateTime|null $expireDate Date on that the cookie should expire, if set |
|
110 | + * to null cookie will be considered as session |
|
111 | + * cookie. |
|
112 | + * @return $this |
|
113 | + * @since 8.0.0 |
|
114 | + */ |
|
115 | + public function addCookie($name, $value, \DateTime $expireDate = null) { |
|
116 | + $this->cookies[$name] = array('value' => $value, 'expireDate' => $expireDate); |
|
117 | + return $this; |
|
118 | + } |
|
119 | + |
|
120 | + |
|
121 | + /** |
|
122 | + * Set the specified cookies |
|
123 | + * @param array $cookies array('foo' => array('value' => 'bar', 'expire' => null)) |
|
124 | + * @return $this |
|
125 | + * @since 8.0.0 |
|
126 | + */ |
|
127 | + public function setCookies(array $cookies) { |
|
128 | + $this->cookies = $cookies; |
|
129 | + return $this; |
|
130 | + } |
|
131 | + |
|
132 | + |
|
133 | + /** |
|
134 | + * Invalidates the specified cookie |
|
135 | + * @param string $name |
|
136 | + * @return $this |
|
137 | + * @since 8.0.0 |
|
138 | + */ |
|
139 | + public function invalidateCookie($name) { |
|
140 | + $this->addCookie($name, 'expired', new \DateTime('1971-01-01 00:00')); |
|
141 | + return $this; |
|
142 | + } |
|
143 | + |
|
144 | + /** |
|
145 | + * Invalidates the specified cookies |
|
146 | + * @param array $cookieNames array('foo', 'bar') |
|
147 | + * @return $this |
|
148 | + * @since 8.0.0 |
|
149 | + */ |
|
150 | + public function invalidateCookies(array $cookieNames) { |
|
151 | + foreach($cookieNames as $cookieName) { |
|
152 | + $this->invalidateCookie($cookieName); |
|
153 | + } |
|
154 | + return $this; |
|
155 | + } |
|
156 | + |
|
157 | + /** |
|
158 | + * Returns the cookies |
|
159 | + * @return array |
|
160 | + * @since 8.0.0 |
|
161 | + */ |
|
162 | + public function getCookies() { |
|
163 | + return $this->cookies; |
|
164 | + } |
|
165 | + |
|
166 | + /** |
|
167 | + * Adds a new header to the response that will be called before the render |
|
168 | + * function |
|
169 | + * @param string $name The name of the HTTP header |
|
170 | + * @param string $value The value, null will delete it |
|
171 | + * @return $this |
|
172 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
173 | + */ |
|
174 | + public function addHeader($name, $value) { |
|
175 | + $name = trim($name); // always remove leading and trailing whitespace |
|
176 | + // to be able to reliably check for security |
|
177 | + // headers |
|
178 | + |
|
179 | + if(is_null($value)) { |
|
180 | + unset($this->headers[$name]); |
|
181 | + } else { |
|
182 | + $this->headers[$name] = $value; |
|
183 | + } |
|
184 | + |
|
185 | + return $this; |
|
186 | + } |
|
187 | + |
|
188 | + |
|
189 | + /** |
|
190 | + * Set the headers |
|
191 | + * @param array $headers value header pairs |
|
192 | + * @return $this |
|
193 | + * @since 8.0.0 |
|
194 | + */ |
|
195 | + public function setHeaders(array $headers) { |
|
196 | + $this->headers = $headers; |
|
197 | + |
|
198 | + return $this; |
|
199 | + } |
|
200 | + |
|
201 | + |
|
202 | + /** |
|
203 | + * Returns the set headers |
|
204 | + * @return array the headers |
|
205 | + * @since 6.0.0 |
|
206 | + */ |
|
207 | + public function getHeaders() { |
|
208 | + $mergeWith = []; |
|
209 | + |
|
210 | + if($this->lastModified) { |
|
211 | + $mergeWith['Last-Modified'] = |
|
212 | + $this->lastModified->format(\DateTime::RFC2822); |
|
213 | + } |
|
214 | + |
|
215 | + // Build Content-Security-Policy and use default if none has been specified |
|
216 | + if(is_null($this->contentSecurityPolicy)) { |
|
217 | + $this->setContentSecurityPolicy(new ContentSecurityPolicy()); |
|
218 | + } |
|
219 | + $this->headers['Content-Security-Policy'] = $this->contentSecurityPolicy->buildPolicy(); |
|
220 | + |
|
221 | + if($this->ETag) { |
|
222 | + $mergeWith['ETag'] = '"' . $this->ETag . '"'; |
|
223 | + } |
|
224 | + |
|
225 | + return array_merge($mergeWith, $this->headers); |
|
226 | + } |
|
227 | + |
|
228 | + |
|
229 | + /** |
|
230 | + * By default renders no output |
|
231 | + * @return null |
|
232 | + * @since 6.0.0 |
|
233 | + */ |
|
234 | + public function render() { |
|
235 | + return null; |
|
236 | + } |
|
237 | + |
|
238 | + |
|
239 | + /** |
|
240 | + * Set response status |
|
241 | + * @param int $status a HTTP status code, see also the STATUS constants |
|
242 | + * @return Response Reference to this object |
|
243 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
244 | + */ |
|
245 | + public function setStatus($status) { |
|
246 | + $this->status = $status; |
|
247 | + |
|
248 | + return $this; |
|
249 | + } |
|
250 | + |
|
251 | + /** |
|
252 | + * Set a Content-Security-Policy |
|
253 | + * @param EmptyContentSecurityPolicy $csp Policy to set for the response object |
|
254 | + * @return $this |
|
255 | + * @since 8.1.0 |
|
256 | + */ |
|
257 | + public function setContentSecurityPolicy(EmptyContentSecurityPolicy $csp) { |
|
258 | + $this->contentSecurityPolicy = $csp; |
|
259 | + return $this; |
|
260 | + } |
|
261 | + |
|
262 | + /** |
|
263 | + * Get the currently used Content-Security-Policy |
|
264 | + * @return EmptyContentSecurityPolicy|null Used Content-Security-Policy or null if |
|
265 | + * none specified. |
|
266 | + * @since 8.1.0 |
|
267 | + */ |
|
268 | + public function getContentSecurityPolicy() { |
|
269 | + return $this->contentSecurityPolicy; |
|
270 | + } |
|
271 | + |
|
272 | + |
|
273 | + /** |
|
274 | + * Get response status |
|
275 | + * @since 6.0.0 |
|
276 | + */ |
|
277 | + public function getStatus() { |
|
278 | + return $this->status; |
|
279 | + } |
|
280 | + |
|
281 | + |
|
282 | + /** |
|
283 | + * Get the ETag |
|
284 | + * @return string the etag |
|
285 | + * @since 6.0.0 |
|
286 | + */ |
|
287 | + public function getETag() { |
|
288 | + return $this->ETag; |
|
289 | + } |
|
290 | + |
|
291 | + |
|
292 | + /** |
|
293 | + * Get "last modified" date |
|
294 | + * @return \DateTime RFC2822 formatted last modified date |
|
295 | + * @since 6.0.0 |
|
296 | + */ |
|
297 | + public function getLastModified() { |
|
298 | + return $this->lastModified; |
|
299 | + } |
|
300 | + |
|
301 | + |
|
302 | + /** |
|
303 | + * Set the ETag |
|
304 | + * @param string $ETag |
|
305 | + * @return Response Reference to this object |
|
306 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
307 | + */ |
|
308 | + public function setETag($ETag) { |
|
309 | + $this->ETag = $ETag; |
|
310 | + |
|
311 | + return $this; |
|
312 | + } |
|
313 | + |
|
314 | + |
|
315 | + /** |
|
316 | + * Set "last modified" date |
|
317 | + * @param \DateTime $lastModified |
|
318 | + * @return Response Reference to this object |
|
319 | + * @since 6.0.0 - return value was added in 7.0.0 |
|
320 | + */ |
|
321 | + public function setLastModified($lastModified) { |
|
322 | + $this->lastModified = $lastModified; |
|
323 | + |
|
324 | + return $this; |
|
325 | + } |
|
326 | + |
|
327 | + /** |
|
328 | + * Marks the response as to throttle. Will be throttled when the |
|
329 | + * @BruteForceProtection annotation is added. |
|
330 | + * |
|
331 | + * @since 12.0.0 |
|
332 | + */ |
|
333 | + public function throttle() { |
|
334 | + $this->throttled = true; |
|
335 | + } |
|
336 | + |
|
337 | + /** |
|
338 | + * Whether the current response is throttled. |
|
339 | + * |
|
340 | + * @since 12.0.0 |
|
341 | + */ |
|
342 | + public function isThrottled() { |
|
343 | + return $this->throttled; |
|
344 | + } |
|
345 | 345 | } |