-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Listen to all incoming requests and log their session state
Prior to this change when sessions were lost we couldn't distinguish them in the log files. This change will start logging session state information when the controller is called and enriches the logs with a correlation id.
- Loading branch information
Showing
2 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2024 SURFnet B.V. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Surfnet\Tiqr\Attribute; | ||
|
||
use Attribute; | ||
|
||
#[Attribute(Attribute::TARGET_CLASS | Attribute::TARGET_METHOD)] | ||
class RequiresActiveSession | ||
{ | ||
public function __construct() | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
<?php | ||
|
||
/** | ||
* Copyright 2024 SURFnet B.V. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
declare(strict_types = 1); | ||
|
||
namespace Surfnet\Tiqr\EventSubscriber; | ||
|
||
use Psr\Log\LoggerInterface; | ||
use Surfnet\Tiqr\Attribute\RequiresActiveSession; | ||
use Surfnet\Tiqr\Service\SessionCorrelationIdService; | ||
use Surfnet\Tiqr\WithContextLogger; | ||
use Symfony\Component\EventDispatcher\EventSubscriberInterface; | ||
use Symfony\Component\HttpKernel\Event\ControllerArgumentsEvent; | ||
use Symfony\Component\HttpKernel\KernelEvents; | ||
use Symfony\Component\Security\Core\Exception\AccessDeniedException; | ||
use function is_array; | ||
|
||
/** | ||
* Listen to all incoming requests and log the session state information. | ||
* | ||
* The listener also looks for the #[RequiresActiveSession] attribute. | ||
* When a route is marked as to have a required active session this listener will deny access when there is none. | ||
*/ | ||
final readonly class ActiveSessionListener implements EventSubscriberInterface | ||
{ | ||
public function __construct( | ||
private LoggerInterface $logger, | ||
private SessionCorrelationIdService $sessionCorrelationIdService | ||
) { | ||
} | ||
|
||
public function onKernelControllerArguments(ControllerArgumentsEvent $event): void | ||
{ | ||
$logger = WithContextLogger::from($this->logger, [ | ||
'correlationId' => $this->sessionCorrelationIdService->generateCorrelationId() ?? '', | ||
'route' => $event->getRequest()->getRequestUri(), | ||
]); | ||
|
||
$sessionCookieId = $event->getRequest()->cookies->get('PHPSESSID'); | ||
if ($sessionCookieId !== null) { | ||
$logger->info('User made a request with a session cookie.'); | ||
} | ||
|
||
// If the session cookie matches the session id we can stop the execution. | ||
$sessionId = $event->getRequest()->getSession()->getId(); | ||
if ($sessionCookieId === $sessionId) { | ||
$logger->info('User session matches the session cookie.'); | ||
return; | ||
} | ||
|
||
$logger->error('The session cookie does not match the session id.'); | ||
|
||
// If the route does not require an active session we can stop the execution. | ||
if (!is_array($event->getAttributes()[RequiresActiveSession::class] ?? null)) { | ||
return; | ||
} | ||
|
||
// The route requires an active session, no authentication was found. | ||
// We'll log an error that differentiates between whether a session cookie was set, or not. | ||
if ($sessionCookieId !== null) { | ||
$logger->error('Route requires active session. Active session wasn\'t found.'); | ||
throw new AccessDeniedException(); | ||
} | ||
|
||
$logger->error('Route requires active session. Active session wasn\'t found. No session cookie was set.'); | ||
|
||
throw new AccessDeniedException(); | ||
} | ||
|
||
public static function getSubscribedEvents(): array | ||
{ | ||
return [KernelEvents::CONTROLLER_ARGUMENTS => ['onKernelControllerArguments', 20]]; | ||
} | ||
} |