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

Feature/488068 auth common backend and accounts apis #37

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions src/EprPrnIntegration.Api/HostBuilderConfiguration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ private static void ConfigureServices(IConfiguration configuration, IServiceColl
services.AddTransient<NpwdOAuthMiddleware>();

// Add HttpClients
services.AddHttpClient(Common.Constants.HttpClientNames.Npwd)
.AddHttpMessageHandler<NpwdOAuthMiddleware>();
services.AddHttpClient(Common.Constants.HttpClientNames.Npwd).AddHttpMessageHandler<NpwdOAuthMiddleware>();
services.AddHttpClient(Common.Constants.HttpClientNames.Prn).AddHttpMessageHandler<PrnServiceAuthorisationHandler>();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You have named this client but where this has been used?


services.AddServiceBus(configuration);
services.ConfigureOptions(configuration);
Expand Down
1 change: 1 addition & 0 deletions src/EprPrnIntegration.Common/Configuration/Service.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ public class Service
public int? Retries { get; set; }
public string? PrnBaseUrl { get; set; }
public string? PrnEndPointName { get; set; }
public string? ClientId { get; set; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will need 2 client Id's one for Prn Service and one for account service.

}
2 changes: 2 additions & 0 deletions src/EprPrnIntegration.Common/Constants/Constants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
public static class HttpClientNames
{
public const string Npwd = "NpwdClient";
public const string Prn = "PrnClient";

}

public static class HttpHeaderNames
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
using System.Diagnostics.CodeAnalysis;
using System.Net.Http.Headers;
using Azure.Core;
using Azure.Identity;
using Microsoft.Extensions.Options;

namespace EprPrnIntegration.Common.Middleware;

[ExcludeFromCodeCoverage]
public class PrnServiceAuthorisationHandler : DelegatingHandler
{
private readonly TokenRequestContext _tokenRequestContext;
private readonly DefaultAzureCredential? _credentials;
Ehsan-Hatami marked this conversation as resolved.
Show resolved Hide resolved

public PrnServiceAuthorisationHandler(IOptions<Configuration.Service> config)
{
if (!string.IsNullOrEmpty(config.Value.ClientId))
{
_tokenRequestContext = new TokenRequestContext([config.Value.ClientId]);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since we have single handler for two service we need to get token for both services. We discussed this yesterday

_credentials = new DefaultAzureCredential();
}
}

protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
await AddDefaultToken(request, cancellationToken);
return await base.SendAsync(request, cancellationToken);
}

private async Task AddDefaultToken(HttpRequestMessage request, CancellationToken cancellationToken)
{
if (_credentials != null)
{
var tokenResult = await _credentials.GetTokenAsync(_tokenRequestContext, cancellationToken);
request.Headers.Authorization = new AuthenticationHeaderValue(Constants.HttpHeaderNames.Bearer, tokenResult.Token);
}
}
}