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 {
///
/// 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.
protected OAuthClient(string providerName, ServiceProviderDescription serviceDescription, IConsumerTokenManager tokenManager) :
this(providerName, new DotNetOpenAuthWebConsumer(serviceDescription, tokenManager)) {
}
protected OAuthClient(string providerName, IOAuthWebWorker webWorker) {
if (providerName == null) {
throw new ArgumentNullException("providerName");
}
if (webWorker == null) {
throw new ArgumentNullException("webWorker");
}
ProviderName = providerName;
WebWorker = webWorker;
}
///
/// Gets the name of the provider which provides authentication service.
///
public string ProviderName {
get;
private set;
}
///
/// Gets the instance which handles constructing requests
/// to the OAuth providers.
///
protected IOAuthWebWorker WebWorker {
get;
private set;
}
///
/// Attempts to authenticate users by forwarding them to an external website, and
/// upon succcess or failure, redirect users back to the specified url.
///
/// The return url after users have completed authenticating against external website.
public virtual void RequestAuthentication(HttpContextBase context, Uri returnUrl) {
if (returnUrl == null) {
throw new ArgumentNullException("returnUrl");
}
if (context == null) {
throw new ArgumentNullException("context");
}
Uri callback = returnUrl.StripQueryArgumentsWithPrefix("oauth_");
WebWorker.RequestAuthentication(callback);
}
///
/// Check if authentication succeeded after user is redirected back from the service provider.
///
///
/// An instance of containing authentication result.
///
public virtual AuthenticationResult VerifyAuthentication(HttpContextBase context) {
AuthorizedTokenResponse response = WebWorker.ProcessUserAuthorization();
if (response == null) {
return AuthenticationResult.Failed;
}
return VerifyAuthenticationCore(response);
}
///
/// 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);
}
}