Skip to content

Commit

Permalink
Merge pull request #93 from facade/use-middleware-for-ignition-routes
Browse files Browse the repository at this point in the history
Use a middleware to protect ignition routes
  • Loading branch information
freekmurze authored Sep 5, 2019
2 parents a334f01 + 32b8a49 commit 6e5d134
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 14 deletions.
13 changes: 13 additions & 0 deletions config/ignition.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,17 @@
'remote_sites_path' => env('IGNITION_REMOTE_SITES_PATH', ''),
'local_sites_path' => env('IGNITION_LOCAL_SITES_PATH', ''),



/*
|--------------------------------------------------------------------------
| Housekeeping Endpoint Prefix
|--------------------------------------------------------------------------
|
| Ignition registers a couple of routes if it is enabled. Here you can define
| the route prefix it should use.
|
*/
'housekeeping_endpoint_prefix' => '_ignition',

];
2 changes: 1 addition & 1 deletion src/ErrorPage/ErrorPageViewModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ public function toArray(): array
'config' => $this->config(),
'solutions' => $this->solutions(),
'report' => $this->report(),
'housekeepingEndpoint' => config('flare.housekeeping_endpoint_prefix', 'flare'),
'housekeepingEndpoint' => config('ignition.housekeeping_endpoint_prefix', '_ignition'),
'styles' => $this->styles(),
'scripts' => $this->scripts(),
'tabs' => $this->tabs(),
Expand Down
31 changes: 31 additions & 0 deletions src/Http/Middleware/IgnitionEnabled.php
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');
}
}
25 changes: 12 additions & 13 deletions src/IgnitionServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Facade\Ignition;

use Facade\Ignition\Http\Middleware\IgnitionEnabled;
use Monolog\Logger;
use Illuminate\Support\Arr;
use Facade\FlareClient\Flare;
Expand Down Expand Up @@ -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);
});

return $this;
}
Expand Down
34 changes: 34 additions & 0 deletions tests/Http/Middleware/IgnitionEnabledTest.php
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')->assertStatus(404);
}

/** @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')->assertStatus(200);
}
}

0 comments on commit 6e5d134

Please sign in to comment.