//-----------------------------------------------------------------------
//
// Copyright (c) Andrew Arnott. All rights reserved.
//
//-----------------------------------------------------------------------
namespace DotNetOpenAuth.OAuth {
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Web;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OAuth.ChannelElements;
using DotNetOpenAuth.OAuth.Messages;
using DotNetOpenAuth.OpenId.Extensions.OAuth;
using DotNetOpenAuth.OpenId.RelyingParty;
///
/// A website or application that uses OAuth to access the Service Provider on behalf of the User.
///
///
/// The methods on this class are thread-safe. Provided the properties are set and not changed
/// afterward, a single instance of this class may be used by an entire web application safely.
///
public class WebConsumer : ConsumerBase {
///
/// Initializes a new instance of the class.
///
/// The endpoints and behavior of the Service Provider.
/// The host's method of storing and recalling tokens and secrets.
public WebConsumer(ServiceProviderDescription serviceDescription, IConsumerTokenManager tokenManager)
: base(serviceDescription, tokenManager) {
}
///
/// Begins an OAuth authorization request and redirects the user to the Service Provider
/// to provide that authorization. Upon successful authorization, the user is redirected
/// back to the current page.
///
/// The pending user agent redirect based message to be sent as an HttpResponse.
///
/// Requires HttpContext.Current.
///
public UserAuthorizationRequest PrepareRequestUserAuthorization() {
Uri callback = this.Channel.GetRequestFromContext().UrlBeforeRewriting.StripQueryArgumentsWithPrefix(Protocol.ParameterPrefix);
return this.PrepareRequestUserAuthorization(callback, null, null);
}
///
/// Prepares an OAuth message that begins an authorization request that will
/// redirect the user to the Service Provider to provide that authorization.
///
///
/// An optional Consumer URL that the Service Provider should redirect the
/// User Agent to upon successful authorization.
///
/// Extra parameters to add to the request token message. Optional.
/// Extra parameters to add to the redirect to Service Provider message. Optional.
/// The pending user agent redirect based message to be sent as an HttpResponse.
public UserAuthorizationRequest PrepareRequestUserAuthorization(Uri callback, IDictionary requestParameters, IDictionary redirectParameters) {
string token;
return this.PrepareRequestUserAuthorization(callback, requestParameters, redirectParameters, out token);
}
///
/// Processes an incoming authorization-granted message from an SP and obtains an access token.
///
/// The access token, or null if no incoming authorization message was recognized.
///
/// Requires HttpContext.Current.
///
public AuthorizedTokenResponse ProcessUserAuthorization() {
return this.ProcessUserAuthorization(this.Channel.GetRequestFromContext());
}
///
/// Attaches an OAuth authorization request to an outgoing OpenID authentication request.
///
/// The OpenID authentication request.
/// The scope of access that is requested of the service provider.
public void AttachAuthorizationRequest(IAuthenticationRequest openIdAuthenticationRequest, string scope) {
Requires.NotNull(openIdAuthenticationRequest, "openIdAuthenticationRequest");
var authorizationRequest = new AuthorizationRequest {
Consumer = this.ConsumerKey,
Scope = scope,
};
openIdAuthenticationRequest.AddExtension(authorizationRequest);
}
///
/// Processes an incoming authorization-granted message from an SP and obtains an access token.
///
/// The OpenID authentication response that may be carrying an authorized request token.
///
/// The access token, or null if OAuth authorization was denied by the user or service provider.
///
///
/// The access token, if granted, is automatically stored in the .
/// The token manager instance must implement .
///
public AuthorizedTokenResponse ProcessUserAuthorization(IAuthenticationResponse openIdAuthenticationResponse) {
Requires.NotNull(openIdAuthenticationResponse, "openIdAuthenticationResponse");
Requires.ValidState(this.TokenManager is IOpenIdOAuthTokenManager);
var openidTokenManager = this.TokenManager as IOpenIdOAuthTokenManager;
ErrorUtilities.VerifyOperation(openidTokenManager != null, OAuthStrings.OpenIdOAuthExtensionRequiresSpecialTokenManagerInterface, typeof(IOpenIdOAuthTokenManager).FullName);
// The OAuth extension is only expected in positive assertion responses.
if (openIdAuthenticationResponse.Status != AuthenticationStatus.Authenticated) {
return null;
}
// Retrieve the OAuth extension
var positiveAuthorization = openIdAuthenticationResponse.GetExtension();
if (positiveAuthorization == null) {
return null;
}
// Prepare a message to exchange the request token for an access token.
// We are careful to use a v1.0 message version so that the oauth_verifier is not required.
var requestAccess = new AuthorizedTokenRequest(this.ServiceProvider.AccessTokenEndpoint, Protocol.V10.Version) {
RequestToken = positiveAuthorization.RequestToken,
ConsumerKey = this.ConsumerKey,
};
// Retrieve the access token and store it in the token manager.
openidTokenManager.StoreOpenIdAuthorizedRequestToken(this.ConsumerKey, positiveAuthorization);
var grantAccess = this.Channel.Request(requestAccess);
this.TokenManager.ExpireRequestTokenAndStoreNewAccessToken(this.ConsumerKey, positiveAuthorization.RequestToken, grantAccess.AccessToken, grantAccess.TokenSecret);
// Provide the caller with the access token so it may be associated with the user
// that is logging in.
return grantAccess;
}
///
/// Processes an incoming authorization-granted message from an SP and obtains an access token.
///
/// The incoming HTTP request.
/// The access token, or null if no incoming authorization message was recognized.
public AuthorizedTokenResponse ProcessUserAuthorization(HttpRequestInfo request) {
Requires.NotNull(request, "request");
UserAuthorizationResponse authorizationMessage;
if (this.Channel.TryReadFromRequest(request, out authorizationMessage)) {
string requestToken = authorizationMessage.RequestToken;
string verifier = authorizationMessage.VerificationCode;
return this.ProcessUserAuthorization(requestToken, verifier);
} else {
return null;
}
}
}
}