diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2013-03-26 11:19:06 -0700 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2013-03-26 11:19:06 -0700 |
commit | 3d37ff45cab6838d80b22e6b782a0b9b4c2f4aeb (patch) | |
tree | c15816c3d7f6e74334553f2ff98605ce1c22c538 /samples/OAuthResourceServer/Code | |
parent | 5e9014f36b2d53b8e419918675df636540ea24e2 (diff) | |
parent | e6f7409f4caceb7bc2a5b4ddbcb1a4097af340f2 (diff) | |
download | DotNetOpenAuth-3d37ff45cab6838d80b22e6b782a0b9b4c2f4aeb.zip DotNetOpenAuth-3d37ff45cab6838d80b22e6b782a0b9b4c2f4aeb.tar.gz DotNetOpenAuth-3d37ff45cab6838d80b22e6b782a0b9b4c2f4aeb.tar.bz2 |
Move to HttpClient throughout library.
Diffstat (limited to 'samples/OAuthResourceServer/Code')
-rw-r--r-- | samples/OAuthResourceServer/Code/OAuthAuthorizationManager.cs | 85 |
1 files changed, 53 insertions, 32 deletions
diff --git a/samples/OAuthResourceServer/Code/OAuthAuthorizationManager.cs b/samples/OAuthResourceServer/Code/OAuthAuthorizationManager.cs index 31371db..091c9bb 100644 --- a/samples/OAuthResourceServer/Code/OAuthAuthorizationManager.cs +++ b/samples/OAuthResourceServer/Code/OAuthAuthorizationManager.cs @@ -3,11 +3,14 @@ using System.Collections.Generic; using System.IdentityModel.Policy; using System.Linq; + using System.Net.Http; using System.Security.Principal; using System.ServiceModel; using System.ServiceModel.Channels; using System.ServiceModel.Security; using System.ServiceModel.Web; + using System.Threading; + using System.Threading.Tasks; using DotNetOpenAuth.Messaging; using DotNetOpenAuth.OAuth2; using ProtocolException = System.ServiceModel.ProtocolException; @@ -27,53 +30,71 @@ var httpDetails = operationContext.RequestContext.RequestMessage.Properties[HttpRequestMessageProperty.Name] as HttpRequestMessageProperty; var requestUri = operationContext.RequestContext.RequestMessage.Properties.Via; - try { - var principal = VerifyOAuth2(httpDetails, requestUri, operationContext.IncomingMessageHeaders.Action ?? operationContext.IncomingMessageHeaders.To.AbsolutePath); - if (principal != null) { - var policy = new OAuthPrincipalAuthorizationPolicy(principal); - var policies = new List<IAuthorizationPolicy> { - policy, - }; + return Task.Run(async delegate { + ProtocolFaultResponseException exception = null; + try { + var principal = await VerifyOAuth2Async( + httpDetails, + requestUri, + operationContext.IncomingMessageHeaders.Action ?? operationContext.IncomingMessageHeaders.To.AbsolutePath); + if (principal != null) { + var policy = new OAuthPrincipalAuthorizationPolicy(principal); + var policies = new List<IAuthorizationPolicy> { policy, }; - var securityContext = new ServiceSecurityContext(policies.AsReadOnly()); - if (operationContext.IncomingMessageProperties.Security != null) { - operationContext.IncomingMessageProperties.Security.ServiceSecurityContext = securityContext; - } else { - operationContext.IncomingMessageProperties.Security = new SecurityMessageProperty { - ServiceSecurityContext = securityContext, - }; - } + var securityContext = new ServiceSecurityContext(policies.AsReadOnly()); + if (operationContext.IncomingMessageProperties.Security != null) { + operationContext.IncomingMessageProperties.Security.ServiceSecurityContext = securityContext; + } else { + operationContext.IncomingMessageProperties.Security = new SecurityMessageProperty { + ServiceSecurityContext = securityContext, + }; + } - securityContext.AuthorizationContext.Properties["Identities"] = new List<IIdentity> { - principal.Identity, - }; + securityContext.AuthorizationContext.Properties["Identities"] = new List<IIdentity> { principal.Identity, }; - return true; - } else { - return false; + return true; + } else { + return false; + } + } catch (ProtocolFaultResponseException ex) { + Global.Logger.Error("Error processing OAuth messages.", ex); + exception = ex; + } catch (ProtocolException ex) { + Global.Logger.Error("Error processing OAuth messages.", ex); } - } catch (ProtocolFaultResponseException ex) { - Global.Logger.Error("Error processing OAuth messages.", ex); - // Return the appropriate unauthorized response to the client. - var outgoingResponse = ex.CreateErrorResponse(); - outgoingResponse.Respond(WebOperationContext.Current.OutgoingResponse); - } catch (ProtocolException ex) { - Global.Logger.Error("Error processing OAuth messages.", ex); - } + if (exception != null) { + // Return the appropriate unauthorized response to the client. + var outgoingResponse = await exception.CreateErrorResponseAsync(CancellationToken.None); + this.Respond(WebOperationContext.Current.OutgoingResponse, outgoingResponse); + } - return false; + return false; + }).GetAwaiter().GetResult(); } - private static IPrincipal VerifyOAuth2(HttpRequestMessageProperty httpDetails, Uri requestUri, params string[] requiredScopes) { + private static async Task<IPrincipal> VerifyOAuth2Async(HttpRequestMessageProperty httpDetails, Uri requestUri, params string[] requiredScopes) { // for this sample where the auth server and resource server are the same site, // we use the same public/private key. using (var signing = Global.CreateAuthorizationServerSigningServiceProvider()) { using (var encrypting = Global.CreateResourceServerEncryptionServiceProvider()) { var resourceServer = new ResourceServer(new StandardAccessTokenAnalyzer(signing, encrypting)); - return resourceServer.GetPrincipal(httpDetails, requestUri, requiredScopes); + return await resourceServer.GetPrincipalAsync(httpDetails, requestUri, requiredScopes: requiredScopes); } } } + + /// <summary> + /// Submits this response to a WCF response context. Only available when no response body is included. + /// </summary> + /// <param name="responseContext">The response context to apply the response to.</param> + /// <param name="responseMessage">The response message.</param> + private void Respond(OutgoingWebResponseContext responseContext, HttpResponseMessage responseMessage) { + responseContext.StatusCode = responseMessage.StatusCode; + responseContext.SuppressEntityBody = true; + foreach (var header in responseMessage.Headers) { + responseContext.Headers[header.Key] = header.Value.First(); + } + } } } |