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 getAppByRequestToken($token_key)
41 // Look up the full req tokenx
43 $req_token = $this->lookup_token(null,
47 if (empty($req_token)) {
48 common_debug("couldn't get request token from oauth datastore");
52 // Look up the full Token
55 $token->tok = $req_token->key;
56 $result = $token->find(true);
59 common_debug('Couldn\'t find req token in the token table.');
65 $app = new Oauth_application();
66 $app->consumer_key = $token->consumer_key;
67 $result = $app->find(true);
69 if (!empty($result)) {
72 common_debug("Couldn't find the app!");
77 function new_access_token($token, $consumer)
79 common_debug('new_access_token("'.$token->key.'","'.$consumer->key.'")', __FILE__);
82 $rt->consumer_key = $consumer->key;
83 $rt->tok = $token->key;
84 $rt->type = 0; // request
86 $app = Oauth_application::getByConsumerKey($consumer->key);
89 common_debug("empty app!");
92 if ($rt->find(true) && $rt->state == 1) { // authorized
93 common_debug('request token found.', __FILE__);
95 // find the associated user of the app
97 $appUser = new Oauth_application_user();
98 $appUser->application_id = $app->id;
99 $appUser->token = $rt->tok;
100 $result = $appUser->find(true);
102 if (!empty($result)) {
103 common_debug("Oath app user found.");
105 common_debug("Oauth app user not found. app id $app->id token $rt->tok");
109 // go ahead and make the access token
112 $at->consumer_key = $consumer->key;
113 $at->tok = common_good_rand(16);
114 $at->secret = common_good_rand(16);
115 $at->type = 1; // access
116 $at->created = DB_DataObject_Cast::dateTime();
118 if (!$at->insert()) {
119 $e = $at->_lastError;
120 common_debug('access token "'.$at->tok.'" not inserted: "'.$e->message.'"', __FILE__);
123 common_debug('access token "'.$at->tok.'" inserted', __FILE__);
125 $orig_rt = clone($rt);
126 $rt->state = 2; // used
127 if (!$rt->update($orig_rt)) {
130 common_debug('request token "'.$rt->tok.'" updated', __FILE__);
132 // update the token from req to access for the user
134 $orig = clone($appUser);
135 $appUser->token = $at->tok;
137 // It's at this point that we change the access type
138 // to whatever the application's access is. Request
139 // tokens should always have an access type of 0, and
140 // therefore be unuseable for making requests for
141 // protected resources.
143 $appUser->access_type = $app->access_type;
145 $result = $appUser->update($orig);
147 if (empty($result)) {
148 common_debug('couldn\'t update OAuth app user.');
154 return new OAuthToken($at->tok, $at->secret);
163 * Revoke specified access token
165 * Revokes the token specified by $token_key.
166 * Throws exceptions in case of error.
168 * @param string $token_key the token to be revoked
169 * @param int $type type of token (0 = req, 1 = access)
176 public function revoke_token($token_key, $type = 0) {
178 $rt->tok = $token_key;
181 if (!$rt->find(true)) {
182 throw new Exception('Tried to revoke unknown token');
184 if (!$rt->delete()) {
185 throw new Exception('Failed to delete revoked token');