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