[bcl] Rename variables to avoid conflict with later renames
[mono.git] / mcs / class / System.IdentityModel / System.IdentityModel.Tokens / SamlAssertion.cs
1 //
2 // SamlAssertion.cs
3 //
4 // Author:
5 //      Atsushi Enomoto <atsushi@ximian.com>
6 //
7 // Copyright (C) 2005-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.Globalization;
31 using System.Xml;
32 using System.IdentityModel.Selectors;
33
34 namespace System.IdentityModel.Tokens
35 {
36         public class SamlAssertion
37         {
38                 bool is_readonly;
39                 SamlAdvice advice;
40                 SamlConditions conditions;
41                 string assertion_id, issuer;
42                 DateTime issue_instant;
43                 int major, minor;
44                 SigningCredentials signing_credentials;
45                 List<SamlStatement> statements = new List<SamlStatement> ();
46
47                 public SamlAssertion ()
48                 {
49                         assertion_id = "SamlSecurityToken-" + Guid.NewGuid ();
50                         major = 1;
51                         minor = 1;
52                         issue_instant = DateTime.Now.ToUniversalTime ();
53                 }
54
55                 public SamlAssertion (string assertionId, string issuer,
56                         DateTime issueInstant, SamlConditions conditions,
57                         SamlAdvice advice, IEnumerable<SamlStatement> statements)
58                 {
59                         if (IsInvalidAssertionId (assertionId))
60                                 throw new ArgumentException (String.Format ("The assertionId '{0}' must be a valid XML NCName.", assertionId));
61
62                         if (issuer == null || issuer.Length == 0)
63                                 throw new ArgumentException ("issuer");
64                         if (statements == null)
65                                 throw new ArgumentNullException ("statements");
66
67                         major = 1;
68                         minor = 1;
69
70                         assertion_id = assertionId;
71                         this.issuer = issuer;
72                         issue_instant = issueInstant;
73                         this.conditions = conditions;
74                         this.advice = advice;
75                         foreach (SamlStatement s in statements) {
76                                 if (s == null)
77                                         throw new ArgumentException ("statements contain null item.");
78                                 this.statements.Add (s);
79                         }
80                         if (this.statements.Count == 0)
81                                 throw new ArgumentException ("At least one assertion statement is required.");
82                 }
83
84                 bool IsInvalidAssertionId (string assertionId)
85                 {
86                         if (assertionId == null || assertionId.Length == 0)
87                                 return true;
88                         try {
89                                 XmlConvert.VerifyNCName (assertionId);
90                         } catch (XmlException) {
91                                 return true;
92                         }
93                         return false;
94                 }
95
96                 public SamlAdvice Advice {
97                         get { return advice; }
98                         set {
99                                 CheckReadOnly ();
100                                 advice = value;
101                         }
102                 }
103
104                 public string AssertionId {
105                         get { return assertion_id; }
106                         set {
107                                 CheckReadOnly ();
108                                 assertion_id = value;
109                         }
110                 }
111
112                 public SamlConditions Conditions {
113                         get { return conditions; }
114                         set {
115                                 CheckReadOnly ();
116                                 conditions = value;
117                         }
118                 }
119
120                 public DateTime IssueInstant {
121                         get { return issue_instant; }
122                         set {
123                                 CheckReadOnly ();
124                                 issue_instant = value;
125                         }
126                 }
127
128                 public string Issuer {
129                         get { return issuer; }
130                         set {
131                                 CheckReadOnly ();
132                                 issuer = value;
133                         }
134                 }
135
136                 public int MajorVersion {
137                         get { return major; }
138                 }
139
140                 public int MinorVersion {
141                         get { return minor; }
142                 }
143
144                 public SigningCredentials SigningCredentials {
145                         get { return signing_credentials; }
146                         set {
147                                 CheckReadOnly ();
148                                 signing_credentials = value;
149                         }
150                 }
151
152                 [MonoTODO]
153                 public SecurityToken SigningToken {
154                         get {
155                                 if (signing_credentials == null)
156                                         return null;
157                                 throw new NotImplementedException ();
158                         }
159                 }
160
161                 public IList<SamlStatement> Statements {
162                         get { return statements; }
163                 }
164
165                 public bool IsReadOnly {
166                         get { return is_readonly; }
167                 }
168
169                 private void CheckReadOnly ()
170                 {
171                         if (is_readonly)
172                                 throw new InvalidOperationException ("This SAML assertion is read-only.");
173                 }
174
175                 public void MakeReadOnly ()
176                 {
177                         is_readonly = true;
178                 }
179
180                 [MonoTODO]
181                 public virtual void ReadXml (XmlDictionaryReader reader,
182                         SamlSerializer samlSerializer,
183                         SecurityTokenSerializer keyInfoTokenSerializer,
184                         SecurityTokenResolver outOfBandTokenResolver)
185                 {
186                         throw new NotImplementedException ();
187                 }
188
189                 public virtual void WriteXml (XmlDictionaryWriter writer,
190                         SamlSerializer samlSerializer,
191                         SecurityTokenSerializer keyInfoTokenSerializer)
192                 {
193                         if (writer == null)
194                                 throw new ArgumentNullException ("writer");
195
196                         if (Issuer == null || Issuer.Length == 0)
197                                 throw new SecurityTokenException ("Issuer must not be null or empty.");
198                         if (Statements.Count == 0)
199                                 throw new SecurityTokenException ("At least one assertion statement is required.");
200
201                         if (samlSerializer == null)
202                                 throw new ArgumentNullException ("samlSerializer");
203                         CultureInfo invariant = CultureInfo.InvariantCulture;
204
205                         writer.WriteStartElement ("saml", "Assertion", SamlConstants.Namespace);
206                         writer.WriteAttributeString ("MajorVersion", MajorVersion.ToString (invariant));
207                         writer.WriteAttributeString ("MinorVersion", MinorVersion.ToString (invariant));
208                         writer.WriteAttributeString ("AssertionID", AssertionId);
209                         writer.WriteAttributeString ("Issuer", Issuer);
210                         writer.WriteAttributeString ("IssueInstant", IssueInstant.ToString (SamlConstants.DateFormat, invariant));
211
212                         try {
213                                 if (Conditions != null)
214                                         Conditions.WriteXml (writer, samlSerializer, keyInfoTokenSerializer);
215                                 if (Advice != null)
216                                         Advice.WriteXml (writer, samlSerializer, keyInfoTokenSerializer);
217                                 foreach (SamlStatement statement in Statements)
218                                         statement.WriteXml (writer, samlSerializer, keyInfoTokenSerializer);
219                         } catch (NotImplementedException) {
220                                 throw;
221                         } catch (Exception ex) { // bad catch, eh?
222                                 throw new InvalidOperationException ("There is an error on writing assertion statements.", ex);
223                         }
224                         writer.WriteEndElement ();
225                 }
226
227                 [MonoTODO]
228                 protected void ReadSignature (XmlDictionaryReader reader,
229                         SecurityTokenSerializer keyInfoTokenSerializer,
230                         SecurityTokenResolver outOfBandTokenResolver,
231                         SamlSerializer samlSerializer)
232                 {
233                         throw new NotImplementedException ();
234                 }
235         }
236 }