GoogleCredential.cs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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.Collections.Generic;
  33. using System.IO;
  34. using System.Security.Cryptography;
  35. using Google.Apis.Auth.OAuth2;
  36. using Mono.Security.Cryptography;
  37. using Newtonsoft.Json.Linq;
  38. using Org.BouncyCastle.Crypto.Parameters;
  39. using Org.BouncyCastle.Security;
  40. namespace Grpc.Auth
  41. {
  42. // TODO(jtattermusch): Remove this class once possible.
  43. /// <summary>
  44. /// A temporary placeholder for Google credential from
  45. /// Google Auth library for .NET. It emulates the usage pattern
  46. /// for Usable auth.
  47. /// </summary>
  48. public class GoogleCredential
  49. {
  50. private const string GoogleApplicationCredentialsEnvName = "GOOGLE_APPLICATION_CREDENTIALS";
  51. private const string ClientEmailFieldName = "client_email";
  52. private const string PrivateKeyFieldName = "private_key";
  53. private ServiceCredential credential;
  54. private GoogleCredential(ServiceCredential credential)
  55. {
  56. this.credential = credential;
  57. }
  58. public static GoogleCredential GetApplicationDefault()
  59. {
  60. return new GoogleCredential(null);
  61. }
  62. public bool IsCreateScopedRequired
  63. {
  64. get
  65. {
  66. return true;
  67. }
  68. }
  69. public GoogleCredential CreateScoped(IEnumerable<string> scopes)
  70. {
  71. var credsPath = Environment.GetEnvironmentVariable(GoogleApplicationCredentialsEnvName);
  72. if (credsPath == null)
  73. {
  74. // Default to ComputeCredentials if path to JSON key is not set.
  75. // ComputeCredential is not scoped actually, but for our use case it's
  76. // fine to treat is as such.
  77. return new GoogleCredential(new ComputeCredential(new ComputeCredential.Initializer()));
  78. }
  79. JObject o1 = JObject.Parse(File.ReadAllText(credsPath));
  80. string clientEmail = o1.GetValue(ClientEmailFieldName).Value<string>();
  81. string privateKeyString = o1.GetValue(PrivateKeyFieldName).Value<string>();
  82. var privateKey = ParsePrivateKeyFromString(privateKeyString);
  83. var serviceCredential = new ServiceAccountCredential(
  84. new ServiceAccountCredential.Initializer(clientEmail)
  85. {
  86. Scopes = scopes,
  87. Key = privateKey
  88. });
  89. return new GoogleCredential(serviceCredential);
  90. }
  91. internal ServiceCredential InternalCredential
  92. {
  93. get
  94. {
  95. return credential;
  96. }
  97. }
  98. private RSACryptoServiceProvider ParsePrivateKeyFromString(string base64PrivateKey)
  99. {
  100. // TODO(jtattermusch): temporary code to create RSACryptoServiceProvider.
  101. base64PrivateKey = base64PrivateKey.Replace("-----BEGIN PRIVATE KEY-----", "").Replace("\n", "").Replace("-----END PRIVATE KEY-----", "");
  102. PKCS8.PrivateKeyInfo PKI = new PKCS8.PrivateKeyInfo(Convert.FromBase64String(base64PrivateKey));
  103. RsaPrivateCrtKeyParameters key = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(PKI.GetBytes());
  104. RSAParameters rsaParameters = DotNetUtilities.ToRSAParameters(key);
  105. RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();
  106. rsa.ImportParameters(rsaParameters);
  107. return rsa;
  108. }
  109. }
  110. }