| | | 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.Security.Cryptography.X509Certificates; |
| | | 7 | | using System.Xml; |
| | | 8 | | using CoreWCF.IdentityModel.Claims; |
| | | 9 | | |
| | | 10 | | namespace CoreWCF |
| | | 11 | | { |
| | | 12 | | public abstract class EndpointIdentity |
| | | 13 | | { |
| | | 14 | | private Claim _identityClaim; |
| | | 15 | | private IEqualityComparer<Claim> _claimComparer; |
| | | 16 | | |
| | 27 | 17 | | protected EndpointIdentity() |
| | | 18 | | { |
| | 27 | 19 | | } |
| | | 20 | | |
| | | 21 | | internal void Initialize(Claim identityClaim) |
| | | 22 | | { |
| | 27 | 23 | | if (identityClaim == null) |
| | | 24 | | { |
| | 0 | 25 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(identityClaim)); |
| | | 26 | | } |
| | | 27 | | |
| | 27 | 28 | | Initialize(identityClaim, null); |
| | 27 | 29 | | } |
| | | 30 | | |
| | | 31 | | internal void Initialize(Claim identityClaim, IEqualityComparer<Claim> claimComparer) |
| | | 32 | | { |
| | 27 | 33 | | _identityClaim = identityClaim ?? throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(id |
| | 27 | 34 | | _claimComparer = claimComparer; |
| | 27 | 35 | | } |
| | | 36 | | |
| | | 37 | | internal Claim IdentityClaim |
| | | 38 | | { |
| | | 39 | | get |
| | | 40 | | { |
| | 0 | 41 | | if (_identityClaim == null) |
| | | 42 | | { |
| | 0 | 43 | | EnsureIdentityClaim(); |
| | | 44 | | } |
| | 0 | 45 | | return _identityClaim; |
| | | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | public static EndpointIdentity CreateIdentity(Claim identity) |
| | | 50 | | { |
| | 15 | 51 | | if (identity == null) |
| | | 52 | | { |
| | 0 | 53 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(identity)); |
| | | 54 | | } |
| | | 55 | | |
| | 15 | 56 | | if (identity.ClaimType.Equals(ClaimTypes.Dns)) |
| | | 57 | | { |
| | 5 | 58 | | return new DnsEndpointIdentity(identity); |
| | | 59 | | } |
| | 10 | 60 | | else if (identity.ClaimType.Equals(ClaimTypes.Spn)) |
| | | 61 | | { |
| | 0 | 62 | | return new SpnEndpointIdentity(identity); |
| | | 63 | | } |
| | 10 | 64 | | else if (identity.ClaimType.Equals(ClaimTypes.Upn)) |
| | | 65 | | { |
| | 0 | 66 | | return new UpnEndpointIdentity(identity); |
| | | 67 | | } |
| | 10 | 68 | | else if (identity.ClaimType.Equals(ClaimTypes.Rsa)) |
| | | 69 | | { |
| | 0 | 70 | | throw new PlatformNotSupportedException(); |
| | | 71 | | //return new RsaEndpointIdentity(identity); |
| | | 72 | | } |
| | | 73 | | else |
| | | 74 | | { |
| | 10 | 75 | | return new GeneralEndpointIdentity(identity); |
| | | 76 | | } |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | internal static EndpointIdentity CreateDnsIdentity(string dnsName) |
| | | 80 | | { |
| | 0 | 81 | | return new DnsEndpointIdentity(dnsName); |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | internal static EndpointIdentity CreateSpnIdentity(string spnName) |
| | | 85 | | { |
| | 0 | 86 | | return new SpnEndpointIdentity(spnName); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | internal static EndpointIdentity CreateUpnIdentity(string upnName) |
| | | 90 | | { |
| | 0 | 91 | | return new UpnEndpointIdentity(upnName); |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | public static EndpointIdentity CreateX509CertificateIdentity(X509Certificate2 certificate) |
| | | 95 | | { |
| | 0 | 96 | | return new X509CertificateEndpointIdentity(certificate); |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | internal virtual void EnsureIdentityClaim() |
| | | 100 | | { |
| | 0 | 101 | | } |
| | | 102 | | |
| | | 103 | | public override bool Equals(object obj) |
| | | 104 | | { |
| | 0 | 105 | | if (obj == (object)this) |
| | | 106 | | { |
| | 0 | 107 | | return true; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | // as handles null do we need the double null check? |
| | 0 | 111 | | if (obj == null) |
| | | 112 | | { |
| | 0 | 113 | | return false; |
| | | 114 | | } |
| | | 115 | | |
| | 0 | 116 | | if (!(obj is EndpointIdentity otherIdentity)) |
| | | 117 | | { |
| | 0 | 118 | | return false; |
| | | 119 | | } |
| | | 120 | | |
| | 0 | 121 | | return Matches(otherIdentity.IdentityClaim); |
| | | 122 | | } |
| | | 123 | | |
| | | 124 | | public override int GetHashCode() |
| | | 125 | | { |
| | 0 | 126 | | return GetClaimComparer().GetHashCode(IdentityClaim); |
| | | 127 | | } |
| | | 128 | | |
| | | 129 | | internal bool Matches(Claim claim) |
| | | 130 | | { |
| | 0 | 131 | | return GetClaimComparer().Equals(IdentityClaim, claim); |
| | | 132 | | } |
| | | 133 | | |
| | | 134 | | private IEqualityComparer<Claim> GetClaimComparer() |
| | | 135 | | { |
| | 0 | 136 | | if (_claimComparer == null) |
| | | 137 | | { |
| | 0 | 138 | | throw new PlatformNotSupportedException("EndpointIdentity.GetClaimComparer is not supported."); |
| | | 139 | | } |
| | | 140 | | |
| | 0 | 141 | | return _claimComparer; |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | |
| | | 145 | | internal static EndpointIdentity ReadIdentity(XmlDictionaryReader reader) |
| | | 146 | | { |
| | 0 | 147 | | if (reader == null) |
| | | 148 | | { |
| | 0 | 149 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(reader)); |
| | | 150 | | } |
| | | 151 | | |
| | 0 | 152 | | reader.MoveToContent(); |
| | 0 | 153 | | if (reader.IsEmptyElement) |
| | | 154 | | { |
| | 0 | 155 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.UnexpectedEmptyE |
| | | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | reader.ReadStartElement(XD.AddressingDictionary.Identity, XD.AddressingDictionary.IdentityExtensionNamespace |
| | | 159 | | |
| | | 160 | | |
| | | 161 | | EndpointIdentity readIdentity; |
| | | 162 | | //if (reader.IsStartElement(XD.AddressingDictionary.Spn, XD.AddressingDictionary.IdentityExtensionNamespace) |
| | | 163 | | //{ |
| | | 164 | | // readIdentity = new SpnEndpointIdentity(reader.ReadElementString()); |
| | | 165 | | //} |
| | | 166 | | //else if (reader.IsStartElement(XD.AddressingDictionary.Upn, XD.AddressingDictionary.IdentityExtensionNames |
| | | 167 | | //{ |
| | | 168 | | // readIdentity = new UpnEndpointIdentity(reader.ReadElementString()); |
| | | 169 | | //} |
| | | 170 | | //else |
| | 0 | 171 | | if (reader.IsStartElement(XD.AddressingDictionary.Dns, XD.AddressingDictionary.IdentityExtensionNamespace)) |
| | | 172 | | { |
| | 0 | 173 | | readIdentity = new DnsEndpointIdentity(reader.ReadElementString()); |
| | | 174 | | } |
| | 0 | 175 | | else if (reader.IsStartElement(XD.XmlSignatureDictionary.KeyInfo, XD.XmlSignatureDictionary.Namespace)) |
| | | 176 | | { |
| | 0 | 177 | | reader.ReadStartElement(); |
| | 0 | 178 | | if (reader.IsStartElement(XD.XmlSignatureDictionary.X509Data, XD.XmlSignatureDictionary.Namespace)) |
| | | 179 | | { |
| | 0 | 180 | | throw new PlatformNotSupportedException("EndpointIdentity.ReadIdentity X509CertificateEndpointIdenti |
| | | 181 | | //readIdentity = new X509CertificateEndpointIdentity(reader); |
| | | 182 | | } |
| | 0 | 183 | | else if (reader.IsStartElement(XD.XmlSignatureDictionary.RsaKeyValue, XD.XmlSignatureDictionary.Namespac |
| | | 184 | | { |
| | 0 | 185 | | throw new PlatformNotSupportedException("EndpointIdentity.ReadIdentity RsaEndpointIdentity is not su |
| | | 186 | | } |
| | | 187 | | else |
| | | 188 | | { |
| | 0 | 189 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.Unrecognized |
| | | 190 | | } |
| | | 191 | | //reader.ReadEndElement(); |
| | | 192 | | } |
| | 0 | 193 | | else if (reader.NodeType == XmlNodeType.Element) |
| | | 194 | | { |
| | | 195 | | // |
| | | 196 | | // Something unknown |
| | | 197 | | // |
| | 0 | 198 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.Format(SR.UnrecognizedIden |
| | | 199 | | } |
| | | 200 | | else |
| | | 201 | | { |
| | | 202 | | // |
| | | 203 | | // EndpointIdentity element is empty or some other invalid xml |
| | | 204 | | // |
| | 0 | 205 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new XmlException(SR.InvalidIdentityElement)); |
| | | 206 | | } |
| | | 207 | | |
| | 0 | 208 | | reader.ReadEndElement(); |
| | | 209 | | |
| | 0 | 210 | | return readIdentity; |
| | | 211 | | } |
| | | 212 | | |
| | | 213 | | internal void WriteTo(XmlDictionaryWriter writer) |
| | | 214 | | { |
| | 0 | 215 | | if (writer == null) |
| | | 216 | | { |
| | 0 | 217 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(writer)); |
| | | 218 | | } |
| | | 219 | | |
| | 0 | 220 | | writer.WriteStartElement(XD.AddressingDictionary.Identity, XD.AddressingDictionary.IdentityExtensionNamespac |
| | | 221 | | |
| | 0 | 222 | | WriteContentsTo(writer); |
| | | 223 | | |
| | 0 | 224 | | writer.WriteEndElement(); |
| | 0 | 225 | | } |
| | | 226 | | |
| | | 227 | | internal virtual void WriteContentsTo(XmlDictionaryWriter writer) |
| | | 228 | | { |
| | 0 | 229 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.Format(SR.Unrecog |
| | | 230 | | } |
| | | 231 | | } |
| | | 232 | | } |