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)
77 'new_access_token("' . $token->key . '","' . $consumer->key. '","' . $verifier . '")',
83 $rt->consumer_key = $consumer->key;
84 $rt->tok = $token->key;
85 $rt->type = 0; // request
87 $app = Oauth_application::getByConsumerKey($consumer->key);
90 if ($rt->find(true) && $rt->state == 1 && $rt->verifier == $verifier) { // authorized
92 common_debug('request token found.', __FILE__);
94 // find the associated user of the app
96 $appUser = new Oauth_application_user();
98 $appUser->application_id = $app->id;
99 $appUser->token = $rt->tok;
101 $result = $appUser->find(true);
103 if (!empty($result)) {
104 common_debug("Ouath app user found.");
106 common_debug("Oauth app user not found. app id $app->id token $rt->tok");
110 // go ahead and make the access token
113 $at->consumer_key = $consumer->key;
114 $at->tok = common_good_rand(16);
115 $at->secret = common_good_rand(16);
116 $at->type = 1; // access
117 $at->verifier = $verifier;
118 $at->verified_callback = $rt->verified_callback; // 1.0a
119 $at->created = DB_DataObject_Cast::dateTime();
121 if (!$at->insert()) {
122 $e = $at->_lastError;
123 common_debug('access token "'.$at->tok.'" not inserted: "'.$e->message.'"', __FILE__);
126 common_debug('access token "'.$at->tok.'" inserted', __FILE__);
128 $orig_rt = clone($rt);
129 $rt->state = 2; // used
130 if (!$rt->update($orig_rt)) {
133 common_debug('request token "'.$rt->tok.'" updated', __FILE__);
135 // update the token from req to access for the user
137 $orig = clone($appUser);
138 $appUser->token = $at->tok;
140 // It's at this point that we change the access type
141 // to whatever the application's access is. Request
142 // tokens should always have an access type of 0, and
143 // therefore be unuseable for making requests for
144 // protected resources.
146 $appUser->access_type = $app->access_type;
148 $result = $appUser->update($orig);
150 if (empty($result)) {
151 common_debug('couldn\'t update OAuth app user.');
156 return new OAuthToken($at->tok, $at->secret);
164 * Revoke specified access token
166 * Revokes the token specified by $token_key.
167 * Throws exceptions in case of error.
169 * @param string $token_key the token to be revoked
170 * @param int $type type of token (0 = req, 1 = access)
176 public function revoke_token($token_key, $type = 0) {
178 $rt->tok = $token_key;
182 if (!$rt->find(true)) {
183 // TRANS: Exception thrown when an attempt is made to revoke an unknown token.
184 throw new Exception(_('Tried to revoke unknown token.'));
187 if (!$rt->delete()) {
188 // TRANS: Exception thrown when an attempt is made to remove a revoked token.
189 throw new Exception(_('Failed to delete revoked token.'));
194 * Create a new request token. Overrided to support OAuth 1.0a callback
196 * @param OAuthConsumer $consumer the OAuth Consumer for this token
197 * @param string $callback the verified OAuth callback URL
199 * @return OAuthToken $token a new unauthorized OAuth request token
202 function new_request_token($consumer, $callback)
205 $t->consumer_key = $consumer->key;
206 $t->tok = common_good_rand(16);
207 $t->secret = common_good_rand(16);
208 $t->type = 0; // request
209 $t->state = 0; // unauthorized
210 $t->verified_callback = $callback;
212 if ($callback === 'oob') {
214 $t->verifier = mt_rand(0, 9999999);
216 $t->verifier = common_good_rand(8);
219 $t->created = DB_DataObject_Cast::dateTime();
223 return new OAuthToken($t->tok, $t->secret);