PageRenderTime 41ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/mcs/class/referencesource/System.IdentityModel/System/IdentityModel/Tokens/Saml2Assertion.cs

https://github.com/pruiz/mono
C# | 240 lines | 125 code | 24 blank | 91 comment | 10 complexity | b2d6e00902e9d421964cd125ad487a80 MD5 | raw file
Possible License(s): LGPL-2.0, MPL-2.0-no-copyleft-exception, CC-BY-SA-3.0, GPL-2.0
  1. //-----------------------------------------------------------------------
  2. // <copyright file="Saml2Assertion.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. //-----------------------------------------------------------------------
  6. namespace System.IdentityModel.Tokens
  7. {
  8. using System;
  9. using System.Collections.ObjectModel;
  10. using System.Xml;
  11. /// <summary>
  12. /// Represents the Assertion element specified in [Saml2Core, 2.3.3].
  13. /// </summary>
  14. public class Saml2Assertion
  15. {
  16. private Saml2Advice advice;
  17. private Saml2Conditions conditions;
  18. private EncryptingCredentials encryptingCredentials;
  19. private Collection<EncryptedKeyIdentifierClause> externalEncryptedKeys = new Collection<EncryptedKeyIdentifierClause>();
  20. private Saml2Id id = new Saml2Id();
  21. private DateTime issueInstant = DateTime.UtcNow;
  22. private Saml2NameIdentifier issuer;
  23. private SigningCredentials signingCredentials;
  24. private XmlTokenStream sourceData;
  25. private Collection<Saml2Statement> statements = new Collection<Saml2Statement>();
  26. private Saml2Subject subject;
  27. private string version = "2.0";
  28. /// <summary>
  29. /// Creates an instance of a Saml2Assertion.
  30. /// </summary>
  31. /// <param name="issuer">Issuer of the assertion.</param>
  32. public Saml2Assertion(Saml2NameIdentifier issuer)
  33. {
  34. if (issuer == null)
  35. {
  36. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("issuer");
  37. }
  38. this.issuer = issuer;
  39. }
  40. /// <summary>
  41. /// Gets or sets additional information related to the assertion that assists processing in certain
  42. /// situations but which may be ignored by applications that do not understand the
  43. /// advice or do not wish to make use of it. [Saml2Core, 2.3.3]
  44. /// </summary>
  45. public Saml2Advice Advice
  46. {
  47. get { return this.advice; }
  48. set { this.advice = value; }
  49. }
  50. /// <summary>
  51. /// Gets a value indicating whether this assertion was deserialized from XML source
  52. /// and can re-emit the XML data unchanged.
  53. /// </summary>
  54. /// <remarks>
  55. /// <para>
  56. /// The default implementation preserves the source data when read using
  57. /// Saml2AssertionSerializer.ReadAssertion and is willing to re-emit the
  58. /// original data as long as the Id has not changed from the time that
  59. /// assertion was read.
  60. /// </para>
  61. /// <para>
  62. /// Note that it is vitally important that SAML assertions with different
  63. /// data have different IDs. If implementing a scheme whereby an assertion
  64. /// "template" is loaded and certain bits of data are filled in, the Id
  65. /// must be changed.
  66. /// </para>
  67. /// </remarks>
  68. /// <returns>'True' if this instance can write the source data.</returns>
  69. public virtual bool CanWriteSourceData
  70. {
  71. get { return null != this.sourceData; }
  72. }
  73. /// <summary>
  74. /// Gets or sets conditions that must be evaluated when assessing the validity of and/or
  75. /// when using the assertion. [Saml2Core 2.3.3]
  76. /// </summary>
  77. public Saml2Conditions Conditions
  78. {
  79. get { return this.conditions; }
  80. set { this.conditions = value; }
  81. }
  82. /// <summary>
  83. /// Gets or sets the credentials used for encrypting the assertion. The key
  84. /// identifier in the encrypting credentials will be used for the
  85. /// embedded EncryptedKey in the EncryptedData element.
  86. /// </summary>
  87. public EncryptingCredentials EncryptingCredentials
  88. {
  89. get { return this.encryptingCredentials; }
  90. set { this.encryptingCredentials = value; }
  91. }
  92. /// <summary>
  93. /// Gets additional encrypted keys which will be specified external to the
  94. /// EncryptedData element, as children of the EncryptedAssertion element.
  95. /// </summary>
  96. public Collection<EncryptedKeyIdentifierClause> ExternalEncryptedKeys
  97. {
  98. get { return this.externalEncryptedKeys; }
  99. }
  100. /// <summary>
  101. /// Gets or sets the <see cref="Saml2Id"/> identifier for this assertion. [Saml2Core, 2.3.3]
  102. /// </summary>
  103. public Saml2Id Id
  104. {
  105. get
  106. {
  107. return this.id;
  108. }
  109. set
  110. {
  111. if (null == value)
  112. {
  113. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
  114. }
  115. this.id = value;
  116. this.sourceData = null;
  117. }
  118. }
  119. /// <summary>
  120. /// Gets or sets the time instant of issue in UTC. [Saml2Core, 2.3.3]
  121. /// </summary>
  122. public DateTime IssueInstant
  123. {
  124. get { return this.issueInstant; }
  125. set { this.issueInstant = DateTimeUtil.ToUniversalTime(value); }
  126. }
  127. /// <summary>
  128. /// Gets or sets the <see cref="Saml2NameIdentifier"/> as the authority that is making the claim(s) in the assertion. [Saml2Core, 2.3.3]
  129. /// </summary>
  130. public Saml2NameIdentifier Issuer
  131. {
  132. get
  133. {
  134. return this.issuer;
  135. }
  136. set
  137. {
  138. if (value == null)
  139. {
  140. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("value");
  141. }
  142. this.issuer = value;
  143. }
  144. }
  145. /// <summary>
  146. /// Gets or sets the <see cref="SigningCredentials"/> used by the issuer to protect the integrity of the assertion.
  147. /// </summary>
  148. public SigningCredentials SigningCredentials
  149. {
  150. get { return this.signingCredentials; }
  151. set { this.signingCredentials = value; }
  152. }
  153. /// <summary>
  154. /// Gets or sets the <see cref="Saml2Subject"/> of the statement(s) in the assertion. [Saml2Core, 2.3.3]
  155. /// </summary>
  156. public Saml2Subject Subject
  157. {
  158. get { return this.subject; }
  159. set { this.subject = value; }
  160. }
  161. /// <summary>
  162. /// Gets the <see cref="Saml2Statement"/>(s) regarding the subject.
  163. /// </summary>
  164. public Collection<Saml2Statement> Statements
  165. {
  166. get { return this.statements; }
  167. }
  168. /// <summary>
  169. /// Gets the version of this assertion. [Saml2Core, 2.3.3]
  170. /// </summary>
  171. /// <remarks>
  172. /// In this version of the Windows Identity Foundation, only version "2.0" is supported.
  173. /// </remarks>
  174. public string Version
  175. {
  176. get { return this.version; }
  177. }
  178. /// <summary>
  179. /// Writes the source data, if available.
  180. /// </summary>
  181. /// <exception cref="InvalidOperationException">When no source data is available</exception>
  182. /// <param name="writer">A <see cref="XmlWriter"/> for writting the data.</param>
  183. public virtual void WriteSourceData(XmlWriter writer)
  184. {
  185. if (!this.CanWriteSourceData)
  186. {
  187. throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
  188. new InvalidOperationException(SR.GetString(SR.ID4140)));
  189. }
  190. // This call will properly just reuse the existing writer if it already qualifies
  191. XmlDictionaryWriter dictionaryWriter = XmlDictionaryWriter.CreateDictionaryWriter(writer);
  192. this.sourceData.SetElementExclusion(null, null);
  193. this.sourceData.GetWriter().WriteTo(dictionaryWriter, new DictionaryManager());
  194. }
  195. /// <summary>
  196. /// Captures the XML source data from an EnvelopedSignatureReader.
  197. /// </summary>
  198. /// <remarks>
  199. /// The EnvelopedSignatureReader that was used to read the data for this
  200. /// assertion should be passed to this method after the &lt;/Assertion>
  201. /// element has been read. This method will preserve the raw XML data
  202. /// that was read, including the signature, so that it may be re-emitted
  203. /// without changes and without the need to re-sign the data. See
  204. /// CanWriteSourceData and WriteSourceData.
  205. /// </remarks>
  206. /// <param name="reader"><see cref="EnvelopedSignatureReader"/> that contains the data for the assertion.</param>
  207. internal virtual void CaptureSourceData(EnvelopedSignatureReader reader)
  208. {
  209. if (null == reader)
  210. {
  211. throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull("reader");
  212. }
  213. this.sourceData = reader.XmlTokens;
  214. }
  215. }
  216. }