[tests] Separate MONO_PATH directories by PLATFORM_PATH_SEPARATOR
[mono.git] / mcs / class / System.IdentityModel / System.IdentityModel.Tokens / SamlEvidence.cs
1 //
2 // SamlEvidence.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
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
35 namespace System.IdentityModel.Tokens
36 {
37         public class SamlEvidence
38         {
39                 public SamlEvidence ()
40                 {
41                 }
42
43                 public SamlEvidence (IEnumerable<string> assertionIdReferences)
44                         : this (assertionIdReferences, new SamlAssertion [0])
45                 {
46                 }
47
48                 public SamlEvidence (IEnumerable<SamlAssertion> assertions)
49                         : this (new string [0], assertions)
50                 {
51                 }
52
53                 public SamlEvidence (
54                         IEnumerable<string> assertionIdReferences,
55                         IEnumerable<SamlAssertion> assertions)
56                 {
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) {
62                                 if (r == null)
63                                         throw new ArgumentException ("assertionIdReferences contain null item.");
64                                 references.Add (r);
65                         }
66                         foreach (SamlAssertion a in assertions) {
67                                 if (a == null)
68                                         throw new ArgumentException ("assertions contain null item.");
69                                 this.assertions.Add (a);
70                         }
71                 }
72
73                 bool is_readonly;
74                 List<string> references = new List<string> ();
75                 List<SamlAssertion> assertions = new List<SamlAssertion> ();
76
77                 public IList<string> AssertionIdReferences {
78                         get { return references; }
79                 }
80
81                 public IList<SamlAssertion> Assertions {
82                         get { return assertions; }
83                 }
84
85                 public bool IsReadOnly {
86                         get { return is_readonly; }
87                 }
88
89                 private void CheckReadOnly ()
90                 {
91                         if (IsReadOnly)
92                                 throw new InvalidOperationException ("This SAML assertion is read-only.");
93                 }
94
95                 public void MakeReadOnly ()
96                 {
97                         is_readonly = true;
98                 }
99
100                 public virtual void ReadXml (XmlDictionaryReader reader,
101                         SamlSerializer samlSerializer, 
102                         SecurityTokenSerializer keyInfoSerializer, 
103                         SecurityTokenResolver resolver)
104                 {
105                         if (reader == null)
106                                 throw new ArgumentNullException ("reader");
107                         if (samlSerializer == null)
108                                 throw new ArgumentNullException ("samlSerializer");
109                         references.Clear ();
110                         assertions.Clear ();
111
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) {
119                                 case "Assertion":
120                                         SamlAssertion a = new SamlAssertion ();
121                                         a.ReadXml (reader, samlSerializer, keyInfoSerializer, resolver);
122                                         assertions.Add (a);
123                                         break;
124                                 case "AssertionIDReference":
125                                         references.Add (reader.ReadElementContentAsString ());
126                                         break;
127                                 default:
128                                         throw new SecurityTokenException (String.Format ("Invalid SAML Evidence element: SAML element '{0}' is unexpected.", reader.LocalName));
129                                 }
130                         }
131                         reader.ReadEndElement ();
132
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.");
135                 }
136
137                 public virtual void WriteXml (XmlDictionaryWriter writer,
138                         SamlSerializer samlSerializer, 
139                         SecurityTokenSerializer keyInfoSerializer)
140                 {
141                         if (writer == null)
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.");
147
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 ();
154                 }
155         }
156 }