35 lines
1.7 KiB
C#
35 lines
1.7 KiB
C#
using DigitalData.MessagingService.Application.Common.Interfaces;
|
|
using DigitalData.MessagingService.Domain.Entities;
|
|
using DigitalData.MessagingService.Domain.Enums;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
|
|
namespace DigitalData.MessagingService.Infrastructure.Services;
|
|
|
|
/// <summary>
|
|
/// Routes OAuth2 token requests to the correct provider-specific implementation
|
|
/// based on <see cref="EmailAccount.OAuth2Provider"/>.
|
|
/// Registered as the single <see cref="IOAuth2TokenService"/> in DI — all other
|
|
/// services depend on this dispatcher rather than on a concrete provider directly.
|
|
/// </summary>
|
|
public class OAuth2TokenServiceDispatcher(IServiceProvider ServiceProvider) : IOAuth2TokenService
|
|
{
|
|
public Task<string> GetAccessTokenAsync(EmailAccount account, CancellationToken cancellationToken = default)
|
|
{
|
|
var service = account.OAuth2Provider switch
|
|
{
|
|
OAuth2Provider.Microsoft => ServiceProvider.GetRequiredService<MicrosoftOAuth2TokenService>(),
|
|
OAuth2Provider.Google => (IOAuth2TokenService)ServiceProvider.GetRequiredService<GoogleOAuth2TokenService>(),
|
|
|
|
OAuth2Provider.None => throw new InvalidOperationException(
|
|
$"Account '{account.Username}' (Id: {account.Id}) has OAuth2Provider = None. " +
|
|
"Set UseOAuth2 = false or configure a valid OAuth2Provider."),
|
|
|
|
_ => throw new NotSupportedException(
|
|
$"OAuth2Provider '{account.OAuth2Provider}' is not supported. " +
|
|
$"Supported providers: {string.Join(", ", Enum.GetNames<OAuth2Provider>())}")
|
|
};
|
|
|
|
return service.GetAccessTokenAsync(account, cancellationToken);
|
|
}
|
|
}
|