Merge pull request #4453 from lambdageek/bug-49721
[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 other)
65                         : base (other)
66                 {
67                         binding = other.binding;
68                         issuer_address = other.issuer_address;
69                         issuer_meta_address = other.issuer_meta_address;
70                         key_size = other.key_size;
71                         key_type = other.key_type;
72                         token_type = other.token_type;
73                         reqs = new Collection<ClaimTypeRequirement> (other.reqs);
74                         additional_reqs = new Collection<XmlElement> (other.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                 public override string ToString ()
88                 {
89                         return base.ToString ();
90                 }
91
92                 public Collection<XmlElement> AdditionalRequestParameters {
93                         get { return additional_reqs; }
94                 }
95
96                 public Collection<ClaimTypeRequirement> ClaimTypeRequirements { 
97                         get { return reqs; }
98                 }
99
100                 protected override bool HasAsymmetricKey {
101                         get { return false; }
102                 }
103
104                 public EndpointAddress IssuerAddress {
105                         get { return issuer_address; }
106                         set { issuer_address = value; }
107                 }
108
109                 public Binding IssuerBinding {
110                         get { return binding; }
111                         set { binding = value; }
112                 }
113
114                 public EndpointAddress IssuerMetadataAddress {
115                         get { return issuer_meta_address; }
116                         set { issuer_meta_address = value; }
117                 }
118
119                 public int KeySize {
120                         get { return key_size; }
121                         set { key_size = value; }
122                 }
123
124                 public SecurityKeyType KeyType {
125                         get { return key_type; }
126                         set { key_type = value; }
127                 }
128
129                 public string TokenType {
130                         get { return token_type; }
131                         set { token_type = value; }
132                 }
133
134                 protected override bool SupportsClientAuthentication {
135                         get { return true; }
136                 }
137
138                 protected override bool SupportsClientWindowsIdentity {
139                         get { return false; }
140                 }
141
142                 protected override bool SupportsServerAuthentication {
143                         get { return true; }
144                 }
145
146                 protected override SecurityTokenParameters CloneCore ()
147                 {
148                         return new IssuedSecurityTokenParameters (this);
149                 }
150
151                 [MonoTODO]
152                 protected override SecurityKeyIdentifierClause CreateKeyIdentifierClause (
153                         SecurityToken token, SecurityTokenReferenceStyle referenceStyle)
154                 {
155                         throw new NotImplementedException ();
156                 }
157
158                 public Collection<XmlElement> CreateRequestParameters (
159                         MessageSecurityVersion messageSecurityVersion,
160                         SecurityTokenSerializer securityTokenSerializer)
161                 {
162                         XmlDocument doc = new XmlDocument ();
163                         Collection<XmlElement> ret = new Collection<XmlElement> ();
164                         // KeyType
165                         string keyTypeUri =
166                                 KeyType == SecurityKeyType.SymmetricKey ?
167                                 Constants.WstSymmetricKeyTypeUri :
168                                 Constants.WstAsymmetricKeyTypeUri;
169                         XmlElement kt = doc.CreateElement ("t", "KeyType", Constants.WstNamespace);
170                         kt.AppendChild (doc.CreateTextNode (keyTypeUri));
171                         ret.Add (kt);
172
173                         // ClaimTypes
174                         XmlElement cts = doc.CreateElement ("t", "Claims", Constants.WstNamespace);
175                         foreach (ClaimTypeRequirement req in ClaimTypeRequirements) {
176                                 XmlElement el = doc.CreateElement ("wsid", "ClaimType", Constants.WsidNamespace);
177                                 el.SetAttribute ("Uri", req.ClaimType);
178                                 if (req.IsOptional)
179                                         el.SetAttribute ("Optional", "true");
180                                 cts.AppendChild (el);
181                         }
182                         ret.Add (cts);
183
184                         // Additional parameters
185                         foreach (XmlElement el in AdditionalRequestParameters)
186                                 ret.Add (el);
187                         return ret;
188                 }
189
190                 protected internal override void InitializeSecurityTokenRequirement (SecurityTokenRequirement requirement)
191                 {
192                         if (requirement == null)
193                                 throw new ArgumentNullException ("requirement");
194                         requirement.TokenType = TokenType;
195                         requirement.Properties [ReqType.IssuedSecurityTokenParametersProperty] = this.Clone ();
196                         requirement.Properties [ReqType.IssuerAddressProperty] = IssuerAddress;
197                         requirement.Properties [ReqType.IssuerBindingProperty] = IssuerBinding;
198                         requirement.RequireCryptographicToken = true;
199                         requirement.KeyType = KeyType;
200                 }
201         }
202 }