-
Notifications
You must be signed in to change notification settings - Fork 0
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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; } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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. |
||
} |
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]); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
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?