@@ -2773,6 +2773,11 @@ public function setNext(Handler $handler) /*: void*/
2773
2773
$ this ->next = $ handler ;
2774
2774
}
2775
2775
2776
+ protected function getArrayProperty (String $ key , String $ default ): array
2777
+ {
2778
+ return array_filter (array_map ('trim ' , explode (', ' , $ this ->getProperty ($ key , $ default ))));
2779
+ }
2780
+
2776
2781
protected function getProperty (String $ key , $ default )
2777
2782
{
2778
2783
return isset ($ this ->properties [$ key ]) ? $ this ->properties [$ key ] : $ default ;
@@ -2899,6 +2904,25 @@ public function handle(Request $request): Response
2899
2904
2900
2905
}
2901
2906
2907
+ // file: src/Tqdev/PhpCrudApi/Middleware/AjaxOnlyMiddleware.php
2908
+
2909
+ class AjaxOnlyMiddleware extends Middleware
2910
+ {
2911
+ public function handle (Request $ request ): Response
2912
+ {
2913
+ $ method = $ request ->getMethod ();
2914
+ $ excludeMethods = $ this ->getArrayProperty ('excludeMethods ' , 'OPTIONS,GET ' );
2915
+ if (!in_array ($ method , $ excludeMethods )) {
2916
+ $ headerName = $ this ->getProperty ('headerName ' , 'X-Requested-With ' );
2917
+ $ headerValue = $ this ->getProperty ('headerValue ' , 'XMLHttpRequest ' );
2918
+ if ($ headerValue != $ request ->getHeader ($ headerName )) {
2919
+ return $ this ->responder ->error (ErrorCode::ONLY_AJAX_REQUESTS_ALLOWED , '' );
2920
+ }
2921
+ }
2922
+ return $ this ->next ->handle ($ request );
2923
+ }
2924
+ }
2925
+
2902
2926
// file: src/Tqdev/PhpCrudApi/Middleware/AuthorizationMiddleware.php
2903
2927
2904
2928
class AuthorizationMiddleware extends Middleware
@@ -3276,11 +3300,6 @@ private function getVerifiedClaims(String $token, int $time, int $leeway, int $t
3276
3300
return $ claims ;
3277
3301
}
3278
3302
3279
- private function getArrayProperty (String $ property , String $ default ): array
3280
- {
3281
- return array_filter (array_map ('trim ' , explode (', ' , $ this ->getProperty ($ property , $ default ))));
3282
- }
3283
-
3284
3303
private function getClaims (String $ token ): array
3285
3304
{
3286
3305
$ time = (int ) $ this ->getProperty ('time ' , time ());
@@ -3563,7 +3582,8 @@ public function handle(Request $request): Response
3563
3582
{
3564
3583
$ token = $ this ->getToken ();
3565
3584
$ method = $ request ->getMethod ();
3566
- if (!in_array ($ method , ['OPTIONS ' , 'GET ' ])) {
3585
+ $ excludeMethods = $ this ->getArrayProperty ('excludeMethods ' , 'OPTIONS,GET ' );
3586
+ if (!in_array ($ method , $ excludeMethods )) {
3567
3587
$ headerName = $ this ->getProperty ('headerName ' , 'X-XSRF-TOKEN ' );
3568
3588
if ($ token != $ request ->getHeader ($ headerName )) {
3569
3589
return $ this ->responder ->error (ErrorCode::BAD_OR_MISSING_XSRF_TOKEN , '' );
@@ -4237,6 +4257,7 @@ class ErrorCode
4237
4257
const OPERATION_NOT_SUPPORTED = 1015 ;
4238
4258
const TEMPORARY_OR_PERMANENTLY_BLOCKED = 1016 ;
4239
4259
const BAD_OR_MISSING_XSRF_TOKEN = 1017 ;
4260
+ const ONLY_AJAX_REQUESTS_ALLOWED = 1018 ;
4240
4261
4241
4262
private $ values = [
4242
4263
9999 => ["%s " , Response::INTERNAL_SERVER_ERROR ],
@@ -4258,6 +4279,7 @@ class ErrorCode
4258
4279
1015 => ["Operation '%s' not supported " , Response::METHOD_NOT_ALLOWED ],
4259
4280
1016 => ["Temporary or permanently blocked " , Response::FORBIDDEN ],
4260
4281
1017 => ["Bad or missing XSRF token " , Response::FORBIDDEN ],
4282
+ 1018 => ["Only AJAX requests allowed " , Response::FORBIDDEN ],
4261
4283
];
4262
4284
4263
4285
public function __construct (int $ code )
0 commit comments