GoogleGrpcCredentials.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #region Copyright notice and license
  2. // Copyright 2015, Google Inc.
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are
  7. // met:
  8. //
  9. // * Redistributions of source code must retain the above copyright
  10. // notice, this list of conditions and the following disclaimer.
  11. // * Redistributions in binary form must reproduce the above
  12. // copyright notice, this list of conditions and the following disclaimer
  13. // in the documentation and/or other materials provided with the
  14. // distribution.
  15. // * Neither the name of Google Inc. nor the names of its
  16. // contributors may be used to endorse or promote products derived from
  17. // this software without specific prior written permission.
  18. //
  19. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  22. // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  23. // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  25. // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  26. // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  27. // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  28. // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  29. // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  30. #endregion
  31. using System;
  32. using System.Threading;
  33. using System.Threading.Tasks;
  34. using Google.Apis.Auth.OAuth2;
  35. using Grpc.Core;
  36. using Grpc.Core.Utils;
  37. namespace Grpc.Auth
  38. {
  39. /// <summary>
  40. /// Factory/extension methods to create instances of <see cref="ChannelCredentials"/> and <see cref="CallCredentials"/> classes
  41. /// based on credential objects originating from Google auth library.
  42. /// </summary>
  43. public static class GoogleGrpcCredentials
  44. {
  45. /// <summary>
  46. /// Retrieves an instance of Google's Application Default Credentials using
  47. /// <c>GoogleCredential.GetApplicationDefaultAsync()</c> and converts them
  48. /// into a gRPC <see cref="ChannelCredentials"/> that use the default SSL credentials.
  49. /// </summary>
  50. /// <returns>The <c>ChannelCredentials</c> instance.</returns>
  51. public static async Task<ChannelCredentials> GetApplicationDefaultAsync()
  52. {
  53. var googleCredential = await GoogleCredential.GetApplicationDefaultAsync().ConfigureAwait(false);
  54. return googleCredential.ToChannelCredentials();
  55. }
  56. /// <summary>
  57. /// Creates an instance of <see cref="CallCredentials"/> that will use given access token to authenticate
  58. /// with a gRPC service.
  59. /// </summary>
  60. /// <param name="accessToken">OAuth2 access token.</param>
  61. /// /// <returns>The <c>MetadataCredentials</c> instance.</returns>
  62. public static CallCredentials FromAccessToken(string accessToken)
  63. {
  64. return CallCredentials.FromInterceptor(GoogleAuthInterceptors.FromAccessToken(accessToken));
  65. }
  66. /// <summary>
  67. /// Converts a <c>ITokenAccess</c> (e.g. <c>GoogleCredential</c>) object
  68. /// into a gRPC <see cref="CallCredentials"/> object.
  69. /// </summary>
  70. /// <param name="credential">The credential to use to obtain access tokens.</param>
  71. /// <returns>The <c>CallCredentials</c> instance.</returns>
  72. public static CallCredentials ToCallCredentials(this ITokenAccess credential)
  73. {
  74. return CallCredentials.FromInterceptor(GoogleAuthInterceptors.FromCredential(credential));
  75. }
  76. /// <summary>
  77. /// Converts a <c>ITokenAccess</c> (e.g. <c>GoogleCredential</c>) object
  78. /// into a gRPC <see cref="ChannelCredentials"/> object.
  79. /// Default SSL credentials are used.
  80. /// </summary>
  81. /// <param name="googleCredential">The credential to use to obtain access tokens.</param>
  82. /// <returns>>The <c>ChannelCredentials</c> instance.</returns>
  83. public static ChannelCredentials ToChannelCredentials(this ITokenAccess googleCredential)
  84. {
  85. return ChannelCredentials.Create(new SslCredentials(), googleCredential.ToCallCredentials());
  86. }
  87. }
  88. }