5 // Atsushi Enomoto <atsushi@ximian.com>
7 // Copyright (C) 2005-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
;
30 using System
.Globalization
;
32 using System
.IdentityModel
.Selectors
;
34 namespace System
.IdentityModel
.Tokens
36 public class SamlAssertion
40 SamlConditions conditions
;
41 string assertion_id
, issuer
;
42 DateTime issue_instant
;
44 SigningCredentials signing_credentials
;
45 List
<SamlStatement
> statements
= new List
<SamlStatement
> ();
47 public SamlAssertion ()
49 assertion_id
= "SamlSecurityToken-" + Guid
.NewGuid ();
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");
70 assertion_id
= assertionId
;
72 issue_instant
= issueInstant
;
73 this.conditions
= conditions
;
75 foreach (SamlStatement s
in statements
) {
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)
89 XmlConvert
.VerifyNCName (assertionId
);
90 } catch (XmlException
) {
96 public SamlAdvice Advice
{
97 get { return advice; }
104 public string AssertionId
{
105 get { return assertion_id; }
108 assertion_id
= value;
112 public SamlConditions Conditions
{
113 get { return conditions; }
120 public DateTime IssueInstant
{
121 get { return issue_instant; }
124 issue_instant
= value;
128 public string Issuer
{
129 get { return issuer; }
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; }
148 signing_credentials
= value;
153 public SecurityToken SigningToken
{
155 if (signing_credentials
== 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 ()
172 throw new InvalidOperationException ("This SAML assertion is read-only.");
175 public void MakeReadOnly ()
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
)
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
));
213 if (Conditions
!= null)
214 Conditions
.WriteXml (writer
, samlSerializer
, keyInfoTokenSerializer
);
216 Advice
.WriteXml (writer
, samlSerializer
, keyInfoTokenSerializer
);
217 foreach (SamlStatement statement
in Statements
)
218 statement
.WriteXml (writer
, samlSerializer
, keyInfoTokenSerializer
);
219 } catch (NotImplementedException
) {
221 } catch (Exception ex
) { // bad catch, eh?
222 throw new InvalidOperationException ("There is an error on writing assertion statements.", ex
);
224 writer
.WriteEndElement ();
228 protected void ReadSignature (XmlDictionaryReader reader
,
229 SecurityTokenSerializer keyInfoTokenSerializer
,
230 SecurityTokenResolver outOfBandTokenResolver
,
231 SamlSerializer samlSerializer
)
233 throw new NotImplementedException ();