-
Notifications
You must be signed in to change notification settings - Fork 184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use a middleware to protect ignition routes #93
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
namespace Facade\Ignition\Http\Middleware; | ||
|
||
use Closure; | ||
use Illuminate\Http\Request; | ||
|
||
class IgnitionEnabled | ||
{ | ||
/** | ||
* Handle an incoming request. | ||
* | ||
* @param Request $request | ||
* @param Closure $next | ||
* @return mixed | ||
*/ | ||
public function handle($request, Closure $next) | ||
{ | ||
if (! $this->ignitionEnabled()) { | ||
abort(404); | ||
} | ||
|
||
return $next($request); | ||
|
||
} | ||
|
||
protected function ignitionEnabled(): bool | ||
{ | ||
return config('app.debug'); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
|
||
namespace Facade\Ignition; | ||
|
||
use Facade\Ignition\Http\Middleware\IgnitionEnabled; | ||
use Monolog\Logger; | ||
use Illuminate\Support\Arr; | ||
use Facade\FlareClient\Flare; | ||
|
@@ -117,19 +118,17 @@ protected function registerViewEngines() | |
|
||
protected function registerHousekeepingRoutes() | ||
{ | ||
if (! config('app.debug')) { | ||
return $this; | ||
} | ||
|
||
Route::prefix(config('flare.housekeeping_endpoint_prefix', 'flare')) | ||
->group(function () { | ||
Route::get('health-check', HealthCheckController::class); | ||
Route::post('execute-solution', ExecuteSolutionController::class); | ||
Route::post('share-report', ShareReportController::class); | ||
|
||
Route::get('scripts/{script}', ScriptController::class); | ||
Route::get('styles/{style}', StyleController::class); | ||
}); | ||
Route::group([ | ||
'prefix' => config('ignition.housekeeping_endpoint_prefix', '_ignition'), | ||
'middleware' => [IgnitionEnabled::class], | ||
], function () { | ||
Route::get('health-check', HealthCheckController::class); | ||
Route::post('execute-solution', ExecuteSolutionController::class); | ||
Route::post('share-report', ShareReportController::class); | ||
|
||
Route::get('scripts/{script}', ScriptController::class); | ||
Route::get('styles/{style}', StyleController::class); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't routes be registered in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, in our case this would introduce some issues if an exception would occur in a service providers boot method prior to Ignition. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What about There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Marcel and I discussed this. We don't think it is necessary. The router will always have been registered when our provider gets called. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The point is not so much your provider (although you shouldn't rely on the provider ordering). You also should be a good citizen and allow other packages to swap out the router / extend it in some way, before you access it. That's at least 50% of the point of the IoC container and contracts, as I understand them. 😉 |
||
|
||
return $this; | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?php | ||
|
||
namespace Facade\Ignition\Tests\Http\Middleware; | ||
|
||
use Facade\Ignition\Tests\TestCase; | ||
use Illuminate\Support\Facades\Route; | ||
use Facade\Ignition\Http\Middleware\IgnitionEnabled; | ||
|
||
class IgnitionEnabledTest extends TestCase | ||
{ | ||
/** @test */ | ||
public function it_returns_404_with_debug_mode_disabled() | ||
{ | ||
$this->app['config']['app.debug'] = false; | ||
|
||
Route::get('middleware-test', function () { | ||
return 'success'; | ||
})->middleware([IgnitionEnabled::class]); | ||
|
||
$this->get('middleware-test')->assertNotFound(); | ||
} | ||
|
||
/** @test */ | ||
public function it_returns_ok_with_debug_mode_enabled() | ||
{ | ||
$this->app['config']['app.debug'] = true; | ||
|
||
Route::get('middleware-test', function () { | ||
return 'success'; | ||
})->middleware([IgnitionEnabled::class]); | ||
|
||
$this->get('middleware-test')->assertOk(); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe add a test for this middleware, so we're 100% sure it works correctly.