blob: f2354310486ce98f480bdfe085628195aabc02ea [file] [log] [blame]
Jan Tattermuschc9b03fe2017-02-06 08:45:00 -08001#region Copyright notice and license
2
Jan Tattermusch7897ae92017-06-07 22:57:36 +02003// Copyright 2015 gRPC authors.
Jan Tattermuschc9b03fe2017-02-06 08:45:00 -08004//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02005// 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
Jan Tattermuschc9b03fe2017-02-06 08:45:00 -08008//
Jan Tattermusch7897ae92017-06-07 22:57:36 +02009// http://www.apache.org/licenses/LICENSE-2.0
Jan Tattermuschc9b03fe2017-02-06 08:45:00 -080010//
Jan Tattermusch7897ae92017-06-07 22:57:36 +020011// 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.
Jan Tattermuschc9b03fe2017-02-06 08:45:00 -080016
17#endregion
18
19using System;
20using System.Collections.Generic;
21using System.Linq;
22using Grpc.Core.Internal;
23using Grpc.Core.Utils;
24
25namespace 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>
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>
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}