2024-04-18 01:44:37 +02:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Microsoft.Identity.Client;
|
2024-08-05 00:36:26 +02:00
|
|
|
|
using Microsoft.Identity.Client.Broker;
|
|
|
|
|
|
using Microsoft.Identity.Client.Extensions.Msal;
|
2024-11-10 23:28:25 +01:00
|
|
|
|
using Wino.Core.Authenticators.Base;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
using Wino.Core.Domain;
|
2024-11-10 23:28:25 +01:00
|
|
|
|
using Wino.Core.Domain.Entities.Shared;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
using Wino.Core.Domain.Enums;
|
|
|
|
|
|
using Wino.Core.Domain.Exceptions;
|
|
|
|
|
|
using Wino.Core.Domain.Interfaces;
|
|
|
|
|
|
using Wino.Core.Extensions;
|
|
|
|
|
|
using Wino.Core.Services;
|
|
|
|
|
|
|
2024-11-10 23:28:25 +01:00
|
|
|
|
namespace Wino.Core.Authenticators.Mail
|
2024-04-18 01:44:37 +02:00
|
|
|
|
{
|
2024-08-05 00:36:26 +02:00
|
|
|
|
/// <summary>
|
2024-11-10 23:28:25 +01:00
|
|
|
|
/// Authenticator for Outlook Mail provider.
|
2024-08-05 00:36:26 +02:00
|
|
|
|
/// Token cache is managed by MSAL, not by Wino.
|
|
|
|
|
|
/// </summary>
|
2024-11-10 23:28:25 +01:00
|
|
|
|
public class OutlookAuthenticator : OutlookAuthenticatorBase
|
2024-04-18 01:44:37 +02:00
|
|
|
|
{
|
2024-08-05 00:36:26 +02:00
|
|
|
|
private const string TokenCacheFileName = "OutlookCache.bin";
|
|
|
|
|
|
private bool isTokenCacheAttached = false;
|
|
|
|
|
|
|
2024-04-18 01:44:37 +02:00
|
|
|
|
// Outlook
|
|
|
|
|
|
private const string Authority = "https://login.microsoftonline.com/common";
|
|
|
|
|
|
|
2024-11-10 23:28:25 +01:00
|
|
|
|
public override string ClientId { get; } = "b19c2035-d740-49ff-b297-de6ec561b208";
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2024-08-26 22:09:00 +02:00
|
|
|
|
private readonly string[] MailScope =
|
|
|
|
|
|
[
|
|
|
|
|
|
"email",
|
|
|
|
|
|
"mail.readwrite",
|
|
|
|
|
|
"offline_access",
|
|
|
|
|
|
"mail.send",
|
|
|
|
|
|
"Mail.Send.Shared",
|
|
|
|
|
|
"Mail.ReadWrite.Shared",
|
|
|
|
|
|
"User.Read"
|
|
|
|
|
|
];
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
|
|
|
|
|
public override MailProviderType ProviderType => MailProviderType.Outlook;
|
|
|
|
|
|
|
|
|
|
|
|
private readonly IPublicClientApplication _publicClientApplication;
|
2024-08-05 00:36:26 +02:00
|
|
|
|
private readonly IApplicationConfiguration _applicationConfiguration;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2024-08-05 00:36:26 +02:00
|
|
|
|
public OutlookAuthenticator(ITokenService tokenService,
|
|
|
|
|
|
INativeAppService nativeAppService,
|
|
|
|
|
|
IApplicationConfiguration applicationConfiguration) : base(tokenService)
|
2024-04-18 01:44:37 +02:00
|
|
|
|
{
|
2024-08-05 00:36:26 +02:00
|
|
|
|
_applicationConfiguration = applicationConfiguration;
|
|
|
|
|
|
|
2024-04-18 01:44:37 +02:00
|
|
|
|
var authenticationRedirectUri = nativeAppService.GetWebAuthenticationBrokerUri();
|
|
|
|
|
|
|
2024-08-05 00:36:26 +02:00
|
|
|
|
var options = new BrokerOptions(BrokerOptions.OperatingSystems.Windows)
|
|
|
|
|
|
{
|
|
|
|
|
|
Title = "Wino Mail",
|
|
|
|
|
|
ListOperatingSystemAccounts = true,
|
|
|
|
|
|
};
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2024-08-05 00:36:26 +02:00
|
|
|
|
var outlookAppBuilder = PublicClientApplicationBuilder.Create(ClientId)
|
|
|
|
|
|
.WithParentActivityOrWindow(nativeAppService.GetCoreWindowHwnd)
|
|
|
|
|
|
.WithBroker(options)
|
|
|
|
|
|
.WithDefaultRedirectUri()
|
|
|
|
|
|
.WithAuthority(Authority);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2024-08-05 00:36:26 +02:00
|
|
|
|
_publicClientApplication = outlookAppBuilder.Build();
|
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2024-11-10 23:28:25 +01:00
|
|
|
|
public override async Task<TokenInformation> GetTokenAsync(MailAccount account)
|
2024-04-18 01:44:37 +02:00
|
|
|
|
{
|
2024-08-05 00:36:26 +02:00
|
|
|
|
if (!isTokenCacheAttached)
|
2024-04-18 01:44:37 +02:00
|
|
|
|
{
|
2024-08-05 00:36:26 +02:00
|
|
|
|
var storageProperties = new StorageCreationPropertiesBuilder(TokenCacheFileName, _applicationConfiguration.PublisherSharedFolderPath).Build();
|
|
|
|
|
|
var msalcachehelper = await MsalCacheHelper.CreateAsync(storageProperties);
|
|
|
|
|
|
msalcachehelper.RegisterCache(_publicClientApplication.UserTokenCache);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2024-08-05 00:36:26 +02:00
|
|
|
|
isTokenCacheAttached = true;
|
|
|
|
|
|
}
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2024-08-05 00:36:26 +02:00
|
|
|
|
var storedAccount = (await _publicClientApplication.GetAccountsAsync()).FirstOrDefault(a => a.Username == account.Address);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2024-08-05 00:36:26 +02:00
|
|
|
|
// TODO: Handle it from the server.
|
|
|
|
|
|
if (storedAccount == null) throw new AuthenticationAttentionException(account);
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2024-08-05 00:36:26 +02:00
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var authResult = await _publicClientApplication.AcquireTokenSilent(MailScope, storedAccount).ExecuteAsync();
|
2024-04-18 01:44:37 +02:00
|
|
|
|
|
2024-08-05 00:36:26 +02:00
|
|
|
|
return authResult.CreateTokenInformation() ?? throw new Exception("Failed to get Outlook token.");
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (MsalUiRequiredException)
|
|
|
|
|
|
{
|
|
|
|
|
|
// Somehow MSAL is not able to refresh the token silently.
|
|
|
|
|
|
// Force interactive login.
|
|
|
|
|
|
return await GenerateTokenAsync(account, true);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (Exception)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw;
|
2024-04-18 01:44:37 +02:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2024-11-10 23:28:25 +01:00
|
|
|
|
public override async Task<TokenInformation> GenerateTokenAsync(MailAccount account, bool saveToken)
|
2024-04-18 01:44:37 +02:00
|
|
|
|
{
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var authResult = await _publicClientApplication
|
|
|
|
|
|
.AcquireTokenInteractive(MailScope)
|
|
|
|
|
|
.ExecuteAsync();
|
|
|
|
|
|
|
|
|
|
|
|
var tokenInformation = authResult.CreateTokenInformation();
|
|
|
|
|
|
|
|
|
|
|
|
if (saveToken)
|
|
|
|
|
|
{
|
|
|
|
|
|
await SaveTokenInternalAsync(account, tokenInformation);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return tokenInformation;
|
|
|
|
|
|
}
|
|
|
|
|
|
catch (MsalClientException msalClientException)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (msalClientException.ErrorCode == "authentication_canceled" || msalClientException.ErrorCode == "access_denied")
|
|
|
|
|
|
throw new AccountSetupCanceledException();
|
|
|
|
|
|
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
throw new AuthenticationException(Translator.Exception_UnknowErrorDuringAuthentication, new Exception(Translator.Exception_TokenGenerationFailed));
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|