//-----------------------------------------------------------------------
//
// Copyright (c) Outercurve Foundation. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.OpenId.Messages {
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using DotNetOpenAuth.Messaging;
using Validation;
///
/// A common base class for OpenID direct message responses.
///
[DebuggerDisplay("OpenID {Version} response")]
internal class DirectResponseBase : IDirectResponseProtocolMessage {
///
/// The openid.ns parameter in the message.
///
/// "http://specs.openid.net/auth/2.0"
///
/// OpenID 2.0 Section 5.1.2:
/// This particular value MUST be present for the response to be a valid OpenID 2.0 response.
/// Future versions of the specification may define different values in order to allow message
/// recipients to properly interpret the request.
///
[SuppressMessage("Microsoft.Performance", "CA1823:AvoidUnusedPrivateFields", Justification = "Read by reflection.")]
[MessagePart("ns", IsRequired = true, AllowEmpty = false, MinVersion = "2.0")]
#pragma warning disable 0414 // read by reflection
private readonly string OpenIdNamespace = Protocol.OpenId2Namespace;
#pragma warning restore 0414
///
/// Backing store for the properties.
///
private IDirectedProtocolMessage originatingRequest;
///
/// Backing store for the properties.
///
private bool incoming;
///
/// The dictionary of parameters that are not part of the OpenID specification.
///
private Dictionary extraData = new Dictionary();
///
/// Initializes a new instance of the class.
///
/// The OpenID version of the response message.
/// The originating request. May be null in case the request is unrecognizable and this is an error response.
protected DirectResponseBase(Version responseVersion, IDirectedProtocolMessage originatingRequest) {
Requires.NotNull(responseVersion, "responseVersion");
this.Version = responseVersion;
this.originatingRequest = originatingRequest;
}
#region IProtocolMessage Properties
///
/// Gets the version of the protocol this message is prepared to implement.
///
/// Version 2.0
public Version Version { get; private set; }
///
/// Gets the level of protection this message requires.
///
///
public MessageProtections RequiredProtection {
get { return MessageProtections.None; }
}
///
/// Gets a value indicating whether this is a direct or indirect message.
///
///
public MessageTransport Transport {
get { return MessageTransport.Direct; }
}
///
/// Gets the extra, non-OAuth parameters included in the message.
///
public IDictionary ExtraData {
get { return this.extraData; }
}
#endregion
#region IDirectResponseProtocolMessage Members
///
/// Gets the originating request message that caused this response to be formed.
///
///
/// This property may be null if the request message was undecipherable.
///
IDirectedProtocolMessage IDirectResponseProtocolMessage.OriginatingRequest {
get { return this.originatingRequest; }
}
#endregion
///
/// Gets a value indicating whether this message was deserialized as an incoming message.
///
protected internal bool Incoming {
get { return this.incoming; }
}
///
/// Gets the protocol used by this message.
///
protected Protocol Protocol {
get { return Protocol.Lookup(this.Version); }
}
///
/// Gets the originating request message that caused this response to be formed.
///
protected IDirectedProtocolMessage OriginatingRequest {
get { return this.originatingRequest; }
}
#region IProtocolMessage 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.
public virtual void EnsureValidMessage() {
}
#endregion
///
/// Sets a flag indicating that this message is received (as opposed to sent).
///
internal void SetAsIncoming() {
this.incoming = true;
}
}
}