Skip to content

Commit

Permalink
chore(functions): Tokenprovider updated (#609)
Browse files Browse the repository at this point in the history
- [ ] ~~New feature~~
- [ ] ~~Bug fix~~
- [x] High impact

**Description of work:**
The token-provider prevented the team for updating packages due to
breaking changes in the packages used by the token-provider.

**Testing:**
- [ ] ~~Can be tested~~
- [ ] ~~Automatic tests created / updated~~
- [ ] ~~Local tests are passing~~



**Checklist:**
- [ ] ~~Considered automated tests~~
- [ ] ~~Considered updating specification / documentation~~
- [ ] ~~Considered work items~~ 
- [ ] ~~Considered security~~
- [ ] ~~Performed developer testing~~
- [ ] ~~Checklist finalized / ready for review~~
  • Loading branch information
BouVid authored Dec 13, 2023
1 parent 0aa6cf3 commit aea8f9f
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 53 deletions.
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
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

0 comments on commit aea8f9f

Please sign in to comment.