Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(10)

Side by Side Diff: third_party/grpc/src/csharp/Grpc.Core/CallCredentials.cs

Issue 1932353002: Initial checkin of gRPC to third_party/ Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 #region Copyright notice and license
2
3 // Copyright 2015-2016, Google Inc.
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 // * Redistributions in binary form must reproduce the above
13 // copyright notice, this list of conditions and the following disclaimer
14 // in the documentation and/or other materials provided with the
15 // distribution.
16 // * Neither the name of Google Inc. nor the names of its
17 // contributors may be used to endorse or promote products derived from
18 // this software without specific prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
32 #endregion
33
34 using System;
35 using System.Collections.Generic;
36 using System.Threading.Tasks;
37
38 using Grpc.Core.Internal;
39 using Grpc.Core.Utils;
40
41 namespace Grpc.Core
42 {
43 /// <summary>
44 /// Client-side call credentials. Provide authorization with per-call granul arity.
45 /// </summary>
46 public abstract class CallCredentials
47 {
48 /// <summary>
49 /// Composes multiple multiple <c>CallCredentials</c> objects into
50 /// a single <c>CallCredentials</c> object.
51 /// </summary>
52 /// <param name="credentials">credentials to compose</param>
53 /// <returns>The new <c>CompositeCallCredentials</c></returns>
54 public static CallCredentials Compose(params CallCredentials[] credentia ls)
55 {
56 return new CompositeCallCredentials(credentials);
57 }
58
59 /// <summary>
60 /// Creates a new instance of <c>CallCredentials</c> class from an
61 /// interceptor that can attach metadata to outgoing calls.
62 /// </summary>
63 /// <param name="interceptor">authentication interceptor</param>
64 public static CallCredentials FromInterceptor(AsyncAuthInterceptor inter ceptor)
65 {
66 return new MetadataCredentials(interceptor);
67 }
68
69 /// <summary>
70 /// Creates native object for the credentials.
71 /// </summary>
72 /// <returns>The native credentials.</returns>
73 internal abstract CallCredentialsSafeHandle ToNativeCredentials();
74 }
75
76 /// <summary>
77 /// Client-side credentials that delegate metadata based auth to an intercep tor.
78 /// The interceptor is automatically invoked for each remote call that uses <c>MetadataCredentials.</c>
79 /// </summary>
80 internal sealed class MetadataCredentials : CallCredentials
81 {
82 readonly AsyncAuthInterceptor interceptor;
83
84 /// <summary>
85 /// Initializes a new instance of <c>MetadataCredentials</c> class.
86 /// </summary>
87 /// <param name="interceptor">authentication interceptor</param>
88 public MetadataCredentials(AsyncAuthInterceptor interceptor)
89 {
90 this.interceptor = GrpcPreconditions.CheckNotNull(interceptor);
91 }
92
93 internal override CallCredentialsSafeHandle ToNativeCredentials()
94 {
95 NativeMetadataCredentialsPlugin plugin = new NativeMetadataCredentia lsPlugin(interceptor);
96 return plugin.Credentials;
97 }
98 }
99
100 /// <summary>
101 /// Credentials that allow composing multiple credentials objects into one < see cref="CallCredentials"/> object.
102 /// </summary>
103 internal sealed class CompositeCallCredentials : CallCredentials
104 {
105 readonly List<CallCredentials> credentials;
106
107 /// <summary>
108 /// Initializes a new instance of <c>CompositeCallCredentials</c> class.
109 /// The resulting credentials object will be composite of all the creden tials specified as parameters.
110 /// </summary>
111 /// <param name="credentials">credentials to compose</param>
112 public CompositeCallCredentials(params CallCredentials[] credentials)
113 {
114 GrpcPreconditions.CheckArgument(credentials.Length >= 2, "Composite credentials object can only be created from 2 or more credentials.");
115 this.credentials = new List<CallCredentials>(credentials);
116 }
117
118 internal override CallCredentialsSafeHandle ToNativeCredentials()
119 {
120 return ToNativeRecursive(0);
121 }
122
123 // Recursive descent makes managing lifetime of intermediate CredentialS afeHandle instances easier.
124 // In practice, we won't usually see composites from more than two crede ntials anyway.
125 private CallCredentialsSafeHandle ToNativeRecursive(int startIndex)
126 {
127 if (startIndex == credentials.Count - 1)
128 {
129 return credentials[startIndex].ToNativeCredentials();
130 }
131
132 using (var cred1 = credentials[startIndex].ToNativeCredentials())
133 using (var cred2 = ToNativeRecursive(startIndex + 1))
134 {
135 var nativeComposite = CallCredentialsSafeHandle.CreateComposite( cred1, cred2);
136 if (nativeComposite.IsInvalid)
137 {
138 throw new ArgumentException("Error creating native composite credentials. Likely, this is because you are trying to compose incompatible cre dentials.");
139 }
140 return nativeComposite;
141 }
142 }
143 }
144 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698