AuthProperty.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 System.Text;
  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. static readonly Encoding EncodingUTF8 = System.Text.Encoding.UTF8;
  30. string name;
  31. byte[] valueBytes;
  32. Lazy<string> value;
  33. private AuthProperty(string name, byte[] valueBytes)
  34. {
  35. this.name = GrpcPreconditions.CheckNotNull(name);
  36. this.valueBytes = GrpcPreconditions.CheckNotNull(valueBytes);
  37. this.value = new Lazy<string>(() => EncodingUTF8.GetString(this.valueBytes));
  38. }
  39. /// <summary>
  40. /// Gets the name of the property.
  41. /// </summary>
  42. public string Name
  43. {
  44. get
  45. {
  46. return name;
  47. }
  48. }
  49. /// <summary>
  50. /// Gets the string value of the property.
  51. /// </summary>
  52. public string Value
  53. {
  54. get
  55. {
  56. return value.Value;
  57. }
  58. }
  59. /// <summary>
  60. /// Gets the binary value of the property.
  61. /// </summary>
  62. public byte[] ValueBytes
  63. {
  64. get
  65. {
  66. var valueCopy = new byte[valueBytes.Length];
  67. Buffer.BlockCopy(valueBytes, 0, valueCopy, 0, valueBytes.Length);
  68. return valueCopy;
  69. }
  70. }
  71. /// <summary>
  72. /// Creates an instance of <c>AuthProperty</c>.
  73. /// </summary>
  74. /// <param name="name">the name</param>
  75. /// <param name="valueBytes">the binary value of the property</param>
  76. public static AuthProperty Create(string name, byte[] valueBytes)
  77. {
  78. GrpcPreconditions.CheckNotNull(valueBytes);
  79. var valueCopy = new byte[valueBytes.Length];
  80. Buffer.BlockCopy(valueBytes, 0, valueCopy, 0, valueBytes.Length);
  81. return new AuthProperty(name, valueCopy);
  82. }
  83. /// <summary>
  84. /// Gets the binary value of the property (without making a defensive copy).
  85. /// </summary>
  86. internal byte[] ValueBytesUnsafe
  87. {
  88. get
  89. {
  90. return valueBytes;
  91. }
  92. }
  93. /// <summary>
  94. /// Creates and instance of <c>AuthProperty</c> without making a defensive copy of <c>valueBytes</c>.
  95. /// </summary>
  96. internal static AuthProperty CreateUnsafe(string name, byte[] valueBytes)
  97. {
  98. return new AuthProperty(name, valueBytes);
  99. }
  100. }
  101. }