/
RequestListener.php
159 lines (129 loc) · 4.42 KB
/
RequestListener.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
declare(strict_types=1);
namespace Sentry\SentryBundle\EventListener;
use Sentry\State\HubInterface;
use Sentry\State\Scope;
use Sentry\UserDataBag;
use Symfony\Component\HttpKernel\Event\ControllerEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
use Symfony\Component\Security\Core\User\UserInterface;
/**
* This listener ensures that a new {@see \Sentry\State\Scope} is created for
* each request and that it is filled with useful information, e.g. the IP
* address of the client.
*/
final class RequestListener
{
use KernelEventForwardCompatibilityTrait;
/**
* @var HubInterface The current hub
*/
private $hub;
/**
* @var TokenStorageInterface|null The token storage
*/
private $tokenStorage;
/**
* Constructor.
*
* @param HubInterface $hub The current hub
* @param TokenStorageInterface|null $tokenStorage The token storage
*/
public function __construct(HubInterface $hub, ?TokenStorageInterface $tokenStorage)
{
$this->hub = $hub;
$this->tokenStorage = $tokenStorage;
}
/**
* This method is called for each request handled by the framework and
* fills the Sentry scope with information about the current user.
*
* @param RequestEvent $event The event
*/
public function handleKernelRequestEvent(RequestEvent $event): void
{
if (!$this->isMainRequest($event)) {
return;
}
$client = $this->hub->getClient();
if (null === $client || !$client->getOptions()->shouldSendDefaultPii()) {
return;
}
$userData = new UserDataBag();
$userData->setIpAddress($event->getRequest()->getClientIp());
if (null !== $this->tokenStorage) {
$this->setUserData($userData, $this->tokenStorage->getToken());
}
$this->hub->configureScope(static function (Scope $scope) use ($userData): void {
$scope->setUser($userData);
});
}
/**
* This method is called for each request handled by the framework and
* sets the route on the current Sentry scope.
*
* @param ControllerEvent $event The event
*/
public function handleKernelControllerEvent(ControllerEvent $event): void
{
if (!$this->isMainRequest($event)) {
return;
}
$route = $event->getRequest()->attributes->get('_route');
if (!\is_string($route)) {
return;
}
$this->hub->configureScope(static function (Scope $scope) use ($route): void {
$scope->setTag('route', $route);
});
}
/**
* @param UserInterface|object|string|null $user
*/
private function getUsername($user): ?string
{
if ($user instanceof UserInterface) {
if (method_exists($user, 'getUserIdentifier')) {
return $user->getUserIdentifier();
}
if (method_exists($user, 'getUsername')) {
return $user->getUsername();
}
}
if (\is_string($user)) {
return $user;
}
if (\is_object($user) && method_exists($user, '__toString')) {
return (string) $user;
}
return null;
}
private function getImpersonatorUser(TokenInterface $token): ?string
{
if (!$token instanceof SwitchUserToken) {
return null;
}
return $this->getUsername($token->getOriginalToken()->getUser());
}
private function setUserData(UserDataBag $userData, ?TokenInterface $token): void
{
if (null === $token || !$this->isTokenAuthenticated($token)) {
return;
}
$userData->setUsername($this->getUsername($token->getUser()));
$impersonatorUser = $this->getImpersonatorUser($token);
if (null !== $impersonatorUser) {
$userData->setMetadata('impersonator_username', $impersonatorUser);
}
}
private function isTokenAuthenticated(TokenInterface $token): bool
{
if (method_exists($token, 'isAuthenticated') && !$token->isAuthenticated(false)) {
return false;
}
return null !== $token->getUser();
}
}