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

chore(functions): Tokenprovider updated #609

Merged
merged 3 commits into from
Dec 13, 2023
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -274,7 +273,7 @@
}

private static AdaptiveCard AddColumnsAndTextToAdaptiveCard(AdaptiveCard adaptiveCard, string customtext1,
string? customtext2, string value)

Check warning on line 276 in src/backend/function/Fusion.Resources.Functions/Functions/Notifications/ScheduledReportContentBuilderFunction.cs

View workflow job for this annotation

GitHub Actions / build

The annotation for nullable reference types should only be used in code within a '#nullable' annotations context.
{
var container = new AdaptiveContainer();
container.Separator = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
<ItemGroup>
<PackageReference Include="AdaptiveCards" Version="3.0.0" />
<PackageReference Include="Fusion.ApiClients.Org" Version="7.0.0" />
<PackageReference Include="Fusion.Integration" Version="6.0.0" />
<PackageReference Include="Microsoft.Azure.Cosmos.Table" Version="1.0.8" />
<PackageReference Include="Microsoft.NET.Sdk.Functions" Version="4.1.3" />
<PackageReference Include="Microsoft.Azure.Functions.Extensions" Version="1.1.0" />

<PackageReference Include="Fusion.Events.Azure.Functions.Extensions" Version="0.3.2" />
<PackageReference Include="Fusion.Events.Services" Version="0.5.0" />
<PackageReference Include="Fusion.Events.Azure.Functions.Extensions" Version="6.0.2" />
<PackageReference Include="Fusion.Events.Services" Version="8.0.0" />
<PackageReference Include="Microsoft.Azure.WebJobs.Extensions.ServiceBus" Version="5.8.1" />
<PackageReference Include="Microsoft.Extensions.Http" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Http.Polly" Version="6.0.13" />
Expand Down
Original file line number Diff line number Diff line change
@@ -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<AuthOptions> 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<AuthOptions> optionsAccessor)
{
var options = optionsAccessor.Value;

authority = $"https://login.microsoftonline.com/{options.TenantId}";
clientid = options.ClientId;
secret = options.Secret;

appTokenCache = new TokenCache();
}

public async Task<string> GetAppAccessToken()
{
var authContext = new AuthenticationContext(authority, appTokenCache);
var authenticationResult = await authContext.AcquireTokenAsync(clientid, Credentials);
var options = optionsAccessor.Value;

return authenticationResult.AccessToken;
}

public async Task<string> 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<string> GetAppAccessToken()
{
var scopes = new string[] { $"{_app.AppConfig.ClientId}/.default" };
var clientToken = await _app.AcquireTokenForClient(scopes).ExecuteAsync();

return clientToken.AccessToken;
}

public async Task<string> GetAppAccessToken(string resource)
{
var scopes = new string[] { $"{resource}/.default" };
var clientToken = await _app.AcquireTokenForClient(scopes).ExecuteAsync();

return clientToken.AccessToken;
}

}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
Loading