mirror of
https://github.com/bitwarden/server.git
synced 2025-03-31 17:45:44 +02:00
upgrade more lgos. remove sendgrid
This commit is contained in:
parent
cd0ec26b07
commit
518e94f60f
@ -43,7 +43,7 @@
|
||||
<PackageReference Include="Quartz" Version="3.0.7" />
|
||||
<PackageReference Include="Serilog.AspNetCore" Version="3.2.0" />
|
||||
<PackageReference Include="Serilog.Extensions.Logging" Version="3.0.1" />
|
||||
<PackageReference Include="Serilog.Extensions.Logging.File" Version="1.1.0" />
|
||||
<PackageReference Include="Serilog.Extensions.Logging.File" Version="2.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.AzureDocumentDB" Version="3.8.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Sentry.AspNetCore" Version="2.4.2" />
|
||||
<PackageReference Include="IdentityServer4" Version="3.1.2" />
|
||||
@ -52,7 +52,6 @@
|
||||
<PackageReference Include="System.Text.Json" Version="4.7.1" />
|
||||
<PackageReference Include="AspNetCoreRateLimit" Version="2.1.0" />
|
||||
<PackageReference Include="Braintree" Version="4.17.0" />
|
||||
<PackageReference Include="Sendgrid" Version="9.12.0" />
|
||||
<PackageReference Include="Stripe.net" Version="28.8.0" />
|
||||
<PackageReference Include="U2F.Core" Version="1.0.4" />
|
||||
<PackageReference Include="Otp.NET" Version="1.2.2" />
|
||||
|
@ -114,7 +114,6 @@ namespace Bit.Core
|
||||
public class MailSettings
|
||||
{
|
||||
public string ReplyToEmail { get; set; }
|
||||
public string SendGridApiKey { get; set; }
|
||||
public string AmazonConfigSetName { get; set; }
|
||||
public SmtpSettings Smtp { get; set; } = new SmtpSettings();
|
||||
|
||||
|
@ -1,98 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using SendGrid;
|
||||
using SendGrid.Helpers.Mail;
|
||||
using Bit.Core.Models.Mail;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
|
||||
namespace Bit.Core.Services
|
||||
{
|
||||
public class SendGridMailDeliveryService : IMailDeliveryService
|
||||
{
|
||||
private readonly GlobalSettings _globalSettings;
|
||||
private readonly SendGridClient _client;
|
||||
|
||||
public SendGridMailDeliveryService(GlobalSettings globalSettings)
|
||||
{
|
||||
if(globalSettings.Mail?.SendGridApiKey == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(globalSettings.Mail.SendGridApiKey));
|
||||
}
|
||||
|
||||
_globalSettings = globalSettings;
|
||||
_client = new SendGridClient(_globalSettings.Mail.SendGridApiKey);
|
||||
}
|
||||
|
||||
public async Task SendEmailAsync(MailMessage message)
|
||||
{
|
||||
var sendGridMessage = new SendGridMessage
|
||||
{
|
||||
Subject = message.Subject,
|
||||
From = new EmailAddress(_globalSettings.Mail.ReplyToEmail, _globalSettings.SiteName),
|
||||
HtmlContent = message.HtmlContent,
|
||||
PlainTextContent = message.TextContent
|
||||
};
|
||||
|
||||
sendGridMessage.SetClickTracking(true, false);
|
||||
sendGridMessage.SetOpenTracking(true, null);
|
||||
sendGridMessage.AddTos(message.ToEmails.Select(e => new EmailAddress(e)).ToList());
|
||||
if(message.BccEmails?.Any() ?? false)
|
||||
{
|
||||
sendGridMessage.AddBccs(message.BccEmails.Select(e => new EmailAddress(e)).ToList());
|
||||
}
|
||||
|
||||
if(message.MetaData?.ContainsKey("SendGridTemplateId") ?? false)
|
||||
{
|
||||
sendGridMessage.HtmlContent = " ";
|
||||
sendGridMessage.PlainTextContent = " ";
|
||||
sendGridMessage.TemplateId = message.MetaData["SendGridTemplateId"].ToString();
|
||||
}
|
||||
|
||||
if(message.MetaData?.ContainsKey("SendGridSubstitutions") ?? false)
|
||||
{
|
||||
var subs = message.MetaData["SendGridSubstitutions"] as Dictionary<string, string>;
|
||||
sendGridMessage.AddSubstitutions(subs);
|
||||
}
|
||||
|
||||
var cats = new List<string> { "Bitwarden Server" };
|
||||
if(!string.IsNullOrWhiteSpace(message.Category))
|
||||
{
|
||||
cats.Add(message.Category);
|
||||
}
|
||||
sendGridMessage.AddCategories(cats);
|
||||
|
||||
if(message.MetaData?.ContainsKey("SendGridBypassListManagement") ?? false)
|
||||
{
|
||||
var bypass = message.MetaData["SendGridBypassListManagement"] as bool?;
|
||||
sendGridMessage.SetBypassListManagement(bypass.GetValueOrDefault(false));
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
await SendAsync(sendGridMessage, false);
|
||||
}
|
||||
catch(HttpRequestException)
|
||||
{
|
||||
await SendAsync(sendGridMessage, true);
|
||||
}
|
||||
catch(WebException)
|
||||
{
|
||||
await SendAsync(sendGridMessage, true);
|
||||
}
|
||||
}
|
||||
|
||||
private async Task SendAsync(SendGridMessage sendGridMessage, bool retry)
|
||||
{
|
||||
if(retry)
|
||||
{
|
||||
// wait and try again
|
||||
await Task.Delay(2000);
|
||||
}
|
||||
|
||||
await _client.SendEmailAsync(sendGridMessage);
|
||||
}
|
||||
}
|
||||
}
|
@ -128,11 +128,7 @@ namespace Bit.Core.Utilities
|
||||
services.AddSingleton<IApplicationCacheService, InMemoryApplicationCacheService>();
|
||||
}
|
||||
|
||||
if(CoreHelpers.SettingHasValue(globalSettings.Mail.SendGridApiKey))
|
||||
{
|
||||
services.AddSingleton<IMailDeliveryService, SendGridMailDeliveryService>();
|
||||
}
|
||||
else if(CoreHelpers.SettingHasValue(globalSettings.Amazon?.AccessKeySecret))
|
||||
if(CoreHelpers.SettingHasValue(globalSettings.Amazon?.AccessKeySecret))
|
||||
{
|
||||
services.AddSingleton<IMailDeliveryService, AmazonSesMailDeliveryService>();
|
||||
}
|
||||
|
@ -1,29 +0,0 @@
|
||||
using System;
|
||||
using Bit.Core.Services;
|
||||
using NSubstitute;
|
||||
using Xunit;
|
||||
|
||||
namespace Bit.Core.Test.Services
|
||||
{
|
||||
public class SendGridMailDeliveryServiceTests
|
||||
{
|
||||
private readonly SendGridMailDeliveryService _sut;
|
||||
|
||||
private readonly GlobalSettings _globalSettings;
|
||||
|
||||
public SendGridMailDeliveryServiceTests()
|
||||
{
|
||||
_globalSettings = new GlobalSettings();
|
||||
|
||||
_sut = new SendGridMailDeliveryService(_globalSettings);
|
||||
}
|
||||
|
||||
// Remove this test when we add actual tests. It only proves that
|
||||
// we've properly constructed the system under test.
|
||||
[Fact(Skip = "Needs additional work")]
|
||||
public void ServiceExists()
|
||||
{
|
||||
Assert.NotNull(_sut);
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user