| | | 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.ObjectModel; |
| | | 6 | | using System.ComponentModel; |
| | | 7 | | using System.Configuration; |
| | | 8 | | using System.IO; |
| | | 9 | | using System.Text; |
| | | 10 | | using System.Xml; |
| | | 11 | | using CoreWCF.IdentityModel.Tokens; |
| | | 12 | | using CoreWCF.Runtime; |
| | | 13 | | using CoreWCF.Security.Tokens; |
| | | 14 | | |
| | | 15 | | namespace CoreWCF.Configuration |
| | | 16 | | { |
| | | 17 | | public sealed class IssuedTokenParametersElement : ServiceModelConfigurationElement |
| | | 18 | | { |
| | | 19 | | private Collection<IssuedTokenParametersElement> _optionalIssuedTokenParameters = null; |
| | | 20 | | |
| | | 21 | | [ConfigurationProperty(ConfigurationStrings.DefaultMessageSecurityVersion)] |
| | | 22 | | [TypeConverter(typeof(MessageSecurityVersionConverter))] |
| | | 23 | | public MessageSecurityVersion DefaultMessageSecurityVersion |
| | | 24 | | { |
| | 2 | 25 | | get { return (MessageSecurityVersion)base[ConfigurationStrings.DefaultMessageSecurityVersion]; } |
| | 0 | 26 | | set { base[ConfigurationStrings.DefaultMessageSecurityVersion] = value; } |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | [ConfigurationProperty(ConfigurationStrings.AdditionalRequestParameters)] |
| | | 30 | | public XmlElementElementCollection AdditionalRequestParameters |
| | | 31 | | { |
| | 4 | 32 | | get { return (XmlElementElementCollection)base[ConfigurationStrings.AdditionalRequestParameters]; } |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | [ConfigurationProperty(ConfigurationStrings.ClaimTypeRequirements)] |
| | | 36 | | public ClaimTypeElementCollection ClaimTypeRequirements |
| | | 37 | | { |
| | 4 | 38 | | get { return (ClaimTypeElementCollection)base[ConfigurationStrings.ClaimTypeRequirements]; } |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | [ConfigurationProperty(ConfigurationStrings.Issuer)] |
| | | 42 | | public IssuedTokenParametersEndpointAddressElement Issuer |
| | | 43 | | { |
| | 0 | 44 | | get { return (IssuedTokenParametersEndpointAddressElement)base[ConfigurationStrings.Issuer]; } |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | [ConfigurationProperty(ConfigurationStrings.IssuerMetadata)] |
| | | 48 | | public EndpointAddressElementBase IssuerMetadata |
| | | 49 | | { |
| | 0 | 50 | | get { return (EndpointAddressElementBase)base[ConfigurationStrings.IssuerMetadata]; } |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | [ConfigurationProperty(ConfigurationStrings.KeySize, DefaultValue = 0)] |
| | | 54 | | [IntegerValidator(MinValue = 0)] |
| | | 55 | | public int KeySize |
| | | 56 | | { |
| | 2 | 57 | | get { return (int)base[ConfigurationStrings.KeySize]; } |
| | 0 | 58 | | set { base[ConfigurationStrings.KeySize] = value; } |
| | | 59 | | } |
| | | 60 | | |
| | | 61 | | [ConfigurationProperty(ConfigurationStrings.KeyType, DefaultValue = SecurityBindingDefaults.DefaultKeyType)] |
| | | 62 | | public SecurityKeyType KeyType |
| | | 63 | | { |
| | 2 | 64 | | get { return (SecurityKeyType)base[ConfigurationStrings.KeyType]; } |
| | 0 | 65 | | set { base[ConfigurationStrings.KeyType] = value; } |
| | | 66 | | } |
| | | 67 | | |
| | | 68 | | internal Collection<IssuedTokenParametersElement> OptionalIssuedTokenParameters |
| | | 69 | | { |
| | | 70 | | get |
| | | 71 | | { |
| | | 72 | | // OptionalIssuedTokenParameters built on assumption that configuration is writable. |
| | | 73 | | // This should be protected at the callers site. If assumption is invalid, then |
| | | 74 | | // configuration system is in an indeterminate state. Need to stop in a manner that |
| | | 75 | | // user code can not capture. |
| | 0 | 76 | | if (IsReadOnly()) |
| | | 77 | | { |
| | | 78 | | Fx.Assert("IssuedTokenParametersElement.OptionalIssuedTokenParameters should only be called by Admin |
| | 0 | 79 | | DiagnosticUtility.FailFast("IssuedTokenParametersElement.OptionalIssuedTokenParameters should only b |
| | | 80 | | } |
| | | 81 | | |
| | | 82 | | // No need to worry about a race condition here-- this method is not meant to be called by multi-threade |
| | | 83 | | // apps. It is only supposed to be called by svcutil and single threaded equivalents. |
| | 0 | 84 | | if (_optionalIssuedTokenParameters == null) |
| | | 85 | | { |
| | 0 | 86 | | _optionalIssuedTokenParameters = new Collection<IssuedTokenParametersElement>(); |
| | | 87 | | } |
| | 0 | 88 | | return _optionalIssuedTokenParameters; |
| | | 89 | | } |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | |
| | | 93 | | [ConfigurationProperty(ConfigurationStrings.TokenType, DefaultValue = "")] |
| | | 94 | | [StringValidator(MinLength = 0)] |
| | | 95 | | public string TokenType |
| | | 96 | | { |
| | 2 | 97 | | get { return (string)base[ConfigurationStrings.TokenType]; } |
| | | 98 | | set |
| | | 99 | | { |
| | 0 | 100 | | if (string.IsNullOrEmpty(value)) |
| | | 101 | | { |
| | 0 | 102 | | value = string.Empty; |
| | | 103 | | } |
| | | 104 | | |
| | 0 | 105 | | base[ConfigurationStrings.TokenType] = value; |
| | 0 | 106 | | } |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | [ConfigurationProperty(ConfigurationStrings.UseStrTransform, DefaultValue = false)] |
| | | 110 | | public bool UseStrTransform |
| | | 111 | | { |
| | 2 | 112 | | get { return (bool)base[ConfigurationStrings.UseStrTransform]; } |
| | 0 | 113 | | set { base[ConfigurationStrings.UseStrTransform] = value; } |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | internal void ApplyConfiguration(IssuedSecurityTokenParameters parameters) |
| | | 117 | | { |
| | 2 | 118 | | if (parameters == null) |
| | 0 | 119 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException(nameof(parameters))) |
| | | 120 | | |
| | 2 | 121 | | if (AdditionalRequestParameters != null) |
| | | 122 | | { |
| | 4 | 123 | | foreach (XmlElementElement e in AdditionalRequestParameters) |
| | | 124 | | { |
| | 0 | 125 | | parameters.AdditionalRequestParameters.Add(e.XmlElement); |
| | | 126 | | } |
| | | 127 | | } |
| | | 128 | | |
| | 2 | 129 | | if (ClaimTypeRequirements != null) |
| | | 130 | | { |
| | 4 | 131 | | foreach (ClaimTypeElement c in ClaimTypeRequirements) |
| | | 132 | | { |
| | 0 | 133 | | parameters.ClaimTypeRequirements.Add(new ClaimTypeRequirement(c.ClaimType, c.IsOptional)); |
| | | 134 | | } |
| | | 135 | | } |
| | | 136 | | |
| | 2 | 137 | | parameters.KeySize = KeySize; |
| | 2 | 138 | | parameters.KeyType = this.KeyType; |
| | 2 | 139 | | parameters.DefaultMessageSecurityVersion = DefaultMessageSecurityVersion; |
| | 2 | 140 | | parameters.UseStrTransform = UseStrTransform; |
| | | 141 | | |
| | 2 | 142 | | if (!string.IsNullOrEmpty(TokenType)) |
| | | 143 | | { |
| | 0 | 144 | | parameters.TokenType = TokenType; |
| | | 145 | | } |
| | 2 | 146 | | if (PropertyValueOrigin.Default != ElementInformation.Properties[ConfigurationStrings.Issuer].ValueOrigin) |
| | | 147 | | { |
| | | 148 | | //TODO: ? I am not sure of the best way of loading the Issuer EndPoint and Binding |
| | | 149 | | |
| | 1 | 150 | | throw new PlatformNotSupportedException(nameof(Issuer)); |
| | | 151 | | //TODO: Implement IssuedTokenParameters |
| | | 152 | | //this.Issuer.Validate(); |
| | | 153 | | |
| | | 154 | | //TODO: Implement IssuedTokenParameters |
| | | 155 | | //parameters.IssuerAddress = ConfigLoader.LoadEndpointAddress(this.Issuer); |
| | | 156 | | |
| | | 157 | | // if (!string.IsNullOrEmpty(Issuer.Binding)) |
| | | 158 | | //{ |
| | | 159 | | //TODO: Implement IssuedTokenParameters |
| | | 160 | | //parameters.IssuerBinding = ConfigLoader.LookupBinding(this.Issuer.Binding, this.Issuer.BindingConf |
| | | 161 | | //} |
| | | 162 | | } |
| | | 163 | | |
| | 1 | 164 | | if (PropertyValueOrigin.Default != ElementInformation.Properties[ConfigurationStrings.IssuerMetadata].ValueO |
| | | 165 | | { |
| | 1 | 166 | | throw new PlatformNotSupportedException(nameof(IssuerMetadata)); |
| | | 167 | | //TODO: Implement IssuedTokenParameters |
| | | 168 | | //parameters.IssuerMetadataAddress = ConfigLoader.LoadEndpointAddress(this.IssuerMetadata); |
| | | 169 | | } |
| | 0 | 170 | | } |
| | | 171 | | |
| | | 172 | | internal void Copy(IssuedTokenParametersElement source) |
| | | 173 | | { |
| | 0 | 174 | | if (IsReadOnly()) |
| | | 175 | | { |
| | 0 | 176 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.Format(SR. |
| | | 177 | | } |
| | 0 | 178 | | if (null == source) |
| | | 179 | | { |
| | 0 | 180 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(source)); |
| | | 181 | | } |
| | | 182 | | |
| | 0 | 183 | | foreach (XmlElementElement xmlElement in source.AdditionalRequestParameters) |
| | | 184 | | { |
| | 0 | 185 | | XmlElementElement newElement = new XmlElementElement(); |
| | 0 | 186 | | newElement.Copy(xmlElement); |
| | 0 | 187 | | this.AdditionalRequestParameters.Add(newElement); |
| | | 188 | | } |
| | | 189 | | |
| | 0 | 190 | | foreach (ClaimTypeElement c in source.ClaimTypeRequirements) |
| | | 191 | | { |
| | 0 | 192 | | this.ClaimTypeRequirements.Add(new ClaimTypeElement(c.ClaimType, c.IsOptional)); |
| | | 193 | | } |
| | | 194 | | |
| | 0 | 195 | | KeySize = source.KeySize; |
| | 0 | 196 | | KeyType = source.KeyType; |
| | 0 | 197 | | TokenType = source.TokenType; |
| | 0 | 198 | | DefaultMessageSecurityVersion = source.DefaultMessageSecurityVersion; |
| | 0 | 199 | | UseStrTransform = source.UseStrTransform; |
| | | 200 | | |
| | 0 | 201 | | if (PropertyValueOrigin.Default != source.ElementInformation.Properties[ConfigurationStrings.Issuer].ValueOr |
| | | 202 | | { |
| | 0 | 203 | | Issuer.Copy(source.Issuer); |
| | | 204 | | } |
| | 0 | 205 | | if (PropertyValueOrigin.Default != source.ElementInformation.Properties[ConfigurationStrings.IssuerMetadata] |
| | | 206 | | { |
| | 0 | 207 | | IssuerMetadata.Copy(source.IssuerMetadata); |
| | | 208 | | } |
| | 0 | 209 | | } |
| | | 210 | | |
| | | 211 | | internal IssuedSecurityTokenParameters Create(bool createTemplateOnly, SecurityKeyType templateKeyType) |
| | | 212 | | { |
| | 2 | 213 | | IssuedSecurityTokenParameters result = new IssuedSecurityTokenParameters(); |
| | 2 | 214 | | if (!createTemplateOnly) |
| | | 215 | | { |
| | 2 | 216 | | ApplyConfiguration(result); |
| | | 217 | | } |
| | | 218 | | else |
| | | 219 | | { |
| | 0 | 220 | | result.KeyType = templateKeyType; |
| | | 221 | | } |
| | 0 | 222 | | return result; |
| | | 223 | | } |
| | | 224 | | |
| | | 225 | | internal void InitializeFrom(IssuedSecurityTokenParameters source, bool initializeNestedBindings) |
| | | 226 | | { |
| | 0 | 227 | | if (null == source) |
| | 0 | 228 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(source)); |
| | | 229 | | |
| | 0 | 230 | | SetPropertyValueIfNotDefaultValue(ConfigurationStrings.KeyType, source.KeyType); |
| | 0 | 231 | | if (source.KeySize > 0) |
| | | 232 | | { |
| | 0 | 233 | | SetPropertyValueIfNotDefaultValue(ConfigurationStrings.KeySize, source.KeySize); |
| | | 234 | | } |
| | 0 | 235 | | SetPropertyValueIfNotDefaultValue(ConfigurationStrings.TokenType, source.TokenType); |
| | 0 | 236 | | SetPropertyValueIfNotDefaultValue(ConfigurationStrings.UseStrTransform, source.UseStrTransform); |
| | | 237 | | |
| | 0 | 238 | | if (source.IssuerAddress != null) |
| | 0 | 239 | | Issuer.InitializeFrom(source.IssuerAddress); |
| | | 240 | | |
| | 0 | 241 | | if (source.DefaultMessageSecurityVersion != null) |
| | 0 | 242 | | SetPropertyValueIfNotDefaultValue(ConfigurationStrings.DefaultMessageSecurityVersion, source.DefaultMess |
| | | 243 | | |
| | 0 | 244 | | if (source.IssuerBinding != null && initializeNestedBindings) |
| | | 245 | | { |
| | 0 | 246 | | Issuer.BindingConfiguration = Issuer.Address.ToString(); |
| | | 247 | | |
| | | 248 | | |
| | | 249 | | //TODO: Implement IssuedTokenParameters |
| | 0 | 250 | | throw new PlatformNotSupportedException(nameof(IssuedTokenParametersEndpointAddressElement)); |
| | | 251 | | //string bindingSectionName; |
| | | 252 | | |
| | | 253 | | //BindingsSection.TryAdd(this.Issuer.BindingConfiguration, |
| | | 254 | | // source.IssuerBinding, |
| | | 255 | | // out bindingSectionName); |
| | | 256 | | //this.Issuer.Binding = bindingSectionName; |
| | | 257 | | } |
| | | 258 | | |
| | 0 | 259 | | if (source.IssuerMetadataAddress != null) |
| | | 260 | | { |
| | 0 | 261 | | IssuerMetadata.InitializeFrom(source.IssuerMetadataAddress); |
| | | 262 | | } |
| | | 263 | | |
| | 0 | 264 | | foreach (XmlElement element in source.AdditionalRequestParameters) |
| | | 265 | | { |
| | 0 | 266 | | this.AdditionalRequestParameters.Add(new XmlElementElement(element)); |
| | | 267 | | } |
| | | 268 | | |
| | 0 | 269 | | foreach (ClaimTypeRequirement c in source.ClaimTypeRequirements) |
| | | 270 | | { |
| | 0 | 271 | | this.ClaimTypeRequirements.Add(new ClaimTypeElement(c.ClaimType, c.IsOptional)); |
| | | 272 | | } |
| | | 273 | | |
| | | 274 | | //TODO: Implement IssuedTokenParameters |
| | | 275 | | //foreach (IssuedSecurityTokenParameters.AlternativeIssuerEndpoint alternativeIssuer in source.AlternativeIs |
| | | 276 | | //{ |
| | | 277 | | // IssuedTokenParametersElement element = new IssuedTokenParametersElement(); |
| | | 278 | | // element.Issuer.InitializeFrom(alternativeIssuer.IssuerAddress); |
| | | 279 | | // if (initializeNestedBindings) |
| | | 280 | | // { |
| | | 281 | | // element.Issuer.BindingConfiguration = element.Issuer.Address.ToString(); |
| | | 282 | | // string bindingSectionName; |
| | | 283 | | // BindingsSection.TryAdd(element.Issuer.BindingConfiguration, |
| | | 284 | | // alternativeIssuer.IssuerBinding, |
| | | 285 | | // out bindingSectionName); |
| | | 286 | | // element.Issuer.Binding = bindingSectionName; |
| | | 287 | | // } |
| | | 288 | | // this.OptionalIssuedTokenParameters.Add(element); |
| | | 289 | | //} |
| | 0 | 290 | | } |
| | | 291 | | |
| | | 292 | | protected override bool SerializeToXmlElement(XmlWriter writer, string elementName) |
| | | 293 | | { |
| | 0 | 294 | | bool writeMe = base.SerializeToXmlElement(writer, elementName); |
| | 0 | 295 | | bool writeComment = OptionalIssuedTokenParameters.Count > 0; |
| | 0 | 296 | | if (writeComment && writer != null) |
| | | 297 | | { |
| | 0 | 298 | | MemoryStream memoryStream = new MemoryStream(); |
| | 0 | 299 | | using (XmlTextWriter commentWriter = new XmlTextWriter(memoryStream, Encoding.UTF8)) |
| | | 300 | | { |
| | 0 | 301 | | commentWriter.Formatting = Formatting.Indented; |
| | 0 | 302 | | commentWriter.WriteStartElement(ConfigurationStrings.AlternativeIssuedTokenParameters); |
| | 0 | 303 | | foreach (IssuedTokenParametersElement element in OptionalIssuedTokenParameters) |
| | | 304 | | { |
| | 0 | 305 | | element.SerializeToXmlElement(commentWriter, ConfigurationStrings.IssuedTokenParameters); |
| | | 306 | | } |
| | 0 | 307 | | commentWriter.WriteEndElement(); |
| | 0 | 308 | | commentWriter.Flush(); |
| | 0 | 309 | | string commentString = new UTF8Encoding().GetString(memoryStream.GetBuffer(), 0, (int)memoryStream.L |
| | 0 | 310 | | writer.WriteComment(commentString.Substring(1, commentString.Length - 1)); |
| | 0 | 311 | | commentWriter.Close(); |
| | 0 | 312 | | } |
| | | 313 | | } |
| | 0 | 314 | | return writeMe || writeComment; |
| | | 315 | | } |
| | | 316 | | |
| | | 317 | | protected override void Unmerge(ConfigurationElement sourceElement, ConfigurationElement parentElement, Configur |
| | | 318 | | { |
| | 0 | 319 | | if (sourceElement is IssuedTokenParametersElement) |
| | | 320 | | { |
| | 0 | 321 | | IssuedTokenParametersElement source = (IssuedTokenParametersElement)sourceElement; |
| | 0 | 322 | | _optionalIssuedTokenParameters = source._optionalIssuedTokenParameters; |
| | | 323 | | } |
| | | 324 | | |
| | 0 | 325 | | base.Unmerge(sourceElement, parentElement, saveMode); |
| | 0 | 326 | | } |
| | | 327 | | } |
| | | 328 | | } |