IssuedSecurityTokenParameters.cs 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. public override string ToString ()
  80. {
  81. return base.ToString ();
  82. }
  83. public Collection<XmlElement> AdditionalRequestParameters {
  84. get { return additional_reqs; }
  85. }
  86. public Collection<ClaimTypeRequirement> ClaimTypeRequirements {
  87. get { return reqs; }
  88. }
  89. protected override bool HasAsymmetricKey {
  90. get { return false; }
  91. }
  92. public EndpointAddress IssuerAddress {
  93. get { return issuer_address; }
  94. set { issuer_address = value; }
  95. }
  96. public Binding IssuerBinding {
  97. get { return binding; }
  98. set { binding = value; }
  99. }
  100. public EndpointAddress IssuerMetadataAddress {
  101. get { return issuer_meta_address; }
  102. set { issuer_meta_address = value; }
  103. }
  104. public int KeySize {
  105. get { return key_size; }
  106. set { key_size = value; }
  107. }
  108. public SecurityKeyType KeyType {
  109. get { return key_type; }
  110. set { key_type = value; }
  111. }
  112. public string TokenType {
  113. get { return token_type; }
  114. set { token_type = value; }
  115. }
  116. protected override bool SupportsClientAuthentication {
  117. get { return true; }
  118. }
  119. protected override bool SupportsClientWindowsIdentity {
  120. get { return false; }
  121. }
  122. protected override bool SupportsServerAuthentication {
  123. get { return true; }
  124. }
  125. protected override SecurityTokenParameters CloneCore ()
  126. {
  127. return new IssuedSecurityTokenParameters (this);
  128. }
  129. [MonoTODO]
  130. protected override SecurityKeyIdentifierClause CreateKeyIdentifierClause (
  131. SecurityToken token, SecurityTokenReferenceStyle referenceStyle)
  132. {
  133. throw new NotImplementedException ();
  134. }
  135. public Collection<XmlElement> CreateRequestParameters (
  136. MessageSecurityVersion messageSecurityVersion,
  137. SecurityTokenSerializer securityTokenSerializer)
  138. {
  139. XmlDocument doc = new XmlDocument ();
  140. Collection<XmlElement> ret = new Collection<XmlElement> ();
  141. // KeyType
  142. string keyTypeUri =
  143. KeyType == SecurityKeyType.SymmetricKey ?
  144. Constants.WstSymmetricKeyTypeUri :
  145. Constants.WstAsymmetricKeyTypeUri;
  146. XmlElement kt = doc.CreateElement ("t", "KeyType", Constants.WstNamespace);
  147. kt.AppendChild (doc.CreateTextNode (keyTypeUri));
  148. ret.Add (kt);
  149. // ClaimTypes
  150. XmlElement cts = doc.CreateElement ("t", "Claims", Constants.WstNamespace);
  151. foreach (ClaimTypeRequirement req in ClaimTypeRequirements) {
  152. XmlElement el = doc.CreateElement ("wsid", "ClaimType", Constants.WsidNamespace);
  153. el.SetAttribute ("Uri", req.ClaimType);
  154. if (req.IsOptional)
  155. el.SetAttribute ("Optional", "true");
  156. cts.AppendChild (el);
  157. }
  158. ret.Add (cts);
  159. // Additional parameters
  160. foreach (XmlElement el in AdditionalRequestParameters)
  161. ret.Add (el);
  162. return ret;
  163. }
  164. protected override void InitializeSecurityTokenRequirement (SecurityTokenRequirement requirement)
  165. {
  166. if (requirement == null)
  167. throw new ArgumentNullException ("requirement");
  168. requirement.TokenType = TokenType;
  169. requirement.Properties [ReqType.IssuedSecurityTokenParametersProperty] = this.Clone ();
  170. requirement.Properties [ReqType.IssuerAddressProperty] = IssuerAddress;
  171. requirement.Properties [ReqType.IssuerBindingProperty] = IssuerBinding;
  172. requirement.RequireCryptographicToken = true;
  173. requirement.KeyType = KeyType;
  174. }
  175. }
  176. }