Skip to content

Commit

Permalink
fix(api): add authorization token for function request
Browse files Browse the repository at this point in the history
refs #273
  • Loading branch information
steveoh committed Dec 13, 2023
1 parent b39b50b commit dc689f8
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 0 deletions.
1 change: 1 addition & 0 deletions build/Packages.targets
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

<!-- Cloud -->
<PackageReference Update="Google.Cloud.Storage.V1" Version="4.7.0" />
<PackageReference Update="Google.Apis.Auth" Version ="1.64.0" />

<!-- Email -->
<PackageReference Update="SendGrid" Version="9.28.1" />
Expand Down
10 changes: 10 additions & 0 deletions src/api/Features/Inventories/InventoryCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Threading;
using System.Threading.Tasks;
using api.Infrastructure;
using Google.Apis.Auth.OAuth2;
using MediatR;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
Expand Down Expand Up @@ -251,8 +252,17 @@ public async Task<HttpContent> Handle(Command request, CancellationToken cancell

var payload = new ReportPayload(new InventoryPayload(site.Inventories.Single(), site), site.Contacts.Select(x => new ContactPayload(x)));

_log.Debug("Requesting Application Default Credentials");

var credential = await GoogleCredential.GetApplicationDefaultAsync(cancellationToken);
var accessToken = await credential.UnderlyingCredential.GetAccessTokenForRequestAsync(cancellationToken: cancellationToken);

_client.DefaultRequestHeaders.Authorization = new("Bearer", accessToken);

var response = await _client.PostAsJsonAsync(_functionUrl, payload, cancellationToken);

response.EnsureSuccessStatusCode();

return response.Content;
}
}
Expand Down
1 change: 1 addition & 0 deletions src/api/api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
<PackageReference Include="Serilog.Sinks.GoogleCloudLogging" />
<PackageReference Include="System.Text.Encoding.CodePages" />
<PackageReference Include="Google.Cloud.Storage.V1" />
<PackageReference Include="Google.Apis.Auth" />
<PackageReference Include="SendGrid" />
<PackageReference Include="SendGrid.Extensions.DependencyInjection" />
</ItemGroup>
Expand Down

0 comments on commit dc689f8

Please sign in to comment.