2010-06-03 Jb Evain <jbevain@novell.com>
[mcs.git] / class / System.IdentityModel / System.IdentityModel.Tokens / SamlAssertion.cs
blob65715ee56b9e5e5ffb00b06f5672221426bb47e2
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.
28 using System;
29 using System.Collections.Generic;
30 using System.Globalization;
31 using System.Xml;
32 using System.IdentityModel.Selectors;
34 namespace System.IdentityModel.Tokens
36 public class SamlAssertion
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> ();
47 public SamlAssertion ()
49 assertion_id = "SamlSecurityToken-" + Guid.NewGuid ();
50 major = 1;
51 minor = 1;
52 issue_instant = DateTime.Now.ToUniversalTime ();
55 public SamlAssertion (string assertionId, string issuer,
56 DateTime issueInstant, SamlConditions conditions,
57 SamlAdvice advice, IEnumerable<SamlStatement> statements)
59 if (IsInvalidAssertionId (assertionId))
60 throw new ArgumentException (String.Format ("The assertionId '{0}' must be a valid XML NCName.", assertionId));
62 if (issuer == null || issuer.Length == 0)
63 throw new ArgumentException ("issuer");
64 if (statements == null)
65 throw new ArgumentNullException ("statements");
67 major = 1;
68 minor = 1;
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);
80 if (this.statements.Count == 0)
81 throw new ArgumentException ("At least one assertion statement is required.");
84 bool IsInvalidAssertionId (string assertionId)
86 if (assertionId == null || assertionId.Length == 0)
87 return true;
88 try {
89 XmlConvert.VerifyNCName (assertionId);
90 } catch (XmlException) {
91 return true;
93 return false;
96 public SamlAdvice Advice {
97 get { return advice; }
98 set {
99 CheckReadOnly ();
100 advice = value;
104 public string AssertionId {
105 get { return assertion_id; }
106 set {
107 CheckReadOnly ();
108 assertion_id = value;
112 public SamlConditions Conditions {
113 get { return conditions; }
114 set {
115 CheckReadOnly ();
116 conditions = value;
120 public DateTime IssueInstant {
121 get { return issue_instant; }
122 set {
123 CheckReadOnly ();
124 issue_instant = value;
128 public string Issuer {
129 get { return issuer; }
130 set {
131 CheckReadOnly ();
132 issuer = value;
136 public int MajorVersion {
137 get { return major; }
140 public int MinorVersion {
141 get { return minor; }
144 public SigningCredentials SigningCredentials {
145 get { return signing_credentials; }
146 set {
147 CheckReadOnly ();
148 signing_credentials = value;
152 [MonoTODO]
153 public SecurityToken SigningToken {
154 get {
155 if (signing_credentials == null)
156 return null;
157 throw new NotImplementedException ();
161 public IList<SamlStatement> Statements {
162 get { return statements; }
165 public bool IsReadOnly {
166 get { return is_readonly; }
169 private void CheckReadOnly ()
171 if (is_readonly)
172 throw new InvalidOperationException ("This SAML assertion is read-only.");
175 public void MakeReadOnly ()
177 is_readonly = true;
180 [MonoTODO]
181 public virtual void ReadXml (XmlDictionaryReader reader,
182 SamlSerializer samlSerializer,
183 SecurityTokenSerializer keyInfoTokenSerializer,
184 SecurityTokenResolver outOfBandTokenResolver)
186 throw new NotImplementedException ();
189 public virtual void WriteXml (XmlDictionaryWriter writer,
190 SamlSerializer samlSerializer,
191 SecurityTokenSerializer keyInfoTokenSerializer)
193 if (writer == null)
194 throw new ArgumentNullException ("writer");
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.");
201 if (samlSerializer == null)
202 throw new ArgumentNullException ("samlSerializer");
203 CultureInfo invariant = CultureInfo.InvariantCulture;
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));
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);
224 writer.WriteEndElement ();
227 [MonoTODO]
228 protected void ReadSignature (XmlDictionaryReader reader,
229 SecurityTokenSerializer keyInfoTokenSerializer,
230 SecurityTokenResolver outOfBandTokenResolver,
231 SamlSerializer samlSerializer)
233 throw new NotImplementedException ();