3 * Laconica - a distributed open-source microblogging tool
4 * Copyright (C) 2008, Controlez-Vous, 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('LACONICA')) { exit(1); }
22 require_once(INSTALLDIR.'/lib/omb.php');
24 class LaconicaOAuthDataStore extends OAuthDataStore
27 // We keep a record of who's contacted us
29 function lookup_consumer($consumer_key)
31 $con = Consumer::staticGet('consumer_key', $consumer_key);
33 $con = new Consumer();
34 $con->consumer_key = $consumer_key;
35 $con->seed = common_good_rand(16);
36 $con->created = DB_DataObject_Cast::dateTime();
37 if (!$con->insert()) {
41 return new OAuthConsumer($con->consumer_key, '');
44 function lookup_token($consumer, $token_type, $token_key)
47 $t->consumer_key = $consumer->key;
49 $t->type = ($token_type == 'access') ? 1 : 0;
51 return new OAuthToken($t->tok, $t->secret);
57 function lookup_nonce($consumer, $token, $nonce, $timestamp)
60 $n->consumer_key = $consumer->key;
61 $n->tok = $token->key;
66 $n->timestamp = $timestamp;
67 $n->created = DB_DataObject_Cast::dateTime();
73 function new_request_token($consumer)
76 $t->consumer_key = $consumer->key;
77 $t->tok = common_good_rand(16);
78 $t->secret = common_good_rand(16);
79 $t->type = 0; // request
80 $t->state = 0; // unauthorized
81 $t->created = DB_DataObject_Cast::dateTime();
85 return new OAuthToken($t->tok, $t->secret);
89 // defined in OAuthDataStore, but not implemented anywhere
91 function fetch_request_token($consumer)
93 return $this->new_request_token($consumer);
96 function new_access_token($token, $consumer)
98 common_debug('new_access_token("'.$token->key.'","'.$consumer->key.'")', __FILE__);
100 $rt->consumer_key = $consumer->key;
101 $rt->tok = $token->key;
102 $rt->type = 0; // request
103 if ($rt->find(true) && $rt->state == 1) { // authorized
104 common_debug('request token found.', __FILE__);
106 $at->consumer_key = $consumer->key;
107 $at->tok = common_good_rand(16);
108 $at->secret = common_good_rand(16);
109 $at->type = 1; // access
110 $at->created = DB_DataObject_Cast::dateTime();
111 if (!$at->insert()) {
112 $e = $at->_lastError;
113 common_debug('access token "'.$at->tok.'" not inserted: "'.$e->message.'"', __FILE__);
116 common_debug('access token "'.$at->tok.'" inserted', __FILE__);
118 $orig_rt = clone($rt);
119 $rt->state = 2; // used
120 if (!$rt->update($orig_rt)) {
123 common_debug('request token "'.$rt->tok.'" updated', __FILE__);
124 // Update subscription
125 // XXX: mixing levels here
126 $sub = Subscription::staticGet('token', $rt->tok);
130 common_debug('subscription for request token found', __FILE__);
131 $orig_sub = clone($sub);
132 $sub->token = $at->tok;
133 $sub->secret = $at->secret;
134 if (!$sub->update($orig_sub)) {
137 common_debug('subscription updated to use access token', __FILE__);
138 return new OAuthToken($at->tok, $at->secret);
146 // defined in OAuthDataStore, but not implemented anywhere
148 function fetch_access_token($consumer)
150 return $this->new_access_token($consumer);