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__);
44 $rt->consumer_key = $consumer->key;
45 $rt->tok = $token->key;
46 $rt->type = 0; // request
48 $app = Oauth_application::getByConsumerKey($consumer->key);
51 common_debug("empty app!");
54 if ($rt->find(true) && $rt->state == 1) { // authorized
55 common_debug('request token found.', __FILE__);
57 // find the associated user of the app
59 $appUser = new Oauth_application_user();
60 $appUser->application_id = $app->id;
61 $appUser->token = $rt->tok;
62 $result = $appUser->find(true);
64 if (!empty($result)) {
65 common_debug("Oath app user found.");
67 common_debug("Oauth app user not found.");
71 // go ahead and make the access token
74 $at->consumer_key = $consumer->key;
75 $at->tok = common_good_rand(16);
76 $at->secret = common_good_rand(16);
77 $at->type = 1; // access
78 $at->created = DB_DataObject_Cast::dateTime();
82 common_debug('access token "'.$at->tok.'" not inserted: "'.$e->message.'"', __FILE__);
85 common_debug('access token "'.$at->tok.'" inserted', __FILE__);
87 $orig_rt = clone($rt);
88 $rt->state = 2; // used
89 if (!$rt->update($orig_rt)) {
92 common_debug('request token "'.$rt->tok.'" updated', __FILE__);
94 // update the token from req to access for the user
96 $orig = clone($appUser);
97 $appUser->token = $at->tok;
98 $result = $appUser->update($orig);
100 if (empty($result)) {
101 common_debug('couldn\'t update OAuth app user.');
107 return new OAuthToken($at->tok, $at->secret);