File tree Expand file tree Collapse file tree 3 files changed +60
-0
lines changed Expand file tree Collapse file tree 3 files changed +60
-0
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+
3+ namespace Givebutter \LaravelKeyable \Exceptions ;
4+
5+ class ForbidenRequestParamException extends \Exception
6+ {
7+ //
8+ }
Original file line number Diff line number Diff line change 33namespace Givebutter \LaravelKeyable \Http \Middleware ;
44
55use Closure ;
6+ use Givebutter \LaravelKeyable \Exceptions \ForbidenRequestParamException ;
67use Givebutter \LaravelKeyable \Models \ApiKey ;
8+ use Illuminate \Http \Request ;
79
810class AuthenticateApiKey
911{
@@ -18,6 +20,8 @@ class AuthenticateApiKey
1820 */
1921 public function handle ($ request , Closure $ next , $ guard = null )
2022 {
23+ $ this ->checkForbidenRequestParams ($ request );
24+
2125 //Get API token from request
2226 $ token = $ this ->getKeyFromRequest ($ request );
2327
@@ -86,4 +90,17 @@ protected function unauthorizedResponse()
8690 ],
8791 ], 401 );
8892 }
93+
94+ private function checkForbidenRequestParams (Request $ request ): void
95+ {
96+ $ forbidenParams = ['apiKey ' , 'keyable ' ];
97+
98+ foreach ($ forbidenParams as $ forbidenParam ) {
99+ if ($ request ->missing ($ forbidenParam )) {
100+ continue ;
101+ }
102+
103+ throw new ForbidenRequestParamException ("Request param ' {$ forbidenParam }' is not allowed. " );
104+ }
105+ }
89106}
Original file line number Diff line number Diff line change 22
33namespace Givebutter \Tests \Feature ;
44
5+ use Givebutter \LaravelKeyable \Exceptions \ForbidenRequestParamException ;
56use Givebutter \Tests \TestCase ;
67use Givebutter \Tests \Support \Account ;
78use Illuminate \Support \Facades \Route ;
@@ -81,4 +82,38 @@ public function request_without_api_key_responds_unauthorized()
8182
8283 $ this ->get ("/api/posts " )->assertUnauthorized ();
8384 }
85+
86+ /**
87+ * @test
88+ * @dataProvider forbiddenRequestParams
89+ */
90+ public function throw_exception_if_unauthorized_get_request_has_forbidden_request_query_params (string $ queryParam ): void
91+ {
92+ Route::get ('/api/posts ' , function () {
93+ return response ('All good ' , 200 );
94+ })->middleware (['api ' , 'auth.apikey ' ]);
95+
96+ $ this ->get ("/api/posts? {$ queryParam }=value " )->assertInternalServerError ();
97+ }
98+
99+ /**
100+ * @test
101+ * @dataProvider forbiddenRequestParams
102+ */
103+ public function throw_exception_if_unauthorized_post_request_has_forbidden_request_body_params (string $ bodyParam ): void
104+ {
105+ Route::post ('/api/posts ' , function () {
106+ return response ('All good ' , 200 );
107+ })->middleware (['api ' , 'auth.apikey ' ]);
108+
109+ $ this ->post ('/api/posts ' , [$ bodyParam => 'value ' ])->assertInternalServerError ();
110+ }
111+
112+ public function forbiddenRequestParams (): array
113+ {
114+ return [
115+ ['keyable ' ],
116+ ['apiKey ' ],
117+ ];
118+ }
84119}
You can’t perform that action at this time.
0 commit comments