//-----------------------------------------------------------------------
//
// Copyright (c) Microsoft. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.AspNet.Clients {
using System;
using System.Web;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OAuth;
using DotNetOpenAuth.OAuth.ChannelElements;
using DotNetOpenAuth.OAuth.Messages;
///
/// Represents base class for OAuth 1.0 clients
///
public abstract class OAuthClient : IAuthenticationClient {
#region Constructors and Destructors
///
/// Initializes a new instance of the class.
///
///
/// Name of the provider.
///
///
/// The service description.
///
///
/// The consumer key.
///
///
/// The consumer secret.
///
protected OAuthClient(
string providerName, ServiceProviderDescription serviceDescription, string consumerKey, string consumerSecret)
: this(providerName, serviceDescription, new InMemoryOAuthTokenManager(consumerKey, consumerSecret)) {}
///
/// Initializes a new instance of the class.
///
///
/// Name of the provider.
///
///
/// The service Description.
///
///
/// The token Manager.
///
protected OAuthClient(
string providerName, ServiceProviderDescription serviceDescription, IConsumerTokenManager tokenManager)
: this(providerName, new DotNetOpenAuthWebConsumer(serviceDescription, tokenManager)) {}
///
/// Initializes a new instance of the class.
///
///
/// The provider name.
///
///
/// The web worker.
///
///
///
///
///
protected OAuthClient(string providerName, IOAuthWebWorker webWorker) {
Requires.NotNull(providerName, "providerName");
Requires.NotNull(webWorker, "webWorker");
this.ProviderName = providerName;
this.WebWorker = webWorker;
}
#endregion
#region Public Properties
///
/// Gets the name of the provider which provides authentication service.
///
public string ProviderName { get; private set; }
#endregion
#region Properties
///
/// Gets the OAuthWebConsumer instance which handles constructing requests to the OAuth providers.
///
protected IOAuthWebWorker WebWorker { get; private set; }
#endregion
#region Public Methods and Operators
///
/// Attempts to authenticate users by forwarding them to an external website, and upon succcess or failure, redirect users back to the specified url.
///
///
/// The context.
///
///
/// The return url after users have completed authenticating against external website.
///
public virtual void RequestAuthentication(HttpContextBase context, Uri returnUrl) {
Requires.NotNull(returnUrl, "returnUrl");
Requires.NotNull(context, "context");
Uri callback = returnUrl.StripQueryArgumentsWithPrefix("oauth_");
this.WebWorker.RequestAuthentication(callback);
}
///
/// Check if authentication succeeded after user is redirected back from the service provider.
///
///
/// The context.
///
///
/// An instance of containing authentication result.
///
public virtual AuthenticationResult VerifyAuthentication(HttpContextBase context) {
AuthorizedTokenResponse response = this.WebWorker.ProcessUserAuthorization();
if (response == null) {
return AuthenticationResult.Failed;
}
// add the access token to the user data dictionary just in case page developers want to use it
AuthenticationResult result = this.VerifyAuthenticationCore(response);
if (result.IsSuccessful && result.ExtraData != null) {
result.ExtraData["accesstoken"] = response.AccessToken;
}
return result;
}
#endregion
#region Methods
///
/// Check if authentication succeeded after user is redirected back from the service provider.
///
///
/// The response token returned from service provider
///
///
/// Authentication result
///
protected abstract AuthenticationResult VerifyAuthenticationCore(AuthorizedTokenResponse response);
#endregion
}
}