Imported Upstream version 1.27.0
[platform/upstream/grpc.git] / src / csharp / Grpc.Core.Api / ChannelCredentials.cs
1 #region Copyright notice and license
2
3 // Copyright 2015 gRPC authors.
4 //
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
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
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.
16
17 #endregion
18
19 using System;
20 using System.Collections.Generic;
21 using System.Threading.Tasks;
22
23 using Grpc.Core.Internal;
24 using Grpc.Core.Utils;
25
26 namespace Grpc.Core
27 {
28     /// <summary>
29     /// Client-side channel credentials. Used for creation of a secure channel.
30     /// </summary>
31     public abstract class ChannelCredentials
32     {
33         static readonly ChannelCredentials InsecureInstance = new InsecureCredentials();
34
35         /// <summary>
36         /// Creates a new instance of channel credentials
37         /// </summary>
38         public ChannelCredentials()
39         {
40         }
41
42         /// <summary>
43         /// Returns instance of credentials that provides no security and 
44         /// will result in creating an unsecure channel with no encryption whatsoever.
45         /// </summary>
46         public static ChannelCredentials Insecure
47         {
48             get
49             {
50                 return InsecureInstance;
51             }
52         }
53
54         /// <summary>
55         /// Creates a new instance of <c>ChannelCredentials</c> class by composing
56         /// given channel credentials with call credentials.
57         /// </summary>
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)
62         {
63             return new CompositeChannelCredentials(channelCredentials, callCredentials);
64         }
65
66         /// <summary>
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.
69         /// </summary>
70         public abstract void InternalPopulateConfiguration(ChannelCredentialsConfiguratorBase configurator, object state);
71
72         /// <summary>
73         /// Returns <c>true</c> if this credential type allows being composed by <c>CompositeCredentials</c>.
74         /// </summary>
75         internal virtual bool IsComposable => false;
76
77         private sealed class InsecureCredentials : ChannelCredentials
78         {
79             public override void InternalPopulateConfiguration(ChannelCredentialsConfiguratorBase configurator, object state)
80             {
81                 configurator.SetInsecureCredentials(state);
82             }
83         }
84
85         /// <summary>
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"/>.
88         /// </summary>
89         private sealed class CompositeChannelCredentials : ChannelCredentials
90         {
91             readonly ChannelCredentials channelCredentials;
92             readonly CallCredentials callCredentials;
93
94             /// <summary>
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.
97             /// </summary>
98             /// <param name="channelCredentials">channelCredentials to compose</param>
99             /// <param name="callCredentials">channelCredentials to compose</param>
100             public CompositeChannelCredentials(ChannelCredentials channelCredentials, CallCredentials callCredentials)
101             {
102                 this.channelCredentials = GrpcPreconditions.CheckNotNull(channelCredentials);
103                 this.callCredentials = GrpcPreconditions.CheckNotNull(callCredentials);
104
105                 if (!channelCredentials.IsComposable)
106                 {
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));
108                 }
109             }
110
111             public override void InternalPopulateConfiguration(ChannelCredentialsConfiguratorBase configurator, object state)
112             {
113                 configurator.SetCompositeCredentials(state, channelCredentials, callCredentials);
114             }
115         }
116     }
117 }