diff options
Diffstat (limited to 'src/DotNetOpenAuth.OAuth.Consumer/OAuth')
-rw-r--r-- | src/DotNetOpenAuth.OAuth.Consumer/OAuth/ConsumerBase.cs | 90 | ||||
-rw-r--r-- | src/DotNetOpenAuth.OAuth.Consumer/OAuth/OAuth1HttpMessageHandlerBase.cs | 48 |
2 files changed, 37 insertions, 101 deletions
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> |