diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2009-03-28 07:32:39 -0700 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2009-03-28 15:26:27 -0700 |
commit | 5d8fc6c5cbab705be51c344ab295df4fd656f04e (patch) | |
tree | f94a361b77f2542c2c2e489727171f3c8ba753f5 | |
parent | d4bd79666ee0dcd7076789ec495e2b785cee64d0 (diff) | |
download | DotNetOpenAuth-5d8fc6c5cbab705be51c344ab295df4fd656f04e.zip DotNetOpenAuth-5d8fc6c5cbab705be51c344ab295df4fd656f04e.tar.gz DotNetOpenAuth-5d8fc6c5cbab705be51c344ab295df4fd656f04e.tar.bz2 |
Refactored OpenIdProvider host out of the XAML window code.
-rw-r--r-- | samples/OpenIdOfflineProvider/HostedProvider.cs | 210 | ||||
-rw-r--r-- | samples/OpenIdOfflineProvider/MainWindow.xaml.cs | 129 | ||||
-rw-r--r-- | samples/OpenIdOfflineProvider/OpenIdOfflineProvider.csproj | 1 |
3 files changed, 226 insertions, 114 deletions
diff --git a/samples/OpenIdOfflineProvider/HostedProvider.cs b/samples/OpenIdOfflineProvider/HostedProvider.cs new file mode 100644 index 0000000..7a8cf01 --- /dev/null +++ b/samples/OpenIdOfflineProvider/HostedProvider.cs @@ -0,0 +1,210 @@ +//----------------------------------------------------------------------- +// <copyright file="HostedProvider.cs" company="Andrew Arnott"> +// Copyright (c) Andrew Arnott. All rights reserved. +// </copyright> +//----------------------------------------------------------------------- + +namespace DotNetOpenAuth.OpenIdOfflineProvider { + using System; + using System.Collections.Generic; + using System.Diagnostics.Contracts; + using System.IO; + using System.Net; + using DotNetOpenAuth.Messaging; + using DotNetOpenAuth.OpenId.Provider; + + /// <summary> + /// The OpenID Provider host. + /// </summary> + internal class HostedProvider : 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()); + + /// <summary> + /// The HTTP listener that acts as the OpenID Provider socket. + /// </summary> + private HttpHost httpHost; + + /// <summary> + /// Initializes a new instance of the <see cref="HostedProvider"/> class. + /// </summary> + internal HostedProvider() { + this.AffirmativeIdentities = new HashSet<Uri>(); + this.NegativeIdentitities = new HashSet<Uri>(); + } + + /// <summary> + /// Gets a value indicating whether this instance is running. + /// </summary> + /// <value> + /// <c>true</c> if this instance is running; otherwise, <c>false</c>. + /// </value> + internal bool IsRunning { + get { return this.httpHost != null; } + } + + /// <summary> + /// Gets a collection of identity URLs that always produce positive assertions. + /// </summary> + internal ICollection<Uri> AffirmativeIdentities { get; private set; } + + /// <summary> + /// Gets a collection of identity URLs that always produce cancellation responses. + /// </summary> + internal ICollection<Uri> NegativeIdentitities { get; private set; } + + /// <summary> + /// Gets the provider endpoint. + /// </summary> + internal Uri ProviderEndpoint { + get { + Contract.Requires(this.IsRunning); + return new Uri(this.httpHost.BaseUri, ProviderPath); + } + } + + /// <summary> + /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources. + /// </summary> + public void Dispose() { + this.Dispose(true); + } + + /// <summary> + /// Starts the provider. + /// </summary> + internal void StartProvider() { + Contract.Ensures(this.IsRunning); + this.httpHost = HttpHost.CreateHost(this.RequestHandler); + this.AffirmativeIdentities.Add(new Uri(this.httpHost.BaseUri, YesIdentity)); + this.NegativeIdentitities.Add(new Uri(this.httpHost.BaseUri, NoIdentity)); + } + + /// <summary> + /// Stops the provider. + /// </summary> + internal void StopProvider() { + Contract.Ensures(!this.IsRunning); + if (this.httpHost != null) { + this.httpHost.Dispose(); + this.httpHost = null; + } + } + + #region IDisposable Members + + /// <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 virtual void Dispose(bool disposing) { + if (disposing) { + var host = this.httpHost as IDisposable; + if (host != null) { + host.Dispose(); + } + + this.httpHost = null; + } + } + + #endregion + + /// <summary> + /// Generates HTML for an identity page. + /// </summary> + /// <param name="providerEndpoint">The provider endpoint.</param> + /// <param name="localId">The local id.</param> + /// <returns>The HTML document to return to the RP.</returns> + 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 incoming HTTP requests. + /// </summary> + /// <param name="context">The HttpListener context.</param> + private void RequestHandler(HttpListenerContext context) { + Contract.Requires(context != null); + 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); + IRequest providerRequest = this.provider.GetRequest(requestInfo); + 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(outputStream)) { + sw.WriteLine("<html><body>This is an OpenID Provider endpoint.</body></html>"); + } + return; + } + + if (!providerRequest.IsResponseReady) { + var authRequest = providerRequest as IAuthenticationRequest; + if (authRequest.IsDirectedIdentity) { + throw new NotImplementedException(); + } + + authRequest.IsAuthenticated = new Uri(authRequest.ClaimedIdentifier).AbsolutePath == YesIdentity; + } + + this.provider.PrepareResponse(providerRequest).Send(context.Response); + } else if (context.Request.Url.AbsolutePath == YesIdentity || context.Request.Url.AbsolutePath == NoIdentity) { + 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); + sw.WriteLine(html); + } + + context.Response.StatusCode = (int)HttpStatusCode.OK; + context.Response.OutputStream.Close(); + } else { + context.Response.StatusCode = (int)HttpStatusCode.NotFound; + context.Response.OutputStream.Close(); + } + } + } +} diff --git a/samples/OpenIdOfflineProvider/MainWindow.xaml.cs b/samples/OpenIdOfflineProvider/MainWindow.xaml.cs index 8cfe157..859558e 100644 --- a/samples/OpenIdOfflineProvider/MainWindow.xaml.cs +++ b/samples/OpenIdOfflineProvider/MainWindow.xaml.cs @@ -23,42 +23,15 @@ namespace DotNetOpenAuth.OpenIdOfflineProvider { using System.Windows.Media.Imaging; using System.Windows.Navigation; using System.Windows.Shapes; - using DotNetOpenAuth.Messaging; - using DotNetOpenAuth.OpenId.Provider; /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window, IDisposable { /// <summary> - /// The path to the Provider Endpoint. + /// The OpenID Provider host object. /// </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()); - - /// <summary> - /// The HTTP listener that acts as the OpenID Provider socket. - /// </summary> - private HttpHost httpHost; + private HostedProvider hostedProvider = new HostedProvider(); /// <summary> /// Initializes a new instance of the <see cref="MainWindow"/> class. @@ -82,12 +55,12 @@ namespace DotNetOpenAuth.OpenIdOfflineProvider { /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param> protected virtual void Dispose(bool disposing) { if (disposing) { - var host = this.httpHost as IDisposable; + var host = this.hostedProvider as IDisposable; if (host != null) { host.Dispose(); } - this.httpHost = null; + this.hostedProvider = null; } } @@ -98,106 +71,34 @@ namespace DotNetOpenAuth.OpenIdOfflineProvider { /// </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(); + this.hostedProvider.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> /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param> private void startButton_Click(object sender, RoutedEventArgs e) { - this.StartProvider(); - } - - private void StartProvider() { - this.httpHost = HttpHost.CreateHost(this.RequestHandler); - this.portLabel .Content = this.httpHost.Port.ToString(CultureInfo.InvariantCulture); - string url = "http://localhost:{0}{1}"; + this.hostedProvider.StartProvider(); + this.portLabel.Content = this.hostedProvider.ProviderEndpoint.Port; 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); - 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); - IRequest providerRequest = this.provider.GetRequest(requestInfo); - 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(outputStream)) { - sw.WriteLine("<html><body>This is an OpenID Provider endpoint.</body></html>"); - } - return; - } - - if (!providerRequest.IsResponseReady) { - var authRequest = providerRequest as IAuthenticationRequest; - if (authRequest.IsDirectedIdentity) { - throw new NotImplementedException(); - } - - authRequest.IsAuthenticated = new Uri(authRequest.ClaimedIdentifier).AbsolutePath == YesIdentity; - } - - this.provider.PrepareResponse(providerRequest).Send(context.Response); - } else if (context.Request.Url.AbsolutePath == YesIdentity || context.Request.Url.AbsolutePath == NoIdentity) { - 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); - sw.WriteLine(html); - } - - context.Response.StatusCode = (int)HttpStatusCode.OK; - context.Response.OutputStream.Close(); - } else { - context.Response.StatusCode = (int)HttpStatusCode.NotFound; - context.Response.OutputStream.Close(); - } + this.noIdentity.Content = this.hostedProvider.NegativeIdentitities.First().AbsoluteUri; + this.yesIdentity.Content = this.hostedProvider.AffirmativeIdentities.First().AbsoluteUri; } + /// <summary> + /// Handles the Click event of the stopButton control. + /// </summary> + /// <param name="sender">The source of the event.</param> + /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param> private void stopButton_Click(object sender, RoutedEventArgs e) { - this.StopProvider(); + this.hostedProvider.StopProvider(); this.portLabel.Content = string.Empty; this.noIdentity.Content = string.Empty; this.yesIdentity.Content = string.Empty; this.opIdentifierLabel.Content = string.Empty; } - - private void StopProvider() { - if (this.httpHost != null) { - this.httpHost.Dispose(); - this.httpHost = null; - } - } } } diff --git a/samples/OpenIdOfflineProvider/OpenIdOfflineProvider.csproj b/samples/OpenIdOfflineProvider/OpenIdOfflineProvider.csproj index 2e6db1b..7bb4aa7 100644 --- a/samples/OpenIdOfflineProvider/OpenIdOfflineProvider.csproj +++ b/samples/OpenIdOfflineProvider/OpenIdOfflineProvider.csproj @@ -106,6 +106,7 @@ </Compile> </ItemGroup> <ItemGroup> + <Compile Include="HostedProvider.cs" /> <Compile Include="HttpHost.cs" /> <Compile Include="Properties\AssemblyInfo.cs"> <SubType>Code</SubType> |