1
//-----------------------------------------------------------------------
2 // <copyright file="ExtensionBase.cs" company="Andrew Arnott">
3 // Copyright (c) Andrew Arnott. All rights reserved.
5 //-----------------------------------------------------------------------
7 namespace DotNetOpenAuth
.OpenId
.Extensions
{
9 using System
.Collections
.Generic
;
12 using DotNetOpenAuth
.Messaging
;
13 using DotNetOpenAuth
.OpenId
.Messages
;
16 /// A handy base class for built-in extensions.
18 public class ExtensionBase
: IOpenIdMessageExtension
{
20 /// Backing store for the <see cref="IOpenIdMessageExtension.TypeUri"/> property.
22 private string typeUri
;
25 /// Backing store for the <see cref="IOpenIdMessageExtension.AdditionalSupportedTypeUris"/> property.
27 private IEnumerable
<string> additionalSupportedTypeUris
;
30 /// Backing store for the <see cref="IMessage.ExtraData"/> property.
32 private Dictionary
<string, string> extraData
= new Dictionary
<string, string>();
35 /// Initializes a new instance of the <see cref="ExtensionBase"/> class.
37 /// <param name="version">The version of the extension.</param>
38 /// <param name="typeUri">The type URI to use in the OpenID message.</param>
39 /// <param name="additionalSupportedTypeUris">The additional supported type URIs by which this extension might be recognized. May be null.</param>
40 protected ExtensionBase(Version version
, string typeUri
, IEnumerable
<string> additionalSupportedTypeUris
) {
41 this.Version
= version
;
42 this.typeUri
= typeUri
;
43 this.additionalSupportedTypeUris
= additionalSupportedTypeUris
?? EmptyList
<string>.Instance
;
46 #region IOpenIdProtocolMessageExtension Members
49 /// Gets the TypeURI the extension uses in the OpenID protocol and in XRDS advertisements.
51 string IOpenIdMessageExtension
.TypeUri
{
52 get { return this.TypeUri; }
56 /// Gets the additional TypeURIs that are supported by this extension, in preferred order.
57 /// May be empty if none other than <see cref="IOpenIdMessageExtension.TypeUri"/> is supported, but
58 /// should not be null.
61 /// Useful for reading in messages with an older version of an extension.
62 /// The value in the <see cref="IOpenIdMessageExtension.TypeUri"/> property is always checked before
64 /// If you do support multiple versions of an extension using this method,
65 /// consider adding a CreateResponse method to your request extension class
66 /// so that the response can have the context it needs to remain compatible
67 /// given the version of the extension in the request message.
68 /// The <see cref="SimpleRegistration.ClaimsRequest.CreateResponse"/> for an example.
70 IEnumerable
<string> IOpenIdMessageExtension
.AdditionalSupportedTypeUris
{
71 get { return this.AdditionalSupportedTypeUris; }
76 #region IMessage Properties
79 /// Gets the version of the protocol or extension this message is prepared to implement.
81 public Version Version { get; private set; }
84 /// Gets the extra, non-standard Protocol parameters included in the message.
87 /// Implementations of this interface should ensure that this property never returns null.
89 IDictionary
<string, string> IMessage
.ExtraData
{
90 get { return this.ExtraData; }
96 /// Gets the TypeURI the extension uses in the OpenID protocol and in XRDS advertisements.
98 protected string TypeUri
{
99 get { return this.typeUri; }
103 /// Gets the additional TypeURIs that are supported by this extension, in preferred order.
104 /// May be empty if none other than <see cref="IOpenIdMessageExtension.TypeUri"/> is supported, but
105 /// should not be null.
109 /// Useful for reading in messages with an older version of an extension.
110 /// The value in the <see cref="IOpenIdMessageExtension.TypeUri"/> property is always checked before
111 /// trying this list.
112 /// If you do support multiple versions of an extension using this method,
113 /// consider adding a CreateResponse method to your request extension class
114 /// so that the response can have the context it needs to remain compatible
115 /// given the version of the extension in the request message.
116 /// The <see cref="Extensions.SimpleRegistration.ClaimsRequest.CreateResponse"/> for an example.
118 protected IEnumerable
<string> AdditionalSupportedTypeUris
{
119 get { return this.additionalSupportedTypeUris; }
123 /// Gets the extra, non-standard Protocol parameters included in the message.
127 /// Implementations of this interface should ensure that this property never returns null.
129 protected IDictionary
<string, string> ExtraData
{
130 get { return this.extraData; }
133 #region IMessage Methods
136 /// Checks the message state for conformity to the protocol specification
137 /// and throws an exception if the message is invalid.
140 /// <para>Some messages have required fields, or combinations of fields that must relate to each other
141 /// in specialized ways. After deserializing a message, this method checks the state of the
142 /// message to see if it conforms to the protocol.</para>
143 /// <para>Note that this property should <i>not</i> check signatures or perform any state checks
144 /// outside this scope of this particular message.</para>
146 /// <exception cref="ProtocolException">Thrown if the message is invalid.</exception>
147 void IMessage
.EnsureValidMessage() {
148 this.EnsureValidMessage();
154 /// Checks the message state for conformity to the protocol specification
155 /// and throws an exception if the message is invalid.
158 /// <para>Some messages have required fields, or combinations of fields that must relate to each other
159 /// in specialized ways. After deserializing a message, this method checks the state of the
160 /// message to see if it conforms to the protocol.</para>
161 /// <para>Note that this property should <i>not</i> check signatures or perform any state checks
162 /// outside this scope of this particular message.</para>
164 /// <exception cref="ProtocolException">Thrown if the message is invalid.</exception>
165 protected virtual void EnsureValidMessage() {