blob: a763d5eb14b7e0a3bd5393055fb0f9998e1e211a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
|
//-----------------------------------------------------------------------
// <copyright file="OAuth1HttpMessageHandler.cs" company="Andrew Arnott">
// Copyright (c) Andrew Arnott. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
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;
/// <summary>
/// A delegated HTTP handler that automatically signs outgoing requests.
/// </summary>
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;
}
/// <summary>
/// Gets or sets the access token.
/// </summary>
/// <value>
/// The access token.
/// </value>
public string AccessToken { get; set; }
/// <summary>
/// Gets or sets the consumer.
/// </summary>
/// <value>
/// The consumer.
/// </value>
public ConsumerBase Consumer { get; set; }
/// <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>
/// <param name="cancellationToken">A cancellation token to cancel operation.</param>
/// <returns>
/// Returns <see cref="T:System.Threading.Tasks.Task`1" />. The task object representing the asynchronous operation.
/// </returns>
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);
}
}
}
|