SamlAdvice.cs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. //
  2. // SamlAdvice.cs
  3. //
  4. // Author:
  5. // Atsushi Enomoto <[email protected]>
  6. //
  7. // Copyright (C) 2005 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.Selectors;
  32. namespace System.IdentityModel.Tokens
  33. {
  34. public class SamlAdvice
  35. {
  36. List<string> idrefs = new List<string> ();
  37. List<SamlAssertion> assertions = new List<SamlAssertion> ();
  38. bool is_readonly;
  39. public SamlAdvice ()
  40. {
  41. }
  42. public SamlAdvice (IEnumerable<SamlAssertion> assertions)
  43. : this (new string [0], assertions)
  44. {
  45. }
  46. public SamlAdvice (IEnumerable<string> references)
  47. : this (references, new SamlAssertion [0])
  48. {
  49. }
  50. public SamlAdvice (IEnumerable<string> references, IEnumerable<SamlAssertion> assertions)
  51. {
  52. if (references == null)
  53. throw new ArgumentException ("references are null.");
  54. if (assertions == null)
  55. throw new ArgumentException ("assertions are null.");
  56. foreach (string r in references) {
  57. if (r == null)
  58. throw new ArgumentException ("references contain null item.");
  59. idrefs.Add (r);
  60. }
  61. foreach (SamlAssertion a in assertions) {
  62. if (a == null)
  63. throw new ArgumentException ("assertions contain null item.");
  64. this.assertions.Add (a);
  65. }
  66. }
  67. public bool IsReadOnly {
  68. get { return is_readonly; }
  69. }
  70. public IList<SamlAssertion> Assertions {
  71. get { return assertions; }
  72. }
  73. public IList<string> AssertionIdReferences {
  74. get { return idrefs; }
  75. }
  76. public void MakeReadOnly ()
  77. {
  78. is_readonly = true;
  79. }
  80. public virtual void ReadXml (XmlDictionaryReader reader,
  81. SamlSerializer samlSerializer,
  82. SecurityTokenSerializer keyInfoTokenSerializer,
  83. SecurityTokenResolver outOfBandTokenResolver)
  84. {
  85. if (reader == null)
  86. throw new ArgumentNullException ("reader");
  87. if (samlSerializer == null)
  88. throw new ArgumentNullException ("samlSerializer");
  89. reader.ReadStartElement ("Advice", SamlConstants.Namespace);
  90. for (reader.MoveToContent ();
  91. reader.NodeType == XmlNodeType.Element;
  92. reader.MoveToContent ()) {
  93. if (reader.NamespaceURI != SamlConstants.Namespace)
  94. throw new SecurityTokenException (String.Format ("Invalid SAML Advice element: element '{0}' in namespace '{1}' is unexpected.", reader.LocalName, reader.NamespaceURI));
  95. switch (reader.LocalName) {
  96. case "Assertion":
  97. SamlAssertion a = new SamlAssertion ();
  98. a.ReadXml (reader, samlSerializer, keyInfoTokenSerializer, outOfBandTokenResolver);
  99. assertions.Add (a);
  100. break;
  101. case "AssertionIDReference":
  102. idrefs.Add (reader.ReadElementContentAsString ());
  103. break;
  104. default:
  105. throw new SecurityTokenException (String.Format ("Invalid SAML Advice element: SAML element '{0}' is unexpected.", reader.LocalName));
  106. }
  107. }
  108. reader.ReadEndElement ();
  109. }
  110. public virtual void WriteXml (XmlDictionaryWriter writer,
  111. SamlSerializer samlSerializer,
  112. SecurityTokenSerializer keyInfoTokenSerializer)
  113. {
  114. if (writer == null)
  115. throw new ArgumentNullException ("writer");
  116. if (samlSerializer == null)
  117. throw new ArgumentNullException ("samlSerializer");
  118. writer.WriteStartElement ("saml", "Advice", SamlConstants.Namespace);
  119. foreach (string idref in AssertionIdReferences)
  120. writer.WriteElementString ("saml", "AssertionIDReference", SamlConstants.Namespace, idref);
  121. foreach (SamlAssertion assertion in Assertions)
  122. assertion.WriteXml (writer, samlSerializer, keyInfoTokenSerializer);
  123. writer.WriteEndElement ();
  124. }
  125. }
  126. }