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

@@ -206,8 +206,9 @@ namespace Wino.Core.Services
var authenticator = _authenticationProvider.GetAuthenticator(account.ProviderType);
// This will re-generate token.
var token = await authenticator.GenerateTokenAsync(account, true);
var token = await authenticator.GenerateTokenInformationAsync(account);
// TODO: Rest?
Guard.IsNotNull(token);
}
@@ -267,10 +268,10 @@ namespace Wino.Core.Services
public async Task DeleteAccountAsync(MailAccount account)
{
// TODO: Delete mime messages and attachments.
// TODO: Delete token cache by underlying provider.
await Connection.ExecuteAsync("DELETE FROM MailCopy WHERE Id IN(SELECT Id FROM MailCopy WHERE FolderId IN (SELECT Id from MailItemFolder WHERE MailAccountId == ?))", account.Id);
await Connection.Table<TokenInformation>().Where(a => a.AccountId == account.Id).DeleteAsync();
await Connection.Table<MailItemFolder>().DeleteAsync(a => a.MailAccountId == account.Id);
await Connection.Table<AccountSignature>().DeleteAsync(a => a.MailAccountId == account.Id);
await Connection.Table<MailAccountAlias>().DeleteAsync(a => a.AccountId == account.Id);
@@ -333,6 +334,10 @@ namespace Wino.Core.Services
account.SenderName = profileInformation.SenderName;
account.Base64ProfilePictureData = profileInformation.Base64ProfilePictureData;
if (string.IsNullOrEmpty(account.Address))
{
account.Address = profileInformation.AccountAddress;
}
// Forcefully add or update a contact data with the provided information.
var accountContact = new AccountContact()
@@ -469,7 +474,7 @@ namespace Wino.Core.Services
await Connection.ExecuteAsync(query.GetRawQuery()).ConfigureAwait(false);
}
public async Task CreateAccountAsync(MailAccount account, TokenInformation tokenInformation, CustomServerInformation customServerInformation)
public async Task CreateAccountAsync(MailAccount account, CustomServerInformation customServerInformation)
{
Guard.IsNotNull(account);
@@ -518,12 +523,6 @@ namespace Wino.Core.Services
if (customServerInformation != null)
await Connection.InsertAsync(customServerInformation);
// Outlook token cache is managed by MSAL.
// Don't save it to database.
if (tokenInformation != null && (account.ProviderType != MailProviderType.Outlook || account.ProviderType == MailProviderType.Office365))
await Connection.InsertAsync(tokenInformation);
}
public async Task<string> UpdateSynchronizationIdentifierAsync(Guid accountId, string newIdentifier)

View File

@@ -1,5 +1,5 @@
using System;
using Wino.Core.Authenticators.Mail;
using Wino.Authentication;
using Wino.Core.Domain;
using Wino.Core.Domain.Enums;
using Wino.Core.Domain.Interfaces;
@@ -10,14 +10,16 @@ namespace Wino.Core.Services
public class AuthenticationProvider : IAuthenticationProvider
{
private readonly INativeAppService _nativeAppService;
private readonly ITokenService _tokenService;
private readonly IApplicationConfiguration _applicationConfiguration;
private readonly IAuthenticatorConfig _authenticatorConfig;
public AuthenticationProvider(INativeAppService nativeAppService, ITokenService tokenService, IApplicationConfiguration applicationConfiguration)
public AuthenticationProvider(INativeAppService nativeAppService,
IApplicationConfiguration applicationConfiguration,
IAuthenticatorConfig authenticatorConfig)
{
_nativeAppService = nativeAppService;
_tokenService = tokenService;
_applicationConfiguration = applicationConfiguration;
_authenticatorConfig = authenticatorConfig;
}
public IAuthenticator GetAuthenticator(MailProviderType providerType)
@@ -25,9 +27,9 @@ namespace Wino.Core.Services
// TODO: Move DI
return providerType switch
{
MailProviderType.Outlook => new OutlookAuthenticator(_tokenService, _nativeAppService, _applicationConfiguration),
MailProviderType.Office365 => new Office365Authenticator(_tokenService, _nativeAppService, _applicationConfiguration),
MailProviderType.Gmail => new GmailAuthenticator(_tokenService, _nativeAppService),
MailProviderType.Outlook => new OutlookAuthenticator(_nativeAppService, _applicationConfiguration, _authenticatorConfig),
MailProviderType.Office365 => new Office365Authenticator(_nativeAppService, _applicationConfiguration, _authenticatorConfig),
MailProviderType.Gmail => new GmailAuthenticator(_authenticatorConfig),
_ => throw new ArgumentException(Translator.Exception_UnsupportedProvider),
};
}

View File

@@ -1,5 +1,4 @@
using System;
using System.IO;
using System.IO;
using System.Threading.Tasks;
using SQLite;
using Wino.Core.Domain.Entities.Mail;
@@ -35,16 +34,7 @@ namespace Wino.Core.Services
var publisherCacheFolder = _folderConfiguration.PublisherSharedFolderPath;
var databaseFileName = Path.Combine(publisherCacheFolder, DatabaseName);
Connection = new SQLiteAsyncConnection(databaseFileName)
{
// Enable for debugging sqlite.
Trace = true,
Tracer = new Action<string>((t) =>
{
// Debug.WriteLine(t);
// Log.Debug(t);
})
};
Connection = new SQLiteAsyncConnection(databaseFileName);
await CreateTablesAsync();
@@ -57,7 +47,6 @@ namespace Wino.Core.Services
typeof(MailCopy),
typeof(MailItemFolder),
typeof(MailAccount),
typeof(TokenInformation),
typeof(AccountContact),
typeof(CustomServerInformation),
typeof(AccountSignature),

View File

@@ -1,31 +0,0 @@
using System;
using System.Threading.Tasks;
using Wino.Core.Domain.Entities.Shared;
namespace Wino.Core.Services
{
public interface ITokenService
{
Task<TokenInformation> GetTokenInformationAsync(Guid accountId);
Task SaveTokenInformationAsync(Guid accountId, TokenInformation tokenInformation);
}
public class TokenService : BaseDatabaseService, ITokenService
{
public TokenService(IDatabaseService databaseService) : base(databaseService) { }
public Task<TokenInformation> GetTokenInformationAsync(Guid accountId)
=> Connection.Table<TokenInformation>().FirstOrDefaultAsync(a => a.AccountId == accountId);
public async Task SaveTokenInformationAsync(Guid accountId, TokenInformation tokenInformation)
{
// Delete all tokens for this account.
await Connection.Table<TokenInformation>().DeleteAsync(a => a.AccountId == accountId);
// Save new token info to the account.
tokenInformation.AccountId = accountId;
await Connection.InsertOrReplaceAsync(tokenInformation);
}
}
}