DocSpace-client/web/ASC.Web.Studio/WebHooks/WebhookSender.cs

127 lines
5.0 KiB
C#
Raw Normal View History

// (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
2023-02-21 11:22:34 +00:00
using System.Net;
2022-11-09 11:31:22 +00:00
namespace ASC.Webhooks;
[Singletone]
public class WebhookSender
{
private readonly IHttpClientFactory _clientFactory;
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";
2022-08-16 10:36:48 +00:00
public WebhookSender(ILoggerProvider options, IServiceScopeFactory scopeFactory, IHttpClientFactory clientFactory)
{
_log = options.CreateLogger("ASC.Webhooks.Core");
_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
};
}
2021-08-19 09:11:26 +00:00
public async Task Send(WebhookRequest webhookRequest, CancellationToken cancellationToken)
{
using var scope = _scopeFactory.CreateScope();
2022-04-01 14:17:24 +00:00
var dbWorker = scope.ServiceProvider.GetRequiredService<DbWorker>();
2022-08-18 19:06:51 +00:00
var entry = await dbWorker.ReadJournal(webhookRequest.Id);
2022-08-16 10:36:48 +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;
var delivery = DateTime.MinValue;
2022-08-16 10:36:48 +00:00
try
{
var httpClient = _clientFactory.CreateClient("webhook");
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);
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 (SslException e)
{
2023-02-17 12:56:40 +00:00
status = (int)e.Errors;
responsePayload = e.Message;
_log.ErrorSSLVerification(e);
}
2022-08-16 10:36:48 +00:00
catch (HttpRequestException e)
{
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);
}
2023-02-21 11:22:34 +00:00
delivery = DateTime.UtcNow;
await dbWorker.UpdateWebhookJournal(entry.Id, status, delivery, requestHeaders, responsePayload, responseHeaders);
}
2021-08-19 09:11:26 +00:00
private string GetSecretHash(string secretKey, string body)
2022-08-16 10:36:48 +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);
}
2022-02-11 14:33:00 +00:00
}