-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathApplication.php
More file actions
219 lines (184 loc) · 6.35 KB
/
Application.php
File metadata and controls
219 lines (184 loc) · 6.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
<?php
namespace App;
use App\Http\Middleware\CorsMiddleware;
use App\Http\Middleware\DemoRateLimitMiddleware;
use App\Http\Middleware\MissingControllerMiddleware;
use App\Http\Middleware\RedirectMiddleware;
use Authentication\AuthenticationService;
use Authentication\AuthenticationServiceInterface;
use Authentication\AuthenticationServiceProviderInterface;
use Authentication\Identifier\PasswordIdentifier;
use Authentication\Middleware\AuthenticationMiddleware;
use Authorization\AuthorizationService;
use Authorization\AuthorizationServiceInterface;
use Authorization\AuthorizationServiceProviderInterface;
use Authorization\Middleware\AuthorizationMiddleware;
use Authorization\Policy\MapResolver;
use Cache\Routing\Middleware\CacheMiddleware;
use Cake\Core\Configure;
use Cake\Core\ContainerInterface;
use Cake\Core\Exception\MissingPluginException;
use Cake\Http\BaseApplication;
use Cake\Http\MiddlewareQueue;
use Cake\Http\ServerRequest;
use Cake\Routing\Middleware\AssetMiddleware;
use Cake\Routing\Middleware\RoutingMiddleware;
use Cake\Routing\Router;
use Exception;
use League\Container\ReflectionContainer;
use Psr\Http\Message\ServerRequestInterface;
use Setup\Middleware\MaintenanceMiddleware;
use TinyAuth\Middleware\RequestAuthorizationMiddleware;
use TinyAuth\Policy\RequestPolicy;
use Tools\Error\Middleware\ErrorHandlerMiddleware;
/**
* Application setup class.
*
* This defines the bootstrapping logic and middleware layers you
* want to use in your application.
*/
class Application extends BaseApplication implements AuthenticationServiceProviderInterface, AuthorizationServiceProviderInterface {
/**
* @return void
*/
public function bootstrap(): void {
// Call parent to load bootstrap from files.
parent::bootstrap();
if (PHP_SAPI === 'cli') {
$this->bootstrapCli();
}
if (Configure::read('debug')) {
$this->addPlugin('DebugKit');
try {
$this->addPlugin('TestHelper');
} catch (Exception $exception) {
// This is OK live
}
}
// Load more plugins here
}
/**
* Setup the middleware your application will use.
*
* @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to setup.
* @return \Cake\Http\MiddlewareQueue The updated middleware.
*/
public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue {
$middlewareQueue
->add(MaintenanceMiddleware::class)
// Catch any exceptions in the lower layers,
// and make an error page/response
// Removed for now because of Whoops Error Handler
->add(new ErrorHandlerMiddleware())
// Handle plugin/theme assets like CakePHP normally does.
->add(AssetMiddleware::class)
// Handle cached files
->add(CacheMiddleware::class)
// CORS headers for API access from documentation sites
->add(CorsMiddleware::class)
->add(RedirectMiddleware::class)
// Apply routing
->add(new RoutingMiddleware($this))
// Return 404 for non-existent controllers (before auth checks)
->add(MissingControllerMiddleware::class)
// CakePHP 5.3: Rate limit demo (limits to 10 requests/minute per IP)
// Applied AFTER routing so route params are available for skipCheck
// Only applies to Sandbox.CakeExamples::rateLimiter action via skipCheck callback
->add(new DemoRateLimitMiddleware())
// Authentication middleware
->add(new AuthenticationMiddleware($this))
// Authorization middleware
->add(new AuthorizationMiddleware($this))
// TinyAuth Request Authorization middleware for INI-based RBAC
->add(new RequestAuthorizationMiddleware([
'unauthorizedHandler' => [
'className' => 'TinyAuth.ForbiddenCakeRedirect',
'url' => [
'prefix' => false,
'plugin' => false,
'controller' => 'Account',
'action' => 'login',
],
'unauthorizedMessage' => 'You are not authorized to access that location.',
],
]));
return $middlewareQueue;
}
/**
* Register application container services.
*
* @param \Cake\Core\ContainerInterface $container The Container to update.
* @return void
*/
public function services(ContainerInterface $container): void {
$container->delegate(
new ReflectionContainer(true),
);
}
/**
* @return void
*/
protected function bootstrapCli() {
try {
$this->addPlugin('IdeHelper');
$this->addPlugin('Bake');
$this->addPlugin('ModelGraph');
} catch (MissingPluginException $e) {
// Do not halt if the plugin is missing
}
}
/**
* @param \Psr\Http\Message\ServerRequestInterface $request
* @return \Authentication\AuthenticationServiceInterface
*/
public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface {
$service = new AuthenticationService();
$loginUrl = [
'prefix' => false,
'plugin' => false,
'controller' => 'Account',
'action' => 'login',
];
// Define where users should be redirected to when they are not authenticated
$service->setConfig([
'unauthenticatedRedirect' => Router::url($loginUrl),
'queryParam' => 'redirect',
]);
// Form field mapping (HTML form uses 'login' field)
$formFields = [
PasswordIdentifier::CREDENTIAL_USERNAME => 'login',
PasswordIdentifier::CREDENTIAL_PASSWORD => 'password',
];
// Password identifier configuration for multi-column authentication
// The username can match EITHER 'username' OR 'email' columns
$passwordIdentifier = [
'className' => 'Authentication.Password',
'fields' => [
PasswordIdentifier::CREDENTIAL_USERNAME => ['username', 'email'],
PasswordIdentifier::CREDENTIAL_PASSWORD => 'password',
],
'resolver' => [
'className' => 'Authentication.Orm',
'userModel' => 'Users',
],
];
// Load the authenticators. Session should be first.
$service->loadAuthenticator('Authentication.Session');
// Form authenticator for login
// Note: No loginUrl restriction to allow multiple login pages (Account and AuthSandbox)
$service->loadAuthenticator('Authentication.Form', [
'identifier' => $passwordIdentifier,
'fields' => $formFields,
]);
return $service;
}
/**
* @param \Psr\Http\Message\ServerRequestInterface $request
* @return \Authorization\AuthorizationServiceInterface
*/
public function getAuthorizationService(ServerRequestInterface $request): AuthorizationServiceInterface {
$mapResolver = new MapResolver();
$mapResolver->map(ServerRequest::class, new RequestPolicy());
return new AuthorizationService($mapResolver);
}
}