AuthProperty.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. /// A property of an <see cref="AuthContext"/>.
  25. /// Note: experimental API that can change or be removed without any prior notice.
  26. /// </summary>
  27. public class AuthProperty
  28. {
  29. string name;
  30. byte[] valueBytes;
  31. Lazy<string> value;
  32. private AuthProperty(string name, byte[] valueBytes)
  33. {
  34. this.name = GrpcPreconditions.CheckNotNull(name);
  35. this.valueBytes = GrpcPreconditions.CheckNotNull(valueBytes);
  36. this.value = new Lazy<string>(() => MarshalUtils.GetStringUTF8(this.valueBytes));
  37. }
  38. /// <summary>
  39. /// Gets the name of the property.
  40. /// </summary>
  41. public string Name
  42. {
  43. get
  44. {
  45. return name;
  46. }
  47. }
  48. /// <summary>
  49. /// Gets the string value of the property.
  50. /// </summary>
  51. public string Value
  52. {
  53. get
  54. {
  55. return value.Value;
  56. }
  57. }
  58. /// <summary>
  59. /// Gets the binary value of the property.
  60. /// </summary>
  61. public byte[] ValueBytes
  62. {
  63. get
  64. {
  65. var valueCopy = new byte[valueBytes.Length];
  66. Buffer.BlockCopy(valueBytes, 0, valueCopy, 0, valueBytes.Length);
  67. return valueCopy;
  68. }
  69. }
  70. /// <summary>
  71. /// Creates an instance of <c>AuthProperty</c>.
  72. /// </summary>
  73. /// <param name="name">the name</param>
  74. /// <param name="valueBytes">the binary value of the property</param>
  75. public static AuthProperty Create(string name, byte[] valueBytes)
  76. {
  77. GrpcPreconditions.CheckNotNull(valueBytes);
  78. var valueCopy = new byte[valueBytes.Length];
  79. Buffer.BlockCopy(valueBytes, 0, valueCopy, 0, valueBytes.Length);
  80. return new AuthProperty(name, valueCopy);
  81. }
  82. /// <summary>
  83. /// Gets the binary value of the property (without making a defensive copy).
  84. /// </summary>
  85. internal byte[] ValueBytesUnsafe
  86. {
  87. get
  88. {
  89. return valueBytes;
  90. }
  91. }
  92. /// <summary>
  93. /// Creates and instance of <c>AuthProperty</c> without making a defensive copy of <c>valueBytes</c>.
  94. /// </summary>
  95. internal static AuthProperty CreateUnsafe(string name, byte[] valueBytes)
  96. {
  97. return new AuthProperty(name, valueBytes);
  98. }
  99. }
  100. }