//-----------------------------------------------------------------------
//
// Copyright (c) Andrew Arnott. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOAuth.Messages {
using System;
using System.Collections.Generic;
using DotNetOAuth.ChannelElements;
using DotNetOAuth.Messaging;
using DotNetOAuth.Messaging.Bindings;
///
/// A base class for all OAuth messages.
///
internal abstract class MessageBase : IDirectedProtocolMessage, ITamperResistantOAuthMessage, IExpiringProtocolMessage, IReplayProtectedProtocolMessage {
///
/// A store for extra name/value data pairs that are attached to this message.
///
private Dictionary extraData = new Dictionary();
///
/// Gets a value indicating whether signing this message is required.
///
private MessageProtection protectionRequired;
///
/// Gets a value indicating whether this is a direct or indirect message.
///
private MessageTransport transport;
///
/// The URI to the remote endpoint to send this message to.
///
private Uri recipient;
///
/// Initializes a new instance of the class.
///
/// The level of protection the message requires.
/// A value indicating whether this message requires a direct or indirect transport.
protected MessageBase(MessageProtection protectionRequired, MessageTransport transport) {
this.protectionRequired = protectionRequired;
this.transport = transport;
}
///
/// Initializes a new instance of the class.
///
/// The level of protection the message requires.
/// A value indicating whether this message requires a direct or indirect transport.
/// The URI that a directed message will be delivered to.
protected MessageBase(MessageProtection protectionRequired, MessageTransport transport, Uri recipient) {
if (recipient == null) {
throw new ArgumentNullException("recipient");
}
this.protectionRequired = protectionRequired;
this.transport = transport;
this.recipient = recipient;
}
#region IProtocolMessage Properties
///
/// Gets the version of the protocol this message is prepared to implement.
///
Version IProtocolMessage.ProtocolVersion {
get { return new Version(1, 0); }
}
///
/// Gets the level of protection this message requires.
///
MessageProtection IProtocolMessage.RequiredProtection {
get { return this.protectionRequired; }
}
///
/// Gets a value indicating whether this is a direct or indirect message.
///
MessageTransport IProtocolMessage.Transport {
get { return this.transport; }
}
///
/// Gets the dictionary of additional name/value fields tacked on to this message.
///
IDictionary IProtocolMessage.ExtraData {
get { return this.extraData; }
}
#endregion
#region IDirectedProtocolMessage Members
///
/// Gets the URI to the Service Provider endpoint to send this message to.
///
Uri IDirectedProtocolMessage.Recipient {
get { return this.recipient; }
}
#endregion
#region ITamperResistantOAuthMessage Members
///
/// Gets or sets the message signature.
///
[MessagePart("oauth_signature")]
string ITamperResistantProtocolMessage.Signature { get; set; }
///
/// Gets or sets the signature method used to sign the request.
///
[MessagePart("oauth_signature_method")]
string ITamperResistantOAuthMessage.SignatureMethod { get; set; }
#endregion
#region IExpiringProtocolMessage Members
///
/// Gets or sets the OAuth timestamp of the message.
///
[MessagePart("oauth_timestamp")]
DateTime IExpiringProtocolMessage.UtcCreationDate { get; set; }
#endregion
#region IReplayProtectedProtocolMessage Members
///
/// Gets or sets the message nonce used for replay detection.
///
[MessagePart("oauth_nonce")]
string IReplayProtectedProtocolMessage.Nonce { get; set; }
#endregion
///
/// Gets or sets the version of the protocol this message was created with.
///
///
/// This property is useful for handling the oauth_version message part.
///
protected string VersionString {
get {
return ((IProtocolMessage)this).ProtocolVersion.ToString();
}
set {
if (value != this.VersionString) {
throw new ArgumentOutOfRangeException("value");
}
}
}
#region IProtocolMessage Methods
///
/// Checks the message state for conformity to the protocol specification
/// and throws an exception if the message is invalid.
///
void IProtocolMessage.EnsureValidMessage() {
this.EnsureValidMessage();
}
#endregion
///
/// Checks the message state for conformity to the protocol specification
/// and throws an exception if the message is invalid.
///
protected virtual void EnsureValidMessage() { }
}
}