//----------------------------------------------------------------------- // // Copyright (c) Andrew Arnott. All rights reserved. // //----------------------------------------------------------------------- namespace DotNetOpenAuth.OpenId.Extensions.ProviderAuthenticationPolicy { using System; using System.Globalization; using DotNetOpenAuth.Messaging; using DotNetOpenAuth.Messaging.Reflection; /// /// An encoder/decoder design for DateTimes that must conform to the PAPE spec. /// /// /// The timestamp MUST be formatted as specified in section 5.6 of [RFC3339] (Klyne, G. and C. Newman, “Date and Time on the Internet: Timestamps,” .), with the following restrictions: /// * All times must be in the UTC timezone, indicated with a "Z". /// * No fractional seconds are allowed /// For example: 2005-05-15T17:11:51Z /// internal class DateTimeEncoder : IMessagePartEncoder { /// /// An array of the date/time formats allowed by the PAPE extension. /// /// /// TODO: This array of formats is not yet a complete list. /// private static readonly string[] PermissibleDateTimeFormats = { "yyyy-MM-ddTHH:mm:ssZ" }; #region IMessagePartEncoder Members /// /// Encodes the specified value. /// /// The value. Guaranteed to never be null. /// /// The in string form, ready for message transport. /// public string Encode(object value) { DateTime? dateTime = value as DateTime?; if (dateTime.HasValue) { return dateTime.Value.ToUniversalTimeSafe().ToString(PermissibleDateTimeFormats[0], CultureInfo.InvariantCulture); } else { return null; } } /// /// Decodes the specified value. /// /// The string value carried by the transport. Guaranteed to never be null, although it may be empty. /// /// The deserialized form of the given string. /// /// Thrown when the string value given cannot be decoded into the required object type. public object Decode(string value) { DateTime dateTime; if (DateTime.TryParse(value, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal, out dateTime) && dateTime.Kind == DateTimeKind.Utc) { // may be unspecified per our option above return dateTime; } else { Logger.OpenId.ErrorFormat("Invalid format for message part: {0}", value); return null; } } #endregion } }