Skip to content

Commit

Permalink
Do not set session and cookies for API requests and correctly invalid…
Browse files Browse the repository at this point in the history
…ate session and cookie on login/logout
  • Loading branch information
leepeuker committed Feb 27, 2024
1 parent 8485c83 commit fde8cd6
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 6 deletions.
2 changes: 0 additions & 2 deletions public/index.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
<?php declare(strict_types=1);

session_start();

/** @var DI\Container $container */

use Movary\HttpController\Web\ErrorController;
Expand Down
2 changes: 1 addition & 1 deletion settings/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ function addWebRoutes(RouterService $routerService, FastRoute\RouteCollector $ro
$routes->add('POST', '/add-movie-to-watchlist', [Web\WatchlistController::class, 'addMovieToWatchlist'], [Web\Middleware\UserIsAuthenticated::class]);
$routes->add('GET', '/fetchMovieRatingByTmdbdId', [Web\Movie\MovieRatingController::class, 'fetchMovieRatingByTmdbdId'], [Web\Middleware\UserIsAuthenticated::class]);

$routerService->addRoutesToRouteCollector($routeCollector, $routes);
$routerService->addRoutesToRouteCollector($routeCollector, $routes, true);
}

function addApiRoutes(RouterService $routerService, FastRoute\RouteCollector $routeCollector) : void
Expand Down
3 changes: 2 additions & 1 deletion src/Domain/User/Service/Authentication.php
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,8 @@ public function logout() : void

public function setAuthenticationCookieAndNewSession(int $userId, string $token, DateTime $expirationDate) : void
{
session_regenerate_id();
$this->sessionWrapper->destroy();
$this->sessionWrapper->start();
setcookie(self::AUTHENTICATION_COOKIE_NAME, $token, [
'expires' => (int)$expirationDate->format('U'),
'path' => '/',
Expand Down
21 changes: 21 additions & 0 deletions src/HttpController/Web/Middleware/StartSession.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php declare(strict_types=1);

namespace Movary\HttpController\Web\Middleware;

use Movary\Util\SessionWrapper;
use Movary\ValueObject\Http\Response;

class StartSession implements MiddlewareInterface
{
public function __construct(
private readonly SessionWrapper $sessionWrapper,
) {
}

public function __invoke() : ?Response
{
$this->sessionWrapper->start();

return null;
}
}
10 changes: 8 additions & 2 deletions src/Service/Router/RouterService.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,25 @@
namespace Movary\Service\Router;

use FastRoute\RouteCollector;
use Movary\HttpController\Web;
use Movary\Service\Router\Dto\RouteList;

class RouterService
{
public function addRoutesToRouteCollector(RouteCollector $routeCollector, RouteList $routeList) : void
public function addRoutesToRouteCollector(RouteCollector $routeCollector, RouteList $routeList, bool $isWebRoute = false) : void
{
foreach ($routeList as $route) {
$middleware = $route->getMiddleware();
if ($isWebRoute === true) {
$middleware[] = Web\Middleware\StartSession::class;
}

$routeCollector->addRoute(
$route->getMethod(),
$route->getRoute(),
[
'handler' => $route->getHandler(),
'middleware' => $route->getMiddleware()
'middleware' => $middleware
],
);
}
Expand Down
12 changes: 12 additions & 0 deletions src/Util/SessionWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,19 @@ class SessionWrapper
{
public function destroy() : void
{
$_SESSION = array();

if (ini_get('session.use_cookies')) {
$params = session_get_cookie_params();
setcookie(
session_name(), '', time() - 42000,
$params['path'], $params['domain'],
$params['secure'], $params['httponly'],
);
}

session_destroy();
session_regenerate_id();
}

public function find(string $key) : mixed
Expand Down

0 comments on commit fde8cd6

Please sign in to comment.