Merge pull request #21616 from amanda-tarafa/token-with-extra-info

[csharp] Grpc.Auth supports Google.Apis.Auth.ITokenAccessWithHeaders
pull/23300/head
Jan Tattermusch 4 years ago committed by GitHub
commit a3eaeb6eac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 28
      src/csharp/Grpc.Auth/GoogleAuthInterceptors.cs
  2. 4
      src/csharp/Grpc.Auth/Grpc.Auth.csproj

@ -16,7 +16,6 @@
#endregion
using System;
using System.Threading;
using System.Threading.Tasks;
@ -43,6 +42,11 @@ namespace Grpc.Auth
/// <returns>The interceptor.</returns>
public static AsyncAuthInterceptor FromCredential(ITokenAccess credential)
{
if (credential is ITokenAccessWithHeaders credentialWithHeaders)
{
return FromCredential(credentialWithHeaders);
}
return new AsyncAuthInterceptor(async (context, metadata) =>
{
var accessToken = await credential.GetAccessTokenForRequestAsync(context.ServiceUrl, CancellationToken.None).ConfigureAwait(false);
@ -50,6 +54,28 @@ namespace Grpc.Auth
});
}
/// <summary>
/// Creates an <see cref="AsyncAuthInterceptor"/> that will obtain access token and associated information
/// from any credential type that implements <see cref="ITokenAccessWithHeaders"/>
/// </summary>
/// <param name="credential">The credential to use to obtain access tokens.</param>
/// <returns>The interceptor.</returns>
public static AsyncAuthInterceptor FromCredential(ITokenAccessWithHeaders credential)
{
return new AsyncAuthInterceptor(async (context, metadata) =>
{
AccessTokenWithHeaders tokenAndHeaders = await credential.GetAccessTokenWithHeadersForRequestAsync(context.ServiceUrl, CancellationToken.None).ConfigureAwait(false);
metadata.Add(CreateBearerTokenHeader(tokenAndHeaders.AccessToken));
foreach (var header in tokenAndHeaders.Headers)
{
foreach (var headerValue in header.Value)
{
metadata.Add(new Metadata.Entry(header.Key, headerValue));
}
}
});
}
/// <summary>
/// Creates an <see cref="AsyncAuthInterceptor"/> that will use given access token as authorization.
/// </summary>

@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\Grpc.Core\Common.csproj.include" />
@ -33,7 +33,7 @@
</ItemGroup>
<ItemGroup>
<PackageReference Include="Google.Apis.Auth" Version="1.21.0" />
<PackageReference Include="Google.Apis.Auth" Version="1.46.0" />
</ItemGroup>
<ItemGroup Condition=" '$(TargetFramework)' == 'net45' ">

Loading…
Cancel
Save