1 //------------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //------------------------------------------------------------------------------
4 
5 namespace System.ServiceModel.Configuration
6 {
7     using System.Configuration;
8     using System.ServiceModel.Channels;
9     using System.ServiceModel.Description;
10     using System.Globalization;
11     using System.Net;
12     using System.Net.Security;
13     using System.ServiceModel;
14     using System.ServiceModel.Security;
15     using System.ServiceModel.Security.Tokens;
16     using System.IdentityModel.Tokens;
17     using System.ComponentModel;
18     using System.Xml;
19 
20     public sealed partial class FederatedMessageSecurityOverHttpElement : ServiceModelConfigurationElement
21     {
22 
23         [ConfigurationProperty(ConfigurationStrings.AlgorithmSuite, DefaultValue = ConfigurationStrings.Default)]
24         [TypeConverter(typeof(SecurityAlgorithmSuiteConverter))]
25         public SecurityAlgorithmSuite AlgorithmSuite
26         {
27             get { return (SecurityAlgorithmSuite)base[ConfigurationStrings.AlgorithmSuite]; }
28             set { base[ConfigurationStrings.AlgorithmSuite] = value; }
29         }
30 
31         [ConfigurationProperty(ConfigurationStrings.ClaimTypeRequirements)]
32         public ClaimTypeElementCollection ClaimTypeRequirements
33         {
34             get { return (ClaimTypeElementCollection)base[ConfigurationStrings.ClaimTypeRequirements]; }
35         }
36 
37         [ConfigurationProperty(ConfigurationStrings.EstablishSecurityContext, DefaultValue = FederatedMessageSecurityOverHttp.DefaultEstablishSecurityContext)]
38         public bool EstablishSecurityContext
39         {
40             get { return (bool)base[ConfigurationStrings.EstablishSecurityContext]; }
41             set { base[ConfigurationStrings.EstablishSecurityContext] = value; }
42         }
43 
44         [ConfigurationProperty(ConfigurationStrings.IssuedKeyType, DefaultValue = FederatedMessageSecurityOverHttp.DefaultIssuedKeyType)]
45         [ServiceModelEnumValidator(typeof(System.IdentityModel.Tokens.SecurityKeyTypeHelper))]
46         public SecurityKeyType IssuedKeyType
47         {
48             get { return (SecurityKeyType)base[ConfigurationStrings.IssuedKeyType]; }
49             set { base[ConfigurationStrings.IssuedKeyType] = value; }
50         }
51 
52         [ConfigurationProperty(ConfigurationStrings.IssuedTokenType, DefaultValue = "")]
53         [StringValidator(MinLength = 0)]
54         public string IssuedTokenType
55         {
56             get { return (string)base[ConfigurationStrings.IssuedTokenType]; }
57             set
58             {
59                 if (String.IsNullOrEmpty(value))
60                 {
61                     value = string.Empty;
62                 }
63 
64                 base[ConfigurationStrings.IssuedTokenType] = value;
65             }
66         }
67 
68         [ConfigurationProperty(ConfigurationStrings.Issuer)]
69         public IssuedTokenParametersEndpointAddressElement Issuer
70         {
71             get { return (IssuedTokenParametersEndpointAddressElement)base[ConfigurationStrings.Issuer]; }
72         }
73 
74         [ConfigurationProperty(ConfigurationStrings.IssuerMetadata)]
75         public EndpointAddressElementBase IssuerMetadata
76         {
77             get { return (EndpointAddressElementBase)base[ConfigurationStrings.IssuerMetadata]; }
78         }
79 
80         [ConfigurationProperty(ConfigurationStrings.NegotiateServiceCredential, DefaultValue = MessageSecurityOverHttp.DefaultNegotiateServiceCredential)]
81         public bool NegotiateServiceCredential
82         {
83             get { return (bool)base[ConfigurationStrings.NegotiateServiceCredential]; }
84             set { base[ConfigurationStrings.NegotiateServiceCredential] = value; }
85         }
86 
87         [ConfigurationProperty(ConfigurationStrings.TokenRequestParameters)]
88         public XmlElementElementCollection TokenRequestParameters
89         {
90             get { return (XmlElementElementCollection)base[ConfigurationStrings.TokenRequestParameters]; }
91         }
92 
ApplyConfiguration(FederatedMessageSecurityOverHttp security)93         internal void ApplyConfiguration(FederatedMessageSecurityOverHttp security)
94         {
95             if (security == null)
96             {
97                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("security");
98             }
99             security.NegotiateServiceCredential = this.NegotiateServiceCredential;
100             security.AlgorithmSuite = this.AlgorithmSuite;
101             security.IssuedKeyType = this.IssuedKeyType;
102             security.EstablishSecurityContext = this.EstablishSecurityContext;
103             if (!string.IsNullOrEmpty(this.IssuedTokenType))
104             {
105                 security.IssuedTokenType = this.IssuedTokenType;
106             }
107             if (PropertyValueOrigin.Default != this.ElementInformation.Properties[ConfigurationStrings.Issuer].ValueOrigin)
108             {
109                 security.IssuerAddress = ConfigLoader.LoadEndpointAddress(this.Issuer);
110 
111                 if (!string.IsNullOrEmpty(this.Issuer.Binding))
112                 {
113                     security.IssuerBinding = ConfigLoader.LookupBinding(this.Issuer.Binding, this.Issuer.BindingConfiguration, this.EvaluationContext);
114                 }
115             }
116             if (PropertyValueOrigin.Default != this.ElementInformation.Properties[ConfigurationStrings.IssuerMetadata].ValueOrigin)
117             {
118                 security.IssuerMetadataAddress = ConfigLoader.LoadEndpointAddress(this.IssuerMetadata);
119             }
120             foreach (XmlElementElement xmlElement in this.TokenRequestParameters)
121             {
122                 security.TokenRequestParameters.Add(xmlElement.XmlElement);
123             }
124             foreach (ClaimTypeElement claimType in this.ClaimTypeRequirements)
125             {
126                 security.ClaimTypeRequirements.Add(new ClaimTypeRequirement(claimType.ClaimType, claimType.IsOptional));
127             }
128         }
129 
InitializeFrom(FederatedMessageSecurityOverHttp security)130         internal void InitializeFrom(FederatedMessageSecurityOverHttp security)
131         {
132             if (security == null)
133             {
134                 throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("security");
135             }
136             SetPropertyValueIfNotDefaultValue(ConfigurationStrings.NegotiateServiceCredential, security.NegotiateServiceCredential);
137             SetPropertyValueIfNotDefaultValue(ConfigurationStrings.AlgorithmSuite, security.AlgorithmSuite);
138             SetPropertyValueIfNotDefaultValue(ConfigurationStrings.IssuedKeyType, security.IssuedKeyType);
139             SetPropertyValueIfNotDefaultValue(ConfigurationStrings.EstablishSecurityContext, security.EstablishSecurityContext);
140             if (security.IssuedTokenType != null)
141             {
142                 this.IssuedTokenType = security.IssuedTokenType;
143             }
144             if (security.IssuerAddress != null)
145             {
146                 this.Issuer.InitializeFrom(security.IssuerAddress);
147             }
148             if (security.IssuerMetadataAddress != null)
149             {
150                 this.IssuerMetadata.InitializeFrom(security.IssuerMetadataAddress);
151             }
152             string bindingType = null;
153             if (security.IssuerBinding != null)
154             {
155                 if (null == this.Issuer.Address)
156                 {
157                     throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ConfigurationErrorsException(SR.GetString(SR.ConfigNullIssuerAddress)));
158                 }
159 
160                 this.Issuer.BindingConfiguration = this.Issuer.Address.ToString();
161                 BindingsSection.TryAdd(this.Issuer.BindingConfiguration,
162                     security.IssuerBinding, out bindingType);
163                 this.Issuer.Binding = bindingType;
164             }
165             foreach (XmlElement element in security.TokenRequestParameters)
166             {
167                 this.TokenRequestParameters.Add(new XmlElementElement(element));
168             }
169             foreach (ClaimTypeRequirement claimTypeRequirement in security.ClaimTypeRequirements)
170             {
171                 ClaimTypeElement element = new ClaimTypeElement(claimTypeRequirement.ClaimType, claimTypeRequirement.IsOptional);
172                 this.ClaimTypeRequirements.Add(element);
173             }
174         }
175     }
176 }
177