diff options
Diffstat (limited to 'samples/OpenIdOfflineProvider/MainWindow.xaml.cs')
-rw-r--r-- | samples/OpenIdOfflineProvider/MainWindow.xaml.cs | 115 |
1 files changed, 70 insertions, 45 deletions
diff --git a/samples/OpenIdOfflineProvider/MainWindow.xaml.cs b/samples/OpenIdOfflineProvider/MainWindow.xaml.cs index 1975e91..8cfe157 100644 --- a/samples/OpenIdOfflineProvider/MainWindow.xaml.cs +++ b/samples/OpenIdOfflineProvider/MainWindow.xaml.cs @@ -7,7 +7,12 @@ namespace DotNetOpenAuth.OpenIdOfflineProvider { using System; using System.Collections.Generic; + using System.Diagnostics; + using System.Diagnostics.Contracts; + using System.Globalization; + using System.IO; using System.Linq; + using System.Net; using System.Text; using System.Windows; using System.Windows.Controls; @@ -18,18 +23,34 @@ namespace DotNetOpenAuth.OpenIdOfflineProvider { using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; - using DotNetOpenAuth.OpenId.Provider; - using System.Globalization; - using System.Net; using DotNetOpenAuth.Messaging; - using System.Diagnostics.Contracts; - using System.IO; + using DotNetOpenAuth.OpenId.Provider; /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window, IDisposable { /// <summary> + /// The path to the Provider Endpoint. + /// </summary> + private const string ProviderPath = "/provider"; + + /// <summary> + /// The path to the OP Identifier. + /// </summary> + private const string OPIdentifier = "/"; + + /// <summary> + /// The path to the user identity page that always generates a positive assertion. + /// </summary> + private const string YesIdentity = "/user"; + + /// <summary> + /// The path to the user identity page that always generates a negative response. + /// </summary> + private const string NoIdentity = "/no"; + + /// <summary> /// The <see cref="OpenIdProvider"/> instance that processes incoming requests. /// </summary> private OpenIdProvider provider = new OpenIdProvider(new StandardProviderApplicationStore()); @@ -46,15 +67,6 @@ namespace DotNetOpenAuth.OpenIdOfflineProvider { this.InitializeComponent(); } - /// <summary> - /// Raises the <see cref="E:Closing"/> event. - /// </summary> - /// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param> - protected override void OnClosing(System.ComponentModel.CancelEventArgs e) { - this.StopProvider(); - base.OnClosing(e); - } - #region IDisposable Members /// <summary> @@ -82,6 +94,36 @@ namespace DotNetOpenAuth.OpenIdOfflineProvider { #endregion /// <summary> + /// Raises the <see cref="E:Closing"/> event. + /// </summary> + /// <param name="e">The <see cref="System.ComponentModel.CancelEventArgs"/> instance containing the event data.</param> + protected override void OnClosing(System.ComponentModel.CancelEventArgs e) { + this.StopProvider(); + base.OnClosing(e); + } + + private static string GenerateHtmlDiscoveryDocument(string providerEndpoint, string localId) { + Contract.Requires(providerEndpoint != null && providerEndpoint.Length > 0); + + const string DelegatedHtmlDiscoveryFormat = @"<html><head> + <link rel=""openid.server"" href=""{0}"" /> + <link rel=""openid.delegate"" href=""{1}"" /> + <link rel=""openid2.provider"" href=""{0}"" /> + <link rel=""openid2.local_id"" href=""{1}"" /> + </head><body></body></html>"; + + const string NonDelegatedHtmlDiscoveryFormat = @"<html><head> + <link rel=""openid.server"" href=""{0}"" /> + <link rel=""openid2.provider"" href=""{0}"" /> + </head><body></body></html>"; + + return string.Format( + localId != null ? DelegatedHtmlDiscoveryFormat : NonDelegatedHtmlDiscoveryFormat, + providerEndpoint, + localId); + } + + /// <summary> /// Handles the Click event of the startButton control. /// </summary> /// <param name="sender">The source of the event.</param> @@ -92,16 +134,18 @@ namespace DotNetOpenAuth.OpenIdOfflineProvider { private void StartProvider() { this.httpHost = HttpHost.CreateHost(this.RequestHandler); - this.portTextBox.Text = this.httpHost.Port.ToString(CultureInfo.InvariantCulture); - this.opIdentifierLabel.Content = string.Format("http://localhost:{0}/", this.httpHost.Port); + this.portLabel .Content = this.httpHost.Port.ToString(CultureInfo.InvariantCulture); + string url = "http://localhost:{0}{1}"; + this.opIdentifierLabel.Content = "not yet supported"; // string.Format(url, this.httpHost.Port, OPIdentifier); + this.noIdentity.Content = string.Format(url, this.httpHost.Port, NoIdentity); + this.yesIdentity.Content = string.Format(url, this.httpHost.Port, YesIdentity); } private void RequestHandler(HttpListenerContext context) { Contract.Requires(context != null); - - const string ProviderPath = "/provider"; - const string YesIdentity = "/user"; - const string NoIdentity = "/no"; + Contract.Requires(context.Response.OutputStream != null); + Stream outputStream = context.Response.OutputStream; + Contract.Assume(outputStream != null); // CC static verification shortcoming. if (context.Request.Url.AbsolutePath == ProviderPath) { HttpRequestInfo requestInfo = new HttpRequestInfo(context.Request); @@ -109,7 +153,7 @@ namespace DotNetOpenAuth.OpenIdOfflineProvider { if (providerRequest == null) { App.Logger.Error("A request came in that did not carry an OpenID message."); context.Response.StatusCode = (int)HttpStatusCode.BadRequest; - using (StreamWriter sw = new StreamWriter(context.Response.OutputStream)) { + using (StreamWriter sw = new StreamWriter(outputStream)) { sw.WriteLine("<html><body>This is an OpenID Provider endpoint.</body></html>"); } return; @@ -124,10 +168,9 @@ namespace DotNetOpenAuth.OpenIdOfflineProvider { authRequest.IsAuthenticated = new Uri(authRequest.ClaimedIdentifier).AbsolutePath == YesIdentity; } - Contract.Assert(providerRequest.IsResponseReady); this.provider.PrepareResponse(providerRequest).Send(context.Response); } else if (context.Request.Url.AbsolutePath == YesIdentity || context.Request.Url.AbsolutePath == NoIdentity) { - using (StreamWriter sw = new StreamWriter(context.Response.OutputStream)) { + using (StreamWriter sw = new StreamWriter(outputStream)) { string providerEndpoint = string.Format("http://localhost:{0}{1}", context.Request.Url.Port, ProviderPath); string localId = null; // string.Format("http://localhost:{0}/user", context.Request.Url.Port); string html = GenerateHtmlDiscoveryDocument(providerEndpoint, localId); @@ -142,30 +185,12 @@ namespace DotNetOpenAuth.OpenIdOfflineProvider { } } - private string GenerateHtmlDiscoveryDocument(string providerEndpoint, string localId) { - Contract.Requires(providerEndpoint != null && providerEndpoint.Length > 0); - - const string DelegatedHtmlDiscoveryFormat = @"<html><head> - <link rel=""openid.server"" href=""{0}"" /> - <link rel=""openid.delegate"" href=""{1}"" /> - <link rel=""openid2.provider"" href=""{0}"" /> - <link rel=""openid2.local_id"" href=""{1}"" /> - </head><body></body></html>"; - - const string NonDelegatedHtmlDiscoveryFormat = @"<html><head> - <link rel=""openid.server"" href=""{0}"" /> - <link rel=""openid2.provider"" href=""{0}"" /> - </head><body></body></html>"; - - return string.Format( - localId != null ? DelegatedHtmlDiscoveryFormat : NonDelegatedHtmlDiscoveryFormat, - providerEndpoint, - localId); - } - private void stopButton_Click(object sender, RoutedEventArgs e) { this.StopProvider(); - this.portTextBox.Text = string.Empty; + this.portLabel.Content = string.Empty; + this.noIdentity.Content = string.Empty; + this.yesIdentity.Content = string.Empty; + this.opIdentifierLabel.Content = string.Empty; } private void StopProvider() { |