Skip to content

feat: detect user from sanctum token #542

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Unreleased

- Add support for normalized route names when using Lumen (#449)
- Add support for adding the user ID to the user scope when using Laravel Sanctum (#542)

## 2.11.1

Expand Down
74 changes: 49 additions & 25 deletions src/Sentry/Laravel/EventHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@
use Illuminate\Queue\Events\JobProcessing;
use Illuminate\Queue\Events\WorkerStopping;
use Illuminate\Queue\QueueManager;
use Laravel\Octane\Events as Octane;
use Illuminate\Routing\Events\RouteMatched;
use Illuminate\Routing\Route;
use Laravel\Octane\Events as Octane;
use Laravel\Sanctum\Events as Sanctum;
use RuntimeException;
use Sentry\Breadcrumb;
use Sentry\SentrySdk;
Expand All @@ -33,14 +34,14 @@ class EventHandler
* @var array
*/
protected static $eventHandlerMap = [
'router.matched' => 'routerMatched', // Until Laravel 5.1
'Illuminate\Routing\Events\RouteMatched' => 'routeMatched', // Since Laravel 5.2
'router.matched' => 'routerMatched', // Until Laravel 5.1
'Illuminate\Routing\Events\RouteMatched' => 'routeMatched', // Since Laravel 5.2

'illuminate.query' => 'query', // Until Laravel 5.1
'Illuminate\Database\Events\QueryExecuted' => 'queryExecuted', // Since Laravel 5.2
'illuminate.query' => 'query', // Until Laravel 5.1
'Illuminate\Database\Events\QueryExecuted' => 'queryExecuted', // Since Laravel 5.2

'illuminate.log' => 'log', // Until Laravel 5.3
'Illuminate\Log\Events\MessageLogged' => 'messageLogged', // Since Laravel 5.4
'illuminate.log' => 'log', // Until Laravel 5.3
'Illuminate\Log\Events\MessageLogged' => 'messageLogged', // Since Laravel 5.4

'Illuminate\Console\Events\CommandStarting' => 'commandStarting', // Since Laravel 5.5
'Illuminate\Console\Events\CommandFinished' => 'commandFinished', // Since Laravel 5.5
Expand All @@ -52,7 +53,8 @@ class EventHandler
* @var array
*/
protected static $authEventHandlerMap = [
'Illuminate\Auth\Events\Authenticated' => 'authenticated', // Since Laravel 5.3
'Illuminate\Auth\Events\Authenticated' => 'authenticated', // Since Laravel 5.3
'Laravel\Sanctum\Events\TokenAuthenticated' => 'sanctumTokenAuthenticated', // Since Sanctum 2.13
];

/**
Expand All @@ -61,10 +63,10 @@ class EventHandler
* @var array
*/
protected static $queueEventHandlerMap = [
'Illuminate\Queue\Events\JobProcessing' => 'queueJobProcessing', // Since Laravel 5.2
'Illuminate\Queue\Events\JobProcessed' => 'queueJobProcessed', // Since Laravel 5.2
'Illuminate\Queue\Events\JobProcessed' => 'queueJobProcessed', // Since Laravel 5.2
'Illuminate\Queue\Events\JobProcessing' => 'queueJobProcessing', // Since Laravel 5.2
'Illuminate\Queue\Events\WorkerStopping' => 'queueWorkerStopping', // Since Laravel 5.2
'Illuminate\Queue\Events\JobExceptionOccurred' => 'queueJobExceptionOccurred', // Since Laravel 5.2
'Illuminate\Queue\Events\WorkerStopping' => 'queueWorkerStopping', // Since Laravel 5.2
];

/**
Expand All @@ -73,17 +75,17 @@ class EventHandler
* @var array
*/
protected static $octaneEventHandlerMap = [
'Laravel\Octane\Events\RequestReceived' => 'octaneRequestReceived',
'Laravel\Octane\Events\RequestReceived' => 'octaneRequestReceived',
'Laravel\Octane\Events\RequestTerminated' => 'octaneRequestTerminated',

'Laravel\Octane\Events\TaskReceived' => 'octaneTaskReceived',
'Laravel\Octane\Events\TaskReceived' => 'octaneTaskReceived',
'Laravel\Octane\Events\TaskTerminated' => 'octaneTaskTerminated',

'Laravel\Octane\Events\TickReceived' => 'octaneTickReceived',
'Laravel\Octane\Events\TickReceived' => 'octaneTickReceived',
'Laravel\Octane\Events\TickTerminated' => 'octaneTickTerminated',

'Laravel\Octane\Events\WorkerErrorOccurred' => 'octaneWorkerErrorOccurred',
'Laravel\Octane\Events\WorkerStopping' => 'octaneWorkerStopping',
'Laravel\Octane\Events\WorkerStopping' => 'octaneWorkerStopping',
];

/**
Expand Down Expand Up @@ -166,11 +168,11 @@ public function __construct(Container $container, array $config)
{
$this->container = $container;

$this->recordSqlQueries = ($config['breadcrumbs.sql_queries'] ?? $config['breadcrumbs']['sql_queries'] ?? true) === true;
$this->recordSqlBindings = ($config['breadcrumbs.sql_bindings'] ?? $config['breadcrumbs']['sql_bindings'] ?? false) === true;
$this->recordLaravelLogs = ($config['breadcrumbs.logs'] ?? $config['breadcrumbs']['logs'] ?? true) === true;
$this->recordQueueInfo = ($config['breadcrumbs.queue_info'] ?? $config['breadcrumbs']['queue_info'] ?? true) === true;
$this->recordCommandInfo = ($config['breadcrumbs.command_info'] ?? $config['breadcrumbs']['command_info'] ?? true) === true;
$this->recordSqlQueries = ($config['breadcrumbs.sql_queries'] ?? $config['breadcrumbs']['sql_queries'] ?? true) === true;
$this->recordSqlBindings = ($config['breadcrumbs.sql_bindings'] ?? $config['breadcrumbs']['sql_bindings'] ?? false) === true;
$this->recordLaravelLogs = ($config['breadcrumbs.logs'] ?? $config['breadcrumbs']['logs'] ?? true) === true;
$this->recordQueueInfo = ($config['breadcrumbs.queue_info'] ?? $config['breadcrumbs']['queue_info'] ?? true) === true;
$this->recordCommandInfo = ($config['breadcrumbs.command_info'] ?? $config['breadcrumbs']['command_info'] ?? true) === true;
$this->recordOctaneTickInfo = ($config['breadcrumbs.octane_tick_info'] ?? $config['breadcrumbs']['octane_tick_info'] ?? true) === true;
$this->recordOctaneTaskInfo = ($config['breadcrumbs.octane_task_info'] ?? $config['breadcrumbs']['octane_task_info'] ?? true) === true;
}
Expand Down Expand Up @@ -419,10 +421,32 @@ private function addLogBreadcrumb(string $level, ?string $message, array $contex
*/
protected function authenticatedHandler(Authenticated $event)
{
$userData = [
$this->configureUserScopeWithRequest([
'id' => $event->user->getAuthIdentifier(),
];
]);
}

/**
* Since Sanctum 2.13
*
* @param \Laravel\Sanctum\Events\TokenAuthenticated $event
*/
protected function sanctumTokenAuthenticatedHandler(Sanctum\TokenAuthenticated $event)
{
$this->configureUserScopeWithRequest([
'id' => $event->token->tokenable->getAuthIdentifier(),
]);
}

/**
* Configures the user scope with the user data and values from the HTTP request.
*
* @param array $userData
*
* @return void
*/
private function configureUserScopeWithRequest(array $userData): void
{
try {
/** @var \Illuminate\Http\Request $request */
$request = $this->container->make('request');
Expand Down Expand Up @@ -461,9 +485,9 @@ protected function queueJobProcessingHandler(JobProcessing $event)
}

$job = [
'job' => $event->job->getName(),
'queue' => $event->job->getQueue(),
'attempts' => $event->job->attempts(),
'job' => $event->job->getName(),
'queue' => $event->job->getQueue(),
'attempts' => $event->job->attempts(),
'connection' => $event->connectionName,
];

Expand Down