//-----------------------------------------------------------------------
//
// Copyright (c) Outercurve Foundation. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.Messaging {
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
///
/// A collection of message parts that will be serialized into a single string,
/// to be set into a larger message.
///
public abstract class DataBag : IMessage {
///
/// The default version for DataBags.
///
private static readonly Version DefaultVersion = new Version(1, 0);
///
/// The backing field for the property.
///
private Version version;
///
/// A dictionary to contain extra message data.
///
private Dictionary extraData = new Dictionary();
///
/// Initializes a new instance of the class.
///
protected DataBag()
: this(DefaultVersion) {
}
///
/// Initializes a new instance of the class.
///
/// The DataBag version.
protected DataBag(Version version) {
Requires.NotNull(version, "version");
this.version = version;
}
#region IMessage Properties
///
/// Gets the version of the protocol or extension this message is prepared to implement.
///
///
/// Implementations of this interface should ensure that this property never returns null.
///
Version IMessage.Version {
get { return this.version; }
}
///
/// Gets the extra, non-standard Protocol parameters included in the message.
///
///
///
/// Implementations of this interface should ensure that this property never returns null.
///
public IDictionary ExtraData {
get { return this.extraData; }
}
#endregion
///
/// Gets or sets the nonce.
///
/// The nonce.
[MessagePart]
internal byte[] Nonce { get; set; }
///
/// Gets or sets the UTC creation date of this token.
///
/// The UTC creation date.
[MessagePart("ts", IsRequired = true, Encoder = typeof(TimestampEncoder))]
internal DateTime UtcCreationDate { get; set; }
///
/// Gets or sets the signature.
///
/// The signature.
internal byte[] Signature { get; set; }
///
/// Gets or sets the message that delivered this DataBag instance to this host.
///
protected internal IProtocolMessage ContainingMessage { get; set; }
///
/// Gets the type of this instance.
///
/// The type of the bag.
///
/// This ensures that one token cannot be misused as another kind of token.
///
[SuppressMessage("Microsoft.Performance", "CA1811:AvoidUncalledPrivateCode", Justification = "Accessed by reflection")]
[MessagePart("t", IsRequired = true, AllowEmpty = false)]
protected virtual Type BagType {
get { return this.GetType(); }
}
#region IMessage Methods
///
/// Checks the message state for conformity to the protocol specification
/// and throws an exception if the message is invalid.
///
///
/// Some messages have required fields, or combinations of fields that must relate to each other
/// in specialized ways. After deserializing a message, this method checks the state of the
/// message to see if it conforms to the protocol.
/// Note that this property should not check signatures or perform any state checks
/// outside this scope of this particular message.
///
/// Thrown if the message is invalid.
void IMessage.EnsureValidMessage() {
this.EnsureValidMessage();
}
#endregion
///
/// Checks the message state for conformity to the protocol specification
/// and throws an exception if the message is invalid.
///
///
/// Some messages have required fields, or combinations of fields that must relate to each other
/// in specialized ways. After deserializing a message, this method checks the state of the
/// message to see if it conforms to the protocol.
/// Note that this property should not check signatures or perform any state checks
/// outside this scope of this particular message.
///
/// Thrown if the message is invalid.
protected virtual void EnsureValidMessage() {
}
}
}