From 000d0604edbca2ebbc5cac66454df93481d0b8c5 Mon Sep 17 00:00:00 2001 From: Recca Tsai Date: Thu, 18 Feb 2016 05:47:22 +0800 Subject: [PATCH] start session to fix csrf token exception --- composer.json | 9 ++-- src/Http/Controllers/TerminalController.php | 22 ++++++-- src/ServiceProvider.php | 57 ++++++++++----------- tests/TerminalTest.php | 24 ++++++--- 4 files changed, 68 insertions(+), 44 deletions(-) diff --git a/composer.json b/composer.json index e3d3cbd..002030e 100644 --- a/composer.json +++ b/composer.json @@ -11,10 +11,11 @@ ], "require": { "php": ">=5.5.9", - "illuminate/console": "~5.0", - "illuminate/database": "~5.0", - "illuminate/filesystem": "~5.0", - "illuminate/routing": "~5.0", + "illuminate/console": "~5.1", + "illuminate/database": "~5.1", + "illuminate/filesystem": "~5.1", + "illuminate/routing": "~5.1", + "paragonie/random_compat": "~1.1", "symfony/finder": "2.7.*|2.8.*|3.0.*" }, "require-dev": { diff --git a/src/Http/Controllers/TerminalController.php b/src/Http/Controllers/TerminalController.php index 99d47e1..f5f43b2 100644 --- a/src/Http/Controllers/TerminalController.php +++ b/src/Http/Controllers/TerminalController.php @@ -22,15 +22,27 @@ public function __construct(ConsoleKernel $consoleKernel) * index. * * @param \Illuminate\Contracts\Foundation\Application $app - * @param \Illuminate\Filesystem\Filesystem $filesystem + * @param \Illuminate\Session\SessionManager $sessionManager + * @param \Illuminate\Http\Request $request + * @param bool $panel * * @return mixed */ - public function index(ApplicationContract $app, Filesystem $filesystem, SessionManager $sessionManager, $panel = false) - { + public function index( + ApplicationContract $app, + SessionManager $sessionManager, + Request $request, + $panel = false + ) { + $session = $sessionManager->driver(); + if ($session->isStarted() === false) { + $session->setId($request->cookies->get($session->getName())); + $session->setRequestOnHandler($request); + $session->start(); + } $this->consoleKernel->call('--ansi'); $options = json_encode([ - 'csrfToken' => $sessionManager->driver()->getToken(), + 'csrfToken' => $session->token(), 'username' => 'LARAVEL', 'hostname' => php_uname('n'), 'os' => PHP_OS, @@ -66,7 +78,7 @@ public function index(ApplicationContract $app, Filesystem $filesystem, SessionM /** * rpc response. * - * @param \Illuminate\Http\Request $request + * @param \Illuminate\Http\Request $request * * @return mixed */ diff --git a/src/ServiceProvider.php b/src/ServiceProvider.php index ebc70a8..031225a 100644 --- a/src/ServiceProvider.php +++ b/src/ServiceProvider.php @@ -3,10 +3,8 @@ namespace Recca0120\Terminal; use Illuminate\Contracts\Config\Repository as ConfigContract; -use Illuminate\Contracts\Http\Kernel as KernelContract; use Illuminate\Http\Request; use Illuminate\Routing\Router; -use Illuminate\Session\Middleware\StartSession; use Illuminate\Support\ServiceProvider as BaseServiceProvider; class ServiceProvider extends BaseServiceProvider @@ -18,32 +16,28 @@ class ServiceProvider extends BaseServiceProvider */ protected $namespace = 'Recca0120\Terminal\Http\Controllers'; - /** - * Bootstrap any application services. - * - * @param \Illuminate\Http\Request $Request - * @param \Illuminate\Routing\Router $router - * @param \Illuminate\Contracts\Config\Repository $config - * - * @return void - */ - public function boot(Request $request, Router $router, KernelContract $kernel, ConfigContract $config) - { - if ($this->app->runningInConsole() === true) { - $this->handlePublishes(); - } - - if ($config->get('app.debug') === true || - in_array( - $request->getClientIp(), - $config->get('terminal.whitelists', []) - ) === true - ) { - $kernel->pushMiddleware(StartSession::class); - $this->loadViewsFrom(__DIR__.'/../resources/views', 'terminal'); - $this->handleRoutes($router); - } - } + /** + * Bootstrap any application services. + * + * @param \Illuminate\Http\Request $Request + * @param \Illuminate\Routing\Router $router + * @param \Illuminate\Contracts\Config\Repository $config + * + * @return void + */ + public function boot(Request $request, Router $router, ConfigContract $config) + { + $this->handlePublishes(); + if ($config->get('app.debug') === true || + in_array( + $request->getClientIp(), + $config->get('terminal.whitelists', []) + ) === true + ) { + $this->loadViewsFrom(__DIR__.'/../resources/views', 'terminal'); + $this->handleRoutes($router); + } + } /** * Register any application services. @@ -70,7 +64,7 @@ public function handleRoutes(Router $router) 'as' => 'terminal::', 'namespace' => $this->namespace, 'prefix' => $prefix, - ], function () { + ], function (Router $router) { require __DIR__.'/Http/routes.php'; }); } @@ -95,4 +89,9 @@ protected function handlePublishes() __DIR__.'/../public' => public_path('vendor/terminal'), ], 'public'); } + + public function when() + { + return [static::class]; + } } diff --git a/tests/TerminalTest.php b/tests/TerminalTest.php index 9e089e8..fd213e8 100644 --- a/tests/TerminalTest.php +++ b/tests/TerminalTest.php @@ -1,12 +1,14 @@ makePartial(); - $request = m::mock('Illuminate\Http\Request') + $session = m::mock(SessionInterface::class) + ->shouldReceive('isStarted')->andReturn(true) + ->shouldReceive('token')->andReturn('token') + ->mock(); + + $sessionManager = m::mock(SessionManager::class) ->makePartial() - ->shouldReceive('get')->andReturn(['command' => 'list']) + ->shouldReceive('driver')->andReturn($session) ->mock(); - $filesystem = new Filesystem(); + $request = m::mock(Request::class) + ->makePartial(); - $controller->index($app, $filesystem); + $controller->index($app, $sessionManager, $request); + $request = m::mock(Request::class) + ->makePartial() + ->shouldReceive('get')->andReturn(['command' => 'list']) + ->mock(); $controller->endPoint($request); return [$kernel, $app];