Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CSP-1245 - Updated RoATP calls to use MI #998

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 2 additions & 11 deletions src/SFA.DAS.ApplyService.Configuration/RoatpApiAuthentication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,9 @@

namespace SFA.DAS.ApplyService.Configuration
{
public class RoatpApiAuthentication : IClientApiAuthentication
public class RoatpApiAuthentication
{
[JsonRequired] public string Instance { get; set; }

[JsonRequired] public string TenantId { get; set; }

[JsonRequired] public string ClientId { get; set; }

[JsonRequired] public string ClientSecret { get; set; }

[JsonRequired] public string ResourceId { get; set; }

[JsonRequired] public string ApiBaseAddress { get; set; }
[JsonRequired] public string Identifier { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public void Before_each_test()

_config.GetConfig().GetAwaiter().GetResult().RoatpApiAuthentication.ApiBaseAddress = RoatpApiBaseAddress;

_apiClient = new RoatpApiClient(httpClient, logger.Object, new RoatpTokenService(_config));
_apiClient = new RoatpApiClient(httpClient, logger.Object, new RoatpTokenService(hostingEnvironment.Object, _config));
}

[Ignore("Failed test")]
Expand All @@ -55,7 +55,7 @@ public void Client_retrieves_list_of_provider_types()
public void Client_returns_reapply_status_for_existing_UKPRN_that_is_active()
{
var existingUKPRN = 10001123;

var reapplyStatus = _apiClient.GetOrganisationRegisterStatus(existingUKPRN.ToString()).GetAwaiter().GetResult();

reapplyStatus.ProviderTypeId.Should().Be(ProviderType.MainProvider);
Expand All @@ -77,7 +77,7 @@ public void Client_returns_reapply_status_for_existing_UKPRN_that_was_removed()
public void Matching_UKPRN_returns_single_result()
{
var ukprn = "10001724";

var result = _apiClient.GetUkrlpDetails(ukprn).GetAwaiter().GetResult();

result.Should().NotBeNull();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
using System;
using System.Threading.Tasks;

namespace SFA.DAS.ApplyService.InternalApi.Infrastructure
{
public interface IRoatpTokenService
{
string GetToken(Uri baseUri);
Task<string> GetToken();
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
namespace SFA.DAS.ApplyService.InternalApi.Infrastructure
{
using Microsoft.Extensions.Logging;
using System.Collections.Generic;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using SFA.DAS.ApplyService.Domain.Roatp;
using SFA.DAS.ApplyService.Infrastructure.ApiClients;
using SFA.DAS.ApplyService.InternalApi.Models.Roatp;
using SFA.DAS.ApplyService.InternalApi.Models.Ukrlp;
using SFA.DAS.ApplyService.Infrastructure.ApiClients;
using System;

public class RoatpApiClient : ApiClientBase<RoatpApiClient>, IRoatpApiClient
{
public RoatpApiClient(HttpClient httpClient, ILogger<RoatpApiClient> logger, IRoatpTokenService tokenService) : base(httpClient, logger)
{
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenService.GetToken(httpClient.BaseAddress));
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", tokenService.GetToken().Result);
}

public async virtual Task<OrganisationRegisterStatus> GetOrganisationRegisterStatus(string ukprn)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,35 +1,34 @@
using System;
using System.Threading.Tasks;

namespace SFA.DAS.ApplyService.InternalApi.Infrastructure
{
using Configuration;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Azure.Services.AppAuthentication;
using Microsoft.Extensions.Hosting;

public class RoatpTokenService : IRoatpTokenService
{
private readonly IApplyConfig _configuration;
private readonly IWebHostEnvironment _hostingEnvironment;
private readonly IConfigurationService _configurationService;

public RoatpTokenService(IConfigurationService configurationService)
public RoatpTokenService(IWebHostEnvironment hostingEnvironment, IConfigurationService configurationService)
{
_configuration = configurationService.GetConfig().Result;
_hostingEnvironment = hostingEnvironment;
_configurationService = configurationService;
}

public string GetToken(Uri baseUri)
public async Task<string> GetToken()
{
if (baseUri != null && baseUri.IsLoopback)
if (_hostingEnvironment.IsDevelopment())
return string.Empty;

var tenantId = _configuration.RoatpApiAuthentication.TenantId;
var clientId = _configuration.RoatpApiAuthentication.ClientId;
var appKey = _configuration.RoatpApiAuthentication.ClientSecret;
var resourceId = _configuration.RoatpApiAuthentication.ResourceId;
var configuration = await _configurationService.GetConfig();

var authority = $"https://login.microsoftonline.com/{tenantId}";
var clientCredential = new ClientCredential(clientId, appKey);
var context = new AuthenticationContext(authority, true);
var result = context.AcquireTokenAsync(resourceId, clientCredential).Result;
var azureServiceTokenProvider = new AzureServiceTokenProvider();
var generatedToken = await azureServiceTokenProvider.GetAccessTokenAsync(configuration.RoatpApiAuthentication.Identifier);

return result.AccessToken;
return generatedToken;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
bool showErrors = Model.ErrorMessages != null && Model.ErrorMessages.Count > 0;
}

@if (Model.ApplicationId != null && Model.ApplicationId != Guid.Empty)
@if (Model.ApplicationId != Guid.Empty)
{
@section Navigation
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
bool showErrors = Model.ErrorMessages != null && Model.ErrorMessages.Count > 0;
}

@if (Model.ApplicationId != null && Model.ApplicationId != Guid.Empty)
@if (Model.ApplicationId != Guid.Empty)
{
@section Navigation
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
bool showErrors = Model.ErrorMessages != null && Model.ErrorMessages.Count > 0;
}

@if (Model.ApplicationId != null && Model.ApplicationId != Guid.Empty)
@if (Model.ApplicationId != Guid.Empty)
{
@section Navigation
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
Layout = "_Layout";

bool showErrors = TempData["ShowErrors"] as bool? ?? false;
int numberOfTitles = 3;
}

@section Navigation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@

var submitAction = "ProcessRoute";

if (Model.ApplicationId != null && Model.ApplicationId != Guid.Empty)
if (Model.ApplicationId != Guid.Empty)
{
submitAction = "UpdateApplicationProviderRoute";
}
}

@if (Model.ApplicationId != null && Model.ApplicationId != Guid.Empty)
@if (Model.ApplicationId != Guid.Empty)
{
@section Navigation
{
Expand Down
Loading