IssuedTokensHeader.cs 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //------------------------------------------------------------
  2. // Copyright (c) Microsoft Corporation. All rights reserved.
  3. //------------------------------------------------------------
  4. namespace System.ServiceModel.Security
  5. {
  6. using System.Collections.Generic;
  7. using System.ServiceModel.Channels;
  8. using System.Collections.ObjectModel;
  9. using System.Globalization;
  10. using System.ServiceModel;
  11. using System.Xml;
  12. using System.IdentityModel.Selectors;
  13. using System.ServiceModel.Diagnostics;
  14. sealed class IssuedTokensHeader : MessageHeader
  15. {
  16. ReadOnlyCollection<RequestSecurityTokenResponse> tokenIssuances;
  17. SecurityStandardsManager standardsManager;
  18. string actor;
  19. bool mustUnderstand;
  20. bool relay;
  21. bool isRefParam;
  22. public IssuedTokensHeader(RequestSecurityTokenResponse tokenIssuance, MessageSecurityVersion version, SecurityTokenSerializer tokenSerializer)
  23. : this(tokenIssuance, new SecurityStandardsManager(version, tokenSerializer))
  24. {
  25. }
  26. public IssuedTokensHeader(RequestSecurityTokenResponse tokenIssuance, SecurityStandardsManager standardsManager)
  27. : base()
  28. {
  29. if (tokenIssuance == null)
  30. {
  31. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("tokenIssuance");
  32. }
  33. Collection<RequestSecurityTokenResponse> coll = new Collection<RequestSecurityTokenResponse>();
  34. coll.Add(tokenIssuance);
  35. Initialize(coll, standardsManager);
  36. }
  37. public IssuedTokensHeader(IEnumerable<RequestSecurityTokenResponse> tokenIssuances, SecurityStandardsManager standardsManager)
  38. : base()
  39. {
  40. if (tokenIssuances == null)
  41. {
  42. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("tokenIssuances");
  43. }
  44. int index = 0;
  45. Collection<RequestSecurityTokenResponse> coll = new Collection<RequestSecurityTokenResponse>();
  46. foreach (RequestSecurityTokenResponse rstr in tokenIssuances)
  47. {
  48. if (rstr == null)
  49. {
  50. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(String.Format(CultureInfo.InvariantCulture, "tokenIssuances[{0}]", index));
  51. }
  52. coll.Add(rstr);
  53. ++index;
  54. }
  55. Initialize(coll, standardsManager);
  56. }
  57. void Initialize(Collection<RequestSecurityTokenResponse> coll, SecurityStandardsManager standardsManager)
  58. {
  59. if (standardsManager == null)
  60. {
  61. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("standardsManager"));
  62. }
  63. this.standardsManager = standardsManager;
  64. this.tokenIssuances = new ReadOnlyCollection<RequestSecurityTokenResponse>(coll);
  65. this.actor = base.Actor;
  66. this.mustUnderstand = base.MustUnderstand;
  67. this.relay = base.Relay;
  68. }
  69. public IssuedTokensHeader(XmlReader xmlReader, MessageVersion version, SecurityStandardsManager standardsManager)
  70. : base()
  71. {
  72. if (xmlReader == null)
  73. {
  74. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("xmlReader");
  75. }
  76. if (standardsManager == null)
  77. {
  78. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentNullException("standardsManager"));
  79. }
  80. this.standardsManager = standardsManager;
  81. XmlDictionaryReader reader = XmlDictionaryReader.CreateDictionaryReader(xmlReader);
  82. MessageHeader.GetHeaderAttributes(reader, version, out this.actor, out this.mustUnderstand, out this.relay, out this.isRefParam);
  83. reader.ReadStartElement(this.Name, this.Namespace);
  84. Collection<RequestSecurityTokenResponse> coll = new Collection<RequestSecurityTokenResponse>();
  85. if (this.standardsManager.TrustDriver.IsAtRequestSecurityTokenResponseCollection(reader))
  86. {
  87. RequestSecurityTokenResponseCollection rstrColl = this.standardsManager.TrustDriver.CreateRequestSecurityTokenResponseCollection(reader);
  88. foreach (RequestSecurityTokenResponse rstr in rstrColl.RstrCollection)
  89. {
  90. coll.Add(rstr);
  91. }
  92. }
  93. else
  94. {
  95. RequestSecurityTokenResponse rstr = this.standardsManager.TrustDriver.CreateRequestSecurityTokenResponse(reader);
  96. coll.Add(rstr);
  97. }
  98. this.tokenIssuances = new ReadOnlyCollection<RequestSecurityTokenResponse>(coll);
  99. reader.ReadEndElement();
  100. }
  101. public ReadOnlyCollection<RequestSecurityTokenResponse> TokenIssuances
  102. {
  103. get
  104. {
  105. return this.tokenIssuances;
  106. }
  107. }
  108. public override string Actor
  109. {
  110. get
  111. {
  112. return this.actor;
  113. }
  114. }
  115. public override bool IsReferenceParameter
  116. {
  117. get
  118. {
  119. return this.isRefParam;
  120. }
  121. }
  122. public override bool MustUnderstand
  123. {
  124. get
  125. {
  126. return this.mustUnderstand;
  127. }
  128. }
  129. public override bool Relay
  130. {
  131. get
  132. {
  133. return this.relay;
  134. }
  135. }
  136. public override string Name
  137. {
  138. get
  139. {
  140. return this.standardsManager.TrustDriver.IssuedTokensHeaderName;
  141. }
  142. }
  143. public override string Namespace
  144. {
  145. get
  146. {
  147. return this.standardsManager.TrustDriver.IssuedTokensHeaderNamespace;
  148. }
  149. }
  150. protected override void OnWriteHeaderContents(XmlDictionaryWriter writer, MessageVersion messageVersion)
  151. {
  152. if (this.tokenIssuances.Count == 1)
  153. {
  154. this.standardsManager.TrustDriver.WriteRequestSecurityTokenResponse(this.tokenIssuances[0], writer);
  155. }
  156. else
  157. {
  158. RequestSecurityTokenResponseCollection rstrCollection = new RequestSecurityTokenResponseCollection(this.tokenIssuances, this.standardsManager);
  159. rstrCollection.WriteTo(writer);
  160. }
  161. }
  162. internal static Collection<RequestSecurityTokenResponse> ExtractIssuances(Message message, MessageSecurityVersion version, WSSecurityTokenSerializer tokenSerializer, string[] actors, XmlQualifiedName expectedAppliesToQName)
  163. {
  164. return ExtractIssuances(message, new SecurityStandardsManager(version, tokenSerializer), actors, expectedAppliesToQName);
  165. }
  166. // if expectedAppliesToQName is null all issuances matching the actors are returned.
  167. internal static Collection<RequestSecurityTokenResponse> ExtractIssuances(Message message, SecurityStandardsManager standardsManager, string[] actors, XmlQualifiedName expectedAppliesToQName)
  168. {
  169. if (message == null)
  170. {
  171. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("message");
  172. }
  173. if (standardsManager == null)
  174. {
  175. standardsManager = SecurityStandardsManager.DefaultInstance;
  176. }
  177. if (actors == null)
  178. {
  179. throw TraceUtility.ThrowHelperArgumentNull("actors", message);
  180. }
  181. Collection<RequestSecurityTokenResponse> issuances = new Collection<RequestSecurityTokenResponse>();
  182. for (int i = 0; i < message.Headers.Count; ++i)
  183. {
  184. if (message.Headers[i].Name == standardsManager.TrustDriver.IssuedTokensHeaderName && message.Headers[i].Namespace == standardsManager.TrustDriver.IssuedTokensHeaderNamespace)
  185. {
  186. bool isValidActor = false;
  187. for (int j = 0; j < actors.Length; ++j)
  188. {
  189. if (actors[j] == message.Headers[i].Actor)
  190. {
  191. isValidActor = true;
  192. break;
  193. }
  194. }
  195. if (!isValidActor)
  196. {
  197. continue;
  198. }
  199. IssuedTokensHeader issuedTokensHeader = new IssuedTokensHeader(message.Headers.GetReaderAtHeader(i), message.Version, standardsManager);
  200. for (int k = 0; k < issuedTokensHeader.TokenIssuances.Count; ++k)
  201. {
  202. bool isMatch;
  203. if (expectedAppliesToQName != null)
  204. {
  205. string issuanceAppliesToName;
  206. string issuanceAppliesToNs;
  207. issuedTokensHeader.TokenIssuances[k].GetAppliesToQName(out issuanceAppliesToName, out issuanceAppliesToNs);
  208. if (issuanceAppliesToName == expectedAppliesToQName.Name && issuanceAppliesToNs == expectedAppliesToQName.Namespace)
  209. {
  210. isMatch = true;
  211. }
  212. else
  213. {
  214. isMatch = false;
  215. }
  216. }
  217. else
  218. {
  219. isMatch = true;
  220. }
  221. if (isMatch)
  222. {
  223. issuances.Add(issuedTokensHeader.TokenIssuances[k]);
  224. }
  225. }
  226. }
  227. }
  228. return issuances;
  229. }
  230. }
  231. }