Merge branch 'master' of github.com:tgiphil/mono
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Configuration / ServiceCredentialsElement.cs
1 //
2 // ServiceCredentialsElement.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2006 Novell, Inc.  http://www.novell.com
8 //
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
16 // 
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
19 // 
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27 //
28
29 using System;
30 using System.Collections;
31 using System.Collections.Generic;
32 using System.Collections.ObjectModel;
33 using System.ComponentModel;
34 using System.Configuration;
35 using System.Net;
36 using System.Net.Security;
37 using System.Reflection;
38 using System.Security.Cryptography.X509Certificates;
39 using System.Security.Principal;
40 using System.IdentityModel.Claims;
41 using System.IdentityModel.Policy;
42 using System.IdentityModel.Selectors;
43 using System.IdentityModel.Tokens;
44 using System.ServiceModel;
45 using System.ServiceModel.Channels;
46 using System.ServiceModel.Description;
47 using System.ServiceModel.Diagnostics;
48 using System.ServiceModel.Dispatcher;
49 using System.ServiceModel.MsmqIntegration;
50 using System.ServiceModel.PeerResolvers;
51 using System.ServiceModel.Security;
52 using System.Runtime.Serialization;
53 using System.Text;
54 using System.Web.Security;
55 using System.Xml;
56
57 namespace System.ServiceModel.Configuration
58 {
59         public sealed class ServiceCredentialsElement
60                  : BehaviorExtensionElement
61         {
62                 public ServiceCredentialsElement () {
63                 }
64
65
66                 // Properties
67
68                 public override Type BehaviorType {
69                         get { return typeof (ServiceCredentials); }
70                 }
71
72                 [ConfigurationProperty ("clientCertificate",
73                          Options = ConfigurationPropertyOptions.None)]
74                 public X509InitiatorCertificateServiceElement ClientCertificate {
75                         get { return (X509InitiatorCertificateServiceElement) base ["clientCertificate"]; }
76                 }
77
78                 [ConfigurationProperty ("issuedTokenAuthentication",
79                          Options = ConfigurationPropertyOptions.None)]
80                 public IssuedTokenServiceElement IssuedTokenAuthentication {
81                         get { return (IssuedTokenServiceElement) base ["issuedTokenAuthentication"]; }
82                 }
83
84                 [ConfigurationProperty ("peer",
85                          Options = ConfigurationPropertyOptions.None)]
86                 public PeerCredentialElement Peer {
87                         get { return (PeerCredentialElement) base ["peer"]; }
88                 }
89
90                 protected override ConfigurationPropertyCollection Properties {
91                         get { return base.Properties; }
92                 }
93
94                 [ConfigurationProperty ("secureConversationAuthentication",
95                          Options = ConfigurationPropertyOptions.None)]
96                 public SecureConversationServiceElement SecureConversationAuthentication {
97                         get { return (SecureConversationServiceElement) base ["secureConversationAuthentication"]; }
98                 }
99
100                 [ConfigurationProperty ("serviceCertificate",
101                          Options = ConfigurationPropertyOptions.None)]
102                 public X509RecipientCertificateServiceElement ServiceCertificate {
103                         get { return (X509RecipientCertificateServiceElement) base ["serviceCertificate"]; }
104                 }
105
106                 [StringValidator (MinLength = 0,
107                         MaxLength = int.MaxValue,
108                          InvalidCharacters = null)]
109                 [ConfigurationProperty ("type",
110                          Options = ConfigurationPropertyOptions.None,
111                          DefaultValue = "")]
112                 public string Type {
113                         get { return (string) base ["type"]; }
114                         set { base ["type"] = value; }
115                 }
116
117                 [ConfigurationProperty ("userNameAuthentication",
118                          Options = ConfigurationPropertyOptions.None)]
119                 public UserNameServiceElement UserNameAuthentication {
120                         get { return (UserNameServiceElement) base ["userNameAuthentication"]; }
121                 }
122
123                 [ConfigurationProperty ("windowsAuthentication",
124                          Options = ConfigurationPropertyOptions.None)]
125                 public WindowsServiceElement WindowsAuthentication {
126                         get { return (WindowsServiceElement) base ["windowsAuthentication"]; }
127                 }
128
129                 [MonoTODO]
130                 protected internal override object CreateBehavior ()
131                 {
132                         var sb = new ServiceCredentials ();
133
134                         // IssuedToken
135                         foreach (AllowedAudienceUriElement ae in IssuedTokenAuthentication.AllowedAudienceUris)
136                                 sb.IssuedTokenAuthentication.AllowedAudienceUris.Add (ae.AllowedAudienceUri);
137                         sb.IssuedTokenAuthentication.AllowUntrustedRsaIssuers = IssuedTokenAuthentication.AllowUntrustedRsaIssuers;
138                         sb.IssuedTokenAuthentication.AudienceUriMode = IssuedTokenAuthentication.AudienceUriMode;
139
140                         if (!String.IsNullOrEmpty (IssuedTokenAuthentication.CustomCertificateValidatorType))
141                         sb.IssuedTokenAuthentication.CustomCertificateValidator = (X509CertificateValidator) CreateInstance (IssuedTokenAuthentication.CustomCertificateValidatorType);
142                         sb.IssuedTokenAuthentication.CertificateValidationMode = IssuedTokenAuthentication.CertificateValidationMode;
143                         sb.IssuedTokenAuthentication.RevocationMode = IssuedTokenAuthentication.RevocationMode;
144                         sb.IssuedTokenAuthentication.TrustedStoreLocation = IssuedTokenAuthentication.TrustedStoreLocation;
145                         foreach (X509CertificateTrustedIssuerElement ce in IssuedTokenAuthentication.KnownCertificates)
146                                 sb.IssuedTokenAuthentication.KnownCertificates.Add (GetCertificate (ce.StoreLocation, ce.StoreName, ce.X509FindType, ce.FindValue));
147
148                         sb.IssuedTokenAuthentication.SamlSerializer = (SamlSerializer) CreateInstance (IssuedTokenAuthentication.SamlSerializerType);
149
150
151                         // Peer
152                         if (!String.IsNullOrEmpty (Peer.Certificate.FindValue))
153                                 sb.Peer.SetCertificate (Peer.Certificate.StoreLocation, Peer.Certificate.StoreName, Peer.Certificate.X509FindType, Peer.Certificate.FindValue);
154                         // sb.Peer.MeshPassword = /* cannot fill it here */
155                         sb.Peer.MessageSenderAuthentication.CustomCertificateValidator = (X509CertificateValidator) CreateInstance (Peer.MessageSenderAuthentication.CustomCertificateValidatorType);
156                         sb.Peer.MessageSenderAuthentication.CertificateValidationMode = Peer.MessageSenderAuthentication.CertificateValidationMode;
157                         sb.Peer.MessageSenderAuthentication.RevocationMode = Peer.MessageSenderAuthentication.RevocationMode;
158                         sb.Peer.MessageSenderAuthentication.TrustedStoreLocation = Peer.MessageSenderAuthentication.TrustedStoreLocation;
159                         sb.Peer.PeerAuthentication.CustomCertificateValidator = (X509CertificateValidator) CreateInstance (Peer.PeerAuthentication.CustomCertificateValidatorType);
160                         sb.Peer.PeerAuthentication.CertificateValidationMode = Peer.PeerAuthentication.CertificateValidationMode;
161                         sb.Peer.PeerAuthentication.RevocationMode = Peer.PeerAuthentication.RevocationMode;
162                         sb.Peer.PeerAuthentication.TrustedStoreLocation = Peer.PeerAuthentication.TrustedStoreLocation;
163
164                         // WSSC
165                         sb.SecureConversationAuthentication.SecurityStateEncoder = (SecurityStateEncoder) CreateInstance (SecureConversationAuthentication.SecurityStateEncoderType);
166
167                         // X509
168                         if (!String.IsNullOrEmpty (ServiceCertificate.FindValue))
169                                 sb.ServiceCertificate.SetCertificate (ServiceCertificate.StoreLocation, ServiceCertificate.StoreName, ServiceCertificate.X509FindType, ServiceCertificate.FindValue);
170
171                         // UserNamePassword
172                         sb.UserNameAuthentication.CachedLogonTokenLifetime = UserNameAuthentication.CachedLogonTokenLifetime;
173                         sb.UserNameAuthentication.CacheLogonTokens = UserNameAuthentication.CacheLogonTokens;
174                         sb.UserNameAuthentication.CustomUserNamePasswordValidator = (UserNamePasswordValidator) CreateInstance (UserNameAuthentication.CustomUserNamePasswordValidatorType);
175                         sb.UserNameAuthentication.IncludeWindowsGroups = UserNameAuthentication.IncludeWindowsGroups;
176                         sb.UserNameAuthentication.MaxCachedLogonTokens = UserNameAuthentication.MaxCachedLogonTokens;
177                         sb.UserNameAuthentication.MembershipProvider = (MembershipProvider) CreateInstance (UserNameAuthentication.MembershipProviderName);
178                         sb.UserNameAuthentication.UserNamePasswordValidationMode = UserNameAuthentication.UserNamePasswordValidationMode;
179
180                         // Windows
181                         sb.WindowsAuthentication.AllowAnonymousLogons = WindowsAuthentication.AllowAnonymousLogons;
182                         sb.WindowsAuthentication.IncludeWindowsGroups = WindowsAuthentication.IncludeWindowsGroups;
183
184                         return sb;
185                 }
186
187                 X509Certificate2 GetCertificate (StoreLocation storeLocation, StoreName storeName, X509FindType findType, object findValue)
188                 {
189                         throw new NotImplementedException ();
190                 }
191
192                 object CreateInstance (string typeName)
193                 {
194                         return String.IsNullOrEmpty (typeName) ? null : Activator.CreateInstance (System.Type.GetType (typeName, true));
195                 }
196         }
197
198 }