]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - actions/finishremotesubscribe.php
debugging request for access token
[quix0rs-gnu-social.git] / actions / finishremotesubscribe.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/omb.php');
23 require_once('Auth/Yadis/Yadis.php');
24
25 class FinishremotesubscribeAction extends Action {
26         
27         function handle($args) {
28                 
29                 parent::handle($args);
30
31                 if (common_logged_in()) {
32                         common_user_error(_t('You can use the local subscription!'));
33                     return;
34                 }
35                 
36                 $omb = $_SESSION['oauth_authorization_request'];
37                 
38                 if (!$omb) {
39                         common_user_error(_t('Not expecting this response!'));
40                         return;
41                 }
42
43                 common_debug('stored request: '.print_r($omb,true), __FILE__);
44                 
45                 $req = OAuthRequest::from_request();
46
47                 $token = $req->get_parameter('oauth_token');
48
49                 # I think this is the success metric
50                 
51                 if ($token != $omb['token']) {
52                         common_user_error(_t('Not authorized.'));
53                         return;
54                 }
55                 
56                 $version = $req->get_parameter('omb_version');
57                 
58                 if ($version != OMB_VERSION_01) {
59                         common_user_error(_t('Unknown version of OMB protocol.'));
60                         return;
61                 }
62                 
63                 $nickname = $req->get_parameter('omb_listener_nickname');
64                 
65                 if (!$nickname) {
66                         common_user_error(_t('No nickname provided by remote server.'));
67                         return;
68                 }
69
70                 $profile_url = $req->get_parameter('omb_listener_profile');
71                 
72                 if (!$profile_url) {
73                         common_user_error(_t('No profile URL returned by server.'));
74                         return;
75                 }
76
77                 if (!Validate::uri($profile_url, array('allowed_schemes' => array('http', 'https')))) {
78                         common_user_error(_t('Invalid profile URL returned by server.'));
79                         return;
80                 }
81
82                 common_debug('listenee: "'.$omb['listenee'].'"', __FILE__);
83                 
84                 $user = User::staticGet('nickname', $omb['listenee']);
85                 
86                 if (!$user) {
87                         common_user_error(_t('User being listened to doesn\'t exist.'));
88                         return;
89                 }
90                 
91                 $fullname = $req->get_parameter('omb_listener_fullname');
92                 $homepage = $req->get_parameter('omb_listener_homepage');
93                 $bio = $req->get_parameter('omb_listener_bio');
94                 $location = $req->get_parameter('omb_listener_location');
95                 $avatar_url = $req->get_parameter('omb_listener_avatar');
96
97                 list($newtok, $newsecret) = $this->access_token($omb);
98                 
99                 if (!$newtok || !$newsecret) {
100                         common_user_error(_t('Couldn\'t convert request tokens to access tokens.'));
101                         return;
102                 }
103                 
104                 # XXX: possible attack point; subscribe and return someone else's profile URI
105                 
106                 $remote = Remote_profile::staticGet('uri', $omb['listener']);
107                 
108                 if ($remote) {
109                         $exists = true;
110                         $profile = Profile::staticGet($remote->id);
111                         $orig_remote = clone($remote);
112                         $orig_profile = clone($profile);
113                         # XXX: compare current postNotice and updateProfile URLs to the ones
114                         # stored in the DB to avoid (possibly...) above attack
115                 } else {
116                         $exists = false;
117                         $remote = new Remote_profile();
118                         $remote->uri = $omb['listener'];
119                         $profile = new Profile();
120                 }
121
122                 $profile->nickname = $nickname;
123                 $profile->profileurl = $profile_url;
124                 
125                 if ($fullname) {
126                         $profile->fullname = $fullname;
127                 }
128                 if ($homepage) {
129                         $profile->homepage = $homepage;
130                 }
131                 if ($bio) {
132                         $profile->bio = $bio;
133                 }
134                 if ($location) {
135                         $profile->location = $location;
136                 }
137                 
138                 if ($exists) {
139                         $profile->update($orig_profile);
140                 } else {
141                         $profile->created = DB_DataObject_Cast::dateTime(); # current time
142                         $id = $profile->insert();
143                         $remote->id = $id;
144                 }
145
146                 if ($avatar_url) {
147                         $this->add_avatar($profile, $avatar_url);
148                 }
149
150                 $remote->postnoticeurl = $omb[OMB_ENDPOINT_POSTNOTICE];
151                 $remote->updateprofileurl = $omb[OMB_ENDPOINT_UPDATEPROFILE];
152
153                 if ($exists) {
154                         $remote->update($orig_remote);
155                 } else {
156                         $remote->created = DB_DataObject_Cast::dateTime(); # current time
157                         $remote->insert;
158                 }
159                 
160                 $sub = new Subscription();
161                 $sub->subscriber = $remote->id;
162                 $sub->subscribed = $user->id;
163                 $sub->token = $newtok;
164                 $sub->secret = $newsecret;
165                 $sub->created = DB_DataObject_Cast::dateTime(); # current time
166                 
167                 if (!$sub->insert()) {
168                         common_user_error(_t('Couldn\'t insert new subscription.'));
169                         return;
170                 }
171
172                 # Clear the data
173                 unset($_SESSION['oauth_authorization_request']);
174                 
175                 # If we show subscriptions in reverse chron order, this should
176                 # show up close to the top of the page
177                 
178                 common_redirect(common_local_url('subscribed', array('nickname' =>
179                                                                                                                          $user->nickname)));
180         }
181         
182         function add_avatar($profile, $url) {
183                 $temp_filename = tempnam(sys_get_temp_dir(), 'listener_avatar');
184                 copy($url, $temp_filename);
185                 return $profile->setOriginal($temp_filename);
186         }
187         
188         function access_token($omb) {
189
190                 common_debug('starting request for access token', __FILE__);
191                 
192                 $con = omb_oauth_consumer();
193                 $tok = new OAuthToken($omb['token'], $omb['secret']);
194
195                 common_debug('using request token "'.$tok.'"', __FILE__);
196                 
197                 $url = omb_service_uri($omb[OAUTH_ENDPOINT_ACCESS]);
198
199                 common_debug('using access token url "'.$url.'"', __FILE__);
200                 
201                 # XXX: Is this the right thing to do? Strip off GET params and make them
202                 # POST params? Seems wrong to me.
203                 
204                 $parsed = parse_url($url);
205                 $params = array();
206                 parse_str($parsed['query'], $params);
207
208                 $req = OAuthRequest::from_consumer_and_token($con, $tok, "POST", $url, $params);
209                 
210                 $req->set_parameter('omb_version', OMB_VERSION_01);
211                 
212                 # XXX: test to see if endpoint accepts this signature method
213
214                 $req->sign_request(omb_hmac_sha1(), $con, $tok);
215                 
216                 # We re-use this tool's fetcher, since it's pretty good
217
218                 common_debug('posting to access token url "'.$req->get_normalized_http_url().'"', __FILE__);
219                 common_debug('posting request data "'.$req->to_postdata().'"', __FILE__);
220                 
221                 $fetcher = Auth_Yadis_Yadis::getHTTPFetcher();
222                 $result = $fetcher->post($req->get_normalized_http_url(),
223                                                                  $req->to_postdata());
224
225                 common_debug('got result: "'.print_r($result,TRUE).'"', __FILE__);
226                 
227                 if ($result->status != 200) {
228                         return NULL;
229                 }
230
231                 parse_str($result->body, $return);
232                 
233                 return array($return['oauth_token'], $return['oauth_token_secret']);
234         }
235 }