AuthContext.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #region Copyright notice and license
  2. // Copyright 2015 gRPC authors.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #endregion
  16. using System;
  17. using System.Collections.Generic;
  18. using System.Linq;
  19. using Grpc.Core.Internal;
  20. using Grpc.Core.Utils;
  21. namespace Grpc.Core
  22. {
  23. /// <summary>
  24. /// Authentication context for a call.
  25. /// AuthContext is the only reliable source of truth when it comes to authenticating calls.
  26. /// Using any other call/context properties for authentication purposes is wrong and inherently unsafe.
  27. /// Note: experimental API that can change or be removed without any prior notice.
  28. /// </summary>
  29. public class AuthContext
  30. {
  31. string peerIdentityPropertyName;
  32. Dictionary<string, List<AuthProperty>> properties;
  33. /// <summary>
  34. /// Initializes a new instance of the <see cref="T:Grpc.Core.AuthContext"/> class.
  35. /// </summary>
  36. /// <param name="peerIdentityPropertyName">Peer identity property name.</param>
  37. /// <param name="properties">Multimap of auth properties by name.</param>
  38. internal AuthContext(string peerIdentityPropertyName, Dictionary<string, List<AuthProperty>> properties)
  39. {
  40. this.peerIdentityPropertyName = peerIdentityPropertyName;
  41. this.properties = GrpcPreconditions.CheckNotNull(properties);
  42. }
  43. /// <summary>
  44. /// Returns <c>true</c> if the peer is authenticated.
  45. /// </summary>
  46. public bool IsPeerAuthenticated
  47. {
  48. get
  49. {
  50. return peerIdentityPropertyName != null;
  51. }
  52. }
  53. /// <summary>
  54. /// Gets the name of the property that indicates the peer identity. Returns <c>null</c>
  55. /// if the peer is not authenticated.
  56. /// </summary>
  57. public string PeerIdentityPropertyName
  58. {
  59. get
  60. {
  61. return peerIdentityPropertyName;
  62. }
  63. }
  64. /// <summary>
  65. /// Gets properties that represent the peer identity (there can be more than one). Returns an empty collection
  66. /// if the peer is not authenticated.
  67. /// </summary>
  68. public IEnumerable<AuthProperty> PeerIdentity
  69. {
  70. get
  71. {
  72. if (peerIdentityPropertyName == null)
  73. {
  74. return Enumerable.Empty<AuthProperty>();
  75. }
  76. return properties[peerIdentityPropertyName];
  77. }
  78. }
  79. /// <summary>
  80. /// Gets the auth properties of this context.
  81. /// </summary>
  82. public IEnumerable<AuthProperty> Properties
  83. {
  84. get
  85. {
  86. return properties.Values.SelectMany(v => v);
  87. }
  88. }
  89. /// <summary>
  90. /// Returns the auth properties with given name (there can be more than one).
  91. /// If no properties of given name exist, an empty collection will be returned.
  92. /// </summary>
  93. public IEnumerable<AuthProperty> FindPropertiesByName(string propertyName)
  94. {
  95. List<AuthProperty> result;
  96. if (!properties.TryGetValue(propertyName, out result))
  97. {
  98. return Enumerable.Empty<AuthProperty>();
  99. }
  100. return result;
  101. }
  102. }
  103. }