2 // Timestamp.cs: Handles WS-Security "Utility" Timestamp
\r
5 // Sebastien Pouliot (spouliot@motus.com)
\r
7 // (C) 2002, 2003 Motus Technologies Inc. (http://www.motus.com)
\r
9 // Licensed under MIT X11 (see LICENSE) with this specific addition:
\r
11 //
\93This source code may incorporate intellectual property owned by Microsoft
\r
12 // Corporation. Our provision of this source code does not include any licenses
\r
13 // or any other rights to you under any Microsoft intellectual property. If you
\r
14 // would like a license from Microsoft (e.g. rebrand, redistribute), you need
\r
15 // to contact Microsoft directly.
\94 \r
19 using System.Web.Services.Protocols;
\r
21 using Microsoft.Web.Services;
\r
23 using Microsoft.Web.Services.Xml;
\r
26 namespace Microsoft.Web.Services.Timestamp {
\r
29 // a. Web Services Security Addendum, Version 1.0, August 18, 2002
\r
30 // http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnglobspec/html/ws-security.asp
\r
32 public class Timestamp : SoapHeader, IXmlElement {
\r
35 private string idNS;
\r
36 private DateTime created;
\r
37 private string createdId;
\r
38 private string createdIdNS;
\r
39 private DateTime expired;
\r
40 private string expiredId;
\r
41 private string expiredIdNS;
\r
42 private ReceivedCollection rcoll;
\r
43 private long timeToLive;
\r
45 public Timestamp ()
\r
47 created = DateTime.MinValue;
\r
48 expired = DateTime.MaxValue;
\r
49 timeToLive = 300000; // 5 minutes
\r
50 rcoll = new ReceivedCollection ();
\r
53 // we must be able to fix both creation and expiration
\r
54 internal void SetTimestamp (DateTime c)
\r
57 expired = created.AddMilliseconds (timeToLive);
\r
60 public new string Actor {
\r
61 get { return base.Actor; }
\r
64 public DateTime Created {
\r
65 get { return created; }
\r
68 public DateTime Expires {
\r
69 get { return expired; }
\r
72 public ReceivedCollection Receivers {
\r
73 get { return rcoll; }
\r
77 get { return timeToLive; }
\r
80 throw new System.ArgumentException ("value");
\r
82 expired = DateTime.MaxValue;
\r
83 timeToLive = value;
\r
87 // syntactically correct
\r
88 // no checks are done on the dates themselves !
\r
89 public void CheckValid ()
\r
91 if (created == DateTime.MinValue)
\r
92 throw new TimestampFormatException (TimestampFormatException.MissingCreatedElement);
\r
95 public XmlElement GetXml (XmlDocument document)
\r
97 if (document == null)
\r
98 throw new System.ArgumentNullException ("document");
\r
100 // much cleaner than using StringBuilder!
\r
101 XmlElement xel = document.CreateElement (WSTimestamp.Prefix, WSTimestamp.ElementNames.Timestamp, WSTimestamp.NamespaceURI);
\r
102 // FIXME: commented for WSE preview compatibility
\r
104 // xel.SetAttribute (WSTimestamp.AttributeNames.Id, idNS, id);
\r
106 if (created != DateTime.MinValue) {
\r
107 XmlElement xelCreated = document.CreateElement (WSTimestamp.Prefix, WSTimestamp.ElementNames.Created, WSTimestamp.NamespaceURI);
\r
108 xelCreated.InnerText = created.ToString (WSTimestamp.TimeFormat);
\r
109 if (createdId != null)
\r
110 xelCreated.SetAttribute (WSTimestamp.AttributeNames.Id, createdIdNS, createdId);
\r
111 xel.AppendChild (xelCreated);
\r
113 if (expired != DateTime.MaxValue) {
\r
114 XmlElement xelExpires = document.CreateElement (WSTimestamp.Prefix, WSTimestamp.ElementNames.Expires, WSTimestamp.NamespaceURI);
\r
115 xelExpires.InnerText = expired.ToString (WSTimestamp.TimeFormat);
\r
116 if (expiredId != null)
\r
117 xelExpires.SetAttribute (WSTimestamp.AttributeNames.Id, expiredIdNS, expiredId);
\r
118 xel.AppendChild (xelExpires);
\r
120 for (int i=0; i < rcoll.Count ; i++)
\r
122 XmlElement received = rcoll[i].GetXml (document);
\r
123 xel.AppendChild (received);
\r
128 public void LoadXml (XmlElement element)
\r
130 if (element == null)
\r
131 throw new System.ArgumentNullException ("element");
\r
133 if ((element.LocalName != WSTimestamp.ElementNames.Timestamp) || (element.NamespaceURI != WSTimestamp.NamespaceURI))
\r
134 throw new System.ArgumentException ("invalid LocalName or NamespaceURI");
\r
136 XmlAttribute xa = element.Attributes [WSTimestamp.AttributeNames.Id, WSTimestamp.NamespaceURI];
\r
137 id = ((xa == null) ? null : xa.Value);
\r
138 idNS = ((xa == null) ? null : xa.NamespaceURI);
\r
140 XmlNodeList xnl = element.GetElementsByTagName (WSTimestamp.ElementNames.Created, WSTimestamp.NamespaceURI);
\r
142 switch (xnl.Count) {
\r
144 // throw new TimestampFormatException (TimestampFormatException.MissingCreatedElement);
\r
147 created = DateTime.ParseExact (xnl[0].InnerText, WSTimestamp.TimeFormat, null);
\r
148 created = created.ToUniversalTime ();
\r
149 xa = xnl[0].Attributes [WSTimestamp.AttributeNames.Id, WSTimestamp.NamespaceURI];
\r
150 createdId = ((xa == null) ? null : xa.Value);
\r
151 createdIdNS = ((xa == null) ? null : xa.NamespaceURI);
\r
154 throw new TimestampFormatException (TimestampFormatException.DuplicateCreatedElement);
\r
158 xnl = element.GetElementsByTagName (WSTimestamp.ElementNames.Expires, WSTimestamp.NamespaceURI);
\r
160 switch (xnl.Count) {
\r
162 // throw new TimestampFormatException (TimestampFormatException.MissingCreatedElement);
\r
165 expired = DateTime.ParseExact (xnl[0].InnerText, WSTimestamp.TimeFormat, null);
\r
166 expired = expired.ToUniversalTime ();
\r
167 xa = xnl[0].Attributes [WSTimestamp.AttributeNames.Id, WSTimestamp.NamespaceURI];
\r
168 expiredId = ((xa == null) ? null : xa.Value);
\r
169 expiredIdNS = ((xa == null) ? null : xa.NamespaceURI);
\r
172 throw new TimestampFormatException (TimestampFormatException.DuplicateCreatedElement);
\r
176 xnl = element.GetElementsByTagName (WSTimestamp.ElementNames.Received, WSTimestamp.NamespaceURI);
\r
178 for (int i=0; i < xnl.Count; i++) {
\r
179 Received r = new Received ((XmlElement)xnl [i]);
\r