ChannelCredentials.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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.Collections.Generic;
  18. using System.Threading.Tasks;
  19. using Grpc.Core.Internal;
  20. using Grpc.Core.Utils;
  21. namespace Grpc.Core
  22. {
  23. /// <summary>
  24. /// Client-side channel credentials. Used for creation of a secure channel.
  25. /// </summary>
  26. public abstract class ChannelCredentials
  27. {
  28. static readonly ChannelCredentials InsecureInstance = new InsecureCredentials();
  29. /// <summary>
  30. /// Creates a new instance of channel credentials
  31. /// </summary>
  32. public ChannelCredentials()
  33. {
  34. }
  35. /// <summary>
  36. /// Returns instance of credentials that provides no security and
  37. /// will result in creating an unsecure channel with no encryption whatsoever.
  38. /// </summary>
  39. public static ChannelCredentials Insecure
  40. {
  41. get
  42. {
  43. return InsecureInstance;
  44. }
  45. }
  46. /// <summary>
  47. /// Creates a new instance of <c>ChannelCredentials</c> class by composing
  48. /// given channel credentials with call credentials.
  49. /// </summary>
  50. /// <param name="channelCredentials">Channel credentials.</param>
  51. /// <param name="callCredentials">Call credentials.</param>
  52. /// <returns>The new composite <c>ChannelCredentials</c></returns>
  53. public static ChannelCredentials Create(ChannelCredentials channelCredentials, CallCredentials callCredentials)
  54. {
  55. return new CompositeChannelCredentials(channelCredentials, callCredentials);
  56. }
  57. /// <summary>
  58. /// Populates channel credentials configurator with this instance's configuration.
  59. /// End users never need to invoke this method as it is part of internal implementation.
  60. /// </summary>
  61. public abstract void InternalPopulateConfiguration(ChannelCredentialsConfiguratorBase configurator, object state);
  62. /// <summary>
  63. /// Returns <c>true</c> if this credential type allows being composed by <c>CompositeCredentials</c>.
  64. /// </summary>
  65. internal virtual bool IsComposable => false;
  66. private sealed class InsecureCredentials : ChannelCredentials
  67. {
  68. public override void InternalPopulateConfiguration(ChannelCredentialsConfiguratorBase configurator, object state)
  69. {
  70. configurator.SetInsecureCredentials(state);
  71. }
  72. }
  73. /// <summary>
  74. /// Credentials that allow composing one <see cref="ChannelCredentials"/> object and
  75. /// one or more <see cref="CallCredentials"/> objects into a single <see cref="ChannelCredentials"/>.
  76. /// </summary>
  77. private sealed class CompositeChannelCredentials : ChannelCredentials
  78. {
  79. readonly ChannelCredentials channelCredentials;
  80. readonly CallCredentials callCredentials;
  81. /// <summary>
  82. /// Initializes a new instance of <c>CompositeChannelCredentials</c> class.
  83. /// The resulting credentials object will be composite of all the credentials specified as parameters.
  84. /// </summary>
  85. /// <param name="channelCredentials">channelCredentials to compose</param>
  86. /// <param name="callCredentials">channelCredentials to compose</param>
  87. public CompositeChannelCredentials(ChannelCredentials channelCredentials, CallCredentials callCredentials)
  88. {
  89. this.channelCredentials = GrpcPreconditions.CheckNotNull(channelCredentials);
  90. this.callCredentials = GrpcPreconditions.CheckNotNull(callCredentials);
  91. if (!channelCredentials.IsComposable)
  92. {
  93. throw new ArgumentException(string.Format("CallCredentials can't be composed with {0}. CallCredentials must be used with secure channel credentials like SslCredentials.", channelCredentials.GetType().Name));
  94. }
  95. }
  96. public override void InternalPopulateConfiguration(ChannelCredentialsConfiguratorBase configurator, object state)
  97. {
  98. configurator.SetCompositeCredentials(state, channelCredentials, callCredentials);
  99. }
  100. }
  101. }
  102. }