summaryrefslogtreecommitdiffstats
path: root/src/DotNetOpenAuth/OAuth/WebConsumer.cs
diff options
context:
space:
mode:
Diffstat (limited to 'src/DotNetOpenAuth/OAuth/WebConsumer.cs')
-rw-r--r--src/DotNetOpenAuth/OAuth/WebConsumer.cs70
1 files changed, 69 insertions, 1 deletions
diff --git a/src/DotNetOpenAuth/OAuth/WebConsumer.cs b/src/DotNetOpenAuth/OAuth/WebConsumer.cs
index 11f3da8..56d3029 100644
--- a/src/DotNetOpenAuth/OAuth/WebConsumer.cs
+++ b/src/DotNetOpenAuth/OAuth/WebConsumer.cs
@@ -7,10 +7,13 @@
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;
/// <summary>
/// A website or application that uses OAuth to access the Service Provider on behalf of the User.
@@ -71,11 +74,76 @@ namespace DotNetOpenAuth.OAuth {
}
/// <summary>
+ /// Attaches an OAuth authorization request to an outgoing OpenID authentication request.
+ /// </summary>
+ /// <param name="openIdAuthenticationRequest">The OpenID authentication request.</param>
+ /// <param name="scope">The scope of access that is requested of the service provider.</param>
+ public void AttachAuthorizationRequest(IAuthenticationRequest openIdAuthenticationRequest, string scope) {
+ Contract.Requires(openIdAuthenticationRequest != null);
+ ErrorUtilities.VerifyArgumentNotNull(openIdAuthenticationRequest, "openIdAuthenticationRequest");
+
+ var authorizationRequest = new AuthorizationRequest {
+ Consumer = this.ConsumerKey,
+ Scope = scope,
+ };
+
+ openIdAuthenticationRequest.AddExtension(authorizationRequest);
+ }
+
+ /// <summary>
+ /// Processes an incoming authorization-granted message from an SP and obtains an access token.
+ /// </summary>
+ /// <param name="openIdAuthenticationResponse">The OpenID authentication response that may be carrying an authorized request token.</param>
+ /// <returns>
+ /// The access token, or null if OAuth authorization was denied by the user or service provider.
+ /// </returns>
+ /// <remarks>
+ /// The access token, if granted, is automatically stored in the <see cref="ConsumerBase.TokenManager"/>.
+ /// The token manager instance must implement <see cref="IOpenIdOAuthTokenManager"/>.
+ /// </remarks>
+ public AuthorizedTokenResponse ProcessUserAuthorization(IAuthenticationResponse openIdAuthenticationResponse) {
+ Contract.Requires(openIdAuthenticationResponse != null);
+ Contract.Requires(this.TokenManager is IOpenIdOAuthTokenManager);
+ ErrorUtilities.VerifyArgumentNotNull(openIdAuthenticationResponse, "openIdAuthenticationResponse");
+ 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<AuthorizationApprovedResponse>();
+ if (positiveAuthorization == null) {
+ return null;
+ }
+
+ // Prepare a message to exchange the request token for an access token.
+ var requestAccess = new AuthorizedTokenRequest(this.ServiceProvider.AccessTokenEndpoint, this.ServiceProvider.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<AuthorizedTokenResponse>(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;
+ }
+
+ /// <summary>
/// Processes an incoming authorization-granted message from an SP and obtains an access token.
/// </summary>
/// <param name="request">The incoming HTTP request.</param>
/// <returns>The access token, or null if no incoming authorization message was recognized.</returns>
- internal AuthorizedTokenResponse ProcessUserAuthorization(HttpRequestInfo request) {
+ public AuthorizedTokenResponse ProcessUserAuthorization(HttpRequestInfo request) {
+ Contract.Requires(request != null);
+ ErrorUtilities.VerifyArgumentNotNull(request, "request");
+
UserAuthorizationResponse authorizationMessage;
if (this.Channel.TryReadFromRequest<UserAuthorizationResponse>(request, out authorizationMessage)) {
string requestToken = authorizationMessage.RequestToken;