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.Collections.Generic; 20 using System.Collections.ObjectModel; 21 22 using Grpc.Core.Internal; 23 using Grpc.Core.Utils; 24 25 namespace Grpc.Core 26 { 27 /// <summary> 28 /// Client-side call credentials. Provide authorization with per-call granularity. 29 /// </summary> 30 public abstract class CallCredentials 31 { 32 /// <summary> 33 /// Composes multiple <c>CallCredentials</c> objects into 34 /// a single <c>CallCredentials</c> object. 35 /// </summary> 36 /// <param name="credentials">credentials to compose</param> 37 /// <returns>The new <c>CompositeCallCredentials</c></returns> Compose(params CallCredentials[] credentials)38 public static CallCredentials Compose(params CallCredentials[] credentials) 39 { 40 return new CompositeCallCredentials(credentials); 41 } 42 43 /// <summary> 44 /// Creates a new instance of <c>CallCredentials</c> class from an 45 /// interceptor that can attach metadata to outgoing calls. 46 /// </summary> 47 /// <param name="interceptor">authentication interceptor</param> FromInterceptor(AsyncAuthInterceptor interceptor)48 public static CallCredentials FromInterceptor(AsyncAuthInterceptor interceptor) 49 { 50 return new AsyncAuthInterceptorCredentials(interceptor); 51 } 52 53 /// <summary> 54 /// Populates call credentials configurator with this instance's configuration. 55 /// End users never need to invoke this method as it is part of internal implementation. 56 /// </summary> InternalPopulateConfiguration(CallCredentialsConfiguratorBase configurator, object state)57 public abstract void InternalPopulateConfiguration(CallCredentialsConfiguratorBase configurator, object state); 58 59 private class CompositeCallCredentials : CallCredentials 60 { 61 readonly IReadOnlyList<CallCredentials> credentials; 62 CompositeCallCredentials(CallCredentials[] credentials)63 public CompositeCallCredentials(CallCredentials[] credentials) 64 { 65 GrpcPreconditions.CheckArgument(credentials.Length >= 2, "Composite credentials object can only be created from 2 or more credentials."); 66 this.credentials = new List<CallCredentials>(credentials).AsReadOnly(); 67 } 68 InternalPopulateConfiguration(CallCredentialsConfiguratorBase configurator, object state)69 public override void InternalPopulateConfiguration(CallCredentialsConfiguratorBase configurator, object state) 70 { 71 configurator.SetCompositeCredentials(state, credentials); 72 } 73 } 74 75 private class AsyncAuthInterceptorCredentials : CallCredentials 76 { 77 readonly AsyncAuthInterceptor interceptor; 78 AsyncAuthInterceptorCredentials(AsyncAuthInterceptor interceptor)79 public AsyncAuthInterceptorCredentials(AsyncAuthInterceptor interceptor) 80 { 81 this.interceptor = GrpcPreconditions.CheckNotNull(interceptor); 82 } 83 InternalPopulateConfiguration(CallCredentialsConfiguratorBase configurator, object state)84 public override void InternalPopulateConfiguration(CallCredentialsConfiguratorBase configurator, object state) 85 { 86 configurator.SetAsyncAuthInterceptorCredentials(state, interceptor); 87 } 88 } 89 } 90 } 91