summaryrefslogtreecommitdiffstats
path: root/samples/OAuthResourceServer/Code/OAuthAuthorizationManager.cs
diff options
context:
space:
mode:
authorAndrew Arnott <andrewarnott@gmail.com>2013-01-23 21:50:40 -0800
committerAndrew Arnott <andrewarnott@gmail.com>2013-01-23 21:50:40 -0800
commit98976665f90b20e4757e932ce3a33268f7e1daa6 (patch)
treeeeefb570e357a608dbe8a20ab1a033233cc65a7a /samples/OAuthResourceServer/Code/OAuthAuthorizationManager.cs
parentb16591e45dd70602b1899d760200e72fd6d5eaec (diff)
downloadDotNetOpenAuth-98976665f90b20e4757e932ce3a33268f7e1daa6.zip
DotNetOpenAuth-98976665f90b20e4757e932ce3a33268f7e1daa6.tar.gz
DotNetOpenAuth-98976665f90b20e4757e932ce3a33268f7e1daa6.tar.bz2
Fixed a bunch more samples.
Diffstat (limited to 'samples/OAuthResourceServer/Code/OAuthAuthorizationManager.cs')
-rw-r--r--samples/OAuthResourceServer/Code/OAuthAuthorizationManager.cs84
1 files changed, 52 insertions, 32 deletions
diff --git a/samples/OAuthResourceServer/Code/OAuthAuthorizationManager.cs b/samples/OAuthResourceServer/Code/OAuthAuthorizationManager.cs
index 31371db..0038b2e 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,70 @@
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>
+ 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();
+ }
+ }
}
}