1 //-----------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation.  All rights reserved.
3 //-----------------------------------------------------------------------------
4 
5 namespace System.ServiceModel.Security.Tokens
6 {
7     using System;
8     using System.Text;
9     using System.ServiceModel.Channels;
10     using System.ServiceModel;
11     using System.ServiceModel.Description;
12     using System.Xml;
13     using System.ServiceModel.Security;
14     using System.Collections.Generic;
15     using System.IdentityModel.Tokens;
16     using System.IdentityModel.Selectors;
17     using System.Globalization;
18 
19     public abstract class ServiceModelSecurityTokenRequirement : SecurityTokenRequirement
20     {
21         protected const string Namespace = "http://schemas.microsoft.com/ws/2006/05/servicemodel/securitytokenrequirement";
22         const string securityAlgorithmSuiteProperty = Namespace + "/SecurityAlgorithmSuite";
23         const string securityBindingElementProperty = Namespace + "/SecurityBindingElement";
24         const string issuerAddressProperty = Namespace + "/IssuerAddress";
25         const string issuerBindingProperty = Namespace + "/IssuerBinding";
26         const string secureConversationSecurityBindingElementProperty = Namespace + "/SecureConversationSecurityBindingElement";
27         const string supportSecurityContextCancellationProperty = Namespace + "/SupportSecurityContextCancellation";
28         const string messageSecurityVersionProperty = Namespace + "/MessageSecurityVersion";
29         const string defaultMessageSecurityVersionProperty = Namespace + "/DefaultMessageSecurityVersion";
30         const string issuerBindingContextProperty = Namespace + "/IssuerBindingContext";
31         const string transportSchemeProperty = Namespace + "/TransportScheme";
32         const string isInitiatorProperty = Namespace + "/IsInitiator";
33         const string targetAddressProperty = Namespace + "/TargetAddress";
34         const string viaProperty = Namespace + "/Via";
35         const string listenUriProperty = Namespace + "/ListenUri";
36         const string auditLogLocationProperty = Namespace + "/AuditLogLocation";
37         const string suppressAuditFailureProperty = Namespace + "/SuppressAuditFailure";
38         const string messageAuthenticationAuditLevelProperty = Namespace + "/MessageAuthenticationAuditLevel";
39         const string isOutOfBandTokenProperty = Namespace + "/IsOutOfBandToken";
40         const string preferSslCertificateAuthenticatorProperty = Namespace + "/PreferSslCertificateAuthenticator";
41 
42         // the following properties dont have top level OM properties but are part of the property bag
43         const string supportingTokenAttachmentModeProperty = Namespace + "/SupportingTokenAttachmentMode";
44         const string messageDirectionProperty = Namespace + "/MessageDirection";
45         const string httpAuthenticationSchemeProperty = Namespace + "/HttpAuthenticationScheme";
46         const string issuedSecurityTokenParametersProperty = Namespace + "/IssuedSecurityTokenParameters";
47         const string privacyNoticeUriProperty = Namespace + "/PrivacyNoticeUri";
48         const string privacyNoticeVersionProperty = Namespace + "/PrivacyNoticeVersion";
49         const string duplexClientLocalAddressProperty = Namespace + "/DuplexClientLocalAddress";
50         const string endpointFilterTableProperty = Namespace + "/EndpointFilterTable";
51         const string channelParametersCollectionProperty = Namespace + "/ChannelParametersCollection";
52         const string extendedProtectionPolicy = Namespace + "/ExtendedProtectionPolicy";
53 
54         const bool defaultSupportSecurityContextCancellation = false;
55 
ServiceModelSecurityTokenRequirement()56         protected ServiceModelSecurityTokenRequirement()
57             : base()
58         {
59             this.Properties[SupportSecurityContextCancellationProperty] = defaultSupportSecurityContextCancellation;
60         }
61 
62         static public string SecurityAlgorithmSuiteProperty { get { return securityAlgorithmSuiteProperty; } }
63         static public string SecurityBindingElementProperty { get { return securityBindingElementProperty; } }
64         static public string IssuerAddressProperty { get { return issuerAddressProperty; } }
65         static public string IssuerBindingProperty { get { return issuerBindingProperty; } }
66         static public string SecureConversationSecurityBindingElementProperty { get { return secureConversationSecurityBindingElementProperty; } }
67         static public string SupportSecurityContextCancellationProperty { get { return supportSecurityContextCancellationProperty; } }
68         static public string MessageSecurityVersionProperty { get { return messageSecurityVersionProperty; } }
69         static internal string DefaultMessageSecurityVersionProperty { get { return defaultMessageSecurityVersionProperty; } }
70         static public string IssuerBindingContextProperty { get { return issuerBindingContextProperty; } }
71         static public string TransportSchemeProperty { get { return transportSchemeProperty; } }
72         static public string IsInitiatorProperty { get { return isInitiatorProperty; } }
73         static public string TargetAddressProperty { get { return targetAddressProperty; } }
74         static public string ViaProperty { get { return viaProperty; } }
75         static public string ListenUriProperty { get { return listenUriProperty; } }
76         static public string AuditLogLocationProperty { get { return auditLogLocationProperty; } }
77         static public string SuppressAuditFailureProperty { get { return suppressAuditFailureProperty; } }
78         static public string MessageAuthenticationAuditLevelProperty { get { return messageAuthenticationAuditLevelProperty; } }
79         static public string IsOutOfBandTokenProperty { get { return isOutOfBandTokenProperty; } }
80         static public string PreferSslCertificateAuthenticatorProperty { get { return preferSslCertificateAuthenticatorProperty; } }
81 
82         static public string SupportingTokenAttachmentModeProperty { get { return supportingTokenAttachmentModeProperty; } }
83         static public string MessageDirectionProperty { get { return messageDirectionProperty; } }
84         static public string HttpAuthenticationSchemeProperty { get { return httpAuthenticationSchemeProperty; } }
85         static public string IssuedSecurityTokenParametersProperty { get { return issuedSecurityTokenParametersProperty; } }
86         static public string PrivacyNoticeUriProperty { get { return privacyNoticeUriProperty; } }
87         static public string PrivacyNoticeVersionProperty { get { return privacyNoticeVersionProperty; } }
88         static public string DuplexClientLocalAddressProperty { get { return duplexClientLocalAddressProperty; } }
89         static public string EndpointFilterTableProperty { get { return endpointFilterTableProperty; } }
90         static public string ChannelParametersCollectionProperty { get { return channelParametersCollectionProperty; } }
91         static public string ExtendedProtectionPolicy { get { return extendedProtectionPolicy; } }
92 
93         public bool IsInitiator
94         {
95             get
96             {
97                 return GetPropertyOrDefault<bool>(IsInitiatorProperty, false);
98             }
99         }
100 
101         public SecurityAlgorithmSuite SecurityAlgorithmSuite
102         {
103             get
104             {
105                 return GetPropertyOrDefault<SecurityAlgorithmSuite>(SecurityAlgorithmSuiteProperty, null);
106             }
107             set
108             {
109                 this.Properties[SecurityAlgorithmSuiteProperty] = value;
110             }
111         }
112 
113         public SecurityBindingElement SecurityBindingElement
114         {
115             get
116             {
117                 return GetPropertyOrDefault<SecurityBindingElement>(SecurityBindingElementProperty, null);
118             }
119             set
120             {
121                 this.Properties[SecurityBindingElementProperty] = value;
122             }
123         }
124 
125         public EndpointAddress IssuerAddress
126         {
127             get
128             {
129                 return GetPropertyOrDefault<EndpointAddress>(IssuerAddressProperty, null);
130             }
131             set
132             {
133                 this.Properties[IssuerAddressProperty] = value;
134             }
135         }
136 
137         public Binding IssuerBinding
138         {
139             get
140             {
141                 return GetPropertyOrDefault<Binding>(IssuerBindingProperty, null);
142             }
143             set
144             {
145                 this.Properties[IssuerBindingProperty] = value;
146             }
147         }
148 
149         public SecurityBindingElement SecureConversationSecurityBindingElement
150         {
151             get
152             {
153                 return GetPropertyOrDefault<SecurityBindingElement>(SecureConversationSecurityBindingElementProperty, null);
154             }
155             set
156             {
157                 this.Properties[SecureConversationSecurityBindingElementProperty] = value;
158             }
159         }
160 
161         public SecurityTokenVersion MessageSecurityVersion
162         {
163             get
164             {
165                 return GetPropertyOrDefault<SecurityTokenVersion>(MessageSecurityVersionProperty, null);
166             }
167             set
168             {
169                 this.Properties[MessageSecurityVersionProperty] = value;
170             }
171         }
172 
173         internal MessageSecurityVersion DefaultMessageSecurityVersion
174         {
175             get
176             {
177                 MessageSecurityVersion messageSecurityVersion;
178                 return (this.TryGetProperty<MessageSecurityVersion>(DefaultMessageSecurityVersionProperty, out messageSecurityVersion)) ? messageSecurityVersion : null;
179             }
180             set
181             {
182                 this.Properties[DefaultMessageSecurityVersionProperty] = (object)value;
183             }
184         }
185 
186         public string TransportScheme
187         {
188             get
189             {
190                 return GetPropertyOrDefault<string>(TransportSchemeProperty, null);
191             }
192             set
193             {
194                 this.Properties[TransportSchemeProperty] = value;
195             }
196         }
197 
198         internal bool SupportSecurityContextCancellation
199         {
200             get
201             {
202                 return GetPropertyOrDefault<bool>(SupportSecurityContextCancellationProperty, defaultSupportSecurityContextCancellation);
203             }
204             set
205             {
206                 this.Properties[SupportSecurityContextCancellationProperty] = value;
207             }
208         }
209 
210         internal EndpointAddress DuplexClientLocalAddress
211         {
212             get
213             {
214                 return GetPropertyOrDefault<EndpointAddress>(duplexClientLocalAddressProperty, null);
215             }
216             set
217             {
218                 this.Properties[duplexClientLocalAddressProperty] = value;
219             }
220         }
221 
GetPropertyOrDefault(string propertyName, TValue defaultValue)222         internal TValue GetPropertyOrDefault<TValue>(string propertyName, TValue defaultValue)
223         {
224             TValue result;
225             if (!TryGetProperty<TValue>(propertyName, out result))
226             {
227                 result = defaultValue;
228             }
229             return result;
230         }
231 
InternalToString()232         internal string InternalToString()
233         {
234             StringBuilder sb = new StringBuilder();
235 
236             sb.AppendLine(String.Format(CultureInfo.InvariantCulture, "{0}:", this.GetType().ToString()));
237             foreach (string propertyName in this.Properties.Keys)
238             {
239                 object propertyValue = this.Properties[propertyName];
240                 sb.AppendLine(String.Format(CultureInfo.InvariantCulture, "PropertyName: {0}", propertyName));
241                 sb.AppendLine(String.Format(CultureInfo.InvariantCulture, "PropertyValue: {0}", propertyValue));
242                 sb.AppendLine(String.Format(CultureInfo.InvariantCulture, "---"));
243             }
244             return sb.ToString().Trim();
245         }
246     }
247 }
248