3 * StatusNet - the distributed open-source microblogging tool
4 * Copyright (C) 2008, 2009, StatusNet, Inc.
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.
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.
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/>.
20 if (!defined('STATUSNET') && !defined('LACONICA')) { exit(1); }
22 require_once INSTALLDIR . '/lib/oauthstore.php';
24 class ApiStatusNetOAuthDataStore extends StatusNetOAuthDataStore
27 function lookup_consumer($consumer_key)
29 $con = Consumer::staticGet('consumer_key', $consumer_key);
35 return new OAuthConsumer($con->consumer_key,
36 $con->consumer_secret);
39 function new_access_token($token, $consumer)
41 common_debug('new_access_token("'.$token->key.'","'.$consumer->key.'")', __FILE__);
43 $rt->consumer_key = $consumer->key;
44 $rt->tok = $token->key;
45 $rt->type = 0; // request
46 if ($rt->find(true) && $rt->state == 1) { // authorized
47 common_debug('request token found.', __FILE__);
49 $at->consumer_key = $consumer->key;
50 $at->tok = common_good_rand(16);
51 $at->secret = common_good_rand(16);
52 $at->type = 1; // access
53 $at->created = DB_DataObject_Cast::dateTime();
56 common_debug('access token "'.$at->tok.'" not inserted: "'.$e->message.'"', __FILE__);
59 common_debug('access token "'.$at->tok.'" inserted', __FILE__);
61 $orig_rt = clone($rt);
62 $rt->state = 2; // used
63 if (!$rt->update($orig_rt)) {
66 common_debug('request token "'.$rt->tok.'" updated', __FILE__);
67 // Update subscription
68 // XXX: mixing levels here
69 $sub = Subscription::staticGet('token', $rt->tok);
73 common_debug('subscription for request token found', __FILE__);
74 $orig_sub = clone($sub);
75 $sub->token = $at->tok;
76 $sub->secret = $at->secret;
77 if (!$sub->update($orig_sub)) {
80 common_debug('subscription updated to use access token', __FILE__);
81 return new OAuthToken($at->tok, $at->secret);