diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2013-03-04 22:03:42 -0800 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2013-03-04 22:03:42 -0800 |
commit | cb249022036daaebcccadf09d3c62410de3147b6 (patch) | |
tree | 3499d6dc917aa43e82a80020b227d32160ce2624 /samples/OpenIdOfflineProvider/Controllers | |
parent | c1e5503b73929913e2c4ae59e0abe08f3d63736b (diff) | |
download | DotNetOpenAuth-cb249022036daaebcccadf09d3c62410de3147b6.zip DotNetOpenAuth-cb249022036daaebcccadf09d3c62410de3147b6.tar.gz DotNetOpenAuth-cb249022036daaebcccadf09d3c62410de3147b6.tar.bz2 |
Moved Offline OP to use controllers.
Diffstat (limited to 'samples/OpenIdOfflineProvider/Controllers')
3 files changed, 184 insertions, 0 deletions
diff --git a/samples/OpenIdOfflineProvider/Controllers/HomeController.cs b/samples/OpenIdOfflineProvider/Controllers/HomeController.cs new file mode 100644 index 0000000..f5a8763 --- /dev/null +++ b/samples/OpenIdOfflineProvider/Controllers/HomeController.cs @@ -0,0 +1,54 @@ +namespace DotNetOpenAuth.OpenIdOfflineProvider.Controllers { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Net.Http; + using System.Text; + using System.Threading.Tasks; + using System.Web.Http; + + using Validation; + + public class HomeController : ApiController { + public HttpResponseMessage Get() { + App.Logger.Info("Discovery on OP Identifier detected."); + var opEndpoint = this.Url.Link("default", new { controller = "provider" }); + var opEndpointUri = new Uri(opEndpoint); + return new HttpResponseMessage() { + Content = new StringContent(GenerateXrdsOPIdentifierDocument(opEndpointUri, Enumerable.Empty<string>()), Encoding.UTF8, "application/xrds+xml"), + }; + } + + /// <summary> + /// Generates the OP Identifier XRDS document. + /// </summary> + /// <param name="providerEndpoint">The provider endpoint.</param> + /// <param name="supportedExtensions">The supported extensions.</param> + /// <returns>The content of the XRDS document.</returns> + private static string GenerateXrdsOPIdentifierDocument(Uri providerEndpoint, IEnumerable<string> supportedExtensions) { + Requires.NotNull(providerEndpoint, "providerEndpoint"); + Requires.NotNull(supportedExtensions, "supportedExtensions"); + + const string OPIdentifierDiscoveryFormat = @"<xrds:XRDS + xmlns:xrds='xri://$xrds' + xmlns:openid='http://openid.net/xmlns/1.0' + xmlns='xri://$xrd*($v*2.0)'> + <XRD> + <Service priority='10'> + <Type>http://specs.openid.net/auth/2.0/server</Type> + {1} + <URI>{0}</URI> + </Service> + </XRD> +</xrds:XRDS>"; + + string extensions = string.Join( + "\n\t\t\t", + supportedExtensions.Select(ext => "<Type>" + ext + "</Type>").ToArray()); + return string.Format( + OPIdentifierDiscoveryFormat, + providerEndpoint.AbsoluteUri, + extensions); + } + } +} diff --git a/samples/OpenIdOfflineProvider/Controllers/ProviderController.cs b/samples/OpenIdOfflineProvider/Controllers/ProviderController.cs new file mode 100644 index 0000000..b0d243b --- /dev/null +++ b/samples/OpenIdOfflineProvider/Controllers/ProviderController.cs @@ -0,0 +1,81 @@ +namespace DotNetOpenAuth.OpenIdOfflineProvider.Controllers { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Net; + using System.Net.Http; + using System.Text; + using System.Threading; + using System.Threading.Tasks; + using System.Web.Http; + + using DotNetOpenAuth.OpenId; + using DotNetOpenAuth.OpenId.Provider; + + public class ProviderController : ApiController { + private static readonly IOpenIdApplicationStore store = new StandardProviderApplicationStore(); + + private MainWindow MainWindow { + get { return MainWindow.Instance; } + } + + public Task<HttpResponseMessage> Get() { + return this.HandleAsync(); + } + + public Task<HttpResponseMessage> Post() { + return this.HandleAsync(); + } + + private async Task<HttpResponseMessage> HandleAsync() { + var provider = new OpenIdProvider(store); + IRequest request = await provider.GetRequestAsync(this.Request); + if (request == null) { + App.Logger.Error("A request came in that did not carry an OpenID message."); + return new HttpResponseMessage(HttpStatusCode.BadRequest) { + Content = new StringContent("<html><body>This is an OpenID Provider endpoint.</body></html>", Encoding.UTF8, "text/html"), + }; + } + + return await await this.MainWindow.Dispatcher.InvokeAsync(async delegate { + if (!request.IsResponseReady) { + var authRequest = request as IAuthenticationRequest; + if (authRequest != null) { + string userIdentityPageBase = this.Url.Link("default", new { controller = "user" }) + "/"; + var userIdentityPageBaseUri = new Uri(userIdentityPageBase); + switch (this.MainWindow.checkidRequestList.SelectedIndex) { + case 0: + if (authRequest.IsDirectedIdentity) { + if (this.MainWindow.capitalizedHostName.IsChecked.Value) { + userIdentityPageBase = (userIdentityPageBaseUri.Scheme + Uri.SchemeDelimiter + userIdentityPageBaseUri.Authority).ToUpperInvariant() + userIdentityPageBaseUri.PathAndQuery; + } + string leafPath = "directedidentity"; + if (this.MainWindow.directedIdentityTrailingPeriodsCheckbox.IsChecked.Value) { + leafPath += "."; + } + authRequest.ClaimedIdentifier = Identifier.Parse(userIdentityPageBase + leafPath, true); + authRequest.LocalIdentifier = authRequest.ClaimedIdentifier; + } + authRequest.IsAuthenticated = true; + break; + case 1: + authRequest.IsAuthenticated = false; + break; + case 2: + IntPtr oldForegroundWindow = NativeMethods.GetForegroundWindow(); + bool stoleFocus = NativeMethods.SetForegroundWindow(this.MainWindow); + await CheckIdWindow.ProcessAuthenticationAsync(userIdentityPageBaseUri, authRequest, CancellationToken.None); + if (stoleFocus) { + NativeMethods.SetForegroundWindow(oldForegroundWindow); + } + break; + } + } + } + + var responseMessage = await provider.PrepareResponseAsync(request); + return responseMessage; + }); + } + } +} diff --git a/samples/OpenIdOfflineProvider/Controllers/UserController.cs b/samples/OpenIdOfflineProvider/Controllers/UserController.cs new file mode 100644 index 0000000..9385b7f --- /dev/null +++ b/samples/OpenIdOfflineProvider/Controllers/UserController.cs @@ -0,0 +1,49 @@ +namespace DotNetOpenAuth.OpenIdOfflineProvider.Controllers { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Net.Http; + using System.Text; + using System.Threading.Tasks; + using System.Web.Http; + + using Validation; + + public class UserController : ApiController { + public HttpResponseMessage Get(string id) { + string localId = null; // string.Format("http://localhost:{0}/user", context.Request.Url.Port); + var opEndpoint = this.Url.Link("default", new { controller = "provider" }); + var opEndpointUri = new Uri(opEndpoint); + return new HttpResponseMessage() { + Content = new StringContent(GenerateHtmlDiscoveryDocument(opEndpointUri, localId), Encoding.UTF8, "text/html"), + }; + } + + /// <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(Uri providerEndpoint, string localId) { + Requires.NotNull(providerEndpoint, "providerEndpoint"); + + 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.AbsoluteUri, + localId); + } + } +} |