Merge branch 'master' of github.com:tgiphil/mono
[mono.git] / mcs / class / System.ServiceModel / System.ServiceModel.Security.Tokens / IssuedSecurityTokenParameters.cs
1 //
2 // IssuedSecurityTokenParameters.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 using System.Collections.ObjectModel;
29 using System.Xml;
30 using System.Xml.XPath;
31 using System.IdentityModel.Selectors;
32 using System.IdentityModel.Tokens;
33 using System.ServiceModel.Channels;
34 using System.ServiceModel.Security;
35
36 using ReqType = System.ServiceModel.Security.Tokens.ServiceModelSecurityTokenRequirement;
37
38 namespace System.ServiceModel.Security.Tokens
39 {
40         public class IssuedSecurityTokenParameters : SecurityTokenParameters
41         {
42                 public IssuedSecurityTokenParameters ()
43                 {
44                 }
45
46                 public IssuedSecurityTokenParameters (string tokenType)
47                         : this (tokenType, null)
48                 {
49                 }
50
51                 public IssuedSecurityTokenParameters (string tokenType, EndpointAddress issuerAddress)
52                         : this (tokenType, issuerAddress, null)
53                 {
54                 }
55
56                 public IssuedSecurityTokenParameters (string tokenType,
57                         EndpointAddress issuerAddress, Binding issuerBinding)
58                 {
59                         token_type = tokenType;
60                         issuer_address = issuerAddress;
61                         binding = issuerBinding;
62                 }
63
64                 protected IssuedSecurityTokenParameters (IssuedSecurityTokenParameters source)
65                         : base (source)
66                 {
67                         binding = source.binding;
68                         issuer_address = source.issuer_address;
69                         issuer_meta_address = source.issuer_meta_address;
70                         key_size = source.key_size;
71                         key_type = source.key_type;
72                         token_type = source.token_type;
73                         reqs = new Collection<ClaimTypeRequirement> (source.reqs);
74                         additional_reqs = new Collection<XmlElement> (source.additional_reqs);
75                 }
76
77                 Binding binding;
78                 EndpointAddress issuer_address, issuer_meta_address;
79                 int key_size;
80                 SecurityKeyType key_type;
81                 string token_type;
82                 Collection<ClaimTypeRequirement> reqs =
83                         new Collection<ClaimTypeRequirement> ();
84                 Collection<XmlElement> additional_reqs =
85                         new Collection<XmlElement> ();
86
87                 [MonoTODO]
88                 public override string ToString ()
89                 {
90                         return base.ToString ();
91                 }
92
93                 public Collection<XmlElement> AdditionalRequestParameters {
94                         get { return additional_reqs; }
95                 }
96
97                 public Collection<ClaimTypeRequirement> ClaimTypeRequirements { 
98                         get { return reqs; }
99                 }
100
101                 protected override bool HasAsymmetricKey {
102                         get { return false; }
103                 }
104
105                 public EndpointAddress IssuerAddress {
106                         get { return issuer_address; }
107                         set { issuer_address = value; }
108                 }
109
110                 public Binding IssuerBinding {
111                         get { return binding; }
112                         set { binding = value; }
113                 }
114
115                 public EndpointAddress IssuerMetadataAddress {
116                         get { return issuer_meta_address; }
117                         set { issuer_meta_address = value; }
118                 }
119
120                 public int KeySize {
121                         get { return key_size; }
122                         set { key_size = value; }
123                 }
124
125                 public SecurityKeyType KeyType {
126                         get { return key_type; }
127                         set { key_type = value; }
128                 }
129
130                 public string TokenType {
131                         get { return token_type; }
132                         set { token_type = value; }
133                 }
134
135                 protected override bool SupportsClientAuthentication {
136                         get { return true; }
137                 }
138
139                 protected override bool SupportsClientWindowsIdentity {
140                         get { return false; }
141                 }
142
143                 protected override bool SupportsServerAuthentication {
144                         get { return true; }
145                 }
146
147                 protected override SecurityTokenParameters CloneCore ()
148                 {
149                         return new IssuedSecurityTokenParameters (this);
150                 }
151
152                 [MonoTODO]
153                 protected override SecurityKeyIdentifierClause CreateKeyIdentifierClause (
154                         SecurityToken token, SecurityTokenReferenceStyle referenceStyle)
155                 {
156                         throw new NotImplementedException ();
157                 }
158
159                 public Collection<XmlElement> CreateRequestParameters (
160                         MessageSecurityVersion messageSecurityVersion,
161                         SecurityTokenSerializer securityTokenSerializer)
162                 {
163                         XmlDocument doc = new XmlDocument ();
164                         Collection<XmlElement> ret = new Collection<XmlElement> ();
165                         // KeyType
166                         string keyTypeUri =
167                                 KeyType == SecurityKeyType.SymmetricKey ?
168                                 Constants.WstSymmetricKeyTypeUri :
169                                 Constants.WstAsymmetricKeyTypeUri;
170                         XmlElement kt = doc.CreateElement ("t", "KeyType", Constants.WstNamespace);
171                         kt.AppendChild (doc.CreateTextNode (keyTypeUri));
172                         ret.Add (kt);
173
174                         // ClaimTypes
175                         XmlElement cts = doc.CreateElement ("t", "Claims", Constants.WstNamespace);
176                         foreach (ClaimTypeRequirement req in ClaimTypeRequirements) {
177                                 XmlElement el = doc.CreateElement ("wsid", "ClaimType", Constants.WsidNamespace);
178                                 el.SetAttribute ("Uri", req.ClaimType);
179                                 if (req.IsOptional)
180                                         el.SetAttribute ("Optional", "true");
181                                 cts.AppendChild (el);
182                         }
183                         ret.Add (cts);
184
185                         // Additional parameters
186                         foreach (XmlElement el in AdditionalRequestParameters)
187                                 ret.Add (el);
188                         return ret;
189                 }
190
191                 protected override void InitializeSecurityTokenRequirement (SecurityTokenRequirement requirement)
192                 {
193                         if (requirement == null)
194                                 throw new ArgumentNullException ("requirement");
195                         requirement.TokenType = TokenType;
196                         requirement.Properties [ReqType.IssuedSecurityTokenParametersProperty] = this.Clone ();
197                         requirement.Properties [ReqType.IssuerAddressProperty] = IssuerAddress;
198                         requirement.Properties [ReqType.IssuerBindingProperty] = IssuerBinding;
199                         requirement.RequireCryptographicToken = true;
200                         requirement.KeyType = KeyType;
201                 }
202         }
203 }