|
|
|
@ -33,6 +33,7 @@ |
|
|
|
|
|
|
|
|
|
using System; |
|
|
|
|
using System.Threading; |
|
|
|
|
using System.Threading.Tasks; |
|
|
|
|
|
|
|
|
|
using Google.Apis.Auth.OAuth2; |
|
|
|
|
using Grpc.Core; |
|
|
|
@ -41,53 +42,55 @@ using Grpc.Core.Utils; |
|
|
|
|
namespace Grpc.Auth |
|
|
|
|
{ |
|
|
|
|
/// <summary> |
|
|
|
|
/// Factory methods to create instances of <see cref="ChannelCredentials"/> and <see cref="CallCredentials"/> classes. |
|
|
|
|
/// Factory/extension methods to create instances of <see cref="ChannelCredentials"/> and <see cref="CallCredentials"/> classes |
|
|
|
|
/// based on credential objects originating from Google auth library. |
|
|
|
|
/// </summary> |
|
|
|
|
public static class GrpcCredentials |
|
|
|
|
public static class GoogleGrpcCredentials |
|
|
|
|
{ |
|
|
|
|
/// <summary> |
|
|
|
|
/// Creates a <see cref="MetadataCredentials"/> instance that will obtain access tokens |
|
|
|
|
/// from any credential that implements <c>ITokenAccess</c>. (e.g. <c>GoogleCredential</c>). |
|
|
|
|
/// Retrieves an instance of Google's Application Default Credentials using |
|
|
|
|
/// <c>GoogleCredential.GetApplicationDefaultAsync()</c> and converts them |
|
|
|
|
/// into a gRPC <see cref="ChannelCredentials"/> that use the default SSL credentials. |
|
|
|
|
/// </summary> |
|
|
|
|
/// <param name="credential">The credential to use to obtain access tokens.</param> |
|
|
|
|
/// <returns>The <c>MetadataCredentials</c> instance.</returns> |
|
|
|
|
public static MetadataCredentials Create(ITokenAccess credential) |
|
|
|
|
/// <returns>The <c>ChannelCredentials</c> instance.</returns> |
|
|
|
|
public static async Task<ChannelCredentials> GetApplicationDefaultAsync() |
|
|
|
|
{ |
|
|
|
|
return new MetadataCredentials(AuthInterceptors.FromCredential(credential)); |
|
|
|
|
var googleCredential = await GoogleCredential.GetApplicationDefaultAsync().ConfigureAwait(false); |
|
|
|
|
return googleCredential.ToChannelCredentials(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// Convenience method to create a <see cref="ChannelCredentials"/> instance from |
|
|
|
|
/// <c>ITokenAccess</c> credential and <c>SslCredentials</c> instance. |
|
|
|
|
/// Creates an instance of <see cref="CallCredentials"/> that will use given access token to authenticate |
|
|
|
|
/// with a gRPC service. |
|
|
|
|
/// </summary> |
|
|
|
|
/// <param name="credential">The credential to use to obtain access tokens.</param> |
|
|
|
|
/// <param name="sslCredentials">The <c>SslCredentials</c> instance.</param> |
|
|
|
|
/// <returns>The channel credentials for access token based auth over a secure channel.</returns> |
|
|
|
|
public static ChannelCredentials Create(ITokenAccess credential, SslCredentials sslCredentials) |
|
|
|
|
/// <param name="accessToken">OAuth2 access token.</param> |
|
|
|
|
/// /// <returns>The <c>MetadataCredentials</c> instance.</returns> |
|
|
|
|
public static CallCredentials FromAccessToken(string accessToken) |
|
|
|
|
{ |
|
|
|
|
return ChannelCredentials.Create(sslCredentials, Create(credential)); |
|
|
|
|
return CallCredentials.FromInterceptor(GoogleAuthInterceptors.FromAccessToken(accessToken)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// Creates an instance of <see cref="MetadataCredentials"/> that will use given access token to authenticate |
|
|
|
|
/// with a gRPC service. |
|
|
|
|
/// Converts a <c>ITokenAccess</c> (e.g. <c>GoogleCredential</c>) object |
|
|
|
|
/// into a gRPC <see cref="CallCredentials"/> object. |
|
|
|
|
/// </summary> |
|
|
|
|
/// <param name="accessToken">OAuth2 access token.</param> |
|
|
|
|
/// /// <returns>The <c>MetadataCredentials</c> instance.</returns> |
|
|
|
|
public static MetadataCredentials FromAccessToken(string accessToken) |
|
|
|
|
/// <param name="credential">The credential to use to obtain access tokens.</param> |
|
|
|
|
/// <returns>The <c>CallCredentials</c> instance.</returns> |
|
|
|
|
public static CallCredentials ToCallCredentials(this ITokenAccess credential) |
|
|
|
|
{ |
|
|
|
|
return new MetadataCredentials(AuthInterceptors.FromAccessToken(accessToken)); |
|
|
|
|
return CallCredentials.FromInterceptor(GoogleAuthInterceptors.FromCredential(credential)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// <summary> |
|
|
|
|
/// Converts a <c>ITokenAccess</c> object into a <see cref="MetadataCredentials"/> object supported |
|
|
|
|
/// by gRPC. |
|
|
|
|
/// Converts a <c>ITokenAccess</c> (e.g. <c>GoogleCredential</c>) object |
|
|
|
|
/// into a gRPC <see cref="ChannelCredentials"/> object. |
|
|
|
|
/// Default SSL credentials are used. |
|
|
|
|
/// </summary> |
|
|
|
|
/// <param name="credential"></param> |
|
|
|
|
/// <returns></returns> |
|
|
|
|
public static MetadataCredentials ToGrpcCredentials(this ITokenAccess credential) |
|
|
|
|
/// <param name="googleCredential">The credential to use to obtain access tokens.</param> |
|
|
|
|
/// <returns>>The <c>ChannelCredentials</c> instance.</returns> |
|
|
|
|
public static ChannelCredentials ToChannelCredentials(this ITokenAccess googleCredential) |
|
|
|
|
{ |
|
|
|
|
return GrpcCredentials.Create(credential); |
|
|
|
|
return ChannelCredentials.Create(new SslCredentials(), googleCredential.ToCallCredentials()); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |