diff options
author | Andrew Arnott <andrewarnott@gmail.com> | 2013-02-10 14:34:14 -0800 |
---|---|---|
committer | Andrew Arnott <andrewarnott@gmail.com> | 2013-02-10 14:34:14 -0800 |
commit | 4e85bc002819c123a6e2dd88f18fcc82fa78c38f (patch) | |
tree | 7f58a9f6be09e8e19b64b46a40f8d99c57c810c4 | |
parent | bfb7f04a6c71b54b27763eccf69c3d26c0764272 (diff) | |
download | DotNetOpenAuth-4e85bc002819c123a6e2dd88f18fcc82fa78c38f.zip DotNetOpenAuth-4e85bc002819c123a6e2dd88f18fcc82fa78c38f.tar.gz DotNetOpenAuth-4e85bc002819c123a6e2dd88f18fcc82fa78c38f.tar.bz2 |
Adds a delegating HTTP handler that applies OAuth 1 signatures.
6 files changed, 69 insertions, 3 deletions
diff --git a/samples/DotNetOpenAuth.ApplicationBlock/TwitterConsumer.cs b/samples/DotNetOpenAuth.ApplicationBlock/TwitterConsumer.cs index 0e5387f..0cec2da 100644 --- a/samples/DotNetOpenAuth.ApplicationBlock/TwitterConsumer.cs +++ b/samples/DotNetOpenAuth.ApplicationBlock/TwitterConsumer.cs @@ -135,9 +135,9 @@ namespace DotNetOpenAuth.ApplicationBlock { public static async Task<JArray> GetUpdatesAsync( ConsumerBase twitter, string accessToken, CancellationToken cancellationToken = default(CancellationToken)) { - var request = await twitter.PrepareAuthorizedRequestAsync(GetFriendTimelineStatusEndpoint, accessToken, cancellationToken); - using (var httpClient = twitter.Channel.HostFactories.CreateHttpClient()) { - using (var response = await httpClient.SendAsync(request)) { + var authorizingHandler = new OAuth1HttpMessageHandler(twitter.Channel.HostFactories.CreateHttpMessageHandler(), twitter, accessToken); + using (var httpClient = twitter.Channel.HostFactories.CreateHttpClient(authorizingHandler)) { + using (var response = await httpClient.GetAsync(GetFriendTimelineStatusEndpoint.Location, cancellationToken)) { response.EnsureSuccessStatusCode(); string jsonString = await response.Content.ReadAsStringAsync(); var json = JArray.Parse(jsonString); diff --git a/src/DotNetOpenAuth.OAuth.Consumer/DotNetOpenAuth.OAuth.Consumer.csproj b/src/DotNetOpenAuth.OAuth.Consumer/DotNetOpenAuth.OAuth.Consumer.csproj index faadf13..cda3b68 100644 --- a/src/DotNetOpenAuth.OAuth.Consumer/DotNetOpenAuth.OAuth.Consumer.csproj +++ b/src/DotNetOpenAuth.OAuth.Consumer/DotNetOpenAuth.OAuth.Consumer.csproj @@ -25,6 +25,7 @@ <Compile Include="OAuth\ChannelElements\RsaSha1ConsumerSigningBindingElement.cs" /> <Compile Include="OAuth\ConsumerBase.cs" /> <Compile Include="OAuth\DesktopConsumer.cs" /> + <Compile Include="OAuth\OAuth1HttpMessageHandler.cs" /> <Compile Include="OAuth\WebConsumer.cs" /> <Compile Include="Properties\AssemblyInfo.cs"> <SubType> diff --git a/src/DotNetOpenAuth.OAuth.Consumer/OAuth/ConsumerBase.cs b/src/DotNetOpenAuth.OAuth.Consumer/OAuth/ConsumerBase.cs index 80a1381..b59b438 100644 --- a/src/DotNetOpenAuth.OAuth.Consumer/OAuth/ConsumerBase.cs +++ b/src/DotNetOpenAuth.OAuth.Consumer/OAuth/ConsumerBase.cs @@ -79,6 +79,14 @@ namespace DotNetOpenAuth.OAuth { internal OAuthChannel OAuthChannel { get; set; } /// <summary> + /// Creates a message handler that signs outbound requests with a previously obtained authorization. + /// </summary> + /// <returns>A message handler.</returns> + public OAuth1HttpMessageHandler CreateMessageHandler() { + return new OAuth1HttpMessageHandler(this); + } + + /// <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> diff --git a/src/DotNetOpenAuth.OAuth.Consumer/OAuth/OAuth1HttpMessageHandler.cs b/src/DotNetOpenAuth.OAuth.Consumer/OAuth/OAuth1HttpMessageHandler.cs new file mode 100644 index 0000000..cfbb70e --- /dev/null +++ b/src/DotNetOpenAuth.OAuth.Consumer/OAuth/OAuth1HttpMessageHandler.cs @@ -0,0 +1,54 @@ +namespace DotNetOpenAuth.OAuth { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Net.Http; + using System.Text; + using System.Threading; + using System.Threading.Tasks; + + using DotNetOpenAuth.Messaging; + + using Validation; + + public class OAuth1HttpMessageHandler : DelegatingHandler { + /// <summary> + /// Initializes a new instance of the <see cref="OAuth1HttpMessageHandler" /> class. + /// </summary> + /// <param name="consumer">The consumer.</param> + /// <param name="accessToken">The access token.</param> + public OAuth1HttpMessageHandler(ConsumerBase consumer = null, string accessToken = null) { + this.Consumer = consumer; + this.AccessToken = accessToken; + } + + /// <summary> + /// Initializes a new instance of the <see cref="OAuth1HttpMessageHandler" /> class. + /// </summary> + /// <param name="innerHandler">The inner handler.</param> + /// <param name="consumer">The consumer.</param> + /// <param name="accessToken">The access token.</param> + public OAuth1HttpMessageHandler(HttpMessageHandler innerHandler, ConsumerBase consumer = null, string accessToken = null) + : base(innerHandler) { + this.Consumer = consumer; + this.AccessToken = accessToken; + } + + public string AccessToken { get; set; } + + public ConsumerBase Consumer { get; set; } + + protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { + Verify.Operation(this.Consumer != null, Strings.RequiredPropertyNotYetPreset, "Consumer"); + Verify.Operation(!string.IsNullOrEmpty(this.AccessToken), Strings.RequiredPropertyNotYetPreset, "AccessToken"); + + var deliveryMethods = MessagingUtilities.GetHttpDeliveryMethod(request.Method.Method) | HttpDeliveryMethods.AuthorizationHeaderRequest; + var signed = await + this.Consumer.PrepareAuthorizedRequestAsync( + new MessageReceivingEndpoint(request.RequestUri, deliveryMethods), this.AccessToken, cancellationToken); + request.Headers.Authorization = signed.Headers.Authorization; + + return await base.SendAsync(request, cancellationToken); + } + } +} diff --git a/src/DotNetOpenAuth.OpenIdOAuth/DotNetOpenAuth.OpenIdOAuth.csproj b/src/DotNetOpenAuth.OpenIdOAuth/DotNetOpenAuth.OpenIdOAuth.csproj index cf16a15..6f4c60d 100644 --- a/src/DotNetOpenAuth.OpenIdOAuth/DotNetOpenAuth.OpenIdOAuth.csproj +++ b/src/DotNetOpenAuth.OpenIdOAuth/DotNetOpenAuth.OpenIdOAuth.csproj @@ -54,6 +54,8 @@ </ProjectReference> </ItemGroup> <ItemGroup> + <Reference Include="System.Net.Http" /> + <Reference Include="System.Net.Http.WebRequest" /> <Reference Include="Validation, Version=2.0.0.0, Culture=neutral, PublicKeyToken=2fc06f0d701809a7, processorArchitecture=MSIL"> <SpecificVersion>False</SpecificVersion> <HintPath>..\packages\Validation.2.0.2.13022\lib\portable-windows8+net40+sl5+windowsphone8\Validation.dll</HintPath> diff --git a/src/DotNetOpenAuth.OpenIdOAuth/packages.config b/src/DotNetOpenAuth.OpenIdOAuth/packages.config index e3309bc..d32d62f 100644 --- a/src/DotNetOpenAuth.OpenIdOAuth/packages.config +++ b/src/DotNetOpenAuth.OpenIdOAuth/packages.config @@ -1,4 +1,5 @@ <?xml version="1.0" encoding="utf-8"?> <packages> + <package id="Microsoft.Net.Http" version="2.0.20710.0" targetFramework="net45" /> <package id="Validation" version="2.0.2.13022" targetFramework="net45" /> </packages>
\ No newline at end of file |