-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added MicrosoftGraph implementation for OAuth. (#170)
- Loading branch information
Showing
6 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
...mmerce.NotificationsModule.MicrosoftGraph/MicrosoftGraphEmailNotificationMessageSender.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
using System; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Threading.Tasks; | ||
using Azure.Identity; | ||
using Microsoft.Extensions.Options; | ||
using Microsoft.Graph; | ||
using Microsoft.Graph.Models; | ||
using Microsoft.Graph.Users.Item.SendMail; | ||
using VirtoCommerce.NotificationsModule.Core.Exceptions; | ||
using VirtoCommerce.NotificationsModule.Core.Model; | ||
using VirtoCommerce.NotificationsModule.Core.Services; | ||
using VirtoCommerce.Platform.Core.Common; | ||
|
||
namespace VirtoCommerce.NotificationsModule.MicrosoftGraph; | ||
|
||
public class MicrosoftGraphEmailNotificationMessageSender( | ||
IOptions<MicrosoftGraphSenderOptions> microsoftGraphOptions, | ||
IOptions<EmailSendingOptions> emailSendingOptions) | ||
: INotificationMessageSender | ||
{ | ||
public const string Name = "MicrosoftGraph"; | ||
|
||
private readonly MicrosoftGraphSenderOptions _microsoftGraphOptions = microsoftGraphOptions.Value; | ||
private readonly EmailSendingOptions _emailSendingOptions = emailSendingOptions.Value; | ||
|
||
public virtual bool CanSend(NotificationMessage message) => message is EmailNotificationMessage; | ||
|
||
public async Task SendNotificationAsync(NotificationMessage message) | ||
{ | ||
var emailNotificationMessage = message as EmailNotificationMessage | ||
?? throw new ArgumentException($"The message is not {nameof(EmailNotificationMessage)} type"); | ||
|
||
try | ||
{ | ||
var graphMessage = new Message | ||
{ | ||
Subject = emailNotificationMessage.Subject, | ||
Body = new ItemBody { ContentType = BodyType.Html, Content = emailNotificationMessage.Body }, | ||
From = NewRecipient(emailNotificationMessage.From ?? _emailSendingOptions.DefaultSender), | ||
ToRecipients = [NewRecipient(emailNotificationMessage.To)], | ||
CcRecipients = emailNotificationMessage.CC?.Select(NewRecipient).ToList(), | ||
BccRecipients = emailNotificationMessage.BCC?.Select(NewRecipient).ToList() | ||
}; | ||
|
||
if (!emailNotificationMessage.Attachments.IsNullOrEmpty()) | ||
{ | ||
graphMessage.Attachments ??= []; | ||
foreach (var attachment in emailNotificationMessage.Attachments) | ||
{ | ||
graphMessage.Attachments.Add(new FileAttachment | ||
{ | ||
ContentType = attachment.MimeType, | ||
Name = attachment.FileName, | ||
Size = Convert.ToInt32(attachment.Size), | ||
ContentBytes = await File.ReadAllBytesAsync(attachment.FileName), | ||
}); | ||
} | ||
} | ||
|
||
var request = new SendMailPostRequestBody { SaveToSentItems = false, Message = graphMessage }; | ||
|
||
var options = new TokenCredentialOptions { AuthorityHost = AzureAuthorityHosts.AzurePublicCloud }; | ||
var clientSecretCredential = new ClientSecretCredential( | ||
_microsoftGraphOptions.TenantId, | ||
_microsoftGraphOptions.ApplicationId, | ||
_microsoftGraphOptions.SecretValue, | ||
options | ||
); | ||
var scopes = new[] { "https://graph.microsoft.com/.default" }; | ||
|
||
var microsoftGraphClient = new GraphServiceClient(clientSecretCredential, scopes); | ||
await microsoftGraphClient.Users[emailNotificationMessage.From].SendMail.PostAsync(request); | ||
} | ||
catch (Exception ex) | ||
{ | ||
throw new SentNotificationException(ex); | ||
} | ||
} | ||
|
||
private static Recipient NewRecipient(string emailAddress) => new() | ||
{ | ||
EmailAddress = new EmailAddress { Address = emailAddress } | ||
}; | ||
} |
10 changes: 10 additions & 0 deletions
10
src/VirtoCommerce.NotificationsModule.MicrosoftGraph/MicrosoftGraphSenderOptions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
namespace VirtoCommerce.NotificationsModule.MicrosoftGraph; | ||
|
||
public class MicrosoftGraphSenderOptions | ||
{ | ||
public string ApplicationId { get; set; } | ||
|
||
public string TenantId { get; set; } | ||
|
||
public string SecretValue { get; set; } | ||
} |
24 changes: 24 additions & 0 deletions
24
...otificationsModule.MicrosoftGraph/VirtoCommerce.NotificationsModule.MicrosoftGraph.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<noWarn>1591</noWarn> | ||
<IsPackable>True</IsPackable> | ||
<PublishRepositoryUrl>true</PublishRepositoryUrl> | ||
<EmbedUntrackedSources>true</EmbedUntrackedSources> | ||
<IncludeSymbols>true</IncludeSymbols> | ||
<SymbolPackageFormat>snupkg</SymbolPackageFormat> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="Azure.Core" Version="1.41.0" /> | ||
<PackageReference Include="Azure.Identity" Version="1.12.1" /> | ||
<PackageReference Include="Microsoft.Graph" Version="5.56.0" /> | ||
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="8.0.0" PrivateAssets="All" /> | ||
</ItemGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\VirtoCommerce.NotificationsModule.Core\VirtoCommerce.NotificationsModule.Core.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters