diff options
Diffstat (limited to 'src/DotNetOpenAuth.OAuth.Consumer/OAuth/OAuth1HttpMessageHandler.cs')
-rw-r--r-- | src/DotNetOpenAuth.OAuth.Consumer/OAuth/OAuth1HttpMessageHandler.cs | 54 |
1 files changed, 54 insertions, 0 deletions
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); + } + } +} |