質問編集履歴

1

cookieにcsrftokenあるか確認。追加コード

2020/11/27 13:43

投稿

YuhiUsui
YuhiUsui

スコア11

test CHANGED
File without changes
test CHANGED
@@ -46,7 +46,37 @@
46
46
 
47
47
  ```ここに言語を入力
48
48
 
49
-
49
+ <?php
50
+
51
+ declare(strict_types=1);
52
+
53
+
54
+
55
+ /**
56
+
57
+ * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
58
+
59
+ * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
60
+
61
+ *
62
+
63
+ * Licensed under The MIT License
64
+
65
+ * For full copyright and license information, please see the LICENSE.txt
66
+
67
+ * Redistributions of files must retain the above copyright notice.
68
+
69
+ *
70
+
71
+ * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
72
+
73
+ * @link https://cakephp.org CakePHP(tm) Project
74
+
75
+ * @since 3.3.0
76
+
77
+ * @license https://opensource.org/licenses/mit-license.php MIT License
78
+
79
+ */
50
80
 
51
81
  namespace App;
52
82
 
@@ -70,7 +100,293 @@
70
100
 
71
101
  use Cake\Routing\Middleware\RoutingMiddleware;
72
102
 
73
-
103
+ // use Cake\Network\Exception\InvalidCsrfTokenException;
104
+
105
+
106
+
107
+ // use Authentication\AuthenticationService;
108
+
109
+ // use Authentication\AuthenticationServiceInterface;
110
+
111
+ // use Authentication\AuthenticationServiceProviderInterface;
112
+
113
+ // use Authentication\Middleware\AuthenticationMiddleware;
114
+
115
+ // use Psr\Http\Message\ServerRequestInterface;
116
+
117
+
118
+
119
+ /**
120
+
121
+ * Application setup class.
122
+
123
+ *
124
+
125
+ * This defines the bootstrapping logic and middleware layers you
126
+
127
+ * want to use in your application.
128
+
129
+ */
130
+
131
+ class Application extends BaseApplication
132
+
133
+ // implements AuthenticationServiceProviderInterface
134
+
135
+ {
136
+
137
+ /**
138
+
139
+ * Load all the application configuration and bootstrap logic.
140
+
141
+ *
142
+
143
+ * @return void
144
+
145
+ */
146
+
147
+ public function bootstrap(): void
148
+
149
+ {
150
+
151
+ $this->addPlugin('Migrations');
152
+
153
+
154
+
155
+ // Call parent to load bootstrap from files.
156
+
157
+ parent::bootstrap();
158
+
159
+
160
+
161
+ if (PHP_SAPI === 'cli') {
162
+
163
+ $this->bootstrapCli();
164
+
165
+ }
166
+
167
+
168
+
169
+ /*
170
+
171
+ * Only try to load DebugKit in development mode
172
+
173
+ * Debug Kit should not be installed on a production system
174
+
175
+ */
176
+
177
+ if (Configure::read('debug')) {
178
+
179
+ $this->addPlugin('DebugKit');
180
+
181
+ }
182
+
183
+
184
+
185
+ // Load more plugins here
186
+
187
+ }
188
+
189
+
190
+
191
+ /**
192
+
193
+ * Setup the middleware queue your application will use.
194
+
195
+ *
196
+
197
+ * @param \Cake\Http\MiddlewareQueue $middlewareQueue The middleware queue to setup.
198
+
199
+ * @return \Cake\Http\MiddlewareQueue The updated middleware queue.
200
+
201
+ */
202
+
203
+ public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
204
+
205
+ {
206
+
207
+ $middlewareQueue
208
+
209
+ // Catch any exceptions in the lower layers,
210
+
211
+ // and make an error page/response
212
+
213
+ ->add(new ErrorHandlerMiddleware(Configure::read('Error')))
214
+
215
+
216
+
217
+ // Handle plugin/theme assets like CakePHP normally does.
218
+
219
+ ->add(new AssetMiddleware([
220
+
221
+ 'cacheTime' => Configure::read('Asset.cacheTime'),
222
+
223
+ ]))
224
+
225
+
226
+
227
+ // Add routing middleware.
228
+
229
+ // If you have a large number of routes connected, turning on routes
230
+
231
+ // caching in production could improve performance. For that when
232
+
233
+ // creating the middleware instance specify the cache config name by
234
+
235
+ // using it's second constructor argument:
236
+
237
+ // `new RoutingMiddleware($this, '_cake_routes_')`
238
+
239
+ ->add(new RoutingMiddleware($this))
240
+
241
+
242
+
243
+ // Parse various types of encoded request bodies so that they are
244
+
245
+ // available as array through $request->getData()
246
+
247
+ // https://book.cakephp.org/4/en/controllers/middleware.html#body-parser-middleware
248
+
249
+ ->add(new BodyParserMiddleware())
250
+
251
+
252
+
253
+ // Cross Site Request Forgery (CSRF) Protection Middleware
254
+
255
+ // https://book.cakephp.org/4/en/controllers/middleware.html#cross-site-request-forgery-csrf-middleware
256
+
257
+ ->add(new CsrfProtectionMiddleware([
258
+
259
+ 'httponly' => true,
260
+
261
+ ]));
262
+
263
+
264
+
265
+ return $middlewareQueue;
266
+
267
+ }
268
+
269
+
270
+
271
+ /**
272
+
273
+ * Bootstrapping for CLI application.
274
+
275
+ *
276
+
277
+ * That is when running commands.
278
+
279
+ *
280
+
281
+ * @return void
282
+
283
+ */
284
+
285
+ protected function bootstrapCli(): void
286
+
287
+ {
288
+
289
+ try {
290
+
291
+ $this->addPlugin('Bake');
292
+
293
+ } catch (MissingPluginException $e) {
294
+
295
+ // Do not halt if the plugin is missing
296
+
297
+ }
298
+
299
+
300
+
301
+ $this->addPlugin('Migrations');
302
+
303
+
304
+
305
+ // Load more plugins here
306
+
307
+ }
308
+
309
+
310
+
311
+ // public function middleware(MiddlewareQueue $middlewareQueue): MiddlewareQueue
312
+
313
+ // {
314
+
315
+ // $middlewareQueue
316
+
317
+ // // ... 前に追加された他のミドルウェア
318
+
319
+ // ->add(new RoutingMiddleware($this))
320
+
321
+ // // RoutingMiddleware の後に認証を追加
322
+
323
+ // ->add(new AuthenticationMiddleware($this));
324
+
325
+
326
+
327
+ // return $middlewareQueue;
328
+
329
+ // }
330
+
331
+
332
+
333
+ // public function getAuthenticationService(ServerRequestInterface $request): AuthenticationServiceInterface
334
+
335
+ // {
336
+
337
+ // $authenticationService = new AuthenticationService([
338
+
339
+ // 'unauthenticatedRedirect' => '/users/login',
340
+
341
+ // 'queryParam' => 'redirect',
342
+
343
+ // ]);
344
+
345
+
346
+
347
+ // // identifiers を読み込み、email と password のフィールドを確認します
348
+
349
+ // $authenticationService->loadIdentifier('Authentication.Password', [
350
+
351
+ // 'fields' => [
352
+
353
+ // 'username' => 'email',
354
+
355
+ // 'password' => 'password',
356
+
357
+ // ]
358
+
359
+ // ]);
360
+
361
+
362
+
363
+ // // authenticatorsをロードしたら, 最初にセッションが必要です
364
+
365
+ // $authenticationService->loadAuthenticator('Authentication.Session');
366
+
367
+ // // 入力した email と password をチェックする為のフォームデータを設定します
368
+
369
+ // $authenticationService->loadAuthenticator('Authentication.Form', [
370
+
371
+ // 'fields' => [
372
+
373
+ // 'username' => 'email',
374
+
375
+ // 'password' => 'password',
376
+
377
+ // ],
378
+
379
+ // 'loginUrl' => '/users/login',
380
+
381
+ // ]);
382
+
383
+
384
+
385
+ // return $authenticationService;
386
+
387
+ // }
388
+
389
+ }
74
390
 
75
391
  ```
76
392
 
@@ -284,6 +600,8 @@
284
600
 
285
601
  ![イメージ説明](4b785e8d09270aefc7efaf7bb9d72a79.png)
286
602
 
603
+ ![![CSRF cookie](ac8d817446730583082c50fdb99971e6.png)](b36e4c632f1d76be4dc36127af49f19b.png)
604
+
287
605
 
288
606
 
289
607
  ### 補足情報(FW/ツールのバージョンなど)