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

@@ -1,25 +1,29 @@
using System;
using System.Net.Http;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using Google.Apis.Http;
using Wino.Core.Domain.Entities.Shared;
using Wino.Core.Domain.Interfaces;
namespace Wino.Core.Http
{
internal class GmailClientMessageHandler : ConfigurableMessageHandler
{
public Func<Task<TokenInformation>> TokenRetrieveDelegate { get; }
private readonly IGmailAuthenticator _gmailAuthenticator;
private readonly MailAccount _mailAccount;
public GmailClientMessageHandler(Func<Task<TokenInformation>> tokenRetrieveDelegate) : base(new HttpClientHandler())
public GmailClientMessageHandler(IGmailAuthenticator gmailAuthenticator, MailAccount mailAccount) : base(new HttpClientHandler())
{
TokenRetrieveDelegate = tokenRetrieveDelegate;
_gmailAuthenticator = gmailAuthenticator;
_mailAccount = mailAccount;
}
protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
var tokenizationTask = TokenRetrieveDelegate.Invoke();
var tokenInformation = await tokenizationTask;
// This call here will automatically trigger Google Auth's interactive login if the token is not found.
// or refresh the token based on the FileDataStore.
var tokenInformation = await _gmailAuthenticator.GetTokenInformationAsync(_mailAccount);
request.Headers.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", tokenInformation.AccessToken);

View File

@@ -22,12 +22,12 @@ namespace Wino.Core.Http
public AllowedHostsValidator AllowedHostsValidator { get; }
public async Task<string> GetAuthorizationTokenAsync(Uri uri,
Dictionary<string, object> additionalAuthenticationContext = null,
CancellationToken cancellationToken = default)
Dictionary<string, object> additionalAuthenticationContext = null,
CancellationToken cancellationToken = default)
{
var token = await _authenticator.GetTokenAsync(_account).ConfigureAwait(false);
var tokenInfo = await _authenticator.GetTokenInformationAsync(_account);
return token?.AccessToken;
return tokenInfo.AccessToken;
}
}
}