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 {
26 # We keep a record of who's contacted us
28 function lookup_consumer($consumer_key)
30 $con = Consumer::staticGet('consumer_key', $consumer_key);
32 $con = new Consumer();
33 $con->consumer_key = $consumer_key;
34 $con->seed = common_good_rand(16);
35 $con->created = DB_DataObject_Cast::dateTime();
36 if (!$con->insert()) {
40 return new OAuthConsumer($con->consumer_key, '');
43 function lookup_token($consumer, $token_type, $token_key)
46 $t->consumer_key = $consumer->key;
48 $t->type = ($token_type == 'access') ? 1 : 0;
50 return new OAuthToken($t->tok, $t->secret);
56 function lookup_nonce($consumer, $token, $nonce, $timestamp)
59 $n->consumer_key = $consumer->key;
60 $n->tok = $token->key;
65 $n->timestamp = $timestamp;
66 $n->created = DB_DataObject_Cast::dateTime();
72 function new_request_token($consumer)
75 $t->consumer_key = $consumer->key;
76 $t->tok = common_good_rand(16);
77 $t->secret = common_good_rand(16);
78 $t->type = 0; # request
79 $t->state = 0; # unauthorized
80 $t->created = DB_DataObject_Cast::dateTime();
84 return new OAuthToken($t->tok, $t->secret);
88 # defined in OAuthDataStore, but not implemented anywhere
90 function fetch_request_token($consumer)
92 return $this->new_request_token($consumer);
95 function new_access_token($token, $consumer)
97 common_debug('new_access_token("'.$token->key.'","'.$consumer->key.'")', __FILE__);
99 $rt->consumer_key = $consumer->key;
100 $rt->tok = $token->key;
101 $rt->type = 0; # request
102 if ($rt->find(TRUE) && $rt->state == 1) { # authorized
103 common_debug('request token found.', __FILE__);
105 $at->consumer_key = $consumer->key;
106 $at->tok = common_good_rand(16);
107 $at->secret = common_good_rand(16);
108 $at->type = 1; # access
109 $at->created = DB_DataObject_Cast::dateTime();
110 if (!$at->insert()) {
111 $e = $at->_lastError;
112 common_debug('access token "'.$at->tok.'" not inserted: "'.$e->message.'"', __FILE__);
115 common_debug('access token "'.$at->tok.'" inserted', __FILE__);
117 $orig_rt = clone($rt);
118 $rt->state = 2; # used
119 if (!$rt->update($orig_rt)) {
122 common_debug('request token "'.$rt->tok.'" updated', __FILE__);
123 # Update subscription
124 # XXX: mixing levels here
125 $sub = Subscription::staticGet('token', $rt->tok);
129 common_debug('subscription for request token found', __FILE__);
130 $orig_sub = clone($sub);
131 $sub->token = $at->tok;
132 $sub->secret = $at->secret;
133 if (!$sub->update($orig_sub)) {
136 common_debug('subscription updated to use access token', __FILE__);
137 return new OAuthToken($at->tok, $at->secret);
145 # defined in OAuthDataStore, but not implemented anywhere
147 function fetch_access_token($consumer)
149 return $this->new_access_token($consumer);