Skip to content

Commit

Permalink
start session to fix csrf token exception
Browse files Browse the repository at this point in the history
  • Loading branch information
Recca Tsai committed Feb 17, 2016
1 parent 8a0ea32 commit 97021ed
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 44 deletions.
9 changes: 5 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
22 changes: 17 additions & 5 deletions src/Http/Controllers/TerminalController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
*/
Expand Down
57 changes: 28 additions & 29 deletions src/ServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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.
Expand All @@ -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';
});
}
Expand All @@ -95,4 +89,9 @@ protected function handlePublishes()
__DIR__.'/../public' => public_path('vendor/terminal'),
], 'public');
}

public function when()
{
return [static::class];
}
}
24 changes: 18 additions & 6 deletions tests/TerminalTest.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
<?php

use Illuminate\Filesystem\Filesystem;
use Illuminate\Http\Request;
use Illuminate\Session\SessionInterface;
use Illuminate\Session\SessionManager;
use Mockery as m;
use Recca0120\Terminal\Console\Commands\Artisan;
use Recca0120\Terminal\Console\Commands\ArtisanTinker;
use Recca0120\Terminal\Console\Commands\Find;
use Recca0120\Terminal\Console\Commands\Mysql;
use Recca0120\Terminal\Http\Controllers\TerminalController;

class TerminalTest extends PHPUnit_Framework_TestCase
{
Expand Down Expand Up @@ -39,18 +41,28 @@ public function test_controller($arguments)
$kernel = $arguments[0];
$app = $arguments[1];

$controller = m::mock(new Recca0120\Terminal\Http\Controllers\TerminalController($kernel))
$controller = m::mock(new TerminalController($kernel))
->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];
Expand Down

0 comments on commit 97021ed

Please sign in to comment.