diff --git a/src/backend/function/Fusion.Resources.Functions/Functions/Notifications/ScheduledReportContentBuilderFunction.cs b/src/backend/function/Fusion.Resources.Functions/Functions/Notifications/ScheduledReportContentBuilderFunction.cs index 517d4a000..eb2c2bd5c 100644 --- a/src/backend/function/Fusion.Resources.Functions/Functions/Notifications/ScheduledReportContentBuilderFunction.cs +++ b/src/backend/function/Fusion.Resources.Functions/Functions/Notifications/ScheduledReportContentBuilderFunction.cs @@ -13,7 +13,6 @@ using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.ServiceBus; using Microsoft.Extensions.Logging; -using Microsoft.IdentityModel.Tokens; using Newtonsoft.Json; using static Fusion.Resources.Functions.ApiClients.IResourcesApiClient; diff --git a/src/backend/function/Fusion.Resources.Functions/Fusion.Resources.Functions.csproj b/src/backend/function/Fusion.Resources.Functions/Fusion.Resources.Functions.csproj index 13a555427..eb55b98d5 100644 --- a/src/backend/function/Fusion.Resources.Functions/Fusion.Resources.Functions.csproj +++ b/src/backend/function/Fusion.Resources.Functions/Fusion.Resources.Functions.csproj @@ -7,12 +7,13 @@ + - - + + diff --git a/src/backend/function/Fusion.Resources.Functions/Integration/Authentication/FunctionTokenProvider.cs b/src/backend/function/Fusion.Resources.Functions/Integration/Authentication/FunctionTokenProvider.cs index 726444791..3c49846e5 100644 --- a/src/backend/function/Fusion.Resources.Functions/Integration/Authentication/FunctionTokenProvider.cs +++ b/src/backend/function/Fusion.Resources.Functions/Integration/Authentication/FunctionTokenProvider.cs @@ -1,58 +1,36 @@ using Microsoft.Extensions.Options; -using Microsoft.IdentityModel.Clients.ActiveDirectory; using System.Threading.Tasks; +using Microsoft.Identity.Client; -namespace Fusion.Resources.Functions.Integration.Authentication +namespace Fusion.Resources.Functions.Integration.Authentication; + +internal class FunctionTokenProvider : ITokenProvider { + private readonly IConfidentialClientApplication _app; - internal class FunctionTokenProvider : ITokenProvider + public FunctionTokenProvider(IOptions optionsAccessor) { - private readonly string clientid; - private readonly string authority; - private readonly string secret; - private readonly TokenCache appTokenCache; - - static FunctionTokenProvider() - { - LoggerCallbackHandler.UseDefaultLogging = false; - } - - ClientCredential Credentials - { - get - { - return new ClientCredential(clientid, secret); - } - } - - public FunctionTokenProvider(IOptions optionsAccessor) - { - var options = optionsAccessor.Value; - - authority = $"https://login.microsoftonline.com/{options.TenantId}"; - clientid = options.ClientId; - secret = options.Secret; - - appTokenCache = new TokenCache(); - } - - public async Task GetAppAccessToken() - { - var authContext = new AuthenticationContext(authority, appTokenCache); - var authenticationResult = await authContext.AcquireTokenAsync(clientid, Credentials); + var options = optionsAccessor.Value; - return authenticationResult.AccessToken; - } - - public async Task GetAppAccessToken(string resource) - { - var authContext = new AuthenticationContext(authority, appTokenCache); - var authenticationResult = await authContext.AcquireTokenAsync(resource, Credentials); + _app = ConfidentialClientApplicationBuilder.Create(options.ClientId) + .WithClientSecret(options.Secret) + .WithAuthority(AzureCloudInstance.AzurePublic, options.TenantId) + .Build(); + } - return authenticationResult.AccessToken; - } + public async Task GetAppAccessToken() + { + var scopes = new string[] { $"{_app.AppConfig.ClientId}/.default" }; + var clientToken = await _app.AcquireTokenForClient(scopes).ExecuteAsync(); + + return clientToken.AccessToken; + } + public async Task GetAppAccessToken(string resource) + { + var scopes = new string[] { $"{resource}/.default" }; + var clientToken = await _app.AcquireTokenForClient(scopes).ExecuteAsync(); + return clientToken.AccessToken; } - -} +} \ No newline at end of file diff --git a/src/backend/function/Fusion.Resources.Functions/ServiceBus/QueueMessageProcessor.cs b/src/backend/function/Fusion.Resources.Functions/ServiceBus/QueueMessageProcessor.cs index 83280465c..5d2ae2abe 100644 --- a/src/backend/function/Fusion.Resources.Functions/ServiceBus/QueueMessageProcessor.cs +++ b/src/backend/function/Fusion.Resources.Functions/ServiceBus/QueueMessageProcessor.cs @@ -1,10 +1,7 @@ using System; using System.Text; -using System.Threading; using System.Threading.Tasks; using Azure.Messaging.ServiceBus; -using Microsoft.Azure.ServiceBus; -using Microsoft.Azure.ServiceBus.Core; using Microsoft.Azure.WebJobs; using Microsoft.Azure.WebJobs.ServiceBus; using Microsoft.Extensions.Logging;