OSDN Git Service

Merge branch 'csharp-translation' into master
[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                 private string _token;
43
44                 private string _tokenSecret;
45
46                 public Uri Realm
47                 {
48                         set { this._realm = value; }
49                 }
50
51                 public Uri ServiceProvider
52                 {
53                         set { this._serviceProvider = value; }
54                 }
55
56                 protected override void AppendOAuthInfo( HttpWebRequest webRequest, Dictionary< string, string > query, string token, string tokenSecret )
57                 {
58                         // OAuth共通情報取得
59                         Dictionary< string, string > parameter = this.GetOAuthParameter( token );
60                         // OAuth共通情報にquery情報を追加
61                         if ( query != null )
62                                 foreach ( KeyValuePair< string, string > item in query )
63                                         parameter.Add( item.Key, item.Value );
64                         // 署名の作成・追加(GETメソッド固定。ServiceProvider呼び出し用の署名作成)
65                         parameter.Add( "oauth_signature", this.CreateSignature( tokenSecret, HttpConnection.GetMethod, this._serviceProvider, parameter ) );
66                         // HTTPリクエストのヘッダに追加
67                         StringBuilder sb = new StringBuilder( "OAuth " );
68                         sb.AppendFormat( "realm=\"{0}://{1}{2}\",", this._realm.Scheme, this._realm.Host, this._realm.AbsolutePath);
69                         foreach ( KeyValuePair< string, string > item in parameter )
70                                 if ( item.Key.StartsWith( "oauth_" ) )
71                                         sb.AppendFormat( "{0}=\"{1}\",", item.Key, this.UrlEncode( item.Value ) );
72                         webRequest.Headers.Add( "X-Verify-Credentials-Authorization", sb.ToString() );
73                         webRequest.Headers.Add( "X-Auth-Service-Provider", string.Format("{0}://{1}{2}", this._serviceProvider.Scheme, this._serviceProvider.Host, this._serviceProvider.AbsolutePath));
74                 }
75
76                 public HttpConnectionOAuthEcho( Uri realm, Uri serviceProvider )
77                 {
78                         this._realm = realm;
79                         this._serviceProvider = serviceProvider;
80                 }
81         }
82 }