diff options
Diffstat (limited to 'src')
6 files changed, 58 insertions, 130 deletions
diff --git a/src/DotNetOpenAuth.AspNet/Clients/OAuth/DotNetOpenAuthWebConsumer.cs b/src/DotNetOpenAuth.AspNet/Clients/OAuth/DotNetOpenAuthWebConsumer.cs index d2449af..cc35b76 100644 --- a/src/DotNetOpenAuth.AspNet/Clients/OAuth/DotNetOpenAuthWebConsumer.cs +++ b/src/DotNetOpenAuth.AspNet/Clients/OAuth/DotNetOpenAuthWebConsumer.cs @@ -50,19 +50,23 @@ namespace DotNetOpenAuth.AspNet.Clients { #endregion + /// <summary> + /// Gets the DotNetOpenAuth <see cref="WebConsumer"/> instance that can be used to make OAuth 1.0 authorized HTTP requests. + /// </summary> + public WebConsumer Consumer { + get { return this.webConsumer; } + } + #region Public Methods and Operators /// <summary> - /// The prepare authorized request. + /// Creates an HTTP message handler that authorizes outgoing web requests. /// </summary> - /// <param name="profileEndpoint">The profile endpoint.</param> /// <param name="accessToken">The access token.</param> - /// <param name="cancellationToken">The cancellation token.</param> - /// <returns> - /// An HTTP request. - /// </returns> - public Task<HttpRequestMessage> PrepareAuthorizedRequestAsync(MessageReceivingEndpoint profileEndpoint, string accessToken, CancellationToken cancellationToken = default(CancellationToken)) { - return this.webConsumer.PrepareAuthorizedRequestAsync(profileEndpoint, accessToken, cancellationToken); + public HttpMessageHandler CreateMessageHandler(string accessToken) { + Requires.NotNullOrEmpty(accessToken, "accessToken"); + + return this.Consumer.CreateMessageHandler(accessToken); } /// <summary> diff --git a/src/DotNetOpenAuth.AspNet/Clients/OAuth/IOAuthWebWorker.cs b/src/DotNetOpenAuth.AspNet/Clients/OAuth/IOAuthWebWorker.cs index 205d4c0..7763add 100644 --- a/src/DotNetOpenAuth.AspNet/Clients/OAuth/IOAuthWebWorker.cs +++ b/src/DotNetOpenAuth.AspNet/Clients/OAuth/IOAuthWebWorker.cs @@ -14,21 +14,14 @@ namespace DotNetOpenAuth.AspNet.Clients { using DotNetOpenAuth.OAuth.Messages; /// <summary> - /// The io auth web worker. + /// The interface implemented by all OAuth web authentication modules in this assembly. /// </summary> public interface IOAuthWebWorker { - #region Public Methods and Operators - /// <summary> - /// The prepare authorized request. + /// Creates an HTTP message handler that authorizes outgoing web requests. /// </summary> - /// <param name="profileEndpoint">The profile endpoint.</param> /// <param name="accessToken">The access token.</param> - /// <param name="cancellationToken">The cancellation token.</param> - /// <returns> - /// An HTTP request. - /// </returns> - Task<HttpRequestMessage> PrepareAuthorizedRequestAsync(MessageReceivingEndpoint profileEndpoint, string accessToken, CancellationToken cancellationToken = default(CancellationToken)); + HttpMessageHandler CreateMessageHandler(string accessToken); /// <summary> /// The process user authorization. @@ -46,7 +39,5 @@ namespace DotNetOpenAuth.AspNet.Clients { /// <param name="cancellationToken">The cancellation token.</param> /// <returns>The response message</returns> Task<HttpResponseMessage> RequestAuthenticationAsync(Uri callback, CancellationToken cancellationToken = default(CancellationToken)); - - #endregion } } diff --git a/src/DotNetOpenAuth.AspNet/Clients/OAuth/LinkedInClient.cs b/src/DotNetOpenAuth.AspNet/Clients/OAuth/LinkedInClient.cs index 8128cbb..7aa1dd4 100644 --- a/src/DotNetOpenAuth.AspNet/Clients/OAuth/LinkedInClient.cs +++ b/src/DotNetOpenAuth.AspNet/Clients/OAuth/LinkedInClient.cs @@ -95,12 +95,10 @@ namespace DotNetOpenAuth.AspNet.Clients { string accessToken = response.AccessToken; - var profileEndpoint = new MessageReceivingEndpoint(ProfileRequestUrl, HttpDeliveryMethods.GetRequest); - HttpRequestMessage request = await this.WebWorker.PrepareAuthorizedRequestAsync(profileEndpoint, accessToken, cancellationToken); - + var authorizingHandler = this.WebWorker.CreateMessageHandler(accessToken); try { - using (var httpClient = new HttpClient()) { - using (HttpResponseMessage profileResponse = await httpClient.SendAsync(request, cancellationToken)) { + using (var httpClient = new HttpClient(authorizingHandler)) { + using (HttpResponseMessage profileResponse = await httpClient.GetAsync(ProfileRequestUrl, cancellationToken)) { using (Stream responseStream = await profileResponse.Content.ReadAsStreamAsync()) { XDocument document = LoadXDocumentFromStream(responseStream); string userId = document.Root.Element("id").Value; diff --git a/src/DotNetOpenAuth.AspNet/Clients/OAuth/TwitterClient.cs b/src/DotNetOpenAuth.AspNet/Clients/OAuth/TwitterClient.cs index 3cfa36f..0f5e0db 100644 --- a/src/DotNetOpenAuth.AspNet/Clients/OAuth/TwitterClient.cs +++ b/src/DotNetOpenAuth.AspNet/Clients/OAuth/TwitterClient.cs @@ -96,14 +96,13 @@ namespace DotNetOpenAuth.AspNet.Clients { var profileRequestUrl = new Uri("https://api.twitter.com/1/users/show.xml?user_id=" + MessagingUtilities.EscapeUriDataStringRfc3986(userId)); - var profileEndpoint = new MessageReceivingEndpoint(profileRequestUrl, HttpDeliveryMethods.GetRequest); - HttpRequestMessage request = await this.WebWorker.PrepareAuthorizedRequestAsync(profileEndpoint, accessToken, cancellationToken); + var authorizingHandler = this.WebWorker.CreateMessageHandler(accessToken); var extraData = new Dictionary<string, string>(); extraData.Add("accesstoken", accessToken); try { - using (var httpClient = new HttpClient()) { - using (HttpResponseMessage profileResponse = await httpClient.SendAsync(request)) { + using (var httpClient = new HttpClient(authorizingHandler)) { + using (HttpResponseMessage profileResponse = await httpClient.GetAsync(profileRequestUrl, cancellationToken)) { using (Stream responseStream = await profileResponse.Content.ReadAsStreamAsync()) { XDocument document = LoadXDocumentFromStream(responseStream); extraData.AddDataIfNotEmpty(document, "name"); diff --git a/src/DotNetOpenAuth.OAuth.Consumer/OAuth/ConsumerBase.cs b/src/DotNetOpenAuth.OAuth.Consumer/OAuth/ConsumerBase.cs index d72ad08..1bea2c5 100644 --- a/src/DotNetOpenAuth.OAuth.Consumer/OAuth/ConsumerBase.cs +++ b/src/DotNetOpenAuth.OAuth.Consumer/OAuth/ConsumerBase.cs @@ -111,6 +111,18 @@ namespace DotNetOpenAuth.OAuth { } /// <summary> + /// Creates the HTTP client. + /// </summary> + /// <param name="innerHandler">The inner handler that actually sends the HTTP message on the network.</param> + /// <returns>The HttpClient to use.</returns> + public HttpClient CreateHttpClient(OAuth1HttpMessageHandlerBase innerHandler) { + Requires.NotNull(innerHandler, "innerHandler"); + + var client = this.Channel.HostFactories.CreateHttpClient(innerHandler); + return client; + } + + /// <summary> /// Obtains an access token for a new account at the Service Provider via 2-legged OAuth. /// </summary> /// <param name="requestParameters">Any applicable parameters to include in the query string of the token request.</param> @@ -139,84 +151,6 @@ namespace DotNetOpenAuth.OAuth { return grantAccess.AccessToken; } - /// <summary> - /// Creates a web request prepared with OAuth authorization - /// that may be further tailored by adding parameters by the caller. - /// </summary> - /// <param name="endpoint">The URL and method on the Service Provider to send the request to.</param> - /// <param name="accessToken">The access token that permits access to the protected resource.</param> - /// <param name="cancellationToken">The cancellation token.</param> - /// <returns>The initialized WebRequest object.</returns> - public Task<HttpRequestMessage> PrepareAuthorizedRequestAsync(MessageReceivingEndpoint endpoint, string accessToken, CancellationToken cancellationToken = default(CancellationToken)) { - Requires.NotNull(endpoint, "endpoint"); - Requires.NotNullOrEmpty(accessToken, "accessToken"); - - return this.PrepareAuthorizedRequestAsync(endpoint, accessToken, EmptyDictionary<string, string>.Instance, cancellationToken); - } - - /// <summary> - /// Creates a web request prepared with OAuth authorization - /// that may be further tailored by adding parameters by the caller. - /// </summary> - /// <param name="endpoint">The URL and method on the Service Provider to send the request to.</param> - /// <param name="accessToken">The access token that permits access to the protected resource.</param> - /// <param name="extraData">Extra parameters to include in the message. Must not be null, but may be empty.</param> - /// <param name="cancellationToken">The cancellation token.</param> - /// <returns>The initialized WebRequest object.</returns> - public Task<HttpRequestMessage> PrepareAuthorizedRequestAsync(MessageReceivingEndpoint endpoint, string accessToken, IDictionary<string, string> extraData, CancellationToken cancellationToken = default(CancellationToken)) { - Requires.NotNull(endpoint, "endpoint"); - Requires.NotNullOrEmpty(accessToken, "accessToken"); - Requires.NotNull(extraData, "extraData"); - - IDirectedProtocolMessage message = this.CreateAuthorizingMessage(endpoint, accessToken); - foreach (var pair in extraData) { - message.ExtraData.Add(pair); - } - - return this.OAuthChannel.InitializeRequestAsync(message, cancellationToken); - } - - /// <summary> - /// Prepares an authorized request that carries an HTTP multi-part POST, allowing for binary data. - /// </summary> - /// <param name="endpoint">The URL and method on the Service Provider to send the request to.</param> - /// <param name="accessToken">The access token that permits access to the protected resource.</param> - /// <param name="binaryData">Extra parameters to include in the message. Must not be null, but may be empty.</param> - /// <param name="cancellationToken">The cancellation token.</param> - /// <returns>The initialized WebRequest object.</returns> - public Task<HttpRequestMessage> PrepareAuthorizedRequestAsync(MessageReceivingEndpoint endpoint, string accessToken, IEnumerable<MultipartContentMember> binaryData, CancellationToken cancellationToken = default(CancellationToken)) { - Requires.NotNull(endpoint, "endpoint"); - Requires.NotNullOrEmpty(accessToken, "accessToken"); - Requires.NotNull(binaryData, "binaryData"); - - AccessProtectedResourceRequest message = this.CreateAuthorizingMessage(endpoint, accessToken); - message.BinaryData.AddRange(binaryData); - - return this.OAuthChannel.InitializeRequestAsync(message, cancellationToken); - } - - /// <summary> - /// Prepares an HTTP request that has OAuth authorization already attached to it. - /// </summary> - /// <param name="message">The OAuth authorization message to attach to the HTTP request.</param> - /// <param name="cancellationToken">The cancellation token.</param> - /// <returns> - /// The HttpWebRequest that can be used to send the HTTP request to the remote service provider. - /// </returns> - /// <remarks> - /// If <see cref="IDirectedProtocolMessage.HttpMethods"/> property on the - /// <paramref name="message"/> has the - /// <see cref="HttpDeliveryMethods.AuthorizationHeaderRequest"/> flag set and - /// <see cref="ITamperResistantOAuthMessage.HttpMethod"/> is set to an HTTP method - /// that includes an entity body, the request stream is automatically sent - /// if and only if the <see cref="IMessage.ExtraData"/> dictionary is non-empty. - /// </remarks> - [SuppressMessage("Microsoft.Design", "CA1011:ConsiderPassingBaseTypesAsParameters", Justification = "Type of parameter forces the method to apply only to specific scenario.")] - public Task<HttpRequestMessage> PrepareAuthorizedRequestAsync(AccessProtectedResourceRequest message, CancellationToken cancellationToken = default(CancellationToken)) { - Requires.NotNull(message, "message"); - return this.OAuthChannel.InitializeRequestAsync(message, cancellationToken); - } - #region IDisposable Members /// <summary> diff --git a/src/DotNetOpenAuth.OAuth.Consumer/OAuth/OAuth1HttpMessageHandlerBase.cs b/src/DotNetOpenAuth.OAuth.Consumer/OAuth/OAuth1HttpMessageHandlerBase.cs index 6029d47..8a79adf 100644 --- a/src/DotNetOpenAuth.OAuth.Consumer/OAuth/OAuth1HttpMessageHandlerBase.cs +++ b/src/DotNetOpenAuth.OAuth.Consumer/OAuth/OAuth1HttpMessageHandlerBase.cs @@ -128,6 +128,31 @@ namespace DotNetOpenAuth.OAuth { public int NonceLength { get; set; } /// <summary> + /// Applies OAuth authorization to the specified request. + /// This method is applied automatically to outbound requests that use this message handler instance. + /// However this method may be useful for obtaining the OAuth 1.0 signature without actually sending the request. + /// </summary> + public void ApplyOAuthParameters(HttpRequestMessage request) { + Requires.NotNull(request, "request"); + + var oauthParameters = this.GetOAuthParameters(); + string signature = this.GetSignature(request, oauthParameters); + oauthParameters.Add("oauth_signature", signature); + + // Add parameters and signature to request. + switch (this.Location) { + case OAuthParametersLocation.AuthorizationHttpHeader: + request.Headers.Authorization = new AuthenticationHeaderValue(Protocol.AuthorizationHeaderScheme, MessagingUtilities.AssembleAuthorizationHeader(oauthParameters.AsKeyValuePairs())); + break; + case OAuthParametersLocation.QueryString: + var uriBuilder = new UriBuilder(request.RequestUri); + uriBuilder.AppendQueryArgs(oauthParameters.AsKeyValuePairs()); + request.RequestUri = uriBuilder.Uri; + break; + } + } + + /// <summary> /// Sends an HTTP request to the inner handler to send to the server as an asynchronous operation. /// </summary> /// <param name="request">The HTTP request message to send to the server.</param> @@ -311,29 +336,6 @@ namespace DotNetOpenAuth.OAuth { } /// <summary> - /// Applies OAuth authorization to the specified request. - /// </summary> - private void ApplyOAuthParameters(HttpRequestMessage request) { - Requires.NotNull(request, "request"); - - var oauthParameters = this.GetOAuthParameters(); - string signature = this.GetSignature(request, oauthParameters); - oauthParameters.Add("oauth_signature", signature); - - // Add parameters and signature to request. - switch (this.Location) { - case OAuthParametersLocation.AuthorizationHttpHeader: - request.Headers.Authorization = new AuthenticationHeaderValue(Protocol.AuthorizationHeaderScheme, MessagingUtilities.AssembleAuthorizationHeader(oauthParameters.AsKeyValuePairs())); - break; - case OAuthParametersLocation.QueryString: - var uriBuilder = new UriBuilder(request.RequestUri); - uriBuilder.AppendQueryArgs(oauthParameters.AsKeyValuePairs()); - request.RequestUri = uriBuilder.Uri; - break; - } - } - - /// <summary> /// Gets the OAuth 1.0 signature to apply to the specified request. /// </summary> /// <param name="request">The outbound HTTP request.</param> |