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