//----------------------------------------------------------------------- // // Copyright (c) Andrew Arnott. All rights reserved. // //----------------------------------------------------------------------- namespace WebFormsRelyingParty.Code { using System; using System.Collections.Generic; using System.Linq; using System.Web; using DotNetOpenAuth.Messaging; using DotNetOpenAuth.OAuth; using DotNetOpenAuth.OAuth.ChannelElements; using DotNetOpenAuth.OAuth.Messages; public class OAuthServiceProvider { private const string PendingAuthorizationRequestSessionKey = "PendingAuthorizationRequest"; /// /// The shared service description for this web site. /// private static ServiceProviderDescription serviceDescription; private static OAuthServiceProviderTokenManager tokenManager; /// /// The shared service provider object. /// private static ServiceProvider serviceProvider; /// /// The lock to synchronize initialization of the field. /// private static object initializerLock = new object(); /// /// Gets the service provider. /// /// The service provider. public static ServiceProvider ServiceProvider { get { EnsureInitialized(); return serviceProvider; } } /// /// Gets the service description. /// /// The service description. public static ServiceProviderDescription ServiceDescription { get { EnsureInitialized(); return serviceDescription; } } public static UserAuthorizationRequest PendingAuthorizationRequest { get { return HttpContext.Current.Session[PendingAuthorizationRequestSessionKey] as UserAuthorizationRequest; } set { HttpContext.Current.Session[PendingAuthorizationRequestSessionKey] = value; } } public static WebFormsRelyingParty.Consumer PendingAuthorizationConsumer { get { ITokenContainingMessage message = PendingAuthorizationRequest; if (message == null) { throw new InvalidOperationException(); } return Global.DataContext.IssuedToken.OfType().First(t => t.Token == message.Token).Consumer; } } public static void AuthorizePendingRequestToken() { var pendingRequest = PendingAuthorizationRequest; if (pendingRequest == null) { throw new InvalidOperationException("No pending authorization request to authorize."); } ITokenContainingMessage msg = pendingRequest; var token = Global.DataContext.IssuedToken.OfType().First(t => t.Token == msg.Token); token.Authorize(); var response = serviceProvider.PrepareAuthorizationResponse(pendingRequest); serviceProvider.Channel.Send(response); PendingAuthorizationRequest = null; } /// /// Initializes the field if it has not yet been initialized. /// private static void EnsureInitialized() { if (serviceProvider == null) { lock (initializerLock) { if (serviceDescription == null) { var postEndpoint = new MessageReceivingEndpoint(new Uri(Utilities.ApplicationRoot, "OAuth.ashx"), HttpDeliveryMethods.PostRequest); var getEndpoint = new MessageReceivingEndpoint(postEndpoint.Location, HttpDeliveryMethods.GetRequest); serviceDescription = new ServiceProviderDescription { TamperProtectionElements = new ITamperProtectionChannelBindingElement[] { new HmacSha1SigningBindingElement() }, RequestTokenEndpoint = postEndpoint, AccessTokenEndpoint = postEndpoint, UserAuthorizationEndpoint = getEndpoint, }; } if (tokenManager == null) { tokenManager = new OAuthServiceProviderTokenManager(); } if (serviceProvider == null) { serviceProvider = new ServiceProvider(serviceDescription, tokenManager); } } } } } }