diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2011-08-01 06:49:15 -0600 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2011-08-01 06:49:15 -0600 |
commit | 7c972c147c32a54bf611baaa3de11cea433d8433 (patch) | |
tree | 934b50d076531abdec9b938d297b541b80eeee89 /src/DotNetOpenAuth.OAuth2.Client.UI/OAuth2/ClientAuthorizationView.cs | |
parent | ee09a67e9f3923b01f86a90a31f91c138d0464a6 (diff) | |
download | DotNetOpenAuth-7c972c147c32a54bf611baaa3de11cea433d8433.zip DotNetOpenAuth-7c972c147c32a54bf611baaa3de11cea433d8433.tar.gz DotNetOpenAuth-7c972c147c32a54bf611baaa3de11cea433d8433.tar.bz2 |
Broke out the UI part of OAuth2 client into its own assembly.
Diffstat (limited to 'src/DotNetOpenAuth.OAuth2.Client.UI/OAuth2/ClientAuthorizationView.cs')
-rw-r--r-- | src/DotNetOpenAuth.OAuth2.Client.UI/OAuth2/ClientAuthorizationView.cs | 192 |
1 files changed, 192 insertions, 0 deletions
diff --git a/src/DotNetOpenAuth.OAuth2.Client.UI/OAuth2/ClientAuthorizationView.cs b/src/DotNetOpenAuth.OAuth2.Client.UI/OAuth2/ClientAuthorizationView.cs new file mode 100644 index 0000000..ffa217b --- /dev/null +++ b/src/DotNetOpenAuth.OAuth2.Client.UI/OAuth2/ClientAuthorizationView.cs @@ -0,0 +1,192 @@ +//----------------------------------------------------------------------- +// <copyright file="ClientAuthorizationView.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.OAuth2 { + using System; + using System.Collections.Generic; + using System.ComponentModel; + using System.Data; + using System.Diagnostics.Contracts; + using System.Drawing; + using System.Linq; + using System.Text; + using System.Windows.Forms; + using DotNetOpenAuth.Messaging; + + /// <summary> + /// A WinForms control that hosts a mini-browser for hosting by native applications to + /// allow the user to authorize the client without leaving the application. + /// </summary> + public partial class ClientAuthorizationView : UserControl { + /// <summary> + /// Initializes a new instance of the <see cref="ClientAuthorizationView"/> class. + /// </summary> + public ClientAuthorizationView() { + this.InitializeComponent(); + + this.Authorization = new AuthorizationState(); + } + + /// <summary> + /// Occurs when the authorization flow has completed. + /// </summary> + public event EventHandler<ClientAuthorizationCompleteEventArgs> Completed; + + /// <summary> + /// Gets the authorization tracking object. + /// </summary> + public IAuthorizationState Authorization { get; private set; } + + /// <summary> + /// Gets or sets the client used to coordinate the authorization flow. + /// </summary> + public UserAgentClient Client { get; set; } + + /// <summary> + /// Gets the set of scopes that describe the requested level of access. + /// </summary> + public HashSet<string> Scope { + get { return this.Authorization.Scope; } + } + + /// <summary> + /// Gets or sets the callback URL used to indicate the flow has completed. + /// </summary> + public Uri Callback { + get { return this.Authorization.Callback; } + set { this.Authorization.Callback = value; } + } + + /// <summary> + /// Gets a value indicating whether the authorization flow has been completed. + /// </summary> + public bool IsCompleted { + get { return this.Authorization == null || this.Authorization.AccessToken != null; } + } + + /// <summary> + /// Gets a value indicating whether authorization has been granted. + /// </summary> + /// <value>Null if <see cref="IsCompleted"/> is <c>false</c></value> + public bool? IsGranted { + get { + if (this.Authorization == null) { + return false; + } + + return this.Authorization.AccessToken != null ? (bool?)true : null; + } + } + + /// <summary> + /// Gets a value indicating whether authorization has been rejected. + /// </summary> + /// <value>Null if <see cref="IsCompleted"/> is <c>false</c></value> + public bool? IsRejected { + get { + bool? granted = this.IsGranted; + return granted.HasValue ? (bool?)(!granted.Value) : null; + } + } + + /// <summary> + /// Called when the authorization flow has been completed. + /// </summary> + protected virtual void OnCompleted() { + var completed = this.Completed; + if (completed != null) { + completed(this, new ClientAuthorizationCompleteEventArgs(this.Authorization)); + } + } + + /// <summary> + /// Raises the <see cref="E:System.Windows.Forms.UserControl.Load"/> event. + /// </summary> + /// <param name="e">An <see cref="T:System.EventArgs"/> that contains the event data.</param> + protected override void OnLoad(EventArgs e) { + base.OnLoad(e); + + Uri authorizationUrl = this.Client.RequestUserAuthorization(this.Authorization); + this.webBrowser1.Navigate(authorizationUrl.AbsoluteUri); // use AbsoluteUri to workaround bug in WebBrowser that calls Uri.ToString instead of Uri.AbsoluteUri leading to escaping errors. + } + + /// <summary> + /// Tests whether two URLs are equal for purposes of detecting the conclusion of authorization. + /// </summary> + /// <param name="location1">The first location.</param> + /// <param name="location2">The second location.</param> + /// <param name="components">The components to compare.</param> + /// <returns><c>true</c> if the given components are equal.</returns> + private static bool SignificantlyEqual(Uri location1, Uri location2, UriComponents components) { + string value1 = location1.GetComponents(components, UriFormat.Unescaped); + string value2 = location2.GetComponents(components, UriFormat.Unescaped); + return string.Equals(value1, value2, StringComparison.Ordinal); + } + + /// <summary> + /// Handles the Navigating event of the webBrowser1 control. + /// </summary> + /// <param name="sender">The source of the event.</param> + /// <param name="e">The <see cref="System.Windows.Forms.WebBrowserNavigatingEventArgs"/> instance containing the event data.</param> + private void WebBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e) { + this.ProcessLocationChanged(e.Url); + } + + /// <summary> + /// Processes changes in the URL the browser has navigated to. + /// </summary> + /// <param name="location">The location.</param> + private void ProcessLocationChanged(Uri location) { + if (SignificantlyEqual(location, this.Authorization.Callback, UriComponents.SchemeAndServer | UriComponents.Path)) { + try { + this.Client.ProcessUserAuthorization(location, this.Authorization); + } catch (ProtocolException ex) { + MessageBox.Show(ex.ToStringDescriptive()); + } finally { + this.OnCompleted(); + } + } + } + + /// <summary> + /// Handles the Navigated event of the webBrowser1 control. + /// </summary> + /// <param name="sender">The source of the event.</param> + /// <param name="e">The <see cref="System.Windows.Forms.WebBrowserNavigatedEventArgs"/> instance containing the event data.</param> + private void WebBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e) { + this.ProcessLocationChanged(e.Url); + } + + /// <summary> + /// Handles the LocationChanged event of the webBrowser1 control. + /// </summary> + /// <param name="sender">The source of the event.</param> + /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param> + private void WebBrowser1_LocationChanged(object sender, EventArgs e) { + this.ProcessLocationChanged(this.webBrowser1.Url); + } + + /// <summary> + /// Describes the results of a completed authorization flow. + /// </summary> + public class ClientAuthorizationCompleteEventArgs : EventArgs { + /// <summary> + /// Initializes a new instance of the <see cref="ClientAuthorizationCompleteEventArgs"/> class. + /// </summary> + /// <param name="authorization">The authorization.</param> + public ClientAuthorizationCompleteEventArgs(IAuthorizationState authorization) { + Contract.Requires<ArgumentNullException>(authorization != null); + this.Authorization = authorization; + } + + /// <summary> + /// Gets the authorization tracking object. + /// </summary> + /// <value>Null if authorization was rejected by the user.</value> + public IAuthorizationState Authorization { get; private set; } + } + } +} |