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($consumer_key)
28 $con = Consumer::staticGet('consumer_key', $consumer_key);
34 return new OAuthConsumer($con->consumer_key,
35 $con->consumer_secret);
38 function getAppByRequestToken($token_key)
40 // Look up the full req tokenx
41 $req_token = $this->lookup_token(null,
45 if (empty($req_token)) {
46 common_debug("couldn't get request token from oauth datastore");
50 // Look up the full Token
52 $token->tok = $req_token->key;
53 $result = $token->find(true);
56 common_debug('Couldn\'t find req token in the token table.');
62 $app = new Oauth_application();
63 $app->consumer_key = $token->consumer_key;
64 $result = $app->find(true);
66 if (!empty($result)) {
69 common_debug("Couldn't find the app!");
74 function new_access_token($token, $consumer, $verifier)
78 "%s - New access token from request token %s, consumer %s and verifier %s ",
88 $rt->consumer_key = $consumer->key;
89 $rt->tok = $token->key;
90 $rt->type = 0; // request
92 $app = Oauth_application::getByConsumerKey($consumer->key);
95 if ($rt->find(true) && $rt->state == 1 && $rt->verifier == $verifier) { // authorized
97 common_debug('request token found.', __FILE__);
99 // find the associated user of the app
101 $appUser = new Oauth_application_user();
103 $appUser->application_id = $app->id;
104 $appUser->token = $rt->tok;
106 $result = $appUser->find(true);
108 if (!empty($result)) {
109 common_debug("Ouath app user found.");
111 common_debug("Oauth app user not found. app id $app->id token $rt->tok");
115 // go ahead and make the access token
118 $at->consumer_key = $consumer->key;
119 $at->tok = common_good_rand(16);
120 $at->secret = common_good_rand(16);
121 $at->type = 1; // access
122 $at->verifier = $verifier;
123 $at->verified_callback = $rt->verified_callback; // 1.0a
124 $at->created = DB_DataObject_Cast::dateTime();
126 if (!$at->insert()) {
127 $e = $at->_lastError;
128 common_debug('access token "'.$at->tok.'" not inserted: "'.$e->message.'"', __FILE__);
131 common_debug('access token "'.$at->tok.'" inserted', __FILE__);
133 $orig_rt = clone($rt);
134 $rt->state = 2; // used
135 if (!$rt->update($orig_rt)) {
138 common_debug('request token "'.$rt->tok.'" updated', __FILE__);
140 // update the token from req to access for the user
142 $orig = clone($appUser);
143 $appUser->token = $at->tok;
145 // It's at this point that we change the access type
146 // to whatever the application's access is. Request
147 // tokens should always have an access type of 0, and
148 // therefore be unuseable for making requests for
149 // protected resources.
151 $appUser->access_type = $app->access_type;
153 $result = $appUser->update($orig);
155 if (empty($result)) {
156 common_debug('couldn\'t update OAuth app user.');
161 return new OAuthToken($at->tok, $at->secret);
169 * Revoke specified access token
171 * Revokes the token specified by $token_key.
172 * Throws exceptions in case of error.
174 * @param string $token_key the token to be revoked
175 * @param int $type type of token (0 = req, 1 = access)
181 public function revoke_token($token_key, $type = 0) {
183 $rt->tok = $token_key;
187 if (!$rt->find(true)) {
188 // TRANS: Exception thrown when an attempt is made to revoke an unknown token.
189 throw new Exception(_('Tried to revoke unknown token.'));
192 if (!$rt->delete()) {
193 // TRANS: Exception thrown when an attempt is made to remove a revoked token.
194 throw new Exception(_('Failed to delete revoked token.'));
199 * Create a new request token. Overrided to support OAuth 1.0a callback
201 * @param OAuthConsumer $consumer the OAuth Consumer for this token
202 * @param string $callback the verified OAuth callback URL
204 * @return OAuthToken $token a new unauthorized OAuth request token
207 function new_request_token($consumer, $callback)
210 $t->consumer_key = $consumer->key;
211 $t->tok = common_good_rand(16);
212 $t->secret = common_good_rand(16);
213 $t->type = 0; // request
214 $t->state = 0; // unauthorized
215 $t->verified_callback = $callback;
217 if ($callback === 'oob') {
219 $t->verifier = mt_rand(0, 9999999);
221 $t->verifier = common_good_rand(8);
224 $t->created = DB_DataObject_Cast::dateTime();
228 return new OAuthToken($t->tok, $t->secret);