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 // @todo FIXME: Class documentation missing.
25 class ApiStatusNetOAuthDataStore extends StatusNetOAuthDataStore
27 function lookup_consumer($consumerKey)
29 $con = Consumer::staticGet('consumer_key', $consumerKey);
33 // Create an anon consumer and anon application if one
34 // doesn't exist already
35 if ($consumerKey == 'anonymous') {
37 common_debug("API OAuth - creating anonymous consumer");
38 $con = new Consumer();
39 $con->consumer_key = $consumerKey;
40 $con->consumer_secret = $consumerKey;
41 $con->created = common_sql_now();
43 $result = $con->insert();
45 // TRANS: Server error displayed when trying to create an anynymous OAuth consumer.
46 $this->serverError(_('Could not create anonymous consumer.'));
49 $app = Oauth_application::getByConsumerKey('anonymous');
52 common_debug("API OAuth - creating anonymous application");
53 $app = new OAuth_application();
54 $app->owner = 1; // XXX: What to do here?
55 $app->consumer_key = $con->consumer_key;
56 $app->name = 'anonymous';
57 $app->icon = 'default-avatar-stream.png'; // XXX: Fix this!
58 $app->description = "An anonymous application";
59 // XXX: allow the user to set the access type when
60 // authorizing? Currently we default to r+w for anonymous
61 // OAuth client applications
62 $app->access_type = 3; // read + write
63 $app->type = 2; // desktop
64 $app->created = common_sql_now();
69 // TRANS: Server error displayed when trying to create an anynymous OAuth application.
70 $this->serverError(_("Could not create anonymous OAuth application."));
78 return new OAuthConsumer(
84 function getAppByRequestToken($token_key)
86 // Look up the full req token
87 $req_token = $this->lookup_token(
93 if (empty($req_token)) {
94 common_debug("Couldn't get request token from oauth datastore");
98 // Look up the full Token
100 $token->tok = $req_token->key;
101 $result = $token->find(true);
103 if (empty($result)) {
104 common_debug('Couldn\'t find req token in the token table.');
109 $app = new Oauth_application();
110 $app->consumer_key = $token->consumer_key;
111 $result = $app->find(true);
113 if (!empty($result)) {
116 common_debug("Couldn't find the app!");
121 function new_access_token($token, $consumer, $verifier)
125 "New access token from request token %s, consumer %s and verifier %s ",
135 $rt->consumer_key = $consumer->key;
136 $rt->tok = $token->key;
137 $rt->type = 0; // request
139 $app = Oauth_application::getByConsumerKey($consumer->key);
140 assert(!empty($app));
142 if ($rt->find(true) && $rt->state == 1 && $rt->verifier == $verifier) { // authorized
144 common_debug('Request token found.', __FILE__);
146 // find the app and profile associated with this token
147 $tokenAssoc = Oauth_token_association::staticGet('token', $rt->tok);
151 // TRANS: Exception thrown when no token association could be found.
152 _('Could not find a profile and application associated with the request token.')
156 // Check to see if we have previously issued an access token for
157 // this application and profile; if so we can just return the
158 // existing access token. That seems to be the best practice. It
159 // makes it so users only have to authorize the app once per
162 $appUser = new Oauth_application_user();
164 $appUser->application_id = $app->id;
165 $appUser->profile_id = $tokenAssoc->profile_id;
167 $result = $appUser->find(true);
169 if (!empty($result)) {
173 "Existing access token found for application %s, profile %s.",
175 $tokenAssoc->profile_id
181 // Special case: we used to store request tokens in the
182 // Oauth_application_user record, and the access_type would
183 // always be 0 (no access) as a failsafe until an access
184 // token was issued and replaced the request token. There could
185 // be a few old Oauth_application_user records storing request
186 // tokens still around, and we don't want to accidentally
187 // return a useless request token instead of a new access
188 // token. So if we find one, we generate a new access token
189 // and update the existing Oauth_application_user record before
190 // returning the new access token. This should be rare.
192 if ($appUser->access_type == 0) {
194 $at = $this->generateNewAccessToken($consumer, $rt, $verifier);
195 $this->updateAppUser($appUser, $app, $at);
201 // fetch the full access token
202 $at->consumer_key = $consumer->key;
203 $at->tok = $appUser->token;
205 $result = $at->find(true);
209 // TRANS: Exception thrown when no access token can be issued.
210 _('Could not issue access token.')
215 // Yay, we can re-issue the access token
216 return new OAuthToken($at->tok, $at->secret);
222 "Creating new access token for application %s, profile %s.",
224 $tokenAssoc->profile_id
228 $at = $this->generateNewAccessToken($consumer, $rt, $verifier);
229 $this->newAppUser($tokenAssoc, $app, $at);
232 return new OAuthToken($at->tok, $at->secret);
237 // the token was not authorized or not verfied
241 "API OAuth - Attempt to exchange unauthorized or unverified request token %s for an access token.",
250 * Generate a new access token and save it to the database
252 * @param Consumer $consumer the OAuth consumer
253 * @param Token $rt the authorized request token
254 * @param string $verifier the OAuth 1.0a verifier
258 * @return Token $at the new access token
260 private function generateNewAccessToken($consumer, $rt, $verifier)
264 $at->consumer_key = $consumer->key;
265 $at->tok = common_good_rand(16);
266 $at->secret = common_good_rand(16);
267 $at->type = 1; // access
268 $at->verifier = $verifier;
269 $at->verified_callback = $rt->verified_callback; // 1.0a
270 $at->created = common_sql_now();
272 if (!$at->insert()) {
273 $e = $at->_lastError;
274 common_debug('access token "' . $at->tok . '" not inserted: "' . $e->message . '"', __FILE__);
277 common_debug('access token "' . $at->tok . '" inserted', __FILE__);
279 $orig_rt = clone($rt);
280 $rt->state = 2; // used
281 if (!$rt->update($orig_rt)) {
284 common_debug('request token "' . $rt->tok . '" updated', __FILE__);
291 * Add a new app user (Oauth_application_user) record
293 * @param Oauth_token_association $tokenAssoc token-to-app association
294 * @param Oauth_application $app the OAuth client app
295 * @param Token $at the access token
301 private function newAppUser($tokenAssoc, $app, $at)
303 $appUser = new Oauth_application_user();
305 $appUser->profile_id = $tokenAssoc->profile_id;
306 $appUser->application_id = $app->id;
307 $appUser->access_type = $app->access_type;
308 $appUser->token = $at->tok;
309 $appUser->created = common_sql_now();
311 $result = $appUser->insert();
314 common_log_db_error($appUser, 'INSERT', __FILE__);
317 // TRANS: Exception thrown when a database error occurs.
318 _('Database error inserting OAuth application user.')
324 * Update an existing app user (Oauth_application_user) record
326 * @param Oauth_application_user $appUser existing app user rec
327 * @param Oauth_application $app the OAuth client app
328 * @param Token $at the access token
334 private function updateAppUser($appUser, $app, $at)
336 $original = clone($appUser);
337 $appUser->access_type = $app->access_type;
338 $appUser->token = $at->tok;
340 $result = $appUser->update($original);
343 common_log_db_error($appUser, 'UPDATE', __FILE__);
345 // TRANS: Exception thrown when a database error occurs.
346 _('Database error updating OAuth application user.')
352 * Revoke specified access token
354 * Revokes the token specified by $token_key.
355 * Throws exceptions in case of error.
357 * @param string $token_key the token to be revoked
358 * @param int $type type of token (0 = req, 1 = access)
364 public function revoke_token($token_key, $type = 0) {
366 $rt->tok = $token_key;
370 if (!$rt->find(true)) {
371 // TRANS: Exception thrown when an attempt is made to revoke an unknown token.
372 throw new Exception(_('Tried to revoke unknown token.'));
375 if (!$rt->delete()) {
376 // TRANS: Exception thrown when an attempt is made to remove a revoked token.
377 throw new Exception(_('Failed to delete revoked token.'));
382 * Create a new request token. Overrided to support OAuth 1.0a callback
384 * @param OAuthConsumer $consumer the OAuth Consumer for this token
385 * @param string $callback the verified OAuth callback URL
387 * @return OAuthToken $token a new unauthorized OAuth request token
389 function new_request_token($consumer, $callback)
392 $t->consumer_key = $consumer->key;
393 $t->tok = common_good_rand(16);
394 $t->secret = common_good_rand(16);
395 $t->type = 0; // request
396 $t->state = 0; // unauthorized
397 $t->verified_callback = $callback;
399 if ($callback === 'oob') {
401 $t->verifier = mt_rand(0, 9999999);
403 $t->verifier = common_good_rand(8);
406 $t->created = DB_DataObject_Cast::dateTime();
410 return new OAuthToken($t->tok, $t->secret);