2020-12-02 12:30:09 -06:00
|
|
|
<?php
|
|
|
|
/*
|
|
|
|
* WebhookEventHandler.php
|
|
|
|
* Copyright (c) 2020 james@firefly-iii.org
|
|
|
|
*
|
|
|
|
* This file is part of Firefly III (https://github.com/firefly-iii).
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace FireflyIII\Handlers\Events;
|
|
|
|
|
|
|
|
|
|
|
|
use Exception;
|
2020-12-03 23:21:22 -06:00
|
|
|
use FireflyIII\Models\WebhookAttempt;
|
2020-12-02 12:30:09 -06:00
|
|
|
use FireflyIII\Models\WebhookMessage;
|
|
|
|
use GuzzleHttp\Client;
|
|
|
|
use GuzzleHttp\Exception\ClientException;
|
|
|
|
use JsonException;
|
|
|
|
use Log;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class WebhookEventHandler
|
|
|
|
*/
|
|
|
|
class WebhookEventHandler
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public function sendWebhookMessages(): void
|
|
|
|
{
|
|
|
|
$max = (int)config('firefly.webhooks.max_attempts');
|
|
|
|
$max = 0 === $max ? 3 : $max;
|
2020-12-03 23:21:22 -06:00
|
|
|
$messages = WebhookMessage
|
|
|
|
::where('webhook_messages.sent', 0)
|
|
|
|
->where('webhook_messages.errored', 0)
|
|
|
|
->get(['webhook_messages.*']);
|
|
|
|
Log::debug(sprintf('Found %d webhook message(s) to be send.', $messages->count()));
|
2020-12-02 12:30:09 -06:00
|
|
|
/** @var WebhookMessage $message */
|
|
|
|
foreach ($messages as $message) {
|
2020-12-03 23:21:22 -06:00
|
|
|
$count = $message->webhookAttempts()->count();
|
|
|
|
if ($count >= 3) {
|
|
|
|
Log::info('No send message.');
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// TODO needs its own handler.
|
|
|
|
$this->sendMessageV0($message);
|
2020-12-02 12:30:09 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param WebhookMessage $message
|
|
|
|
*/
|
2020-12-03 23:21:22 -06:00
|
|
|
private function sendMessageV0(WebhookMessage $message): void
|
2020-12-02 12:30:09 -06:00
|
|
|
{
|
|
|
|
Log::debug(sprintf('Trying to send webhook message #%d', $message->id));
|
|
|
|
try {
|
|
|
|
$json = json_encode($message->message, JSON_THROW_ON_ERROR);
|
|
|
|
} catch (JsonException $e) {
|
2020-12-03 23:21:22 -06:00
|
|
|
$attempt = new WebhookAttempt;
|
|
|
|
$attempt->webhookMessage()->associate($message);
|
|
|
|
$attempt->status_code = 0;
|
|
|
|
$attempt->logs = sprintf('Json error: %s', $e->getMessage());
|
|
|
|
$attempt->save();
|
2020-12-02 12:30:09 -06:00
|
|
|
|
|
|
|
return;
|
|
|
|
}
|
2020-12-03 23:21:22 -06:00
|
|
|
// signature v0 is generated using the following structure:
|
|
|
|
// The signed_payload string is created by concatenating:
|
|
|
|
// The timestamp (as a string)
|
|
|
|
// The character .
|
|
|
|
// The character .
|
|
|
|
// The actual JSON payload (i.e., the request body)
|
|
|
|
$timestamp = time();
|
|
|
|
$payload = sprintf('%s.%s', $timestamp, $json);
|
|
|
|
$signature = hash_hmac('sha3-256', $payload, $message->webhook->secret, false);
|
2020-12-02 12:30:09 -06:00
|
|
|
|
2020-12-03 23:21:22 -06:00
|
|
|
// signature string:
|
|
|
|
// header included in each signed event contains a timestamp and one or more signatures.
|
|
|
|
// The timestamp is prefixed by t=, and each signature is prefixed by a scheme.
|
|
|
|
// Schemes start with v, followed by an integer. Currently, the only valid live signature scheme is v0.
|
|
|
|
$signatureString = sprintf('t=%s,v0=%s', $timestamp, $signature);
|
|
|
|
|
|
|
|
$options = [
|
2020-12-02 12:30:09 -06:00
|
|
|
'body' => $json,
|
|
|
|
'headers' => [
|
|
|
|
'Content-Type' => 'application/json',
|
|
|
|
'Accept' => 'application/json',
|
2020-12-03 23:21:22 -06:00
|
|
|
'Signature' => $signatureString,
|
2020-12-02 12:30:09 -06:00
|
|
|
'connect_timeout' => 3.14,
|
|
|
|
'User-Agent' => sprintf('FireflyIII/%s', config('firefly.version')),
|
|
|
|
'timeout' => 10,
|
|
|
|
],
|
|
|
|
];
|
2020-12-03 23:21:22 -06:00
|
|
|
$client = new Client;
|
|
|
|
$logs = $message->logs ?? [];
|
2020-12-02 12:30:09 -06:00
|
|
|
try {
|
2020-12-02 23:54:42 -06:00
|
|
|
$res = $client->request('POST', $message->webhook->url, $options);
|
2020-12-02 12:30:09 -06:00
|
|
|
$message->sent = true;
|
|
|
|
} catch (ClientException|Exception $e) {
|
|
|
|
Log::error($e->getMessage());
|
|
|
|
Log::error($e->getTraceAsString());
|
|
|
|
$logs[] = sprintf('%s: %s', date('Y-m-d H:i:s'), $e->getMessage());
|
|
|
|
$message->errored = true;
|
|
|
|
$message->sent = false;
|
|
|
|
}
|
|
|
|
$message->save();
|
|
|
|
|
2020-12-03 23:21:22 -06:00
|
|
|
$attempt = new WebhookAttempt;
|
|
|
|
$attempt->webhookMessage()->associate($message);
|
|
|
|
$attempt->status_code = $res->getStatusCode();
|
|
|
|
$attempt->logs = '';
|
|
|
|
$attempt->response = (string)$res->getBody();
|
|
|
|
$attempt->save();
|
|
|
|
|
2020-12-02 12:30:09 -06:00
|
|
|
Log::debug(sprintf('Webhook message #%d was sent. Status code %d', $message->id, $res->getStatusCode()));
|
2020-12-02 23:54:42 -06:00
|
|
|
Log::debug(sprintf('Webhook request body size: %d bytes', strlen($json)));
|
2020-12-02 12:30:09 -06:00
|
|
|
Log::debug(sprintf('Response body: %s', $res->getBody()));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|