1 #region Copyright notice and license
3 // Copyright 2015 gRPC authors.
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
9 // http://www.apache.org/licenses/LICENSE-2.0
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
20 using System.Collections.Generic;
21 using System.Threading.Tasks;
23 using Grpc.Core.Internal;
24 using Grpc.Core.Utils;
29 /// Client-side channel credentials. Used for creation of a secure channel.
31 public abstract class ChannelCredentials
33 static readonly ChannelCredentials InsecureInstance = new InsecureCredentials();
36 /// Creates a new instance of channel credentials
38 public ChannelCredentials()
43 /// Returns instance of credentials that provides no security and
44 /// will result in creating an unsecure channel with no encryption whatsoever.
46 public static ChannelCredentials Insecure
50 return InsecureInstance;
55 /// Creates a new instance of <c>ChannelCredentials</c> class by composing
56 /// given channel credentials with call credentials.
58 /// <param name="channelCredentials">Channel credentials.</param>
59 /// <param name="callCredentials">Call credentials.</param>
60 /// <returns>The new composite <c>ChannelCredentials</c></returns>
61 public static ChannelCredentials Create(ChannelCredentials channelCredentials, CallCredentials callCredentials)
63 return new CompositeChannelCredentials(channelCredentials, callCredentials);
67 /// Populates channel credentials configurator with this instance's configuration.
68 /// End users never need to invoke this method as it is part of internal implementation.
70 public abstract void InternalPopulateConfiguration(ChannelCredentialsConfiguratorBase configurator, object state);
73 /// Returns <c>true</c> if this credential type allows being composed by <c>CompositeCredentials</c>.
75 internal virtual bool IsComposable => false;
77 private sealed class InsecureCredentials : ChannelCredentials
79 public override void InternalPopulateConfiguration(ChannelCredentialsConfiguratorBase configurator, object state)
81 configurator.SetInsecureCredentials(state);
86 /// Credentials that allow composing one <see cref="ChannelCredentials"/> object and
87 /// one or more <see cref="CallCredentials"/> objects into a single <see cref="ChannelCredentials"/>.
89 private sealed class CompositeChannelCredentials : ChannelCredentials
91 readonly ChannelCredentials channelCredentials;
92 readonly CallCredentials callCredentials;
95 /// Initializes a new instance of <c>CompositeChannelCredentials</c> class.
96 /// The resulting credentials object will be composite of all the credentials specified as parameters.
98 /// <param name="channelCredentials">channelCredentials to compose</param>
99 /// <param name="callCredentials">channelCredentials to compose</param>
100 public CompositeChannelCredentials(ChannelCredentials channelCredentials, CallCredentials callCredentials)
102 this.channelCredentials = GrpcPreconditions.CheckNotNull(channelCredentials);
103 this.callCredentials = GrpcPreconditions.CheckNotNull(callCredentials);
105 if (!channelCredentials.IsComposable)
107 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));
111 public override void InternalPopulateConfiguration(ChannelCredentialsConfiguratorBase configurator, object state)
113 configurator.SetCompositeCredentials(state, channelCredentials, callCredentials);