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.Linq;
22 using Grpc.Core.Internal;
23 using Grpc.Core.Utils;
24 
25 namespace Grpc.Core
26 {
27     /// <summary>
28     /// Authentication context for a call.
29     /// AuthContext is the only reliable source of truth when it comes to authenticating calls.
30     /// Using any other call/context properties for authentication purposes is wrong and inherently unsafe.
31     /// Note: experimental API that can change or be removed without any prior notice.
32     /// </summary>
33     public class AuthContext
34     {
35         string peerIdentityPropertyName;
36         Dictionary<string, List<AuthProperty>> properties;
37 
38         /// <summary>
39         /// Initializes a new instance of the <see cref="T:Grpc.Core.AuthContext"/> class.
40         /// </summary>
41         /// <param name="peerIdentityPropertyName">Peer identity property name.</param>
42         /// <param name="properties">Multimap of auth properties by name.</param>
AuthContext(string peerIdentityPropertyName, Dictionary<string, List<AuthProperty>> properties)43         internal AuthContext(string peerIdentityPropertyName, Dictionary<string, List<AuthProperty>> properties)
44         {
45             this.peerIdentityPropertyName = peerIdentityPropertyName;
46             this.properties = GrpcPreconditions.CheckNotNull(properties);
47         }
48 
49         /// <summary>
50         /// Returns <c>true</c> if the peer is authenticated.
51         /// </summary>
52         public bool IsPeerAuthenticated
53         {
54             get
55             {
56                 return peerIdentityPropertyName != null;
57             }
58         }
59 
60         /// <summary>
61         /// Gets the name of the property that indicates the peer identity. Returns <c>null</c>
62         /// if the peer is not authenticated.
63         /// </summary>
64         public string PeerIdentityPropertyName
65         {
66             get
67             {
68                 return peerIdentityPropertyName;
69             }
70         }
71 
72         /// <summary>
73         /// Gets properties that represent the peer identity (there can be more than one). Returns an empty collection
74         /// if the peer is not authenticated.
75         /// </summary>
76         public IEnumerable<AuthProperty> PeerIdentity
77         {
78             get
79             {
80                 if (peerIdentityPropertyName == null)
81                 {
82                     return Enumerable.Empty<AuthProperty>();
83                 }
84                 return properties[peerIdentityPropertyName];
85             }
86         }
87 
88         /// <summary>
89         /// Gets the auth properties of this context.
90         /// </summary>
91         public IEnumerable<AuthProperty> Properties
92         {
93             get
94             {
95                 return properties.Values.SelectMany(v => v);
96             }
97         }
98 
99         /// <summary>
100         /// Returns the auth properties with given name (there can be more than one).
101         /// If no properties of given name exist, an empty collection will be returned.
102         /// </summary>
FindPropertiesByName(string propertyName)103         public IEnumerable<AuthProperty> FindPropertiesByName(string propertyName)
104         {
105             List<AuthProperty> result;
106             if (!properties.TryGetValue(propertyName, out result))
107             {
108                 return Enumerable.Empty<AuthProperty>();
109             }
110             return result;
111         }
112     }
113 }
114