Skip to content

Commit

Permalink
CON-4835-EAS calls Provider Registrations API using MI credentials
Browse files Browse the repository at this point in the history
  • Loading branch information
VasanthaKasirajan3008 committed May 25, 2022
1 parent 2cbb6f6 commit 4a5ef23
Show file tree
Hide file tree
Showing 9 changed files with 144 additions and 58 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ public static IContainer Initialize()
c.AddRegistry<CommitmentsV2ApiClientRegistry>();
c.AddRegistry<PollyPolicyRegistry>();
c.AddRegistry<ContentApiClientRegistry>();
c.AddRegistry<ProviderRegistrationApiClientRegistry>();
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,42 +1,25 @@
using System.Threading.Tasks;
using MediatR;
using SFA.DAS.EmployerAccounts.Configuration;
using SFA.DAS.EmployerAccounts.Interfaces;
using SFA.DAS.NLog.Logger;

namespace SFA.DAS.EmployerAccounts.Commands.UnsubscribeProviderEmail
{
public class UnsubscribeProviderEmailQueryHandler : AsyncRequestHandler<UnsubscribeProviderEmailQuery>
{
private readonly EmployerAccountsConfiguration _configuration;
private readonly IHttpService _httpService;
{
private readonly IProviderRegistrationApiClient _providerRegistrationApiClient;
private readonly ILog _logger;

public UnsubscribeProviderEmailQueryHandler(
IHttpServiceFactory httpServiceFactory,
EmployerAccountsConfiguration configuration)
{
_configuration = configuration;
_httpService = httpServiceFactory.Create(
configuration.ProviderRegistrationsApi.ClientId,
configuration.ProviderRegistrationsApi.ClientSecret,
configuration.ProviderRegistrationsApi.IdentifierUri,
configuration.ProviderRegistrationsApi.Tenant
);
public UnsubscribeProviderEmailQueryHandler(IProviderRegistrationApiClient providerRegistrationApiClient, ILog logger)
{
_providerRegistrationApiClient = providerRegistrationApiClient;
_logger = logger;
}

protected override async Task HandleCore(UnsubscribeProviderEmailQuery message)
{
var baseUrl = GetBaseUrl();
var url = $"{baseUrl}api/unsubscribe/{message.CorrelationId.ToString()}";
await _httpService.GetAsync(url, false);
}

private string GetBaseUrl()
{
var baseUrl = _configuration.ProviderRegistrationsApi.BaseUrl.EndsWith("/")
? _configuration.ProviderRegistrationsApi.BaseUrl
: _configuration.ProviderRegistrationsApi.BaseUrl + "/";

return baseUrl;
}
await _providerRegistrationApiClient.Unsubscribe(message.CorrelationId.ToString());
_logger.Info($"Sent ProviderEmail to Unsubscribe {message.CorrelationId}");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ public class EmployerAccountsConfiguration : ITopicMessagePublisherConfiguration
public EventsApiClientConfiguration EventsApi { get; set; }
public string Hashstring { get; set; }
public HmrcConfiguration Hmrc { get; set; }
public PensionRegulatorConfiguration PensionRegulatorApi { get; set; }
public ProviderRegistrationsConfiguration ProviderRegistrationsApi { get; set; }
public PensionRegulatorConfiguration PensionRegulatorApi { get; set; }
public ProviderRegistrationClientApiConfiguration ProviderRegistrationsApi { get; set; }
public IdentityServerConfiguration Identity { get; set; }
public string LegacyServiceBusConnectionString { get; set; }
public string MessageServiceBusConnectionString => LegacyServiceBusConnectionString;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using SFA.DAS.EmployerAccounts.Interfaces;

namespace SFA.DAS.EmployerAccounts.Configuration
{
public class ProviderRegistrationClientApiConfiguration : IProviderRegistrationClientApiConfiguration
{
public string BaseUrl { get; set; }
public string IdentifierUri { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using System.Net.Http;
using SFA.DAS.EmployerAccounts.Configuration;
using SFA.DAS.EmployerAccounts.Interfaces;
using SFA.DAS.EmployerAccounts.Services;
using SFA.DAS.Http;
using SFA.DAS.NLog.Logger.Web.MessageHandlers;
using StructureMap;

namespace SFA.DAS.EmployerAccounts.DependencyResolution
{
public class ProviderRegistrationApiClientRegistry : Registry
{
public ProviderRegistrationApiClientRegistry()
{
For<ProviderRegistrationClientApiConfiguration>().Use(c => c.GetInstance<EmployerAccountsConfiguration>().ProviderRegistrationsApi);
For<IProviderRegistrationClientApiConfiguration>().Use(c => c.GetInstance<ProviderRegistrationClientApiConfiguration>());
For<IProviderRegistrationApiClient>().Use<ProviderRegistrationApiClient>()
.Ctor<HttpClient>().Is(c => CreateClient(c));
}

private HttpClient CreateClient(IContext context)
{
HttpClient httpClient = new HttpClientBuilder()
.WithHandler(new RequestIdMessageRequestHandler())
.WithHandler(new SessionIdMessageRequestHandler())
.WithDefaultHeaders()
.Build();


return httpClient;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Threading.Tasks;

namespace SFA.DAS.EmployerAccounts.Interfaces
{
public interface IProviderRegistrationApiClient
{
Task Unsubscribe(string CorrelationId);

Task<string> GetInvitations(string CorrelationId);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@

namespace SFA.DAS.EmployerAccounts.Interfaces
{
public interface IProviderRegistrationClientApiConfiguration
{
string BaseUrl { get; }
string IdentifierUri { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,49 +1,31 @@
using System.Threading.Tasks;
using MediatR;
using SFA.DAS.EmployerAccounts.Configuration;
using SFA.DAS.EmployerAccounts.Interfaces;
using SFA.DAS.EmployerAccounts.Models;
using Newtonsoft.Json;
using SFA.DAS.EmployerAccounts.Interfaces;
using SFA.DAS.NLog.Logger;

namespace SFA.DAS.EmployerAccounts.Queries.GetProviderInvitation
{
public class GetProviderInvitationQueryHandler : IAsyncRequestHandler<GetProviderInvitationQuery, GetProviderInvitationResponse>
{
private readonly EmployerAccountsConfiguration _configuration;
private readonly IHttpService _httpService;
{
private readonly IProviderRegistrationApiClient _providerRegistrationApiClient;
private readonly ILog _logger;

public GetProviderInvitationQueryHandler(
IHttpServiceFactory httpServiceFactory,
EmployerAccountsConfiguration configuration)
public GetProviderInvitationQueryHandler(IProviderRegistrationApiClient providerRegistrationApiClient, ILog logger)
{
_configuration = configuration;
_httpService = httpServiceFactory.Create(
configuration.ProviderRegistrationsApi.ClientId,
configuration.ProviderRegistrationsApi.ClientSecret,
configuration.ProviderRegistrationsApi.IdentifierUri,
configuration.ProviderRegistrationsApi.Tenant
);
_providerRegistrationApiClient = providerRegistrationApiClient;
_logger = logger;
}

public async Task<GetProviderInvitationResponse> Handle(GetProviderInvitationQuery message)
{
var baseUrl = GetBaseUrl();
var url = $"{baseUrl}api/invitations/{message.CorrelationId.ToString()}";
var json = await _httpService.GetAsync(url, false);

var json = await _providerRegistrationApiClient.GetInvitations(message.CorrelationId.ToString());
_logger.Info($"Get Invitations for {message.CorrelationId}");
return new GetProviderInvitationResponse
{
Result = json == null ? null : JsonConvert.DeserializeObject<ProviderInvitation>(json)
};
}

private string GetBaseUrl()
{
var baseUrl = _configuration.ProviderRegistrationsApi.BaseUrl.EndsWith("/")
? _configuration.ProviderRegistrationsApi.BaseUrl
: _configuration.ProviderRegistrationsApi.BaseUrl + "/";

return baseUrl;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
using System.Configuration;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Microsoft.Azure.Services.AppAuthentication;
using SFA.DAS.Authentication.Extensions.Legacy;
using SFA.DAS.EmployerAccounts.Interfaces;

namespace SFA.DAS.EmployerAccounts.Services
{
public class ProviderRegistrationApiClient : ApiClientBase, IProviderRegistrationApiClient
{
private readonly string _apiBaseUrl;
private readonly string _identifierUri;
private readonly HttpClient _client;

public ProviderRegistrationApiClient(HttpClient client, IProviderRegistrationClientApiConfiguration configuration) : base(client)
{
_apiBaseUrl = configuration.BaseUrl.EndsWith("/")
? configuration.BaseUrl
: configuration.BaseUrl + "/";

_identifierUri = configuration.IdentifierUri;
_client = client;
}

public async Task Unsubscribe(string CorrelationId)
{
await AddAuthenticationHeader();

var url = $"{_apiBaseUrl}api/unsubscribe/{CorrelationId}";
await _client.GetAsync(url);
}

public async Task<string> GetInvitations(string CorrelationId)
{
await AddAuthenticationHeader();

var url = $"{_apiBaseUrl}api/invitations/{CorrelationId}";
var response = await _client.GetAsync(url);
response.EnsureSuccessStatusCode();

return await response.Content.ReadAsStringAsync().ConfigureAwait(false);
}

private async Task AddAuthenticationHeader()
{
if (ConfigurationManager.AppSettings["EnvironmentName"].ToUpper() != "LOCAL")
{
var azureServiceTokenProvider = new AzureServiceTokenProvider();
var accessToken = await azureServiceTokenProvider.GetAccessTokenAsync(_identifierUri);

_client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
}
}
}
}

0 comments on commit 4a5ef23

Please sign in to comment.