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