Merge pull request #4453 from lambdageek/bug-49721
[mono.git] / mcs / class / System.IdentityModel / System.IdentityModel.Tokens / SamlConditions.cs
1 //
2 // SamlConditions.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.Globalization;
31 using System.Xml;
32 using System.IdentityModel.Policy;
33 using System.IdentityModel.Selectors;
34 using System.Security.Cryptography;
35
36 namespace System.IdentityModel.Tokens
37 {
38         public class SamlConditions
39         {
40                 DateTime not_before = DateTime.SpecifyKind (DateTime.MinValue.AddDays (1), DateTimeKind.Utc);
41                 DateTime not_on_after = DateTime.SpecifyKind (DateTime.MaxValue.AddDays (-1), DateTimeKind.Utc);
42                 bool is_readonly, has_not_before, has_not_on_after;
43                 List<SamlCondition> conditions = new List<SamlCondition> ();
44
45                 public SamlConditions ()
46                 {
47                 }
48
49                 public SamlConditions (DateTime notBefore, DateTime notOnOrAfter)
50                 {
51                         this.NotBefore = notBefore;
52                         this.NotOnOrAfter = notOnOrAfter;
53                 }
54
55                 public SamlConditions (DateTime notBefore, DateTime notOnOrAfter,
56                         IEnumerable<SamlCondition> conditions)
57                         : this (notBefore, notOnOrAfter)
58                 {
59                         if (conditions != null) {
60                                 foreach (SamlCondition cond in conditions)
61                                         this.conditions.Add (cond);
62                         }
63                 }
64
65                 public IList<SamlCondition> Conditions {
66                         get { return conditions; }
67                 }
68
69                 public DateTime NotBefore {
70                         get { return not_before; }
71                         set {
72                                 CheckReadOnly ();
73                                 not_before = value;
74                                 has_not_before = true;
75                         }
76                 }
77
78                 public DateTime NotOnOrAfter {
79                         get { return not_on_after; }
80                         set {
81                                 CheckReadOnly ();
82                                 not_on_after = value;
83                                 has_not_on_after = true;
84                         }
85                 }
86
87                 public bool IsReadOnly {
88                         get { return is_readonly; }
89                 }
90
91                 private void CheckReadOnly ()
92                 {
93                         if (is_readonly)
94                                 throw new InvalidOperationException ("This SAML 'Conditions' is read-only.");
95                 }
96
97                 public void MakeReadOnly ()
98                 {
99                         is_readonly = true;
100                 }
101
102                 [MonoTODO]
103                 public virtual void ReadXml (XmlDictionaryReader reader,
104                         SamlSerializer samlSerializer,
105                         SecurityTokenSerializer keyInfoSerializer,
106                         SecurityTokenResolver outOfBandTokenResolver)
107                 {
108                         throw new NotImplementedException ();
109                 }
110
111                 public virtual void WriteXml (XmlDictionaryWriter writer,
112                         SamlSerializer samlSerializer,
113                         SecurityTokenSerializer keyInfoSerializer)
114                 {
115                         if (writer == null)
116                                 throw new ArgumentNullException ("writer");
117                         if (samlSerializer == null)
118                                 throw new ArgumentNullException ("samlSerializer");
119                         writer.WriteStartElement ("saml", "Conditions", SamlConstants.Namespace);
120                         CultureInfo invariant = CultureInfo.InvariantCulture;
121                         if (has_not_before)
122                                 writer.WriteAttributeString ("NotBefore", NotBefore.ToString (SamlConstants.DateFormat, invariant));
123                         if (has_not_on_after)
124                                 writer.WriteAttributeString ("NotOnOrAfter", NotOnOrAfter.ToString (SamlConstants.DateFormat, invariant));
125                         foreach (SamlCondition cond in Conditions)
126                                 cond.WriteXml (writer, samlSerializer, keyInfoSerializer);
127                         writer.WriteEndElement ();
128                 }
129         }
130 }