1 // OpenTween - Client of Twitter
2 // Copyright (c) 2015 kim_upsilon (@kim_upsilon) <https://upsilo.net/~upsilon/>
3 // All rights reserved.
5 // This file is part of OpenTween.
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)
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
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.
23 using System.Collections.Generic;
24 using System.Diagnostics.CodeAnalysis;
26 using System.Net.Http;
28 using System.Threading;
29 using System.Threading.Tasks;
32 namespace OpenTween.Connection
35 /// HttpClientで使用するOAuthハンドラー
37 public class OAuthHandler : DelegatingHandler
39 public string ConsumerKey { get; }
40 public string ConsumerSecret { get; }
41 public string AccessToken { get; }
42 public string AccessSecret { get; }
44 public OAuthHandler(HttpMessageHandler innerHandler, string consumerKey, string consumerSecret, string accessToken, string accessSecret)
47 this.ConsumerKey = consumerKey;
48 this.ConsumerSecret = consumerSecret;
49 this.AccessToken = accessToken;
50 this.AccessSecret = accessSecret;
53 protected override async Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
55 var query = await GetParameters(request.RequestUri, request.Content)
56 .ConfigureAwait(false);
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);
62 var postContent = request.Content as FormUrlEncodedContent;
63 if (postContent != null)
65 request.Content = new StringContent(MyCommon.BuildQueryString(query), Encoding.UTF8, "application/x-www-form-urlencoded");
66 postContent.Dispose();
69 return await base.SendAsync(request, cancellationToken)
70 .ConfigureAwait(false);
74 /// OAuthの署名の対象となるパラメータを抽出します
76 internal static async Task<IEnumerable<KeyValuePair<string, string>>> GetParameters(Uri requestUri, HttpContent content)
78 var parameters = Enumerable.Empty<KeyValuePair<string, string>>();
80 var postContent = content as FormUrlEncodedContent;
81 if (postContent != null)
83 var query = await postContent.ReadAsStringAsync()
84 .ConfigureAwait(false);
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);
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);