OSDN Git Service

FormUrlEncodedContentを使用したリクエストでoauth_signatureの検証に失敗する問題を回避
[opentween/open-tween.git] / OpenTween / Connection / OAuthHandler.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2015 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
4 //
5 // This file is part of OpenTween.
6 //
7 // This program is free software; you can redistribute it and/or modify it
8 // under the terms of the GNU General Public License as published by the Free
9 // Software Foundation; either version 3 of the License, or (at your option)
10 // any later version.
11 //
12 // This program is distributed in the hope that it will be useful, but
13 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
14 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 // for more details.
16 //
17 // You should have received a copy of the GNU General Public License along
18 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
19 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
20 // Boston, MA 02110-1301, USA.
21
22 using System;
23 using System.Collections.Generic;
24 using System.Diagnostics.CodeAnalysis;
25 using System.Linq;
26 using System.Net.Http;
27 using System.Text;
28 using System.Threading;
29 using System.Threading.Tasks;
30 using System.Web;
31
32 namespace OpenTween.Connection
33 {
34     /// <summary>
35     /// HttpClientで使用するOAuthハンドラー
36     /// </summary>
37     public class OAuthHandler : DelegatingHandler
38     {
39         public string ConsumerKey { get; }
40         public string ConsumerSecret { get; }
41         public string AccessToken { get; }
42         public string AccessSecret { get; }
43
44         public OAuthHandler(HttpMessageHandler innerHandler, string consumerKey, string consumerSecret, string accessToken, string accessSecret)
45             : base(innerHandler)
46         {
47             this.ConsumerKey = consumerKey;
48             this.ConsumerSecret = consumerSecret;
49             this.AccessToken = accessToken;
50             this.AccessSecret = accessSecret;
51         }
52
53         protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
54         {
55             var query = await GetParameters(request.RequestUri, request.Content)
56                 .ConfigureAwait(false);
57
58             var credential = OAuthUtility.CreateAuthorization(request.Method.ToString().ToUpperInvariant(), request.RequestUri, query,
59                 this.ConsumerKey, this.ConsumerSecret, this.AccessToken, this.AccessSecret);
60             request.Headers.TryAddWithoutValidation("Authorization", credential);
61
62             var postContent = request.Content as FormUrlEncodedContent;
63             if (postContent != null)
64             {
65                 request.Content = new StringContent(MyCommon.BuildQueryString(query), Encoding.UTF8, "application/x-www-form-urlencoded");
66                 postContent.Dispose();
67             }
68
69             return await base.SendAsync(request, cancellationToken)
70                 .ConfigureAwait(false);
71         }
72
73         /// <summary>
74         /// OAuthの署名の対象となるパラメータを抽出します
75         /// </summary>
76         internal static async Task<IEnumerable<KeyValuePair<string, string>>> GetParameters(Uri requestUri, HttpContent content)
77         {
78             var parameters = Enumerable.Empty<KeyValuePair<string, string>>();
79
80             var postContent = content as FormUrlEncodedContent;
81             if (postContent != null)
82             {
83                 var query = await postContent.ReadAsStringAsync()
84                     .ConfigureAwait(false);
85
86                 var postParams = HttpUtility.ParseQueryString(query);
87                 var postParamsKvp = postParams.AllKeys.Cast<string>()
88                     .Select(x => new KeyValuePair<string, string>(x, postParams[x]));
89                 parameters = parameters.Concat(postParamsKvp);
90             }
91
92             var getParams = HttpUtility.ParseQueryString(requestUri.Query);
93             var getParamsKvp = getParams.AllKeys.Cast<string>()
94                 .Select(x => new KeyValuePair<string, string>(x, getParams[x]));
95             parameters = parameters.Concat(getParamsKvp);
96
97             return parameters;
98         }
99     }
100 }