Merge pull request #5419 from sbwalker/dev
add OAuth support to Notification Job (#5372)
This commit is contained in:
@ -1,16 +1,15 @@
|
|||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
using System.Threading.Tasks;
|
||||||
using MailKit.Net.Smtp;
|
using MailKit.Net.Smtp;
|
||||||
|
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
|
using Microsoft.Identity.Client;
|
||||||
using MimeKit;
|
using MimeKit;
|
||||||
|
|
||||||
using Oqtane.Models;
|
using Oqtane.Models;
|
||||||
using Oqtane.Repository;
|
using Oqtane.Repository;
|
||||||
using Oqtane.Shared;
|
using Oqtane.Shared;
|
||||||
|
using MailKit.Security;
|
||||||
|
|
||||||
namespace Oqtane.Infrastructure
|
namespace Oqtane.Infrastructure
|
||||||
{
|
{
|
||||||
@ -27,7 +26,7 @@ namespace Oqtane.Infrastructure
|
|||||||
}
|
}
|
||||||
|
|
||||||
// job is executed for each tenant in installation
|
// job is executed for each tenant in installation
|
||||||
public override string ExecuteJob(IServiceProvider provider)
|
public async override Task<string> ExecuteJobAsync(IServiceProvider provider)
|
||||||
{
|
{
|
||||||
string log = "";
|
string log = "";
|
||||||
|
|
||||||
@ -48,23 +47,75 @@ namespace Oqtane.Infrastructure
|
|||||||
|
|
||||||
if (!site.IsDeleted && settingRepository.GetSettingValue(settings, "SMTPEnabled", "True") == "True")
|
if (!site.IsDeleted && settingRepository.GetSettingValue(settings, "SMTPEnabled", "True") == "True")
|
||||||
{
|
{
|
||||||
if (settingRepository.GetSettingValue(settings, "SMTPHost", "") != "" &&
|
bool valid = true;
|
||||||
settingRepository.GetSettingValue(settings, "SMTPPort", "") != "" &&
|
if (settingRepository.GetSettingValue(settings, "SMTPAuthentication", "Basic") == "Basic")
|
||||||
settingRepository.GetSettingValue(settings, "SMTPSender", "") != "")
|
{
|
||||||
|
// basic
|
||||||
|
if (settingRepository.GetSettingValue(settings, "SMTPHost", "") == "" ||
|
||||||
|
settingRepository.GetSettingValue(settings, "SMTPPort", "") == "" ||
|
||||||
|
settingRepository.GetSettingValue(settings, "SMTPSender", "") == "")
|
||||||
|
{
|
||||||
|
log += "SMTP Not Configured Properly In Site Settings - Host, Port, And Sender Are All Required" + "<br />";
|
||||||
|
valid = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// oauth
|
||||||
|
if (settingRepository.GetSettingValue(settings, "SMTPHost", "") == "" ||
|
||||||
|
settingRepository.GetSettingValue(settings, "SMTPPort", "") == "" ||
|
||||||
|
settingRepository.GetSettingValue(settings, "SMTPAuthority", "") == "" ||
|
||||||
|
settingRepository.GetSettingValue(settings, "SMTPClientId", "") == "" ||
|
||||||
|
settingRepository.GetSettingValue(settings, "SMTPClientSecret", "") == "" ||
|
||||||
|
settingRepository.GetSettingValue(settings, "SMTPScopes", "") == "" ||
|
||||||
|
settingRepository.GetSettingValue(settings, "SMTPSender", "") == "")
|
||||||
|
{
|
||||||
|
log += "SMTP Not Configured Properly In Site Settings - Host, Port, Authority, Client ID, Client Secret, Scopes, And Sender Are All Required" + "<br />";
|
||||||
|
valid = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (valid)
|
||||||
{
|
{
|
||||||
// construct SMTP Client
|
// construct SMTP Client
|
||||||
using var client = new SmtpClient();
|
using var client = new SmtpClient();
|
||||||
|
|
||||||
client.Connect(host: settingRepository.GetSettingValue(settings, "SMTPHost", ""),
|
await client.ConnectAsync(settingRepository.GetSettingValue(settings, "SMTPHost", ""),
|
||||||
port: int.Parse(settingRepository.GetSettingValue(settings, "SMTPPort", "")),
|
int.Parse(settingRepository.GetSettingValue(settings, "SMTPPort", "")),
|
||||||
options: bool.Parse(settingRepository.GetSettingValue(settings, "SMTPSSL", "False")) ? MailKit.Security.SecureSocketOptions.StartTls : MailKit.Security.SecureSocketOptions.None);
|
bool.Parse(settingRepository.GetSettingValue(settings, "SMTPSSL", "False")) ? SecureSocketOptions.StartTls : SecureSocketOptions.None);
|
||||||
|
|
||||||
|
if (settingRepository.GetSettingValue(settings, "SMTPAuthentication", "Basic") == "Basic")
|
||||||
|
{
|
||||||
|
// it is possible to use basic without any authentication (not recommended)
|
||||||
if (settingRepository.GetSettingValue(settings, "SMTPUsername", "") != "" && settingRepository.GetSettingValue(settings, "SMTPPassword", "") != "")
|
if (settingRepository.GetSettingValue(settings, "SMTPUsername", "") != "" && settingRepository.GetSettingValue(settings, "SMTPPassword", "") != "")
|
||||||
{
|
{
|
||||||
client.Authenticate(settingRepository.GetSettingValue(settings, "SMTPUsername", ""),
|
await client.AuthenticateAsync(settingRepository.GetSettingValue(settings, "SMTPUsername", ""),
|
||||||
settingRepository.GetSettingValue(settings, "SMTPPassword", ""));
|
settingRepository.GetSettingValue(settings, "SMTPPassword", ""));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// oauth authentication
|
||||||
|
var confidentialClientApplication = ConfidentialClientApplicationBuilder.Create(settingRepository.GetSettingValue(settings, "SMTPClientId", ""))
|
||||||
|
.WithAuthority(settingRepository.GetSettingValue(settings, "SMTPAuthority", ""))
|
||||||
|
.WithClientSecret(settingRepository.GetSettingValue(settings, "SMTPClientSecret", ""))
|
||||||
|
.Build();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
var result = await confidentialClientApplication.AcquireTokenForClient(settingRepository.GetSettingValue(settings, "SMTPScopes", "").Split(',')).ExecuteAsync();
|
||||||
|
var oauth2 = new SaslMechanismOAuth2(settingRepository.GetSettingValue(settings, "SMTPSender", ""), result.AccessToken);
|
||||||
|
await client.AuthenticateAsync(oauth2);
|
||||||
|
}
|
||||||
|
catch (Exception ex)
|
||||||
|
{
|
||||||
|
log += "SMTP Not Configured Properly In Site Settings - OAuth Token Could Not Be Retrieved From Authority - " + ex.Message + "<br />";
|
||||||
|
valid = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (valid)
|
||||||
|
{
|
||||||
// iterate through undelivered notifications
|
// iterate through undelivered notifications
|
||||||
int sent = 0;
|
int sent = 0;
|
||||||
List<Notification> notifications = notificationRepository.GetNotifications(site.SiteId, -1, -1).ToList();
|
List<Notification> notifications = notificationRepository.GetNotifications(site.SiteId, -1, -1).ToList();
|
||||||
@ -149,7 +200,7 @@ namespace Oqtane.Infrastructure
|
|||||||
// send mail
|
// send mail
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
client.Send(mailMessage);
|
await client.SendAsync(mailMessage);
|
||||||
sent++;
|
sent++;
|
||||||
notification.IsDelivered = true;
|
notification.IsDelivered = true;
|
||||||
notification.DeliveredOn = DateTime.UtcNow;
|
notification.DeliveredOn = DateTime.UtcNow;
|
||||||
@ -162,12 +213,9 @@ namespace Oqtane.Infrastructure
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
client.Disconnect(true);
|
await client.DisconnectAsync(true);
|
||||||
log += "Notifications Delivered: " + sent + "<br />";
|
log += "Notifications Delivered: " + sent + "<br />";
|
||||||
}
|
}
|
||||||
else
|
|
||||||
{
|
|
||||||
log += "SMTP Not Configured Properly In Site Settings - Host, Port, And Sender Are All Required" + "<br />";
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
|
Reference in New Issue
Block a user