diff --git a/framework/core/composer.json b/framework/core/composer.json index e30e07fa1..268378d99 100644 --- a/framework/core/composer.json +++ b/framework/core/composer.json @@ -16,7 +16,8 @@ "oyejorge/less.php": "dev-master", "intervention/image": "dev-master", "psr/http-message": "^1.0@dev", - "nikic/fast-route": "dev-master" + "nikic/fast-route": "dev-master", + "dflydev/fig-cookies": "0.0.*@dev" }, "require-dev": { "fzaninotto/faker": "1.4.0", diff --git a/framework/core/composer.lock b/framework/core/composer.lock index d0c6df6de..8942ca47e 100644 --- a/framework/core/composer.lock +++ b/framework/core/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#composer-lock-the-lock-file", "This file is @generated automatically" ], - "hash": "8b517a2c5b1d000a443c5ad91c49b2c1", + "hash": "b4919da43ec6d4286a38b1b95b32ae31", "packages": [ { "name": "danielstjules/stringy", @@ -62,6 +62,58 @@ ], "time": "2015-02-10 06:19:18" }, + { + "name": "dflydev/fig-cookies", + "version": "dev-master", + "source": { + "type": "git", + "url": "https://github.com/dflydev/dflydev-fig-cookies.git", + "reference": "adc6bab48d09585f67338e0d00695bf64a9ee2b8" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/dflydev/dflydev-fig-cookies/zipball/adc6bab48d09585f67338e0d00695bf64a9ee2b8", + "reference": "adc6bab48d09585f67338e0d00695bf64a9ee2b8", + "shasum": "" + }, + "require": { + "php": ">=5.4", + "psr/http-message": "~1.0" + }, + "require-dev": { + "codeclimate/php-test-reporter": "~0.1@dev", + "phpunit/phpunit": "~4.5", + "squizlabs/php_codesniffer": "~2.3" + }, + "type": "library", + "extra": { + "branch-alias": { + "dev-master": "0.0.x-dev" + } + }, + "autoload": { + "psr-4": { + "Dflydev\\FigCookies\\": "src/Dflydev/FigCookies" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "MIT" + ], + "authors": [ + { + "name": "Beau Simensen", + "email": "beau@dflydev.com" + } + ], + "description": "Cookies for PSR-7 HTTP Message Interface.", + "keywords": [ + "cookies", + "psr-7", + "psr7" + ], + "time": "2015-06-02 19:02:56" + }, { "name": "doctrine/inflector", "version": "dev-master", @@ -139,7 +191,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/container/zipball/55b81cfeb20745e74957d7ade2773e2bc2510bef", + "url": "https://api.github.com/repos/illuminate/container/zipball/c5a78e53ef15204469b5b072d390af9785a82d32", "reference": "55b81cfeb20745e74957d7ade2773e2bc2510bef", "shasum": "" }, @@ -224,7 +276,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/illuminate/database/zipball/79b6de7d8b76265d5e765ecd828d9e57c7844959", + "url": "https://api.github.com/repos/illuminate/database/zipball/e8933b4765aaa1b65f3f4a3638b6a0ea61b76a5a", "reference": "79ebeb4c169178a24c5eb7f17db94df01c7dd04d", "shasum": "" }, @@ -532,7 +584,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/oyejorge/less.php/zipball/b7f01fb8e86f8d77e0f5367715ec756418232e19", + "url": "https://api.github.com/repos/oyejorge/less.php/zipball/fc971e6d3eb54dff3d3eba4734ff207d37cb4e0e", "reference": "b7f01fb8e86f8d77e0f5367715ec756418232e19", "shasum": "" }, @@ -701,7 +753,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/tobscure/json-api/zipball/31a74d27fd9ab6a9a9bc911614ceb696504d2c39", + "url": "https://api.github.com/repos/tobscure/json-api/zipball/d6c82a496289569e8907f3aa980ace407a35b45e", "reference": "ec101f2b95cb3ef40489b778b01beb76c3a5c13f", "shasum": "" }, @@ -737,7 +789,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/tobscure/permissible/zipball/ac146ee44be5b2c4b99ad065e2cdcd51de1f7860", + "url": "https://api.github.com/repos/tobscure/permissible/zipball/0ba23dd1ed6f5372bf86fa917450cb70d08c012b", "reference": "ac146ee44be5b2c4b99ad065e2cdcd51de1f7860", "shasum": "" }, @@ -776,7 +828,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/Codeception/Codeception/zipball/cdc3569fedb017eea204b40eae0c82cf726e0107", + "url": "https://api.github.com/repos/Codeception/Codeception/zipball/b33bbcbf4344dbdbba86328c42fd3ef409bb286b", "reference": "d3cf78c6053f3fdfa4025bfcdb713f91e3ccdbdf", "shasum": "" }, @@ -1465,7 +1517,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/9ef4b8cbf3e839a44a9b375d8c59e109ac7aa020", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/3ee57a4fa5c2228da154f88239a921c8d54fcedd", "reference": "9ef4b8cbf3e839a44a9b375d8c59e109ac7aa020", "shasum": "" }, @@ -1711,7 +1763,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/3afe303d873a4d64c62ef84de491b97b006fbdac", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/ce9857103d2ec224382eb30847941d8322dc5570", "reference": "3afe303d873a4d64c62ef84de491b97b006fbdac", "shasum": "" }, @@ -1783,7 +1835,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/74ffb87f527f24616f72460e54b595f508dccb5c", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit-mock-objects/zipball/253c005852591fd547fc18cd5b7b43a1ec82d8f7", "reference": "74ffb87f527f24616f72460e54b595f508dccb5c", "shasum": "" }, @@ -2701,7 +2753,7 @@ }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/zendframework/zend-escaper/zipball/84e0c15195adfb3fe3efebb459defc65a1e0314d", + "url": "https://api.github.com/repos/zendframework/zend-escaper/zipball/dea2d94022ce072831d3424fbd0fd5514e81eb75", "reference": "84e0c15195adfb3fe3efebb459defc65a1e0314d", "shasum": "" }, @@ -2745,7 +2797,8 @@ "oyejorge/less.php": 20, "intervention/image": 20, "psr/http-message": 20, - "nikic/fast-route": 20 + "nikic/fast-route": 20, + "dflydev/fig-cookies": 20 }, "prefer-stable": false, "prefer-lowest": false, diff --git a/framework/core/src/Forum/Actions/ConfirmAction.php b/framework/core/src/Forum/Actions/ConfirmAction.php index 61eecf602..74413267b 100644 --- a/framework/core/src/Forum/Actions/ConfirmAction.php +++ b/framework/core/src/Forum/Actions/ConfirmAction.php @@ -1,13 +1,13 @@ id); - $token = $this->dispatch($command); + $token = $this->dispatch(new GenerateAccessTokenCommand($user->id)); - return redirect('/') - ->withCookie($this->makeRememberCookie($token->id)) - ->with('alert', ['type' => 'success', 'message' => 'Thanks for confirming!']); + return $this->withRememberCookie( + $this->redirectTo(''), + $token->id + ); + // TODO: ->with('alert', ['type' => 'success', 'message' => 'Thanks for confirming!']); } } diff --git a/framework/core/src/Forum/Actions/IndexAction.php b/framework/core/src/Forum/Actions/IndexAction.php index 30ba095c6..ade1e20ca 100644 --- a/framework/core/src/Forum/Actions/IndexAction.php +++ b/framework/core/src/Forum/Actions/IndexAction.php @@ -1,18 +1,17 @@ whereIn('key', ['base_url', 'api_url', 'forum_title', 'welcome_title', 'welcome_message'])->lists('value', 'key'); $data = []; @@ -22,7 +21,7 @@ class IndexAction extends BaseAction if (($user = $this->actor->getUser()) && $user->exists) { $session = [ 'userId' => $user->id, - 'token' => Cookie::get('flarum_remember') + 'token' => $request->getCookieParams()['flarum_remember'], ]; $response = app('Flarum\Api\Actions\Users\ShowAction') @@ -35,7 +34,7 @@ class IndexAction extends BaseAction } } - $view = View::make('flarum.forum::index') + $view = view('flarum.forum::index') ->with('title', Config::get('flarum::forum_title', 'Flarum Demo Forum')) ->with('config', $config) ->with('layout', 'flarum.forum::forum') diff --git a/framework/core/src/Forum/Actions/LoginAction.php b/framework/core/src/Forum/Actions/LoginAction.php index b410538af..917325edc 100644 --- a/framework/core/src/Forum/Actions/LoginAction.php +++ b/framework/core/src/Forum/Actions/LoginAction.php @@ -1,13 +1,13 @@ handle(new ApiRequest($request->only('identification', 'password'))); + $params = array_only($request->getAttributes(), ['identification', 'password']); - if ($response->getStatusCode() === 200 && ($data = $response->getData()) && ! empty($data->token)) { - $response->withCookie($this->makeRememberCookie($data->token)); + /** @var \Psr\Http\Message\ResponseInterface $response */ + $response = app('Flarum\Api\Actions\TokenAction')->handle(new ApiRequest($params)); + + if ($response->getStatusCode() === 200) { + $data = json_decode($response->getBody()); event(new UserLoggedIn($this->users->findOrFail($data->userId), $data->token)); + return $this->withRememberCookie($response, $data->token); } return $response; diff --git a/framework/core/src/Forum/Actions/LogoutAction.php b/framework/core/src/Forum/Actions/LogoutAction.php index 6583e579f..947550496 100644 --- a/framework/core/src/Forum/Actions/LogoutAction.php +++ b/framework/core/src/Forum/Actions/LogoutAction.php @@ -1,11 +1,12 @@ actor->getUser(); @@ -16,11 +17,6 @@ class LogoutAction extends BaseAction event(new UserLoggedOut($user)); } - return redirect('')->withCookie($this->makeForgetCookie()); - } - - public function makeForgetCookie() - { - return Cookie::forget('flarum_remember'); + return $this->withForgetCookie($this->redirectTo('')); } } diff --git a/framework/core/src/Forum/Actions/MakesRememberCookie.php b/framework/core/src/Forum/Actions/MakesRememberCookie.php deleted file mode 100644 index b3a4c7f52..000000000 --- a/framework/core/src/Forum/Actions/MakesRememberCookie.php +++ /dev/null @@ -1,11 +0,0 @@ -get('token')); + $token = ResetToken::findOrFail($request->getAttribute('token')); - $password = $request->get('password'); - $confirmation = $request->get('password_confirmation'); + $password = $request->getAttribute('password'); + $confirmation = $request->getAttribute('password_confirmation'); if (! $password || $password !== $confirmation) { - return redirect()->back(); + return $this->redirectTo(''); // TODO: Redirect back } $this->dispatch( @@ -23,6 +23,6 @@ class SavePasswordAction extends BaseAction $token->delete(); - return redirect(''); + return $this->redirectTo(''); } } diff --git a/framework/core/src/Forum/Actions/WritesRememberCookie.php b/framework/core/src/Forum/Actions/WritesRememberCookie.php new file mode 100644 index 000000000..b1b194e0b --- /dev/null +++ b/framework/core/src/Forum/Actions/WritesRememberCookie.php @@ -0,0 +1,26 @@ +withMaxAge(14 * 24 * 60 * 60) + ); + } + + protected function withForgetCookie(ResponseInterface $response) + { + // Delete the cookie by setting it to an expiration date in the past + return FigCookies::setResponseSetCookie( + $response, + SetCookie::create('flarum_remember')->withMaxAge(-2628000) + ); + } +} diff --git a/framework/core/src/Forum/Middleware/LoginWithCookie.php b/framework/core/src/Forum/Middleware/LoginWithCookie.php index 34b512f3a..c0765a1fd 100644 --- a/framework/core/src/Forum/Middleware/LoginWithCookie.php +++ b/framework/core/src/Forum/Middleware/LoginWithCookie.php @@ -2,11 +2,15 @@ use Flarum\Support\Actor; use Flarum\Core\Models\AccessToken; -use Auth; -use Closure; +use Psr\Http\Message\ResponseInterface as Response; +use Psr\Http\Message\ServerRequestInterface as Request; +use Zend\Stratigility\MiddlewareInterface; -class LoginWithCookie +class LoginWithCookie implements MiddlewareInterface { + /** + * @var Actor + */ protected $actor; public function __construct(Actor $actor) @@ -14,16 +18,21 @@ class LoginWithCookie $this->actor = $actor; } - public function handle($request, Closure $next) + /** + * {@inheritdoc} + */ + public function __invoke(Request $request, Response $response, callable $out = null) { - if (($token = $request->cookie('flarum_remember')) && - ($accessToken = AccessToken::where('id', $token)->first()) && - ($user = $accessToken->user)) { - $this->actor->setUser($user); + $cookies = $request->getCookieParams(); + + if (($token = $cookies['flarum_remember']) && + ($accessToken = AccessToken::where('id', $token)->first()) + ) { + $this->actor->setUser($user = $accessToken->user); $user->updateLastSeen()->save(); } - return $next($request); + return $out ? $out($request, $response) : $response; } } diff --git a/framework/core/src/Forum/routes.php b/framework/core/src/Forum/routes.php index 19d4ed31f..5dbf479e5 100755 --- a/framework/core/src/Forum/routes.php +++ b/framework/core/src/Forum/routes.php @@ -1,44 +1,31 @@ app->make($class); - $request = $this->app['request']->instance(); - $parameters = $this->app['router']->current()->parameters(); - return $action->handle($request, $parameters); + + return $action->handle($httpRequest, $routeParams); }; }; -Route::group(['middleware' => 'Flarum\Forum\Middleware\LoginWithCookie'], function () use ($action) { +/** @var Flarum\Http\Router $router */ +$router = $this->app->make('Flarum\Http\Router'); - Route::get('/', [ - 'as' => 'flarum.forum.index', - 'uses' => $action('Flarum\Forum\Actions\IndexAction') - ]); +/** + * Route::group(['middleware' => 'Flarum\Forum\Middleware\LoginWithCookie'], function () use ($action) { + * For the two below + */ - Route::get('logout', [ - 'as' => 'flarum.forum.logout', - 'uses' => $action('Flarum\Forum\Actions\LogoutAction') - ]); +$router->get('/', 'flarum.forum.index', $action('Flarum\Forum\Actions\IndexAction')); -}); +$router->get('/logout', 'flarum.forum.logout', $action('Flarum\Forum\Actions\LogoutAction')); -Route::post('login', [ - 'as' => 'flarum.forum.login', - 'uses' => $action('Flarum\Forum\Actions\LoginAction') -]); +$router->post('/login', 'flarum.forum.login', $action('Flarum\Forum\Actions\LoginAction')); -Route::get('confirm/{id}/{token}', [ - 'as' => 'flarum.forum.confirm', - 'uses' => $action('Flarum\Forum\Actions\ConfirmAction') -]); +$router->get('/confirm/{id}/{token}', 'flarum.forum.confirm', $action('Flarum\Forum\Actions\ConfirmAction')); -Route::get('reset/{token}', [ - 'as' => 'flarum.forum.resetPassword', - 'uses' => $action('Flarum\Forum\Actions\ResetPasswordAction') -]); +$router->get('/reset/{token}', 'flarum.forum.resetPassword', $action('Flarum\Forum\Actions\ResetPasswordAction')); -Route::post('reset', [ - 'as' => 'flarum.forum.savePassword', - 'uses' => $action('Flarum\Forum\Actions\SavePasswordAction') -]); +$router->post('/reset', 'flarum.forum.savePassword', $action('Flarum\Forum\Actions\SavePasswordAction'));