summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth.OpenId.RelyingParty.UI/OpenId/RelyingParty/OpenIdSelector.cs
blob: 47984b476a45c0d58a9eded58a3a8a167a9fb637 (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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
//-----------------------------------------------------------------------
// <copyright file="OpenIdSelector.cs" company="Outercurve Foundation">
//     Copyright (c) Outercurve Foundation. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------

[assembly: System.Web.UI.WebResource(DotNetOpenAuth.OpenId.RelyingParty.OpenIdSelector.EmbeddedScriptResourceName, "text/javascript")]
[assembly: System.Web.UI.WebResource(DotNetOpenAuth.OpenId.RelyingParty.OpenIdSelector.EmbeddedStylesheetResourceName, "text/css")]

namespace DotNetOpenAuth.OpenId.RelyingParty {
	using System;
	using System.Collections.Generic;
	using System.Collections.ObjectModel;
	using System.ComponentModel;
	using System.Globalization;
	using System.IdentityModel.Claims;
	using System.Linq;
	using System.Text;
	using System.Threading;
	using System.Web;
	using System.Web.UI;
	using System.Web.UI.HtmlControls;
	using System.Web.UI.WebControls;
	using DotNetOpenAuth.ComponentModel;
	using DotNetOpenAuth.Messaging;

	/// <summary>
	/// An ASP.NET control that provides a user-friendly way of logging into a web site using OpenID.
	/// </summary>
	[ToolboxData("<{0}:OpenIdSelector runat=\"server\"></{0}:OpenIdSelector>")]
	public class OpenIdSelector : OpenIdRelyingPartyAjaxControlBase {
		/// <summary>
		/// The name of the manifest stream containing the OpenIdButtonPanel.js file.
		/// </summary>
		internal const string EmbeddedScriptResourceName = Util.DefaultNamespace + ".OpenId.RelyingParty.OpenIdSelector.js";

		/// <summary>
		/// The name of the manifest stream containing the OpenIdButtonPanel.css file.
		/// </summary>
		internal const string EmbeddedStylesheetResourceName = Util.DefaultNamespace + ".OpenId.RelyingParty.OpenIdSelector.css";

		/// <summary>
		/// The substring to append to the end of the id or name of this control to form the
		/// unique name of the hidden field that will carry the positive assertion on postback.
		/// </summary>
		private const string AuthDataFormKeySuffix = "_openidAuthData";

		#region ViewState keys

		/// <summary>
		/// The viewstate key to use for storing the value of the <see cref="Buttons"/> property.
		/// </summary>
		private const string ButtonsViewStateKey = "Buttons";

		/// <summary>
		/// The viewstate key to use for storing the value of the <see cref="AuthenticatedAsToolTip"/> property.
		/// </summary>
		private const string AuthenticatedAsToolTipViewStateKey = "AuthenticatedAsToolTip";

		#endregion

		#region Property defaults

		/// <summary>
		/// The default value for the <see cref="AuthenticatedAsToolTip"/> property.
		/// </summary>
		private const string AuthenticatedAsToolTipDefault = "We recognize you!";

		#endregion

		/// <summary>
		/// The OpenIdAjaxTextBox that remains hidden until the user clicks the OpenID button.
		/// </summary>
		private OpenIdAjaxTextBox textBox;

		/// <summary>
		/// The hidden field that will transmit the positive assertion to the RP.
		/// </summary>
		private HiddenField positiveAssertionField;

		/// <summary>
		/// Initializes a new instance of the <see cref="OpenIdSelector"/> class.
		/// </summary>
		public OpenIdSelector() {
		}

		/// <summary>
		/// Gets the text box where applicable.
		/// </summary>
		public OpenIdAjaxTextBox TextBox {
			get {
				this.EnsureChildControlsAreCreatedSafe();
				return this.textBox;
			}
		}

		/// <summary>
		/// Gets or sets the maximum number of OpenID Providers to simultaneously try to authenticate with.
		/// </summary>
		[Browsable(true), DefaultValue(OpenIdAjaxTextBox.ThrottleDefault), Category(BehaviorCategory)]
		[Description("The maximum number of OpenID Providers to simultaneously try to authenticate with.")]
		public int Throttle {
			get {
				this.EnsureChildControlsAreCreatedSafe();
				return this.textBox.Throttle;
			}

			set {
				this.EnsureChildControlsAreCreatedSafe();
				this.textBox.Throttle = value;
			}
		}

		/// <summary>
		/// Gets or sets the time duration for the AJAX control to wait for an OP to respond before reporting failure to the user.
		/// </summary>
		[Browsable(true), DefaultValue(typeof(TimeSpan), "00:00:08"), Category(BehaviorCategory)]
		[Description("The time duration for the AJAX control to wait for an OP to respond before reporting failure to the user.")]
		public TimeSpan Timeout {
			get {
				this.EnsureChildControlsAreCreatedSafe();
				return this.textBox.Timeout;
			}

			set {
				this.EnsureChildControlsAreCreatedSafe();
				this.textBox.Timeout = value;
			}
		}

		/// <summary>
		/// Gets or sets the tool tip text that appears on the green checkmark when authentication succeeds.
		/// </summary>
		[Bindable(true), DefaultValue(AuthenticatedAsToolTipDefault), Localizable(true), Category(AppearanceCategory)]
		[Description("The tool tip text that appears on the green checkmark when authentication succeeds.")]
		public string AuthenticatedAsToolTip {
			get { return (string)(this.ViewState[AuthenticatedAsToolTipViewStateKey] ?? AuthenticatedAsToolTipDefault); }
			set { this.ViewState[AuthenticatedAsToolTipViewStateKey] = value ?? string.Empty; }
		}

		/// <summary>
		/// Gets or sets a value indicating whether the Yahoo! User Interface Library (YUI)
		/// will be downloaded in order to provide a login split button.
		/// </summary>
		/// <value>
		/// 	<c>true</c> to use a split button; otherwise, <c>false</c> to use a standard HTML button
		/// 	or a split button by downloading the YUI library yourself on the hosting web page.
		/// </value>
		/// <remarks>
		/// The split button brings in about 180KB of YUI javascript dependencies.
		/// </remarks>
		[Bindable(true), DefaultValue(OpenIdAjaxTextBox.DownloadYahooUILibraryDefault), Category(BehaviorCategory)]
		[Description("Whether a split button will be used for the \"log in\" when the user provides an identifier that delegates to more than one Provider.")]
		public bool DownloadYahooUILibrary {
			get {
				this.EnsureChildControlsAreCreatedSafe();
				return this.textBox.DownloadYahooUILibrary;
			}

			set {
				this.EnsureChildControlsAreCreatedSafe();
				this.textBox.DownloadYahooUILibrary = value;
			}
		}

		/// <summary>
		/// Gets the collection of buttons this selector should render to the browser.
		/// </summary>
		[PersistenceMode(PersistenceMode.InnerProperty)]
		public Collection<SelectorButton> Buttons {
			get {
				if (this.ViewState[ButtonsViewStateKey] == null) {
					var providers = new Collection<SelectorButton>();
					this.ViewState[ButtonsViewStateKey] = providers;
					return providers;
				} else {
					return (Collection<SelectorButton>)this.ViewState[ButtonsViewStateKey];
				}
			}
		}

		/// <summary>
		/// Gets a <see cref="T:System.Web.UI.ControlCollection"/> object that represents the child controls for a specified server control in the UI hierarchy.
		/// </summary>
		/// <returns>
		/// The collection of child controls for the specified server control.
		/// </returns>
		public override ControlCollection Controls {
			get {
				this.EnsureChildControls();
				return base.Controls;
			}
		}

		/// <summary>
		/// Gets the name of the open id auth data form key (for the value as stored at the user agent as a FORM field).
		/// </summary>
		/// <value>
		/// Usually a concatenation of the control's name and <c>"_openidAuthData"</c>.
		/// </value>
		protected override string OpenIdAuthDataFormKey {
			get { return this.UniqueID + AuthDataFormKeySuffix; }
		}

		/// <summary>
		/// Gets a value indicating whether some button in the selector will want
		/// to display the <see cref="OpenIdAjaxTextBox"/> control.
		/// </summary>
		protected virtual bool OpenIdTextBoxVisible {
			get { return this.Buttons.OfType<SelectorOpenIdButton>().Any(); }
		}

		/// <summary>
		/// Releases unmanaged and - optionally - managed resources
		/// </summary>
		/// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
		protected override void Dispose(bool disposing) {
			if (disposing) {
				foreach (var button in this.Buttons.OfType<IDisposable>()) {
					button.Dispose();
				}
			}

			base.Dispose(disposing);
		}

		/// <summary>
		/// Called by the ASP.NET page framework to notify server controls that use composition-based implementation to create any child controls they contain in preparation for posting back or rendering.
		/// </summary>
		protected override void CreateChildControls() {
			this.EnsureChildControlsAreCreatedSafe();

			base.CreateChildControls();

			// Now do the ID specific work.
			this.EnsureID();
			ErrorUtilities.VerifyInternal(!string.IsNullOrEmpty(this.UniqueID), "Control.EnsureID() failed to give us a unique ID.  Try setting an ID on the OpenIdSelector control.  But please also file this bug with the project owners.");

			this.Controls.Add(this.textBox);

			this.positiveAssertionField.ID = this.ID + AuthDataFormKeySuffix;
			this.Controls.Add(this.positiveAssertionField);
		}

		/// <summary>
		/// Ensures that the child controls have been built, but doesn't set control
		/// properties that require executing <see cref="Control.EnsureID"/> in order to avoid
		/// certain initialization order problems.
		/// </summary>
		/// <remarks>
		/// We don't just call EnsureChildControls() and then set the property on
		/// this.textBox itself because (apparently) setting this property in the ASPX
		/// page and thus calling this EnsureID() via EnsureChildControls() this early
		/// results in no ID.
		/// </remarks>
		protected virtual void EnsureChildControlsAreCreatedSafe() {
			// If we've already created the child controls, this method is a no-op.
			if (this.textBox != null) {
				return;
			}

			this.textBox = new OpenIdAjaxTextBox();
			this.textBox.ID = "openid_identifier";
			this.textBox.HookFormSubmit = false;
			this.textBox.ShowLogOnPostBackButton = true;

			this.positiveAssertionField = new HiddenField();
		}

		/// <summary>
		/// Raises the <see cref="E:System.Web.UI.Control.Init"/> event.
		/// </summary>
		/// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
		protected override void OnInit(EventArgs e) {
			base.OnInit(e);

			// We force child control creation here so that they can get postback events.
			EnsureChildControls();
		}

		/// <summary>
		/// Raises the <see cref="E:System.Web.UI.Control.PreRender"/> event.
		/// </summary>
		/// <param name="e">An <see cref="T:System.EventArgs"/> object that contains the event data.</param>
		protected override void OnPreRender(EventArgs e) {
			base.OnPreRender(e);

			this.EnsureValidButtons();

			var css = new HtmlLink();
			try {
				css.Href = this.Page.ClientScript.GetWebResourceUrl(typeof(OpenIdSelector), EmbeddedStylesheetResourceName);
				css.Attributes["rel"] = "stylesheet";
				css.Attributes["type"] = "text/css";
				ErrorUtilities.VerifyHost(this.Page.Header != null, OpenIdStrings.HeadTagMustIncludeRunatServer);
				this.Page.Header.Controls.AddAt(0, css); // insert at top so host page can override
			} catch {
				css.Dispose();
				throw;
			}

			// Import the .js file where most of the code is.
			this.Page.ClientScript.RegisterClientScriptResource(typeof(OpenIdSelector), EmbeddedScriptResourceName);

			// Provide javascript with a way to post the login assertion.
			const string PostLoginAssertionMethodName = "postLoginAssertion";
			const string PositiveAssertionParameterName = "positiveAssertion";
			const string ScriptFormat = @"window.{2} = function({0}) {{
	$('#{3}')[0].setAttribute('value', {0});
	{1};
}};";
			string script = string.Format(
				CultureInfo.InvariantCulture,
				ScriptFormat,
				PositiveAssertionParameterName,
				this.Page.ClientScript.GetPostBackEventReference(this, null, false),
				PostLoginAssertionMethodName,
				this.positiveAssertionField.ClientID);
			this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "Postback", script, true);

			this.Page.RegisterAsyncTask(new PageAsyncTask(async ct => {
				await this.PreloadDiscoveryAsync(
					this.Buttons.OfType<SelectorProviderButton>().Select(op => op.OPIdentifier).Where(id => id != null),
					ct);
			}));
			this.textBox.Visible = this.OpenIdTextBoxVisible;
		}

		/// <summary>
		/// Sends server control content to a provided <see cref="T:System.Web.UI.HtmlTextWriter"/> object, which writes the content to be rendered on the client.
		/// </summary>
		/// <param name="writer">The <see cref="T:System.Web.UI.HtmlTextWriter"/> object that receives the server control content.</param>
		protected override void Render(HtmlTextWriter writer) {
			Assumes.True(writer != null, "Missing contract");
			writer.AddAttribute(HtmlTextWriterAttribute.Class, "OpenIdProviders");
			writer.RenderBeginTag(HtmlTextWriterTag.Ul);

			foreach (var button in this.Buttons) {
				button.RenderLeadingAttributes(writer);

				writer.RenderBeginTag(HtmlTextWriterTag.Li);

				writer.AddAttribute(HtmlTextWriterAttribute.Href, "#");
				writer.RenderBeginTag(HtmlTextWriterTag.A);

				writer.RenderBeginTag(HtmlTextWriterTag.Div);
				writer.RenderBeginTag(HtmlTextWriterTag.Div);

				button.RenderButtonContent(writer, this);

				writer.RenderEndTag(); // </div>

				writer.AddAttribute(HtmlTextWriterAttribute.Class, "ui-widget-overlay");
				writer.RenderBeginTag(HtmlTextWriterTag.Div);
				writer.RenderEndTag();

				writer.RenderEndTag(); // </div>
				writer.RenderEndTag(); // </a>
				writer.RenderEndTag(); // </li>
			}

			writer.RenderEndTag(); // </ul>

			if (this.textBox.Visible) {
				writer.AddStyleAttribute(HtmlTextWriterStyle.Display, "none");
				writer.AddAttribute(HtmlTextWriterAttribute.Id, "OpenIDForm");
				writer.RenderBeginTag(HtmlTextWriterTag.Div);

				this.textBox.RenderControl(writer);

				writer.RenderEndTag(); // </div>
			}

			this.positiveAssertionField.RenderControl(writer);
		}

		/// <summary>
		/// Ensures the <see cref="Buttons"/> collection has a valid set of buttons.
		/// </summary>
		private void EnsureValidButtons() {
			foreach (var button in this.Buttons) {
				button.EnsureValid();
			}

			// Also make sure that there are appropriate numbers of each type of button.
			// TODO: code here
		}
	}
}