GoogleGrpcCredentials.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. #region Copyright notice and license
  2. // Copyright 2015 gRPC authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.Threading;
  18. using System.Threading.Tasks;
  19. using Google.Apis.Auth.OAuth2;
  20. using Grpc.Core;
  21. using Grpc.Core.Utils;
  22. namespace Grpc.Auth
  23. {
  24. /// <summary>
  25. /// Factory/extension methods to create instances of <see cref="ChannelCredentials"/> and <see cref="CallCredentials"/> classes
  26. /// based on credential objects originating from Google auth library.
  27. /// </summary>
  28. public static class GoogleGrpcCredentials
  29. {
  30. /// <summary>
  31. /// Retrieves an instance of Google's Application Default Credentials using
  32. /// <c>GoogleCredential.GetApplicationDefaultAsync()</c> and converts them
  33. /// into a gRPC <see cref="ChannelCredentials"/> that use the default SSL credentials.
  34. /// </summary>
  35. /// <returns>The <c>ChannelCredentials</c> instance.</returns>
  36. public static async Task<ChannelCredentials> GetApplicationDefaultAsync()
  37. {
  38. var googleCredential = await GoogleCredential.GetApplicationDefaultAsync().ConfigureAwait(false);
  39. return googleCredential.ToChannelCredentials();
  40. }
  41. /// <summary>
  42. /// Creates an instance of <see cref="CallCredentials"/> that will use given access token to authenticate
  43. /// with a gRPC service.
  44. /// </summary>
  45. /// <param name="accessToken">OAuth2 access token.</param>
  46. /// /// <returns>The <c>MetadataCredentials</c> instance.</returns>
  47. public static CallCredentials FromAccessToken(string accessToken)
  48. {
  49. return CallCredentials.FromInterceptor(GoogleAuthInterceptors.FromAccessToken(accessToken));
  50. }
  51. /// <summary>
  52. /// Converts a <c>ITokenAccess</c> (e.g. <c>GoogleCredential</c>) object
  53. /// into a gRPC <see cref="CallCredentials"/> object.
  54. /// </summary>
  55. /// <param name="credential">The credential to use to obtain access tokens.</param>
  56. /// <returns>The <c>CallCredentials</c> instance.</returns>
  57. public static CallCredentials ToCallCredentials(this ITokenAccess credential)
  58. {
  59. return CallCredentials.FromInterceptor(GoogleAuthInterceptors.FromCredential(credential));
  60. }
  61. /// <summary>
  62. /// Converts a <c>ITokenAccess</c> (e.g. <c>GoogleCredential</c>) object
  63. /// into a gRPC <see cref="ChannelCredentials"/> object.
  64. /// Default SSL credentials are used.
  65. /// </summary>
  66. /// <param name="googleCredential">The credential to use to obtain access tokens.</param>
  67. /// <returns>>The <c>ChannelCredentials</c> instance.</returns>
  68. public static ChannelCredentials ToChannelCredentials(this ITokenAccess googleCredential)
  69. {
  70. return ChannelCredentials.Create(new SslCredentials(), googleCredential.ToCallCredentials());
  71. }
  72. }
  73. }