summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.OpenId/OpenId/Extensions/ExtensionBase.cs
blob: 09d690abe32bd1835664b1862c9ba9e066696f0d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//-----------------------------------------------------------------------
// <copyright file="ExtensionBase.cs" company="Outercurve Foundation">
//     Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

namespace DotNetOpenAuth.OpenId.Extensions {
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;
	using DotNetOpenAuth.Messaging;
	using DotNetOpenAuth.OpenId.Messages;

	/// <summary>
	/// A handy base class for built-in extensions.
	/// </summary>
	[Serializable]
	public class ExtensionBase : IOpenIdMessageExtension {
		/// <summary>
		/// Backing store for the <see cref="IOpenIdMessageExtension.TypeUri"/> property.
		/// </summary>
		private string typeUri;

		/// <summary>
		/// Backing store for the <see cref="IOpenIdMessageExtension.AdditionalSupportedTypeUris"/> property.
		/// </summary>
		private IEnumerable<string> additionalSupportedTypeUris;

		/// <summary>
		/// Backing store for the <see cref="IMessage.ExtraData"/> property.
		/// </summary>
		private Dictionary<string, string> extraData = new Dictionary<string, string>();

		/// <summary>
		/// Initializes a new instance of the <see cref="ExtensionBase"/> class.
		/// </summary>
		/// <param name="version">The version of the extension.</param>
		/// <param name="typeUri">The type URI to use in the OpenID message.</param>
		/// <param name="additionalSupportedTypeUris">The additional supported type URIs by which this extension might be recognized.  May be null.</param>
		protected ExtensionBase(Version version, string typeUri, IEnumerable<string> additionalSupportedTypeUris) {
			this.Version = version;
			this.typeUri = typeUri;
			this.additionalSupportedTypeUris = additionalSupportedTypeUris ?? EmptyList<string>.Instance;
		}

		#region IOpenIdProtocolMessageExtension Members

		/// <summary>
		/// Gets the TypeURI the extension uses in the OpenID protocol and in XRDS advertisements.
		/// </summary>
		string IOpenIdMessageExtension.TypeUri {
			get { return this.TypeUri; }
		}

		/// <summary>
		/// Gets the additional TypeURIs that are supported by this extension, in preferred order.
		/// May be empty if none other than <see cref="IOpenIdMessageExtension.TypeUri"/> is supported, but
		/// should not be null.
		/// </summary>
		/// <remarks>
		/// Useful for reading in messages with an older version of an extension.
		/// The value in the <see cref="IOpenIdMessageExtension.TypeUri"/> property is always checked before
		/// trying this list.
		/// If you do support multiple versions of an extension using this method,
		/// consider adding a CreateResponse method to your request extension class
		/// so that the response can have the context it needs to remain compatible
		/// given the version of the extension in the request message.
		/// The <see cref="SimpleRegistration.ClaimsRequest.CreateResponse"/> for an example.
		/// </remarks>
		IEnumerable<string> IOpenIdMessageExtension.AdditionalSupportedTypeUris {
			get { return this.AdditionalSupportedTypeUris; }
		}

		/// <summary>
		/// Gets or sets a value indicating whether this extension was
		/// signed by the OpenID Provider.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance is signed by the provider; otherwise, <c>false</c>.
		/// </value>
		bool IOpenIdMessageExtension.IsSignedByRemoteParty {
			get { return this.IsSignedByRemoteParty; }
			set { this.IsSignedByRemoteParty = value; }
		}

		#endregion

		#region IMessage Properties

		/// <summary>
		/// Gets the version of the protocol or extension this message is prepared to implement.
		/// </summary>
		public Version Version { get; private set; }

		/// <summary>
		/// Gets the extra, non-standard Protocol parameters included in the message.
		/// </summary>
		/// <remarks>
		/// Implementations of this interface should ensure that this property never returns null.
		/// </remarks>
		IDictionary<string, string> IMessage.ExtraData {
			get { return this.ExtraData; }
		}

		#endregion

		/// <summary>
		/// Gets the TypeURI the extension uses in the OpenID protocol and in XRDS advertisements.
		/// </summary>
		protected string TypeUri {
			get { return this.typeUri; }
		}

		/// <summary>
		/// Gets or sets a value indicating whether this extension was
		/// signed by the OpenID Provider.
		/// </summary>
		/// <value>
		/// 	<c>true</c> if this instance is signed by the provider; otherwise, <c>false</c>.
		/// </value>
		protected bool IsSignedByRemoteParty { get; set; }

		/// <summary>
		/// Gets the additional TypeURIs that are supported by this extension, in preferred order.
		/// May be empty if none other than <see cref="IOpenIdMessageExtension.TypeUri"/> is supported, but
		/// should not be null.
		/// </summary>
		/// <value></value>
		/// <remarks>
		/// Useful for reading in messages with an older version of an extension.
		/// The value in the <see cref="IOpenIdMessageExtension.TypeUri"/> property is always checked before
		/// trying this list.
		/// If you do support multiple versions of an extension using this method,
		/// consider adding a CreateResponse method to your request extension class
		/// so that the response can have the context it needs to remain compatible
		/// given the version of the extension in the request message.
		/// The <see cref="Extensions.SimpleRegistration.ClaimsRequest.CreateResponse"/> for an example.
		/// </remarks>
		protected IEnumerable<string> AdditionalSupportedTypeUris {
			get { return this.additionalSupportedTypeUris; }
		}

		/// <summary>
		/// Gets the extra, non-standard Protocol parameters included in the message.
		/// </summary>
		/// <value></value>
		/// <remarks>
		/// Implementations of this interface should ensure that this property never returns null.
		/// </remarks>
		protected IDictionary<string, string> ExtraData {
			get { return this.extraData; }
		}

		#region IMessage Methods

		/// <summary>
		/// Checks the message state for conformity to the protocol specification
		/// and throws an exception if the message is invalid.
		/// </summary>
		/// <remarks>
		/// 	<para>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.</para>
		/// 	<para>Note that this property should <i>not</i> check signatures or perform any state checks
		/// outside this scope of this particular message.</para>
		/// </remarks>
		/// <exception cref="ProtocolException">Thrown if the message is invalid.</exception>
		void IMessage.EnsureValidMessage() {
			this.EnsureValidMessage();
		}

		#endregion

		/// <summary>
		/// Checks the message state for conformity to the protocol specification
		/// and throws an exception if the message is invalid.
		/// </summary>
		/// <remarks>
		/// 	<para>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.</para>
		/// 	<para>Note that this property should <i>not</i> check signatures or perform any state checks
		/// outside this scope of this particular message.</para>
		/// </remarks>
		/// <exception cref="ProtocolException">Thrown if the message is invalid.</exception>
		protected virtual void EnsureValidMessage() {
		}
	}
}