OSDN Git Service

HttpConnection.UrlEncodeメソッドをMyCommonクラスに移動
[opentween/open-tween.git] / OpenTween / Connection / HttpConnectionOAuthEcho.cs
1 // OpenTween - Client of Twitter
2 // Copyright (c) 2007-2011 kiri_feather (@kiri_feather) <kiri.feather@gmail.com>
3 //           (c) 2008-2011 Moz (@syo68k)
4 //           (c) 2008-2011 takeshik (@takeshik) <http://www.takeshik.org/>
5 //           (c) 2010-2011 anis774 (@anis774) <http://d.hatena.ne.jp/anis774/>
6 //           (c) 2010-2011 fantasticswallow (@f_swallow) <http://twitter.com/f_swallow>
7 //           (c) 2011      spinor (@tplantd) <http://d.hatena.ne.jp/spinor/>
8 // All rights reserved.
9 // 
10 // This file is part of OpenTween.
11 // 
12 // This program is free software; you can redistribute it and/or modify it
13 // under the terms of the GNU General Public License as published by the Free
14 // Software Foundation; either version 3 of the License, or (at your option)
15 // any later version.
16 // 
17 // This program is distributed in the hope that it will be useful, but
18 // WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
19 // or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
20 // for more details. 
21 // 
22 // You should have received a copy of the GNU General Public License along
23 // with this program. If not, see <http://www.gnu.org/licenses/>, or write to
24 // the Free Software Foundation, Inc., 51 Franklin Street - Fifth Floor,
25 // Boston, MA 02110-1301, USA.
26
27 using HttpConnectionOAuth = OpenTween.HttpConnectionOAuth;
28 using Uri = System.Uri;
29 using HttpWebRequest = System.Net.HttpWebRequest;
30 using System.Collections.Generic; // for Dictionary<TKey, TValue>, KeyValuePair<TKey, TValue>
31 using HttpConnection = OpenTween.HttpConnection;
32 using StringBuilder = System.Text.StringBuilder;
33
34 namespace OpenTween
35 {
36         public class HttpConnectionOAuthEcho : HttpConnectionOAuth
37         {
38                 private Uri _realm;
39
40                 private Uri _serviceProvider;
41
42                 public Uri Realm
43                 {
44                         set { this._realm = value; }
45                 }
46
47                 public Uri ServiceProvider
48                 {
49                         set { this._serviceProvider = value; }
50                 }
51
52                 protected override void AppendOAuthInfo( HttpWebRequest webRequest, Dictionary< string, string > query, string token, string tokenSecret )
53                 {
54                         // OAuth共通情報取得
55                         Dictionary< string, string > parameter = this.GetOAuthParameter( token );
56                         // OAuth共通情報にquery情報を追加
57                         if ( query != null )
58                                 foreach ( KeyValuePair< string, string > item in query )
59                                         parameter.Add( item.Key, item.Value );
60                         // 署名の作成・追加(GETメソッド固定。ServiceProvider呼び出し用の署名作成)
61                         parameter.Add( "oauth_signature", this.CreateSignature( tokenSecret, HttpConnection.GetMethod, this._serviceProvider, parameter ) );
62                         // HTTPリクエストのヘッダに追加
63                         StringBuilder sb = new StringBuilder( "OAuth " );
64                         sb.AppendFormat( "realm=\"{0}://{1}{2}\",", this._realm.Scheme, this._realm.Host, this._realm.AbsolutePath);
65                         foreach ( KeyValuePair< string, string > item in parameter )
66                                 if ( item.Key.StartsWith( "oauth_" ) )
67                                         sb.AppendFormat( "{0}=\"{1}\",", item.Key, MyCommon.UrlEncode( item.Value ) );
68                         webRequest.Headers.Add( "X-Verify-Credentials-Authorization", sb.ToString() );
69                         webRequest.Headers.Add( "X-Auth-Service-Provider", string.Format("{0}://{1}{2}", this._serviceProvider.Scheme, this._serviceProvider.Host, this._serviceProvider.AbsolutePath));
70                 }
71
72                 public HttpConnectionOAuthEcho( Uri realm, Uri serviceProvider )
73                 {
74                         this._realm = realm;
75                         this._serviceProvider = serviceProvider;
76                 }
77         }
78 }