SamlEvidence.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. //
  2. // SamlEvidence.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;
  29. using System.Collections.Generic;
  30. using System.Xml;
  31. using System.IdentityModel.Claims;
  32. using System.IdentityModel.Policy;
  33. using System.IdentityModel.Selectors;
  34. namespace System.IdentityModel.Tokens
  35. {
  36. public class SamlEvidence
  37. {
  38. public SamlEvidence ()
  39. {
  40. }
  41. public SamlEvidence (IEnumerable<string> assertionIdReferences)
  42. : this (assertionIdReferences, new SamlAssertion [0])
  43. {
  44. }
  45. public SamlEvidence (IEnumerable<SamlAssertion> assertions)
  46. : this (new string [0], assertions)
  47. {
  48. }
  49. public SamlEvidence (
  50. IEnumerable<string> assertionIdReferences,
  51. IEnumerable<SamlAssertion> assertions)
  52. {
  53. if (assertionIdReferences == null)
  54. throw new ArgumentException ("assertionIdReferences are null.");
  55. if (assertions == null)
  56. throw new ArgumentException ("assertions are null.");
  57. foreach (string r in assertionIdReferences) {
  58. if (r == null)
  59. throw new ArgumentException ("assertionIdReferences contain null item.");
  60. references.Add (r);
  61. }
  62. foreach (SamlAssertion a in assertions) {
  63. if (a == null)
  64. throw new ArgumentException ("assertions contain null item.");
  65. this.assertions.Add (a);
  66. }
  67. }
  68. bool is_readonly;
  69. List<string> references = new List<string> ();
  70. List<SamlAssertion> assertions = new List<SamlAssertion> ();
  71. public IList<string> AssertionIdReferences {
  72. get { return references; }
  73. }
  74. public IList<SamlAssertion> Assertions {
  75. get { return assertions; }
  76. }
  77. public bool IsReadOnly {
  78. get { return is_readonly; }
  79. }
  80. private void CheckReadOnly ()
  81. {
  82. if (IsReadOnly)
  83. throw new InvalidOperationException ("This SAML assertion is read-only.");
  84. }
  85. public void MakeReadOnly ()
  86. {
  87. is_readonly = true;
  88. }
  89. public virtual void ReadXml (XmlDictionaryReader reader,
  90. SamlSerializer samlSerializer,
  91. SecurityTokenSerializer keyInfoSerializer,
  92. SecurityTokenResolver resolver)
  93. {
  94. if (reader == null)
  95. throw new ArgumentNullException ("reader");
  96. if (samlSerializer == null)
  97. throw new ArgumentNullException ("samlSerializer");
  98. references.Clear ();
  99. assertions.Clear ();
  100. reader.ReadStartElement ("Evidence", SamlConstants.Namespace);
  101. for (reader.MoveToContent ();
  102. reader.NodeType == XmlNodeType.Element;
  103. reader.MoveToContent ()) {
  104. if (reader.NamespaceURI != SamlConstants.Namespace)
  105. throw new SecurityTokenException (String.Format ("Invalid SAML Evidence element: element '{0}' in namespace '{1}' is unexpected.", reader.LocalName, reader.NamespaceURI));
  106. switch (reader.LocalName) {
  107. case "Assertion":
  108. SamlAssertion a = new SamlAssertion ();
  109. a.ReadXml (reader, samlSerializer, keyInfoSerializer, resolver);
  110. assertions.Add (a);
  111. break;
  112. case "AssertionIDReference":
  113. references.Add (reader.ReadElementContentAsString ());
  114. break;
  115. default:
  116. throw new SecurityTokenException (String.Format ("Invalid SAML Evidence element: SAML element '{0}' is unexpected.", reader.LocalName));
  117. }
  118. }
  119. reader.ReadEndElement ();
  120. if (references.Count == 0 && assertions.Count == 0)
  121. throw new SecurityTokenException ("At least either one of AssertionIDReference or Assertion must exist in SAML Evidence.");
  122. }
  123. public virtual void WriteXml (XmlDictionaryWriter writer,
  124. SamlSerializer samlSerializer,
  125. SecurityTokenSerializer keyInfoSerializer)
  126. {
  127. if (writer == null)
  128. throw new ArgumentNullException ("writer");
  129. if (samlSerializer == null)
  130. throw new ArgumentNullException ("samlSerializer");
  131. if (references.Count == 0 && assertions.Count == 0)
  132. throw new SecurityTokenException ("At least either one of AssertionIDReference or Assertion must exist in SAML Evidence.");
  133. writer.WriteStartElement ("saml", "Evidence", SamlConstants.Namespace);
  134. foreach (string s in references)
  135. writer.WriteElementString ("saml", "AssertionIDReference", SamlConstants.Namespace, s);
  136. foreach (SamlAssertion a in assertions)
  137. a.WriteXml (writer, samlSerializer, keyInfoSerializer);
  138. writer.WriteEndElement ();
  139. }
  140. }
  141. }