2022-03-20 13:47:31 +00:00
|
|
|
|
// (c) Copyright Ascensio System SIA 2010-2022
|
2022-03-15 18:00:53 +00:00
|
|
|
|
//
|
|
|
|
|
// This program is a free software product.
|
|
|
|
|
// You can redistribute it and/or modify it under the terms
|
|
|
|
|
// of the GNU Affero General Public License (AGPL) version 3 as published by the Free Software
|
|
|
|
|
// Foundation. In accordance with Section 7(a) of the GNU AGPL its Section 15 shall be amended
|
|
|
|
|
// to the effect that Ascensio System SIA expressly excludes the warranty of non-infringement of
|
|
|
|
|
// any third-party rights.
|
|
|
|
|
//
|
|
|
|
|
// This program is distributed WITHOUT ANY WARRANTY, without even the implied warranty
|
|
|
|
|
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For details, see
|
|
|
|
|
// the GNU AGPL at: http://www.gnu.org/licenses/agpl-3.0.html
|
|
|
|
|
//
|
|
|
|
|
// You can contact Ascensio System SIA at Lubanas st. 125a-25, Riga, Latvia, EU, LV-1021.
|
|
|
|
|
//
|
|
|
|
|
// The interactive user interfaces in modified source and object code versions of the Program must
|
|
|
|
|
// display Appropriate Legal Notices, as required under Section 5 of the GNU AGPL version 3.
|
|
|
|
|
//
|
|
|
|
|
// Pursuant to Section 7(b) of the License you must retain the original Product logo when
|
|
|
|
|
// distributing the program. Pursuant to Section 7(e) we decline to grant you any rights under
|
|
|
|
|
// trademark law for use of our trademarks.
|
|
|
|
|
//
|
|
|
|
|
// All the Product's GUI elements, including illustrations and icon sets, as well as technical writing
|
|
|
|
|
// content are licensed under the terms of the Creative Commons Attribution-ShareAlike 4.0
|
|
|
|
|
// International. See the License terms at http://creativecommons.org/licenses/by-sa/4.0/legalcode
|
2023-01-30 16:32:46 +00:00
|
|
|
|
|
2022-11-09 11:31:22 +00:00
|
|
|
|
namespace ASC.Webhooks;
|
2022-02-03 13:41:09 +00:00
|
|
|
|
|
|
|
|
|
[Singletone]
|
|
|
|
|
public class WebhookSender
|
2021-06-29 08:40:20 +00:00
|
|
|
|
{
|
2022-02-11 12:34:39 +00:00
|
|
|
|
private readonly IHttpClientFactory _clientFactory;
|
2022-04-26 17:53:48 +00:00
|
|
|
|
private readonly ILogger _log;
|
2022-08-17 09:18:50 +00:00
|
|
|
|
private readonly IServiceScopeFactory _scopeFactory;
|
|
|
|
|
private readonly JsonSerializerOptions _jsonSerializerOptions;
|
2023-02-15 18:26:28 +00:00
|
|
|
|
private const string SignatureHeader = "x-docspace-signature-256";
|
2023-04-06 18:46:23 +00:00
|
|
|
|
|
|
|
|
|
public const string WEBHOOK = "webhook";
|
|
|
|
|
public const string WEBHOOK_SKIP_SSL = "webhookSkipSSL";
|
2022-02-03 13:41:09 +00:00
|
|
|
|
|
2022-08-16 10:36:48 +00:00
|
|
|
|
public WebhookSender(ILoggerProvider options, IServiceScopeFactory scopeFactory, IHttpClientFactory clientFactory)
|
2021-06-29 08:40:20 +00:00
|
|
|
|
{
|
2022-04-26 17:53:48 +00:00
|
|
|
|
_log = options.CreateLogger("ASC.Webhooks.Core");
|
2022-02-03 13:41:09 +00:00
|
|
|
|
_scopeFactory = scopeFactory;
|
2022-08-17 09:18:50 +00:00
|
|
|
|
_clientFactory = clientFactory;
|
|
|
|
|
_jsonSerializerOptions = new JsonSerializerOptions
|
|
|
|
|
{
|
|
|
|
|
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
|
2022-08-18 09:34:48 +00:00
|
|
|
|
IgnoreReadOnlyProperties = true
|
2022-08-17 09:18:50 +00:00
|
|
|
|
};
|
2022-02-03 13:41:09 +00:00
|
|
|
|
}
|
2021-08-19 09:11:26 +00:00
|
|
|
|
|
2023-04-24 10:52:56 +00:00
|
|
|
|
public async Task Send(WebhookRequestIntegrationEvent webhookRequest, CancellationToken cancellationToken)
|
2022-02-03 13:41:09 +00:00
|
|
|
|
{
|
|
|
|
|
using var scope = _scopeFactory.CreateScope();
|
2023-04-06 18:46:23 +00:00
|
|
|
|
var dbWorker = scope.ServiceProvider.GetRequiredService<DbWorker>();
|
2022-02-03 13:41:09 +00:00
|
|
|
|
|
2023-04-24 10:52:56 +00:00
|
|
|
|
var entry = await dbWorker.ReadJournal(webhookRequest.WebhookId);
|
2022-08-16 10:36:48 +00:00
|
|
|
|
|
2023-04-06 18:46:23 +00:00
|
|
|
|
var ssl = entry.Config.SSL;
|
|
|
|
|
|
2022-08-18 09:03:13 +00:00
|
|
|
|
var status = 0;
|
2022-08-16 10:36:48 +00:00
|
|
|
|
string responsePayload = null;
|
|
|
|
|
string responseHeaders = null;
|
2022-08-17 09:18:50 +00:00
|
|
|
|
string requestHeaders = null;
|
2022-08-18 09:03:13 +00:00
|
|
|
|
var delivery = DateTime.MinValue;
|
2022-08-16 10:36:48 +00:00
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
{
|
2023-04-06 18:46:23 +00:00
|
|
|
|
var clientName = ssl ? WEBHOOK : WEBHOOK_SKIP_SSL;
|
|
|
|
|
|
|
|
|
|
var httpClient = _clientFactory.CreateClient(clientName);
|
2022-08-18 19:06:51 +00:00
|
|
|
|
var request = new HttpRequestMessage(HttpMethod.Post, entry.Config.Uri)
|
2022-08-16 10:36:48 +00:00
|
|
|
|
{
|
2022-08-18 19:06:51 +00:00
|
|
|
|
Content = new StringContent(entry.RequestPayload, Encoding.UTF8, "application/json")
|
2022-08-16 10:36:48 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
request.Headers.Add("Accept", "*/*");
|
2023-02-15 18:26:28 +00:00
|
|
|
|
request.Headers.Add(SignatureHeader, $"sha256={GetSecretHash(entry.Config.SecretKey, entry.RequestPayload)}");
|
2022-08-17 09:18:50 +00:00
|
|
|
|
requestHeaders = JsonSerializer.Serialize(request.Headers.ToDictionary(r => r.Key, v => v.Value), _jsonSerializerOptions);
|
|
|
|
|
|
2022-08-16 10:36:48 +00:00
|
|
|
|
var response = await httpClient.SendAsync(request, cancellationToken);
|
|
|
|
|
|
2022-08-18 09:03:13 +00:00
|
|
|
|
status = (int)response.StatusCode;
|
2022-08-17 09:18:50 +00:00
|
|
|
|
responseHeaders = JsonSerializer.Serialize(response.Headers.ToDictionary(r => r.Key, v => v.Value), _jsonSerializerOptions);
|
2022-08-16 10:36:48 +00:00
|
|
|
|
responsePayload = await response.Content.ReadAsStringAsync();
|
|
|
|
|
|
|
|
|
|
_log.DebugResponse(response);
|
|
|
|
|
}
|
|
|
|
|
catch (HttpRequestException e)
|
|
|
|
|
{
|
2022-08-18 09:03:13 +00:00
|
|
|
|
if (e.StatusCode.HasValue)
|
|
|
|
|
{
|
|
|
|
|
status = (int)e.StatusCode.Value;
|
|
|
|
|
}
|
2023-02-15 18:26:28 +00:00
|
|
|
|
|
|
|
|
|
//if (e.InnerException is SocketException se)
|
|
|
|
|
//{
|
|
|
|
|
// status = (int)se.SocketErrorCode;
|
|
|
|
|
//}
|
|
|
|
|
|
2022-08-16 10:36:48 +00:00
|
|
|
|
responsePayload = e.Message;
|
|
|
|
|
|
|
|
|
|
_log.ErrorWithException(e);
|
|
|
|
|
}
|
|
|
|
|
catch (Exception e)
|
|
|
|
|
{
|
2023-02-21 11:22:34 +00:00
|
|
|
|
status = (int)HttpStatusCode.InternalServerError;
|
2022-08-16 10:36:48 +00:00
|
|
|
|
_log.ErrorWithException(e);
|
|
|
|
|
}
|
2022-08-18 09:03:13 +00:00
|
|
|
|
|
2023-02-21 11:22:34 +00:00
|
|
|
|
delivery = DateTime.UtcNow;
|
|
|
|
|
|
|
|
|
|
await dbWorker.UpdateWebhookJournal(entry.Id, status, delivery, requestHeaders, responsePayload, responseHeaders);
|
2022-02-03 13:41:09 +00:00
|
|
|
|
}
|
2021-08-19 09:11:26 +00:00
|
|
|
|
|
2022-02-03 13:41:09 +00:00
|
|
|
|
private string GetSecretHash(string secretKey, string body)
|
2022-08-16 10:36:48 +00:00
|
|
|
|
{
|
2022-02-03 13:41:09 +00:00
|
|
|
|
var secretBytes = Encoding.UTF8.GetBytes(secretKey);
|
2023-02-15 18:26:28 +00:00
|
|
|
|
using var hasher = new HMACSHA256(secretBytes);
|
|
|
|
|
var data = Encoding.UTF8.GetBytes(body);
|
|
|
|
|
var hash = hasher.ComputeHash(data);
|
|
|
|
|
return Convert.ToHexString(hash);
|
2021-06-29 08:40:20 +00:00
|
|
|
|
}
|
2022-02-11 14:33:00 +00:00
|
|
|
|
}
|