Support for ITokenAccessWithHeaders

pull/21616/head
Amanda Tarafa Mas 5 years ago
parent bd9cd82621
commit 7b9ee30caa
  1. 28
      src/csharp/Grpc.Auth/GoogleAuthInterceptors.cs

@ -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>

Loading…
Cancel
Save