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
26 function lookup_consumer($consumerKey)
28 $con = Consumer::staticGet('consumer_key', $consumerKey);
32 // Create an anon consumer and anon application if one
33 // doesn't exist already
34 if ($consumerKey == 'anonymous') {
35 $con = new Consumer();
36 $con->consumer_key = $consumerKey;
37 $con->consumer_secret = $consumerKey;
38 $result = $con->insert();
40 // TRANS: Server error displayed when trying to create an anynymous OAuth consumer.
41 $this->serverError(_('Could not create anonymous consumer.'));
43 $app = new OAuth_application();
44 $app->consumer_key = $con->consumer_key;
45 $app->name = 'anonymous';
47 // XXX: allow the user to set the access type when
48 // authorizing? Currently we default to r+w for anonymous
49 // OAuth client applications
50 $app->access_type = 3; // read + write
53 // TRANS: Server error displayed when trying to create an anynymous OAuth application.
54 $this->serverError(_('Could not create anonymous OAuth application.'));
61 return new OAuthConsumer(
67 function getAppByRequestToken($token_key)
69 // Look up the full req tokenx
70 $req_token = $this->lookup_token(null,
74 if (empty($req_token)) {
75 common_debug("couldn't get request token from oauth datastore");
79 // Look up the full Token
81 $token->tok = $req_token->key;
82 $result = $token->find(true);
85 common_debug('Couldn\'t find req token in the token table.');
91 $app = new Oauth_application();
92 $app->consumer_key = $token->consumer_key;
93 $result = $app->find(true);
95 if (!empty($result)) {
98 common_debug("Couldn't find the app!");
103 function new_access_token($token, $consumer, $verifier)
107 "%s - New access token from request token %s, consumer %s and verifier %s ",
117 $rt->consumer_key = $consumer->key;
118 $rt->tok = $token->key;
119 $rt->type = 0; // request
121 $app = Oauth_application::getByConsumerKey($consumer->key);
122 assert(!empty($app));
124 if ($rt->find(true) && $rt->state == 1 && $rt->verifier == $verifier) { // authorized
126 common_debug('request token found.');
128 // find the associated user of the app
130 $appUser = new Oauth_application_user();
132 $appUser->application_id = $app->id;
133 $appUser->token = $rt->tok;
135 $result = $appUser->find(true);
137 if (!empty($result)) {
138 common_debug("Ouath app user found.");
140 common_debug("Oauth app user not found. app id $app->id token $rt->tok");
144 // go ahead and make the access token
147 $at->consumer_key = $consumer->key;
148 $at->tok = common_good_rand(16);
149 $at->secret = common_good_rand(16);
150 $at->type = 1; // access
151 $at->verifier = $verifier;
152 $at->verified_callback = $rt->verified_callback; // 1.0a
153 $at->created = DB_DataObject_Cast::dateTime();
155 if (!$at->insert()) {
156 $e = $at->_lastError;
157 common_debug('access token "'.$at->tok.'" not inserted: "'.$e->message.'"', __FILE__);
160 common_debug('access token "'.$at->tok.'" inserted', __FILE__);
162 $orig_rt = clone($rt);
163 $rt->state = 2; // used
164 if (!$rt->update($orig_rt)) {
167 common_debug('request token "'.$rt->tok.'" updated', __FILE__);
169 // update the token from req to access for the user
171 $orig = clone($appUser);
173 $appUser->token = $at->tok;
175 // It's at this point that we change the access type
176 // to whatever the application's access is. Request
177 // tokens should always have an access type of 0, and
178 // therefore be unuseable for making requests for
179 // protected resources.
181 $appUser->access_type = $app->access_type;
183 $result = $appUser->updateKeys($orig);
186 throw new Exception('Couldn\'t update OAuth app user.');
190 return new OAuthToken($at->tok, $at->secret);
198 * Revoke specified access token
200 * Revokes the token specified by $token_key.
201 * Throws exceptions in case of error.
203 * @param string $token_key the token to be revoked
204 * @param int $type type of token (0 = req, 1 = access)
210 public function revoke_token($token_key, $type = 0) {
212 $rt->tok = $token_key;
216 if (!$rt->find(true)) {
217 // TRANS: Exception thrown when an attempt is made to revoke an unknown token.
218 throw new Exception(_('Tried to revoke unknown token.'));
221 if (!$rt->delete()) {
222 // TRANS: Exception thrown when an attempt is made to remove a revoked token.
223 throw new Exception(_('Failed to delete revoked token.'));
228 * Create a new request token. Overrided to support OAuth 1.0a callback
230 * @param OAuthConsumer $consumer the OAuth Consumer for this token
231 * @param string $callback the verified OAuth callback URL
233 * @return OAuthToken $token a new unauthorized OAuth request token
235 function new_request_token($consumer, $callback)
238 $t->consumer_key = $consumer->key;
239 $t->tok = common_good_rand(16);
240 $t->secret = common_good_rand(16);
241 $t->type = 0; // request
242 $t->state = 0; // unauthorized
243 $t->verified_callback = $callback;
245 if ($callback === 'oob') {
247 $t->verifier = mt_rand(0, 9999999);
249 $t->verifier = common_good_rand(8);
252 $t->created = DB_DataObject_Cast::dateTime();
256 return new OAuthToken($t->tok, $t->secret);