IssuedSecurityTokenParameters.cs 6.0 KB

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