5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2006 Novell, Inc. http://www.novell.com
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:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
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.
29 using System.Collections.Generic;
31 using System.IdentityModel.Claims;
32 using System.IdentityModel.Policy;
33 using System.IdentityModel.Selectors;
35 namespace System.IdentityModel.Tokens
37 public class SamlEvidence
39 public SamlEvidence ()
43 public SamlEvidence (IEnumerable<string> assertionIdReferences)
44 : this (assertionIdReferences, new SamlAssertion [0])
48 public SamlEvidence (IEnumerable<SamlAssertion> assertions)
49 : this (new string [0], assertions)
54 IEnumerable<string> assertionIdReferences,
55 IEnumerable<SamlAssertion> assertions)
57 if (assertionIdReferences == null)
58 throw new ArgumentException ("assertionIdReferences are null.");
59 if (assertions == null)
60 throw new ArgumentException ("assertions are null.");
61 foreach (string r in assertionIdReferences) {
63 throw new ArgumentException ("assertionIdReferences contain null item.");
66 foreach (SamlAssertion a in assertions) {
68 throw new ArgumentException ("assertions contain null item.");
69 this.assertions.Add (a);
74 List<string> references = new List<string> ();
75 List<SamlAssertion> assertions = new List<SamlAssertion> ();
77 public IList<string> AssertionIdReferences {
78 get { return references; }
81 public IList<SamlAssertion> Assertions {
82 get { return assertions; }
85 public bool IsReadOnly {
86 get { return is_readonly; }
89 private void CheckReadOnly ()
92 throw new InvalidOperationException ("This SAML assertion is read-only.");
95 public void MakeReadOnly ()
100 public virtual void ReadXml (XmlDictionaryReader reader,
101 SamlSerializer samlSerializer,
102 SecurityTokenSerializer keyInfoSerializer,
103 SecurityTokenResolver resolver)
106 throw new ArgumentNullException ("reader");
107 if (samlSerializer == null)
108 throw new ArgumentNullException ("samlSerializer");
112 reader.ReadStartElement ("Evidence", SamlConstants.Namespace);
113 for (reader.MoveToContent ();
114 reader.NodeType == XmlNodeType.Element;
115 reader.MoveToContent ()) {
116 if (reader.NamespaceURI != SamlConstants.Namespace)
117 throw new SecurityTokenException (String.Format ("Invalid SAML Evidence element: element '{0}' in namespace '{1}' is unexpected.", reader.LocalName, reader.NamespaceURI));
118 switch (reader.LocalName) {
120 SamlAssertion a = new SamlAssertion ();
121 a.ReadXml (reader, samlSerializer, keyInfoSerializer, resolver);
124 case "AssertionIDReference":
125 references.Add (reader.ReadElementContentAsString ());
128 throw new SecurityTokenException (String.Format ("Invalid SAML Evidence element: SAML element '{0}' is unexpected.", reader.LocalName));
131 reader.ReadEndElement ();
133 if (references.Count == 0 && assertions.Count == 0)
134 throw new SecurityTokenException ("At least either one of AssertionIDReference or Assertion must exist in SAML Evidence.");
137 public virtual void WriteXml (XmlDictionaryWriter writer,
138 SamlSerializer samlSerializer,
139 SecurityTokenSerializer keyInfoSerializer)
142 throw new ArgumentNullException ("writer");
143 if (samlSerializer == null)
144 throw new ArgumentNullException ("samlSerializer");
145 if (references.Count == 0 && assertions.Count == 0)
146 throw new SecurityTokenException ("At least either one of AssertionIDReference or Assertion must exist in SAML Evidence.");
148 writer.WriteStartElement ("saml", "Evidence", SamlConstants.Namespace);
149 foreach (string s in references)
150 writer.WriteElementString ("saml", "AssertionIDReference", SamlConstants.Namespace, s);
151 foreach (SamlAssertion a in assertions)
152 a.WriteXml (writer, samlSerializer, keyInfoSerializer);
153 writer.WriteEndElement ();