@@ -66,97 +66,97 @@ discard block |
||
66 | 66 | * check fails |
67 | 67 | */ |
68 | 68 | class SecurityMiddleware extends Middleware { |
69 | - /** @var INavigationManager */ |
|
70 | - private $navigationManager; |
|
71 | - /** @var IRequest */ |
|
72 | - private $request; |
|
73 | - /** @var ControllerMethodReflector */ |
|
74 | - private $reflector; |
|
75 | - /** @var string */ |
|
76 | - private $appName; |
|
77 | - /** @var IURLGenerator */ |
|
78 | - private $urlGenerator; |
|
79 | - /** @var ILogger */ |
|
80 | - private $logger; |
|
81 | - /** @var bool */ |
|
82 | - private $isLoggedIn; |
|
83 | - /** @var bool */ |
|
84 | - private $isAdminUser; |
|
85 | - /** @var bool */ |
|
86 | - private $isSubAdmin; |
|
87 | - /** @var IAppManager */ |
|
88 | - private $appManager; |
|
89 | - /** @var IL10N */ |
|
90 | - private $l10n; |
|
91 | - |
|
92 | - public function __construct(IRequest $request, |
|
93 | - ControllerMethodReflector $reflector, |
|
94 | - INavigationManager $navigationManager, |
|
95 | - IURLGenerator $urlGenerator, |
|
96 | - ILogger $logger, |
|
97 | - string $appName, |
|
98 | - bool $isLoggedIn, |
|
99 | - bool $isAdminUser, |
|
100 | - bool $isSubAdmin, |
|
101 | - IAppManager $appManager, |
|
102 | - IL10N $l10n |
|
103 | - ) { |
|
104 | - $this->navigationManager = $navigationManager; |
|
105 | - $this->request = $request; |
|
106 | - $this->reflector = $reflector; |
|
107 | - $this->appName = $appName; |
|
108 | - $this->urlGenerator = $urlGenerator; |
|
109 | - $this->logger = $logger; |
|
110 | - $this->isLoggedIn = $isLoggedIn; |
|
111 | - $this->isAdminUser = $isAdminUser; |
|
112 | - $this->isSubAdmin = $isSubAdmin; |
|
113 | - $this->appManager = $appManager; |
|
114 | - $this->l10n = $l10n; |
|
115 | - } |
|
116 | - |
|
117 | - /** |
|
118 | - * This runs all the security checks before a method call. The |
|
119 | - * security checks are determined by inspecting the controller method |
|
120 | - * annotations |
|
121 | - * @param Controller $controller the controller |
|
122 | - * @param string $methodName the name of the method |
|
123 | - * @throws SecurityException when a security check fails |
|
124 | - */ |
|
125 | - public function beforeController($controller, $methodName) { |
|
126 | - |
|
127 | - // this will set the current navigation entry of the app, use this only |
|
128 | - // for normal HTML requests and not for AJAX requests |
|
129 | - $this->navigationManager->setActiveEntry($this->appName); |
|
130 | - |
|
131 | - // security checks |
|
132 | - $isPublicPage = $this->reflector->hasAnnotation('PublicPage'); |
|
133 | - if(!$isPublicPage) { |
|
134 | - if(!$this->isLoggedIn) { |
|
135 | - throw new NotLoggedInException(); |
|
136 | - } |
|
137 | - |
|
138 | - if($this->reflector->hasAnnotation('SubAdminRequired') |
|
139 | - && !$this->isSubAdmin |
|
140 | - && !$this->isAdminUser) { |
|
141 | - throw new NotAdminException($this->l10n->t('Logged in user must be an admin or sub admin')); |
|
142 | - } |
|
143 | - if(!$this->reflector->hasAnnotation('SubAdminRequired') |
|
144 | - && !$this->reflector->hasAnnotation('NoAdminRequired') |
|
145 | - && !$this->isAdminUser) { |
|
146 | - throw new NotAdminException($this->l10n->t('Logged in user must be an admin')); |
|
147 | - } |
|
148 | - } |
|
149 | - |
|
150 | - // Check for strict cookie requirement |
|
151 | - if($this->reflector->hasAnnotation('StrictCookieRequired') || !$this->reflector->hasAnnotation('NoCSRFRequired')) { |
|
152 | - if(!$this->request->passesStrictCookieCheck()) { |
|
153 | - throw new StrictCookieMissingException(); |
|
154 | - } |
|
155 | - } |
|
156 | - // CSRF check - also registers the CSRF token since the session may be closed later |
|
157 | - Util::callRegister(); |
|
158 | - if(!$this->reflector->hasAnnotation('NoCSRFRequired')) { |
|
159 | - /* |
|
69 | + /** @var INavigationManager */ |
|
70 | + private $navigationManager; |
|
71 | + /** @var IRequest */ |
|
72 | + private $request; |
|
73 | + /** @var ControllerMethodReflector */ |
|
74 | + private $reflector; |
|
75 | + /** @var string */ |
|
76 | + private $appName; |
|
77 | + /** @var IURLGenerator */ |
|
78 | + private $urlGenerator; |
|
79 | + /** @var ILogger */ |
|
80 | + private $logger; |
|
81 | + /** @var bool */ |
|
82 | + private $isLoggedIn; |
|
83 | + /** @var bool */ |
|
84 | + private $isAdminUser; |
|
85 | + /** @var bool */ |
|
86 | + private $isSubAdmin; |
|
87 | + /** @var IAppManager */ |
|
88 | + private $appManager; |
|
89 | + /** @var IL10N */ |
|
90 | + private $l10n; |
|
91 | + |
|
92 | + public function __construct(IRequest $request, |
|
93 | + ControllerMethodReflector $reflector, |
|
94 | + INavigationManager $navigationManager, |
|
95 | + IURLGenerator $urlGenerator, |
|
96 | + ILogger $logger, |
|
97 | + string $appName, |
|
98 | + bool $isLoggedIn, |
|
99 | + bool $isAdminUser, |
|
100 | + bool $isSubAdmin, |
|
101 | + IAppManager $appManager, |
|
102 | + IL10N $l10n |
|
103 | + ) { |
|
104 | + $this->navigationManager = $navigationManager; |
|
105 | + $this->request = $request; |
|
106 | + $this->reflector = $reflector; |
|
107 | + $this->appName = $appName; |
|
108 | + $this->urlGenerator = $urlGenerator; |
|
109 | + $this->logger = $logger; |
|
110 | + $this->isLoggedIn = $isLoggedIn; |
|
111 | + $this->isAdminUser = $isAdminUser; |
|
112 | + $this->isSubAdmin = $isSubAdmin; |
|
113 | + $this->appManager = $appManager; |
|
114 | + $this->l10n = $l10n; |
|
115 | + } |
|
116 | + |
|
117 | + /** |
|
118 | + * This runs all the security checks before a method call. The |
|
119 | + * security checks are determined by inspecting the controller method |
|
120 | + * annotations |
|
121 | + * @param Controller $controller the controller |
|
122 | + * @param string $methodName the name of the method |
|
123 | + * @throws SecurityException when a security check fails |
|
124 | + */ |
|
125 | + public function beforeController($controller, $methodName) { |
|
126 | + |
|
127 | + // this will set the current navigation entry of the app, use this only |
|
128 | + // for normal HTML requests and not for AJAX requests |
|
129 | + $this->navigationManager->setActiveEntry($this->appName); |
|
130 | + |
|
131 | + // security checks |
|
132 | + $isPublicPage = $this->reflector->hasAnnotation('PublicPage'); |
|
133 | + if(!$isPublicPage) { |
|
134 | + if(!$this->isLoggedIn) { |
|
135 | + throw new NotLoggedInException(); |
|
136 | + } |
|
137 | + |
|
138 | + if($this->reflector->hasAnnotation('SubAdminRequired') |
|
139 | + && !$this->isSubAdmin |
|
140 | + && !$this->isAdminUser) { |
|
141 | + throw new NotAdminException($this->l10n->t('Logged in user must be an admin or sub admin')); |
|
142 | + } |
|
143 | + if(!$this->reflector->hasAnnotation('SubAdminRequired') |
|
144 | + && !$this->reflector->hasAnnotation('NoAdminRequired') |
|
145 | + && !$this->isAdminUser) { |
|
146 | + throw new NotAdminException($this->l10n->t('Logged in user must be an admin')); |
|
147 | + } |
|
148 | + } |
|
149 | + |
|
150 | + // Check for strict cookie requirement |
|
151 | + if($this->reflector->hasAnnotation('StrictCookieRequired') || !$this->reflector->hasAnnotation('NoCSRFRequired')) { |
|
152 | + if(!$this->request->passesStrictCookieCheck()) { |
|
153 | + throw new StrictCookieMissingException(); |
|
154 | + } |
|
155 | + } |
|
156 | + // CSRF check - also registers the CSRF token since the session may be closed later |
|
157 | + Util::callRegister(); |
|
158 | + if(!$this->reflector->hasAnnotation('NoCSRFRequired')) { |
|
159 | + /* |
|
160 | 160 | * Only allow the CSRF check to fail on OCS Requests. This kind of |
161 | 161 | * hacks around that we have no full token auth in place yet and we |
162 | 162 | * do want to offer CSRF checks for web requests. |
@@ -164,75 +164,75 @@ discard block |
||
164 | 164 | * Additionally we allow Bearer authenticated requests to pass on OCS routes. |
165 | 165 | * This allows oauth apps (e.g. moodle) to use the OCS endpoints |
166 | 166 | */ |
167 | - if(!$this->request->passesCSRFCheck() && !( |
|
168 | - $controller instanceof OCSController && ( |
|
169 | - $this->request->getHeader('OCS-APIREQUEST') === 'true' || |
|
170 | - strpos($this->request->getHeader('Authorization'), 'Bearer ') === 0 |
|
171 | - ) |
|
172 | - )) { |
|
173 | - throw new CrossSiteRequestForgeryException(); |
|
174 | - } |
|
175 | - } |
|
176 | - |
|
177 | - /** |
|
178 | - * Checks if app is enabled (also includes a check whether user is allowed to access the resource) |
|
179 | - * The getAppPath() check is here since components such as settings also use the AppFramework and |
|
180 | - * therefore won't pass this check. |
|
181 | - * If page is public, app does not need to be enabled for current user/visitor |
|
182 | - */ |
|
183 | - try { |
|
184 | - $appPath = $this->appManager->getAppPath($this->appName); |
|
185 | - } catch (AppPathNotFoundException $e) { |
|
186 | - $appPath = false; |
|
187 | - } |
|
188 | - |
|
189 | - if ($appPath !== false && !$isPublicPage && !$this->appManager->isEnabledForUser($this->appName)) { |
|
190 | - throw new AppNotEnabledException(); |
|
191 | - } |
|
192 | - } |
|
193 | - |
|
194 | - /** |
|
195 | - * If an SecurityException is being caught, ajax requests return a JSON error |
|
196 | - * response and non ajax requests redirect to the index |
|
197 | - * @param Controller $controller the controller that is being called |
|
198 | - * @param string $methodName the name of the method that will be called on |
|
199 | - * the controller |
|
200 | - * @param \Exception $exception the thrown exception |
|
201 | - * @throws \Exception the passed in exception if it can't handle it |
|
202 | - * @return Response a Response object or null in case that the exception could not be handled |
|
203 | - */ |
|
204 | - public function afterException($controller, $methodName, \Exception $exception): Response { |
|
205 | - if($exception instanceof SecurityException) { |
|
206 | - if($exception instanceof StrictCookieMissingException) { |
|
207 | - return new RedirectResponse(\OC::$WEBROOT); |
|
208 | - } |
|
209 | - if (stripos($this->request->getHeader('Accept'),'html') === false) { |
|
210 | - $response = new JSONResponse( |
|
211 | - ['message' => $exception->getMessage()], |
|
212 | - $exception->getCode() |
|
213 | - ); |
|
214 | - } else { |
|
215 | - if($exception instanceof NotLoggedInException) { |
|
216 | - $params = []; |
|
217 | - if (isset($this->request->server['REQUEST_URI'])) { |
|
218 | - $params['redirect_url'] = $this->request->server['REQUEST_URI']; |
|
219 | - } |
|
220 | - $url = $this->urlGenerator->linkToRoute('core.login.showLoginForm', $params); |
|
221 | - $response = new RedirectResponse($url); |
|
222 | - } else { |
|
223 | - $response = new TemplateResponse('core', '403', ['message' => $exception->getMessage()], 'guest'); |
|
224 | - $response->setStatus($exception->getCode()); |
|
225 | - } |
|
226 | - } |
|
227 | - |
|
228 | - $this->logger->logException($exception, [ |
|
229 | - 'level' => ILogger::DEBUG, |
|
230 | - 'app' => 'core', |
|
231 | - ]); |
|
232 | - return $response; |
|
233 | - } |
|
234 | - |
|
235 | - throw $exception; |
|
236 | - } |
|
167 | + if(!$this->request->passesCSRFCheck() && !( |
|
168 | + $controller instanceof OCSController && ( |
|
169 | + $this->request->getHeader('OCS-APIREQUEST') === 'true' || |
|
170 | + strpos($this->request->getHeader('Authorization'), 'Bearer ') === 0 |
|
171 | + ) |
|
172 | + )) { |
|
173 | + throw new CrossSiteRequestForgeryException(); |
|
174 | + } |
|
175 | + } |
|
176 | + |
|
177 | + /** |
|
178 | + * Checks if app is enabled (also includes a check whether user is allowed to access the resource) |
|
179 | + * The getAppPath() check is here since components such as settings also use the AppFramework and |
|
180 | + * therefore won't pass this check. |
|
181 | + * If page is public, app does not need to be enabled for current user/visitor |
|
182 | + */ |
|
183 | + try { |
|
184 | + $appPath = $this->appManager->getAppPath($this->appName); |
|
185 | + } catch (AppPathNotFoundException $e) { |
|
186 | + $appPath = false; |
|
187 | + } |
|
188 | + |
|
189 | + if ($appPath !== false && !$isPublicPage && !$this->appManager->isEnabledForUser($this->appName)) { |
|
190 | + throw new AppNotEnabledException(); |
|
191 | + } |
|
192 | + } |
|
193 | + |
|
194 | + /** |
|
195 | + * If an SecurityException is being caught, ajax requests return a JSON error |
|
196 | + * response and non ajax requests redirect to the index |
|
197 | + * @param Controller $controller the controller that is being called |
|
198 | + * @param string $methodName the name of the method that will be called on |
|
199 | + * the controller |
|
200 | + * @param \Exception $exception the thrown exception |
|
201 | + * @throws \Exception the passed in exception if it can't handle it |
|
202 | + * @return Response a Response object or null in case that the exception could not be handled |
|
203 | + */ |
|
204 | + public function afterException($controller, $methodName, \Exception $exception): Response { |
|
205 | + if($exception instanceof SecurityException) { |
|
206 | + if($exception instanceof StrictCookieMissingException) { |
|
207 | + return new RedirectResponse(\OC::$WEBROOT); |
|
208 | + } |
|
209 | + if (stripos($this->request->getHeader('Accept'),'html') === false) { |
|
210 | + $response = new JSONResponse( |
|
211 | + ['message' => $exception->getMessage()], |
|
212 | + $exception->getCode() |
|
213 | + ); |
|
214 | + } else { |
|
215 | + if($exception instanceof NotLoggedInException) { |
|
216 | + $params = []; |
|
217 | + if (isset($this->request->server['REQUEST_URI'])) { |
|
218 | + $params['redirect_url'] = $this->request->server['REQUEST_URI']; |
|
219 | + } |
|
220 | + $url = $this->urlGenerator->linkToRoute('core.login.showLoginForm', $params); |
|
221 | + $response = new RedirectResponse($url); |
|
222 | + } else { |
|
223 | + $response = new TemplateResponse('core', '403', ['message' => $exception->getMessage()], 'guest'); |
|
224 | + $response->setStatus($exception->getCode()); |
|
225 | + } |
|
226 | + } |
|
227 | + |
|
228 | + $this->logger->logException($exception, [ |
|
229 | + 'level' => ILogger::DEBUG, |
|
230 | + 'app' => 'core', |
|
231 | + ]); |
|
232 | + return $response; |
|
233 | + } |
|
234 | + |
|
235 | + throw $exception; |
|
236 | + } |
|
237 | 237 | |
238 | 238 | } |
@@ -130,17 +130,17 @@ discard block |
||
130 | 130 | |
131 | 131 | // security checks |
132 | 132 | $isPublicPage = $this->reflector->hasAnnotation('PublicPage'); |
133 | - if(!$isPublicPage) { |
|
134 | - if(!$this->isLoggedIn) { |
|
133 | + if (!$isPublicPage) { |
|
134 | + if (!$this->isLoggedIn) { |
|
135 | 135 | throw new NotLoggedInException(); |
136 | 136 | } |
137 | 137 | |
138 | - if($this->reflector->hasAnnotation('SubAdminRequired') |
|
138 | + if ($this->reflector->hasAnnotation('SubAdminRequired') |
|
139 | 139 | && !$this->isSubAdmin |
140 | 140 | && !$this->isAdminUser) { |
141 | 141 | throw new NotAdminException($this->l10n->t('Logged in user must be an admin or sub admin')); |
142 | 142 | } |
143 | - if(!$this->reflector->hasAnnotation('SubAdminRequired') |
|
143 | + if (!$this->reflector->hasAnnotation('SubAdminRequired') |
|
144 | 144 | && !$this->reflector->hasAnnotation('NoAdminRequired') |
145 | 145 | && !$this->isAdminUser) { |
146 | 146 | throw new NotAdminException($this->l10n->t('Logged in user must be an admin')); |
@@ -148,14 +148,14 @@ discard block |
||
148 | 148 | } |
149 | 149 | |
150 | 150 | // Check for strict cookie requirement |
151 | - if($this->reflector->hasAnnotation('StrictCookieRequired') || !$this->reflector->hasAnnotation('NoCSRFRequired')) { |
|
152 | - if(!$this->request->passesStrictCookieCheck()) { |
|
151 | + if ($this->reflector->hasAnnotation('StrictCookieRequired') || !$this->reflector->hasAnnotation('NoCSRFRequired')) { |
|
152 | + if (!$this->request->passesStrictCookieCheck()) { |
|
153 | 153 | throw new StrictCookieMissingException(); |
154 | 154 | } |
155 | 155 | } |
156 | 156 | // CSRF check - also registers the CSRF token since the session may be closed later |
157 | 157 | Util::callRegister(); |
158 | - if(!$this->reflector->hasAnnotation('NoCSRFRequired')) { |
|
158 | + if (!$this->reflector->hasAnnotation('NoCSRFRequired')) { |
|
159 | 159 | /* |
160 | 160 | * Only allow the CSRF check to fail on OCS Requests. This kind of |
161 | 161 | * hacks around that we have no full token auth in place yet and we |
@@ -164,7 +164,7 @@ discard block |
||
164 | 164 | * Additionally we allow Bearer authenticated requests to pass on OCS routes. |
165 | 165 | * This allows oauth apps (e.g. moodle) to use the OCS endpoints |
166 | 166 | */ |
167 | - if(!$this->request->passesCSRFCheck() && !( |
|
167 | + if (!$this->request->passesCSRFCheck() && !( |
|
168 | 168 | $controller instanceof OCSController && ( |
169 | 169 | $this->request->getHeader('OCS-APIREQUEST') === 'true' || |
170 | 170 | strpos($this->request->getHeader('Authorization'), 'Bearer ') === 0 |
@@ -202,17 +202,17 @@ discard block |
||
202 | 202 | * @return Response a Response object or null in case that the exception could not be handled |
203 | 203 | */ |
204 | 204 | public function afterException($controller, $methodName, \Exception $exception): Response { |
205 | - if($exception instanceof SecurityException) { |
|
206 | - if($exception instanceof StrictCookieMissingException) { |
|
205 | + if ($exception instanceof SecurityException) { |
|
206 | + if ($exception instanceof StrictCookieMissingException) { |
|
207 | 207 | return new RedirectResponse(\OC::$WEBROOT); |
208 | 208 | } |
209 | - if (stripos($this->request->getHeader('Accept'),'html') === false) { |
|
209 | + if (stripos($this->request->getHeader('Accept'), 'html') === false) { |
|
210 | 210 | $response = new JSONResponse( |
211 | 211 | ['message' => $exception->getMessage()], |
212 | 212 | $exception->getCode() |
213 | 213 | ); |
214 | 214 | } else { |
215 | - if($exception instanceof NotLoggedInException) { |
|
215 | + if ($exception instanceof NotLoggedInException) { |
|
216 | 216 | $params = []; |
217 | 217 | if (isset($this->request->server['REQUEST_URI'])) { |
218 | 218 | $params['redirect_url'] = $this->request->server['REQUEST_URI']; |
@@ -35,46 +35,46 @@ |
||
35 | 35 | |
36 | 36 | class CSPMiddleware extends Middleware { |
37 | 37 | |
38 | - /** @var ContentSecurityPolicyManager */ |
|
39 | - private $contentSecurityPolicyManager; |
|
40 | - /** @var ContentSecurityPolicyNonceManager */ |
|
41 | - private $cspNonceManager; |
|
42 | - /** @var CsrfTokenManager */ |
|
43 | - private $csrfTokenManager; |
|
38 | + /** @var ContentSecurityPolicyManager */ |
|
39 | + private $contentSecurityPolicyManager; |
|
40 | + /** @var ContentSecurityPolicyNonceManager */ |
|
41 | + private $cspNonceManager; |
|
42 | + /** @var CsrfTokenManager */ |
|
43 | + private $csrfTokenManager; |
|
44 | 44 | |
45 | - public function __construct(ContentSecurityPolicyManager $policyManager, |
|
46 | - ContentSecurityPolicyNonceManager $cspNonceManager, |
|
47 | - CsrfTokenManager $csrfTokenManager) { |
|
48 | - $this->contentSecurityPolicyManager = $policyManager; |
|
49 | - $this->cspNonceManager = $cspNonceManager; |
|
50 | - $this->csrfTokenManager = $csrfTokenManager; |
|
51 | - } |
|
45 | + public function __construct(ContentSecurityPolicyManager $policyManager, |
|
46 | + ContentSecurityPolicyNonceManager $cspNonceManager, |
|
47 | + CsrfTokenManager $csrfTokenManager) { |
|
48 | + $this->contentSecurityPolicyManager = $policyManager; |
|
49 | + $this->cspNonceManager = $cspNonceManager; |
|
50 | + $this->csrfTokenManager = $csrfTokenManager; |
|
51 | + } |
|
52 | 52 | |
53 | - /** |
|
54 | - * Performs the default CSP modifications that may be injected by other |
|
55 | - * applications |
|
56 | - * |
|
57 | - * @param Controller $controller |
|
58 | - * @param string $methodName |
|
59 | - * @param Response $response |
|
60 | - * @return Response |
|
61 | - */ |
|
62 | - public function afterController($controller, $methodName, Response $response): Response { |
|
63 | - $policy = !is_null($response->getContentSecurityPolicy()) ? $response->getContentSecurityPolicy() : new ContentSecurityPolicy(); |
|
53 | + /** |
|
54 | + * Performs the default CSP modifications that may be injected by other |
|
55 | + * applications |
|
56 | + * |
|
57 | + * @param Controller $controller |
|
58 | + * @param string $methodName |
|
59 | + * @param Response $response |
|
60 | + * @return Response |
|
61 | + */ |
|
62 | + public function afterController($controller, $methodName, Response $response): Response { |
|
63 | + $policy = !is_null($response->getContentSecurityPolicy()) ? $response->getContentSecurityPolicy() : new ContentSecurityPolicy(); |
|
64 | 64 | |
65 | - if (get_class($policy) === EmptyContentSecurityPolicy::class) { |
|
66 | - return $response; |
|
67 | - } |
|
65 | + if (get_class($policy) === EmptyContentSecurityPolicy::class) { |
|
66 | + return $response; |
|
67 | + } |
|
68 | 68 | |
69 | - $defaultPolicy = $this->contentSecurityPolicyManager->getDefaultPolicy(); |
|
70 | - $defaultPolicy = $this->contentSecurityPolicyManager->mergePolicies($defaultPolicy, $policy); |
|
69 | + $defaultPolicy = $this->contentSecurityPolicyManager->getDefaultPolicy(); |
|
70 | + $defaultPolicy = $this->contentSecurityPolicyManager->mergePolicies($defaultPolicy, $policy); |
|
71 | 71 | |
72 | - if($this->cspNonceManager->browserSupportsCspV3()) { |
|
73 | - $defaultPolicy->useJsNonce($this->csrfTokenManager->getToken()->getEncryptedValue()); |
|
74 | - } |
|
72 | + if($this->cspNonceManager->browserSupportsCspV3()) { |
|
73 | + $defaultPolicy->useJsNonce($this->csrfTokenManager->getToken()->getEncryptedValue()); |
|
74 | + } |
|
75 | 75 | |
76 | - $response->setContentSecurityPolicy($defaultPolicy); |
|
76 | + $response->setContentSecurityPolicy($defaultPolicy); |
|
77 | 77 | |
78 | - return $response; |
|
79 | - } |
|
78 | + return $response; |
|
79 | + } |
|
80 | 80 | } |
@@ -69,7 +69,7 @@ |
||
69 | 69 | $defaultPolicy = $this->contentSecurityPolicyManager->getDefaultPolicy(); |
70 | 70 | $defaultPolicy = $this->contentSecurityPolicyManager->mergePolicies($defaultPolicy, $policy); |
71 | 71 | |
72 | - if($this->cspNonceManager->browserSupportsCspV3()) { |
|
72 | + if ($this->cspNonceManager->browserSupportsCspV3()) { |
|
73 | 73 | $defaultPolicy->useJsNonce($this->csrfTokenManager->getToken()->getEncryptedValue()); |
74 | 74 | } |
75 | 75 |
@@ -69,351 +69,351 @@ |
||
69 | 69 | |
70 | 70 | class DIContainer extends SimpleContainer implements IAppContainer { |
71 | 71 | |
72 | - /** |
|
73 | - * @var array |
|
74 | - */ |
|
75 | - private $middleWares = []; |
|
76 | - |
|
77 | - /** @var ServerContainer */ |
|
78 | - private $server; |
|
79 | - |
|
80 | - /** |
|
81 | - * Put your class dependencies in here |
|
82 | - * @param string $appName the name of the app |
|
83 | - * @param array $urlParams |
|
84 | - * @param ServerContainer|null $server |
|
85 | - */ |
|
86 | - public function __construct($appName, $urlParams = array(), ServerContainer $server = null){ |
|
87 | - parent::__construct(); |
|
88 | - $this['AppName'] = $appName; |
|
89 | - $this['urlParams'] = $urlParams; |
|
90 | - |
|
91 | - $this->registerAlias('Request', IRequest::class); |
|
92 | - |
|
93 | - /** @var \OC\ServerContainer $server */ |
|
94 | - if ($server === null) { |
|
95 | - $server = \OC::$server; |
|
96 | - } |
|
97 | - $this->server = $server; |
|
98 | - $this->server->registerAppContainer($appName, $this); |
|
99 | - |
|
100 | - // aliases |
|
101 | - $this->registerAlias('appName', 'AppName'); |
|
102 | - $this->registerAlias('webRoot', 'WebRoot'); |
|
103 | - $this->registerAlias('userId', 'UserId'); |
|
104 | - |
|
105 | - /** |
|
106 | - * Core services |
|
107 | - */ |
|
108 | - $this->registerService(IOutput::class, function(){ |
|
109 | - return new Output($this->getServer()->getWebRoot()); |
|
110 | - }); |
|
111 | - |
|
112 | - $this->registerService(Folder::class, function() { |
|
113 | - return $this->getServer()->getUserFolder(); |
|
114 | - }); |
|
115 | - |
|
116 | - $this->registerService(IAppData::class, function (SimpleContainer $c) { |
|
117 | - return $this->getServer()->getAppDataDir($c->query('AppName')); |
|
118 | - }); |
|
119 | - |
|
120 | - $this->registerService(IL10N::class, function($c) { |
|
121 | - return $this->getServer()->getL10N($c->query('AppName')); |
|
122 | - }); |
|
123 | - |
|
124 | - // Log wrapper |
|
125 | - $this->registerService(ILogger::class, function ($c) { |
|
126 | - return new OC\AppFramework\Logger($this->server->query(ILogger::class), $c->query('AppName')); |
|
127 | - }); |
|
128 | - |
|
129 | - $this->registerService(IServerContainer::class, function () { |
|
130 | - return $this->getServer(); |
|
131 | - }); |
|
132 | - $this->registerAlias('ServerContainer', IServerContainer::class); |
|
133 | - |
|
134 | - $this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) { |
|
135 | - return $c->query(Manager::class); |
|
136 | - }); |
|
137 | - |
|
138 | - $this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) { |
|
139 | - return $c; |
|
140 | - }); |
|
141 | - |
|
142 | - // commonly used attributes |
|
143 | - $this->registerService('UserId', function ($c) { |
|
144 | - return $c->query(IUserSession::class)->getSession()->get('user_id'); |
|
145 | - }); |
|
146 | - |
|
147 | - $this->registerService('WebRoot', function ($c) { |
|
148 | - return $c->query('ServerContainer')->getWebRoot(); |
|
149 | - }); |
|
150 | - |
|
151 | - $this->registerService('OC_Defaults', function ($c) { |
|
152 | - return $c->getServer()->getThemingDefaults(); |
|
153 | - }); |
|
154 | - |
|
155 | - $this->registerService(IConfig::class, function ($c) { |
|
156 | - return $c->query(OC\GlobalScale\Config::class); |
|
157 | - }); |
|
158 | - |
|
159 | - $this->registerService('Protocol', function($c){ |
|
160 | - /** @var \OC\Server $server */ |
|
161 | - $server = $c->query('ServerContainer'); |
|
162 | - $protocol = $server->getRequest()->getHttpProtocol(); |
|
163 | - return new Http($_SERVER, $protocol); |
|
164 | - }); |
|
165 | - |
|
166 | - $this->registerService('Dispatcher', function($c) { |
|
167 | - return new Dispatcher( |
|
168 | - $c['Protocol'], |
|
169 | - $c['MiddlewareDispatcher'], |
|
170 | - $c->query(IControllerMethodReflector::class), |
|
171 | - $c['Request'] |
|
172 | - ); |
|
173 | - }); |
|
174 | - |
|
175 | - /** |
|
176 | - * App Framework default arguments |
|
177 | - */ |
|
178 | - $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH'); |
|
179 | - $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept'); |
|
180 | - $this->registerParameter('corsMaxAge', 1728000); |
|
181 | - |
|
182 | - /** |
|
183 | - * Middleware |
|
184 | - */ |
|
185 | - $this->registerService('MiddlewareDispatcher', function(SimpleContainer $c) { |
|
186 | - $server = $this->getServer(); |
|
187 | - |
|
188 | - $dispatcher = new MiddlewareDispatcher(); |
|
189 | - $dispatcher->registerMiddleware( |
|
190 | - $c->query(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class) |
|
191 | - ); |
|
192 | - |
|
193 | - $dispatcher->registerMiddleware( |
|
194 | - new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware( |
|
195 | - $c->query(IRequest::class), |
|
196 | - $c->query(IControllerMethodReflector::class) |
|
197 | - ) |
|
198 | - ); |
|
199 | - $dispatcher->registerMiddleware( |
|
200 | - new CORSMiddleware( |
|
201 | - $c->query(IRequest::class), |
|
202 | - $c->query(IControllerMethodReflector::class), |
|
203 | - $c->query(IUserSession::class), |
|
204 | - $c->query(OC\Security\Bruteforce\Throttler::class) |
|
205 | - ) |
|
206 | - ); |
|
207 | - $dispatcher->registerMiddleware( |
|
208 | - new OCSMiddleware( |
|
209 | - $c->query(IRequest::class) |
|
210 | - ) |
|
211 | - ); |
|
212 | - |
|
213 | - $securityMiddleware = new SecurityMiddleware( |
|
214 | - $c->query(IRequest::class), |
|
215 | - $c->query(IControllerMethodReflector::class), |
|
216 | - $c->query(INavigationManager::class), |
|
217 | - $c->query(IURLGenerator::class), |
|
218 | - $server->getLogger(), |
|
219 | - $c['AppName'], |
|
220 | - $server->getUserSession()->isLoggedIn(), |
|
221 | - $server->getGroupManager()->isAdmin($this->getUserId()), |
|
222 | - $server->getUserSession()->getUser() !== null && $server->query(ISubAdmin::class)->isSubAdmin($server->getUserSession()->getUser()), |
|
223 | - $server->getAppManager(), |
|
224 | - $server->getL10N('lib') |
|
225 | - ); |
|
226 | - $dispatcher->registerMiddleware($securityMiddleware); |
|
227 | - $dispatcher->registerMiddleware( |
|
228 | - new OC\AppFramework\Middleware\Security\CSPMiddleware( |
|
229 | - $server->query(OC\Security\CSP\ContentSecurityPolicyManager::class), |
|
230 | - $server->query(OC\Security\CSP\ContentSecurityPolicyNonceManager::class), |
|
231 | - $server->query(OC\Security\CSRF\CsrfTokenManager::class) |
|
232 | - ) |
|
233 | - ); |
|
234 | - $dispatcher->registerMiddleware( |
|
235 | - new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware( |
|
236 | - $c->query(IControllerMethodReflector::class), |
|
237 | - $c->query(ISession::class), |
|
238 | - $c->query(IUserSession::class), |
|
239 | - $c->query(ITimeFactory::class) |
|
240 | - ) |
|
241 | - ); |
|
242 | - $dispatcher->registerMiddleware( |
|
243 | - new TwoFactorMiddleware( |
|
244 | - $c->query(OC\Authentication\TwoFactorAuth\Manager::class), |
|
245 | - $c->query(IUserSession::class), |
|
246 | - $c->query(ISession::class), |
|
247 | - $c->query(IURLGenerator::class), |
|
248 | - $c->query(IControllerMethodReflector::class), |
|
249 | - $c->query(IRequest::class) |
|
250 | - ) |
|
251 | - ); |
|
252 | - $dispatcher->registerMiddleware( |
|
253 | - new OC\AppFramework\Middleware\Security\BruteForceMiddleware( |
|
254 | - $c->query(IControllerMethodReflector::class), |
|
255 | - $c->query(OC\Security\Bruteforce\Throttler::class), |
|
256 | - $c->query(IRequest::class) |
|
257 | - ) |
|
258 | - ); |
|
259 | - $dispatcher->registerMiddleware( |
|
260 | - new RateLimitingMiddleware( |
|
261 | - $c->query(IRequest::class), |
|
262 | - $c->query(IUserSession::class), |
|
263 | - $c->query(IControllerMethodReflector::class), |
|
264 | - $c->query(OC\Security\RateLimiting\Limiter::class) |
|
265 | - ) |
|
266 | - ); |
|
267 | - $dispatcher->registerMiddleware( |
|
268 | - new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware( |
|
269 | - $c->query(IRequest::class), |
|
270 | - $c->query(ISession::class), |
|
271 | - $c->query(\OCP\IConfig::class) |
|
272 | - ) |
|
273 | - ); |
|
274 | - $dispatcher->registerMiddleware( |
|
275 | - $c->query(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class) |
|
276 | - ); |
|
277 | - |
|
278 | - foreach($this->middleWares as $middleWare) { |
|
279 | - $dispatcher->registerMiddleware($c[$middleWare]); |
|
280 | - } |
|
281 | - |
|
282 | - $dispatcher->registerMiddleware( |
|
283 | - new SessionMiddleware( |
|
284 | - $c->query(IRequest::class), |
|
285 | - $c->query(IControllerMethodReflector::class), |
|
286 | - $c->query(ISession::class) |
|
287 | - ) |
|
288 | - ); |
|
289 | - return $dispatcher; |
|
290 | - }); |
|
291 | - |
|
292 | - $this->registerAlias(\OCP\Collaboration\Resources\IManager::class, OC\Collaboration\Resources\Manager::class); |
|
293 | - } |
|
294 | - |
|
295 | - /** |
|
296 | - * @return \OCP\IServerContainer |
|
297 | - */ |
|
298 | - public function getServer() |
|
299 | - { |
|
300 | - return $this->server; |
|
301 | - } |
|
302 | - |
|
303 | - /** |
|
304 | - * @param string $middleWare |
|
305 | - * @return boolean|null |
|
306 | - */ |
|
307 | - public function registerMiddleWare($middleWare) { |
|
308 | - if (in_array($middleWare, $this->middleWares, true) !== false) { |
|
309 | - return false; |
|
310 | - } |
|
311 | - $this->middleWares[] = $middleWare; |
|
312 | - } |
|
313 | - |
|
314 | - /** |
|
315 | - * used to return the appname of the set application |
|
316 | - * @return string the name of your application |
|
317 | - */ |
|
318 | - public function getAppName() { |
|
319 | - return $this->query('AppName'); |
|
320 | - } |
|
321 | - |
|
322 | - /** |
|
323 | - * @deprecated use IUserSession->isLoggedIn() |
|
324 | - * @return boolean |
|
325 | - */ |
|
326 | - public function isLoggedIn() { |
|
327 | - return \OC::$server->getUserSession()->isLoggedIn(); |
|
328 | - } |
|
329 | - |
|
330 | - /** |
|
331 | - * @deprecated use IGroupManager->isAdmin($userId) |
|
332 | - * @return boolean |
|
333 | - */ |
|
334 | - public function isAdminUser() { |
|
335 | - $uid = $this->getUserId(); |
|
336 | - return \OC_User::isAdminUser($uid); |
|
337 | - } |
|
338 | - |
|
339 | - private function getUserId() { |
|
340 | - return $this->getServer()->getSession()->get('user_id'); |
|
341 | - } |
|
342 | - |
|
343 | - /** |
|
344 | - * @deprecated use the ILogger instead |
|
345 | - * @param string $message |
|
346 | - * @param string $level |
|
347 | - * @return mixed |
|
348 | - */ |
|
349 | - public function log($message, $level) { |
|
350 | - switch($level){ |
|
351 | - case 'debug': |
|
352 | - $level = ILogger::DEBUG; |
|
353 | - break; |
|
354 | - case 'info': |
|
355 | - $level = ILogger::INFO; |
|
356 | - break; |
|
357 | - case 'warn': |
|
358 | - $level = ILogger::WARN; |
|
359 | - break; |
|
360 | - case 'fatal': |
|
361 | - $level = ILogger::FATAL; |
|
362 | - break; |
|
363 | - default: |
|
364 | - $level = ILogger::ERROR; |
|
365 | - break; |
|
366 | - } |
|
367 | - \OCP\Util::writeLog($this->getAppName(), $message, $level); |
|
368 | - } |
|
369 | - |
|
370 | - /** |
|
371 | - * Register a capability |
|
372 | - * |
|
373 | - * @param string $serviceName e.g. 'OCA\Files\Capabilities' |
|
374 | - */ |
|
375 | - public function registerCapability($serviceName) { |
|
376 | - $this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) { |
|
377 | - return $this->query($serviceName); |
|
378 | - }); |
|
379 | - } |
|
380 | - |
|
381 | - public function query(string $name, bool $autoload = true) { |
|
382 | - try { |
|
383 | - return $this->queryNoFallback($name); |
|
384 | - } catch (QueryException $firstException) { |
|
385 | - try { |
|
386 | - return $this->getServer()->query($name, $autoload); |
|
387 | - } catch (QueryException $secondException) { |
|
388 | - if ($firstException->getCode() === 1) { |
|
389 | - throw $secondException; |
|
390 | - } |
|
391 | - throw $firstException; |
|
392 | - } |
|
393 | - } |
|
394 | - } |
|
395 | - |
|
396 | - /** |
|
397 | - * @param string $name |
|
398 | - * @return mixed |
|
399 | - * @throws QueryException if the query could not be resolved |
|
400 | - */ |
|
401 | - public function queryNoFallback($name) { |
|
402 | - $name = $this->sanitizeName($name); |
|
403 | - |
|
404 | - if ($this->offsetExists($name)) { |
|
405 | - return parent::query($name); |
|
406 | - } else { |
|
407 | - if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) { |
|
408 | - return parent::query($name); |
|
409 | - } else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) { |
|
410 | - return parent::query($name); |
|
411 | - } else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) { |
|
412 | - return parent::query($name); |
|
413 | - } |
|
414 | - } |
|
415 | - |
|
416 | - throw new QueryException('Could not resolve ' . $name . '!' . |
|
417 | - ' Class can not be instantiated', 1); |
|
418 | - } |
|
72 | + /** |
|
73 | + * @var array |
|
74 | + */ |
|
75 | + private $middleWares = []; |
|
76 | + |
|
77 | + /** @var ServerContainer */ |
|
78 | + private $server; |
|
79 | + |
|
80 | + /** |
|
81 | + * Put your class dependencies in here |
|
82 | + * @param string $appName the name of the app |
|
83 | + * @param array $urlParams |
|
84 | + * @param ServerContainer|null $server |
|
85 | + */ |
|
86 | + public function __construct($appName, $urlParams = array(), ServerContainer $server = null){ |
|
87 | + parent::__construct(); |
|
88 | + $this['AppName'] = $appName; |
|
89 | + $this['urlParams'] = $urlParams; |
|
90 | + |
|
91 | + $this->registerAlias('Request', IRequest::class); |
|
92 | + |
|
93 | + /** @var \OC\ServerContainer $server */ |
|
94 | + if ($server === null) { |
|
95 | + $server = \OC::$server; |
|
96 | + } |
|
97 | + $this->server = $server; |
|
98 | + $this->server->registerAppContainer($appName, $this); |
|
99 | + |
|
100 | + // aliases |
|
101 | + $this->registerAlias('appName', 'AppName'); |
|
102 | + $this->registerAlias('webRoot', 'WebRoot'); |
|
103 | + $this->registerAlias('userId', 'UserId'); |
|
104 | + |
|
105 | + /** |
|
106 | + * Core services |
|
107 | + */ |
|
108 | + $this->registerService(IOutput::class, function(){ |
|
109 | + return new Output($this->getServer()->getWebRoot()); |
|
110 | + }); |
|
111 | + |
|
112 | + $this->registerService(Folder::class, function() { |
|
113 | + return $this->getServer()->getUserFolder(); |
|
114 | + }); |
|
115 | + |
|
116 | + $this->registerService(IAppData::class, function (SimpleContainer $c) { |
|
117 | + return $this->getServer()->getAppDataDir($c->query('AppName')); |
|
118 | + }); |
|
119 | + |
|
120 | + $this->registerService(IL10N::class, function($c) { |
|
121 | + return $this->getServer()->getL10N($c->query('AppName')); |
|
122 | + }); |
|
123 | + |
|
124 | + // Log wrapper |
|
125 | + $this->registerService(ILogger::class, function ($c) { |
|
126 | + return new OC\AppFramework\Logger($this->server->query(ILogger::class), $c->query('AppName')); |
|
127 | + }); |
|
128 | + |
|
129 | + $this->registerService(IServerContainer::class, function () { |
|
130 | + return $this->getServer(); |
|
131 | + }); |
|
132 | + $this->registerAlias('ServerContainer', IServerContainer::class); |
|
133 | + |
|
134 | + $this->registerService(\OCP\WorkflowEngine\IManager::class, function ($c) { |
|
135 | + return $c->query(Manager::class); |
|
136 | + }); |
|
137 | + |
|
138 | + $this->registerService(\OCP\AppFramework\IAppContainer::class, function ($c) { |
|
139 | + return $c; |
|
140 | + }); |
|
141 | + |
|
142 | + // commonly used attributes |
|
143 | + $this->registerService('UserId', function ($c) { |
|
144 | + return $c->query(IUserSession::class)->getSession()->get('user_id'); |
|
145 | + }); |
|
146 | + |
|
147 | + $this->registerService('WebRoot', function ($c) { |
|
148 | + return $c->query('ServerContainer')->getWebRoot(); |
|
149 | + }); |
|
150 | + |
|
151 | + $this->registerService('OC_Defaults', function ($c) { |
|
152 | + return $c->getServer()->getThemingDefaults(); |
|
153 | + }); |
|
154 | + |
|
155 | + $this->registerService(IConfig::class, function ($c) { |
|
156 | + return $c->query(OC\GlobalScale\Config::class); |
|
157 | + }); |
|
158 | + |
|
159 | + $this->registerService('Protocol', function($c){ |
|
160 | + /** @var \OC\Server $server */ |
|
161 | + $server = $c->query('ServerContainer'); |
|
162 | + $protocol = $server->getRequest()->getHttpProtocol(); |
|
163 | + return new Http($_SERVER, $protocol); |
|
164 | + }); |
|
165 | + |
|
166 | + $this->registerService('Dispatcher', function($c) { |
|
167 | + return new Dispatcher( |
|
168 | + $c['Protocol'], |
|
169 | + $c['MiddlewareDispatcher'], |
|
170 | + $c->query(IControllerMethodReflector::class), |
|
171 | + $c['Request'] |
|
172 | + ); |
|
173 | + }); |
|
174 | + |
|
175 | + /** |
|
176 | + * App Framework default arguments |
|
177 | + */ |
|
178 | + $this->registerParameter('corsMethods', 'PUT, POST, GET, DELETE, PATCH'); |
|
179 | + $this->registerParameter('corsAllowedHeaders', 'Authorization, Content-Type, Accept'); |
|
180 | + $this->registerParameter('corsMaxAge', 1728000); |
|
181 | + |
|
182 | + /** |
|
183 | + * Middleware |
|
184 | + */ |
|
185 | + $this->registerService('MiddlewareDispatcher', function(SimpleContainer $c) { |
|
186 | + $server = $this->getServer(); |
|
187 | + |
|
188 | + $dispatcher = new MiddlewareDispatcher(); |
|
189 | + $dispatcher->registerMiddleware( |
|
190 | + $c->query(OC\AppFramework\Middleware\Security\ReloadExecutionMiddleware::class) |
|
191 | + ); |
|
192 | + |
|
193 | + $dispatcher->registerMiddleware( |
|
194 | + new OC\AppFramework\Middleware\Security\SameSiteCookieMiddleware( |
|
195 | + $c->query(IRequest::class), |
|
196 | + $c->query(IControllerMethodReflector::class) |
|
197 | + ) |
|
198 | + ); |
|
199 | + $dispatcher->registerMiddleware( |
|
200 | + new CORSMiddleware( |
|
201 | + $c->query(IRequest::class), |
|
202 | + $c->query(IControllerMethodReflector::class), |
|
203 | + $c->query(IUserSession::class), |
|
204 | + $c->query(OC\Security\Bruteforce\Throttler::class) |
|
205 | + ) |
|
206 | + ); |
|
207 | + $dispatcher->registerMiddleware( |
|
208 | + new OCSMiddleware( |
|
209 | + $c->query(IRequest::class) |
|
210 | + ) |
|
211 | + ); |
|
212 | + |
|
213 | + $securityMiddleware = new SecurityMiddleware( |
|
214 | + $c->query(IRequest::class), |
|
215 | + $c->query(IControllerMethodReflector::class), |
|
216 | + $c->query(INavigationManager::class), |
|
217 | + $c->query(IURLGenerator::class), |
|
218 | + $server->getLogger(), |
|
219 | + $c['AppName'], |
|
220 | + $server->getUserSession()->isLoggedIn(), |
|
221 | + $server->getGroupManager()->isAdmin($this->getUserId()), |
|
222 | + $server->getUserSession()->getUser() !== null && $server->query(ISubAdmin::class)->isSubAdmin($server->getUserSession()->getUser()), |
|
223 | + $server->getAppManager(), |
|
224 | + $server->getL10N('lib') |
|
225 | + ); |
|
226 | + $dispatcher->registerMiddleware($securityMiddleware); |
|
227 | + $dispatcher->registerMiddleware( |
|
228 | + new OC\AppFramework\Middleware\Security\CSPMiddleware( |
|
229 | + $server->query(OC\Security\CSP\ContentSecurityPolicyManager::class), |
|
230 | + $server->query(OC\Security\CSP\ContentSecurityPolicyNonceManager::class), |
|
231 | + $server->query(OC\Security\CSRF\CsrfTokenManager::class) |
|
232 | + ) |
|
233 | + ); |
|
234 | + $dispatcher->registerMiddleware( |
|
235 | + new OC\AppFramework\Middleware\Security\PasswordConfirmationMiddleware( |
|
236 | + $c->query(IControllerMethodReflector::class), |
|
237 | + $c->query(ISession::class), |
|
238 | + $c->query(IUserSession::class), |
|
239 | + $c->query(ITimeFactory::class) |
|
240 | + ) |
|
241 | + ); |
|
242 | + $dispatcher->registerMiddleware( |
|
243 | + new TwoFactorMiddleware( |
|
244 | + $c->query(OC\Authentication\TwoFactorAuth\Manager::class), |
|
245 | + $c->query(IUserSession::class), |
|
246 | + $c->query(ISession::class), |
|
247 | + $c->query(IURLGenerator::class), |
|
248 | + $c->query(IControllerMethodReflector::class), |
|
249 | + $c->query(IRequest::class) |
|
250 | + ) |
|
251 | + ); |
|
252 | + $dispatcher->registerMiddleware( |
|
253 | + new OC\AppFramework\Middleware\Security\BruteForceMiddleware( |
|
254 | + $c->query(IControllerMethodReflector::class), |
|
255 | + $c->query(OC\Security\Bruteforce\Throttler::class), |
|
256 | + $c->query(IRequest::class) |
|
257 | + ) |
|
258 | + ); |
|
259 | + $dispatcher->registerMiddleware( |
|
260 | + new RateLimitingMiddleware( |
|
261 | + $c->query(IRequest::class), |
|
262 | + $c->query(IUserSession::class), |
|
263 | + $c->query(IControllerMethodReflector::class), |
|
264 | + $c->query(OC\Security\RateLimiting\Limiter::class) |
|
265 | + ) |
|
266 | + ); |
|
267 | + $dispatcher->registerMiddleware( |
|
268 | + new OC\AppFramework\Middleware\PublicShare\PublicShareMiddleware( |
|
269 | + $c->query(IRequest::class), |
|
270 | + $c->query(ISession::class), |
|
271 | + $c->query(\OCP\IConfig::class) |
|
272 | + ) |
|
273 | + ); |
|
274 | + $dispatcher->registerMiddleware( |
|
275 | + $c->query(\OC\AppFramework\Middleware\AdditionalScriptsMiddleware::class) |
|
276 | + ); |
|
277 | + |
|
278 | + foreach($this->middleWares as $middleWare) { |
|
279 | + $dispatcher->registerMiddleware($c[$middleWare]); |
|
280 | + } |
|
281 | + |
|
282 | + $dispatcher->registerMiddleware( |
|
283 | + new SessionMiddleware( |
|
284 | + $c->query(IRequest::class), |
|
285 | + $c->query(IControllerMethodReflector::class), |
|
286 | + $c->query(ISession::class) |
|
287 | + ) |
|
288 | + ); |
|
289 | + return $dispatcher; |
|
290 | + }); |
|
291 | + |
|
292 | + $this->registerAlias(\OCP\Collaboration\Resources\IManager::class, OC\Collaboration\Resources\Manager::class); |
|
293 | + } |
|
294 | + |
|
295 | + /** |
|
296 | + * @return \OCP\IServerContainer |
|
297 | + */ |
|
298 | + public function getServer() |
|
299 | + { |
|
300 | + return $this->server; |
|
301 | + } |
|
302 | + |
|
303 | + /** |
|
304 | + * @param string $middleWare |
|
305 | + * @return boolean|null |
|
306 | + */ |
|
307 | + public function registerMiddleWare($middleWare) { |
|
308 | + if (in_array($middleWare, $this->middleWares, true) !== false) { |
|
309 | + return false; |
|
310 | + } |
|
311 | + $this->middleWares[] = $middleWare; |
|
312 | + } |
|
313 | + |
|
314 | + /** |
|
315 | + * used to return the appname of the set application |
|
316 | + * @return string the name of your application |
|
317 | + */ |
|
318 | + public function getAppName() { |
|
319 | + return $this->query('AppName'); |
|
320 | + } |
|
321 | + |
|
322 | + /** |
|
323 | + * @deprecated use IUserSession->isLoggedIn() |
|
324 | + * @return boolean |
|
325 | + */ |
|
326 | + public function isLoggedIn() { |
|
327 | + return \OC::$server->getUserSession()->isLoggedIn(); |
|
328 | + } |
|
329 | + |
|
330 | + /** |
|
331 | + * @deprecated use IGroupManager->isAdmin($userId) |
|
332 | + * @return boolean |
|
333 | + */ |
|
334 | + public function isAdminUser() { |
|
335 | + $uid = $this->getUserId(); |
|
336 | + return \OC_User::isAdminUser($uid); |
|
337 | + } |
|
338 | + |
|
339 | + private function getUserId() { |
|
340 | + return $this->getServer()->getSession()->get('user_id'); |
|
341 | + } |
|
342 | + |
|
343 | + /** |
|
344 | + * @deprecated use the ILogger instead |
|
345 | + * @param string $message |
|
346 | + * @param string $level |
|
347 | + * @return mixed |
|
348 | + */ |
|
349 | + public function log($message, $level) { |
|
350 | + switch($level){ |
|
351 | + case 'debug': |
|
352 | + $level = ILogger::DEBUG; |
|
353 | + break; |
|
354 | + case 'info': |
|
355 | + $level = ILogger::INFO; |
|
356 | + break; |
|
357 | + case 'warn': |
|
358 | + $level = ILogger::WARN; |
|
359 | + break; |
|
360 | + case 'fatal': |
|
361 | + $level = ILogger::FATAL; |
|
362 | + break; |
|
363 | + default: |
|
364 | + $level = ILogger::ERROR; |
|
365 | + break; |
|
366 | + } |
|
367 | + \OCP\Util::writeLog($this->getAppName(), $message, $level); |
|
368 | + } |
|
369 | + |
|
370 | + /** |
|
371 | + * Register a capability |
|
372 | + * |
|
373 | + * @param string $serviceName e.g. 'OCA\Files\Capabilities' |
|
374 | + */ |
|
375 | + public function registerCapability($serviceName) { |
|
376 | + $this->query('OC\CapabilitiesManager')->registerCapability(function() use ($serviceName) { |
|
377 | + return $this->query($serviceName); |
|
378 | + }); |
|
379 | + } |
|
380 | + |
|
381 | + public function query(string $name, bool $autoload = true) { |
|
382 | + try { |
|
383 | + return $this->queryNoFallback($name); |
|
384 | + } catch (QueryException $firstException) { |
|
385 | + try { |
|
386 | + return $this->getServer()->query($name, $autoload); |
|
387 | + } catch (QueryException $secondException) { |
|
388 | + if ($firstException->getCode() === 1) { |
|
389 | + throw $secondException; |
|
390 | + } |
|
391 | + throw $firstException; |
|
392 | + } |
|
393 | + } |
|
394 | + } |
|
395 | + |
|
396 | + /** |
|
397 | + * @param string $name |
|
398 | + * @return mixed |
|
399 | + * @throws QueryException if the query could not be resolved |
|
400 | + */ |
|
401 | + public function queryNoFallback($name) { |
|
402 | + $name = $this->sanitizeName($name); |
|
403 | + |
|
404 | + if ($this->offsetExists($name)) { |
|
405 | + return parent::query($name); |
|
406 | + } else { |
|
407 | + if ($this['AppName'] === 'settings' && strpos($name, 'OC\\Settings\\') === 0) { |
|
408 | + return parent::query($name); |
|
409 | + } else if ($this['AppName'] === 'core' && strpos($name, 'OC\\Core\\') === 0) { |
|
410 | + return parent::query($name); |
|
411 | + } else if (strpos($name, \OC\AppFramework\App::buildAppNamespace($this['AppName']) . '\\') === 0) { |
|
412 | + return parent::query($name); |
|
413 | + } |
|
414 | + } |
|
415 | + |
|
416 | + throw new QueryException('Could not resolve ' . $name . '!' . |
|
417 | + ' Class can not be instantiated', 1); |
|
418 | + } |
|
419 | 419 | } |