What I did was create middleware to reject all requests with a βtokenβ as a key parameter in the request.
First we need to create middleware:
php artisan make:middleware BeforeMiddleware and, as you can see, this is middleware, which means that it will be launched before the request gets into the application:
<?php namespace App\Http\Middleware; use Closure; use App\Exceptions\BadRequest\RejectTokenAsQuerystring; class BeforeMiddleware { public function handle($request, Closure $next) { if ($request->token) { throw new RejectTokenAsQuerystring('reject_token_as_querystring'); } return $next($request); } }
I also had to add the middleware that I created for my kernel:
<?php namespace App\Http; use Illuminate\Foundation\Http\Kernel as HttpKernel; class Kernel extends HttpKernel { protected $middleware = [ \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, \Barryvdh\Cors\HandleCors::class, ]; protected $middlewareGroups = [ 'api' => [ 'throttle:60,1', 'bindings', ], ]; protected $routeMiddleware = [ 'reject-token-in-url' => \App\Http\Middleware\BeforeMiddleware::class, 'auth' => \Illuminate\Auth\Middleware\Authenticate::class, 'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class, 'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class, 'can' => \Illuminate\Auth\Middleware\Authorize::class, 'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class, 'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class, 'jwt.auth' => \Tymon\JWTAuth\Middleware\GetUserFromToken::class, 'jwt.refresh' => \Tymon\JWTAuth\Middleware\RefreshToken::class, ]; }
And finally, middleware that is defined globally can be used in defining my routes as:
<?php Route::group( [ 'domain' => getenv('API_DOMAIN'), 'middleware' => ['cors', 'reject-token-in-url'], 'prefix' => '/v1', 'namespace' => 'V1' ], function () { } );
I also implemented my own error definition, so I have a list of all the possible errors that I want to cause in my application, and they are defined as follows in my config/errors.php :
<?php return [ "reject_token_as_querystring" => [ "title" => "Reject token as querystring.", "detail" => "Token MUST be passed in the Header of the request." ] ];
Then you need to define your own exception class:
<?php namespace App\Exceptions; use Exception; abstract class CustomException extends Exception { protected $errorId; protected $status; protected $title; protected $detail; public function __construct($message) { parent::__construct($message); } public function getStatus() { return (int) $this->status; } public function toArray() { return [ 'id' => $this->id, 'status' => $this->status, 'title' => $this->title, 'detail' => $this->detail ]; } protected function build(array $args) { $this->id = array_shift($args); $error = config(sprintf('errors.%s', $this->id)); $this->title = $error['title']; $this->detail = vsprintf($error['detail'], $args); return $this->detail; } }
And you will use this class to extend custom errors:
<?php namespace App\Exceptions\BadRequest; use App\Exceptions\CustomException; class BadRequestException extends CustomException { protected $status = '400'; public function __construct() { $message = $this->build(func_get_args()); parent::__construct($message); } }
To create a class that contains an error:
<?php namespace App\Exceptions\BadRequest; use App\Exceptions\BadRequest\BadRequestException; class RejectTokenAsQuerystring extends BadRequestException { }
Finally, if you try to request information with a token key in the URL, you will get:
{ "id": "reject_token_as_querystring", "status": "400", "title": "Reject token as querystring.", "detail": "Token MUST be passed in the Header of the request." }
