Abstraction of authenticators. Reworked Gmail authentication.

This commit is contained in:
Burak Kaan Köse
2024-11-20 01:45:48 +01:00
parent 8367efa174
commit 7fad15524f
44 changed files with 354 additions and 605 deletions

View File

@@ -83,6 +83,19 @@ namespace Wino.Server
services.AddSingleton<IServerMessageHandlerFactory>(serverMessageHandlerFactory);
// Server type related services.
// TODO: Better abstraction.
if (WinoServerType == WinoAppType.Mail)
{
services.AddSingleton<IAuthenticatorConfig, MailAuthenticatorConfiguration>();
}
else
{
// TODO: Calendar config will be added here.
}
return services.BuildServiceProvider();
}
@@ -135,6 +148,8 @@ namespace Wino.Server
{
string processName = WinoServerType == WinoAppType.Mail ? "Wino.Mail" : "Wino.Calendar";
var processs = Process.GetProcesses();
var proc = Process.GetProcessesByName(processName).FirstOrDefault() ?? throw new Exception($"{processName} client is not running.");
for (IntPtr appWindow = FindWindowEx(IntPtr.Zero, IntPtr.Zero, FRAME_WINDOW, null); appWindow != IntPtr.Zero;

View File

@@ -1,27 +1,27 @@
using System;
using System.Threading;
using System.Threading.Tasks;
using Wino.Core.Domain.Entities.Shared;
using Wino.Core.Domain.Interfaces;
using Wino.Core.Domain.Models.Authentication;
using Wino.Core.Domain.Models.Server;
using Wino.Messaging.Server;
using Wino.Server.Core;
namespace Wino.Server.MessageHandlers
{
public class AuthenticationHandler : ServerMessageHandler<AuthorizationRequested, TokenInformation>
public class AuthenticationHandler : ServerMessageHandler<AuthorizationRequested, TokenInformationEx>
{
private readonly IAuthenticationProvider _authenticationProvider;
public override WinoServerResponse<TokenInformation> FailureDefaultResponse(Exception ex)
=> WinoServerResponse<TokenInformation>.CreateErrorResponse(ex.Message);
public override WinoServerResponse<TokenInformationEx> FailureDefaultResponse(Exception ex)
=> WinoServerResponse<TokenInformationEx>.CreateErrorResponse(ex.Message);
public AuthenticationHandler(IAuthenticationProvider authenticationProvider)
{
_authenticationProvider = authenticationProvider;
}
protected override async Task<WinoServerResponse<TokenInformation>> HandleAsync(AuthorizationRequested message,
protected override async Task<WinoServerResponse<TokenInformationEx>> HandleAsync(AuthorizationRequested message,
CancellationToken cancellationToken = default)
{
var authenticator = _authenticationProvider.GetAuthenticator(message.MailProviderType);
@@ -36,10 +36,21 @@ namespace Wino.Server.MessageHandlers
gmailAuthenticator.ProposeCopyAuthURL = true;
}
// Do not save the token here. Call is coming from account creation and things are atomic there.
var generatedToken = await authenticator.GenerateTokenAsync(message.CreatedAccount, saveToken: false);
TokenInformationEx generatedToken = null;
return WinoServerResponse<TokenInformation>.CreateSuccessResponse(generatedToken);
if (message.CreatedAccount != null)
{
generatedToken = await authenticator.GetTokenInformationAsync(message.CreatedAccount);
}
else
{
// Initial authentication request.
// There is no account to get token for.
generatedToken = await authenticator.GenerateTokenInformationAsync(message.CreatedAccount);
}
return WinoServerResponse<TokenInformationEx>.CreateSuccessResponse(generatedToken);
}
}
}

View File

@@ -26,22 +26,7 @@ namespace Wino.Server
[RelayCommand]
public Task LaunchWinoAsync()
{
//var opt = new SynchronizationOptions()
//{
// Type = Wino.Core.Domain.Enums.SynchronizationType.Full,
// AccountId = Guid.Parse("b3620ce7-8a69-4d81-83d5-a94bbe177431")
//};
//var req = new NewSynchronizationRequested(opt, Wino.Core.Domain.Enums.SynchronizationSource.Server);
//WeakReferenceMessenger.Default.Send(req);
// return Task.CompletedTask;
return Launcher.LaunchUriAsync(new Uri($"{App.WinoMailLaunchProtocol}:")).AsTask();
//await _notificationBuilder.CreateNotificationsAsync(Guid.Empty, new List<IMailItem>()
//{
// new MailCopy(){ UniqueId = Guid.Parse("8f25d2a0-4448-4fee-96a9-c9b25a19e866")}
//});
}
/// <summary>

View File

@@ -18,6 +18,7 @@
<None Remove="Images\Wino_Icon.ico" />
</ItemGroup>
<ItemGroup>
<Compile Include="..\Wino.Mail\Services\MailAuthenticatorConfiguration.cs" Link="Services\MailAuthenticatorConfiguration.cs" />
<Compile Include="..\Wino.Core.UWP\Services\ConfigurationService.cs" Link="Services\ConfigurationService.cs" />
<Compile Include="..\Wino.Core.UWP\Services\NativeAppService.cs" Link="Services\NativeAppService.cs" />
<Compile Include="..\Wino.Core.UWP\Services\PreferencesService.cs" Link="Services\PreferencesService.cs" />
@@ -36,6 +37,7 @@
<PackageReference Include="System.Text.Encoding.CodePages" Version="8.0.0" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Wino.Authentication\Wino.Authentication.csproj" />
<ProjectReference Include="..\Wino.Core.Domain\Wino.Core.Domain.csproj" />
<ProjectReference Include="..\Wino.Core\Wino.Core.csproj" />
<ProjectReference Include="..\Wino.Messages\Wino.Messaging.csproj" />