| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System; |
| | | 5 | | using System.Collections.Generic; |
| | | 6 | | using System.Xml; |
| | | 7 | | using DictionaryManager = CoreWCF.IdentityModel.DictionaryManager; |
| | | 8 | | using ISecurityElement = CoreWCF.IdentityModel.ISecurityElement; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF.Security |
| | | 11 | | { |
| | | 12 | | internal sealed class ReferenceList : ISecurityElement |
| | | 13 | | { |
| | 2 | 14 | | internal static readonly XmlDictionaryString ElementName = XD.XmlEncryptionDictionary.ReferenceList; |
| | | 15 | | private const string NamespacePrefix = XmlEncryptionStrings.Prefix; |
| | 2 | 16 | | internal static readonly XmlDictionaryString NamespaceUri = XD.XmlEncryptionDictionary.Namespace;// EncryptedTyp |
| | 2 | 17 | | internal static readonly XmlDictionaryString UriAttribute = XD.XmlEncryptionDictionary.URI; |
| | 0 | 18 | | private readonly List<string> _referredIds = new List<string>(); |
| | | 19 | | |
| | 0 | 20 | | public ReferenceList() |
| | | 21 | | { |
| | 0 | 22 | | } |
| | | 23 | | |
| | 0 | 24 | | public int DataReferenceCount => _referredIds.Count; |
| | | 25 | | |
| | 0 | 26 | | public bool HasId => false; |
| | | 27 | | |
| | 0 | 28 | | public string Id => throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new NotSupportedException()); |
| | | 29 | | |
| | | 30 | | public void AddReferredId(string id) |
| | | 31 | | { |
| | 0 | 32 | | if (id == null) |
| | | 33 | | { |
| | 0 | 34 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(id))); |
| | | 35 | | } |
| | 0 | 36 | | _referredIds.Add(id); |
| | 0 | 37 | | } |
| | | 38 | | |
| | | 39 | | public bool ContainsReferredId(string id) |
| | | 40 | | { |
| | 0 | 41 | | if (id == null) |
| | | 42 | | { |
| | 0 | 43 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(id))); |
| | | 44 | | } |
| | 0 | 45 | | return _referredIds.Contains(id); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | public string GetReferredId(int index) |
| | | 49 | | { |
| | 0 | 50 | | return _referredIds[index]; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | public void ReadFrom(XmlDictionaryReader reader) |
| | | 54 | | { |
| | 0 | 55 | | reader.ReadStartElement(ElementName, NamespaceUri); |
| | 0 | 56 | | while (reader.IsStartElement()) |
| | | 57 | | { |
| | 0 | 58 | | string id = DataReference.ReadFrom(reader); |
| | 0 | 59 | | if (_referredIds.Contains(id)) |
| | | 60 | | { |
| | 0 | 61 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 62 | | new Exception(SR.Format("InvalidDataReferenceInReferenceList", "#" + id))); |
| | | 63 | | } |
| | 0 | 64 | | _referredIds.Add(id); |
| | | 65 | | } |
| | 0 | 66 | | reader.ReadEndElement(); // ReferenceList |
| | 0 | 67 | | if (DataReferenceCount == 0) |
| | | 68 | | { |
| | 0 | 69 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new Exception(SR.Format("ReferenceListCannotBe |
| | | 70 | | } |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | public bool TryRemoveReferredId(string id) |
| | | 74 | | { |
| | 0 | 75 | | if (id == null) |
| | | 76 | | { |
| | 0 | 77 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(id))); |
| | | 78 | | } |
| | 0 | 79 | | return _referredIds.Remove(id); |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | public void WriteTo(XmlDictionaryWriter writer, DictionaryManager dictionaryManager) |
| | | 83 | | { |
| | 0 | 84 | | if (DataReferenceCount == 0) |
| | | 85 | | { |
| | 0 | 86 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format("Refer |
| | | 87 | | } |
| | 0 | 88 | | writer.WriteStartElement(NamespacePrefix, ElementName, NamespaceUri); |
| | 0 | 89 | | for (int i = 0; i < DataReferenceCount; i++) |
| | | 90 | | { |
| | 0 | 91 | | DataReference.WriteTo(writer, _referredIds[i]); |
| | | 92 | | } |
| | 0 | 93 | | writer.WriteEndElement(); // ReferenceList |
| | 0 | 94 | | } |
| | | 95 | | |
| | | 96 | | private static class DataReference |
| | | 97 | | { |
| | 0 | 98 | | internal static readonly XmlDictionaryString ElementName = XD.XmlEncryptionDictionary.DataReference; |
| | 0 | 99 | | internal static readonly XmlDictionaryString NamespaceUri = XD.XmlEncryptionDictionary.Namespace; |
| | | 100 | | |
| | | 101 | | public static string ReadFrom(XmlDictionaryReader reader) |
| | | 102 | | { |
| | 0 | 103 | | throw new NotImplementedException(); |
| | | 104 | | //string prefix; |
| | | 105 | | //string uri = XmlHelper.ReadEmptyElementAndRequiredAttribute(reader, ElementName, NamespaceUri, Referen |
| | | 106 | | //if (uri.Length < 2 || uri[0] != '#') |
| | | 107 | | //{ |
| | | 108 | | // throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | | 109 | | // new SecurityMessageSerializationException(SR.GetString(SR.InvalidDataReferenceInReferenceList, |
| | | 110 | | //} |
| | | 111 | | //return uri.Substring(1); |
| | | 112 | | } |
| | | 113 | | |
| | | 114 | | public static void WriteTo(XmlDictionaryWriter writer, string referredId) |
| | | 115 | | { |
| | 0 | 116 | | writer.WriteStartElement(XD.XmlEncryptionDictionary.Prefix.Value, ElementName, NamespaceUri); |
| | 0 | 117 | | writer.WriteStartAttribute(UriAttribute, null); |
| | 0 | 118 | | writer.WriteString("#"); |
| | 0 | 119 | | writer.WriteString(referredId); |
| | 0 | 120 | | writer.WriteEndAttribute(); |
| | 0 | 121 | | writer.WriteEndElement(); |
| | 0 | 122 | | } |
| | | 123 | | } |
| | | 124 | | } |
| | | 125 | | } |