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) {
29 $con = Consumer::staticGet('consumer_key', $consumer_key);
31 $con = new Consumer();
32 $con->consumer_key = $consumer_key;
33 $con->seed = common_good_rand(16);
34 $con->created = DB_DataObject_Cast::dateTime();
35 if (!$con->insert()) {
39 return new OAuthConsumer($con->consumer_key, '');
42 function lookup_token($consumer, $token_type, $token_key) {
44 $t->consumer_key = $consumer->key;
46 $t->type = ($token_type == 'access') ? 1 : 0;
48 return new OAuthToken($t->tok, $t->secret);
54 function lookup_nonce($consumer, $token, $nonce, $timestamp) {
56 $n->consumer_key = $consumer->key;
57 $n->tok = $token->key;
62 $n->timestamp = $timestamp;
63 $n->created = DB_DataObject_Cast::dateTime();
69 function new_request_token($consumer) {
71 $t->consumer_key = $consumer->key;
72 $t->tok = common_good_rand(16);
73 $t->secret = common_good_rand(16);
74 $t->type = 0; # request
75 $t->state = 0; # unauthorized
76 $t->created = DB_DataObject_Cast::dateTime();
80 return new OAuthToken($t->tok, $t->secret);
84 # defined in OAuthDataStore, but not implemented anywhere
86 function fetch_request_token($consumer) {
87 return $this->new_request_token($consumer);
90 function new_access_token($token, $consumer) {
91 common_debug('new_access_token("'.$token->key.'","'.$consumer->key.'")', __FILE__);
93 $rt->consumer_key = $consumer->key;
94 $rt->tok = $token->key;
95 $rt->type = 0; # request
96 if ($rt->find(TRUE) && $rt->state == 1) { # authorized
97 common_debug('request token found.', __FILE__);
99 $at->consumer_key = $consumer->key;
100 $at->tok = common_good_rand(16);
101 $at->secret = common_good_rand(16);
102 $at->type = 1; # access
103 $at->created = DB_DataObject_Cast::dateTime();
104 if (!$at->insert()) {
105 $e = $at->_lastError;
106 common_debug('access token "'.$at->tok.'" not inserted: "'.$e->message.'"', __FILE__);
109 common_debug('access token "'.$at->tok.'" inserted', __FILE__);
111 $orig_rt = clone($rt);
112 $rt->state = 2; # used
113 if (!$rt->update($orig_rt)) {
116 common_debug('request token "'.$rt->tok.'" updated', __FILE__);
117 # Update subscription
118 # XXX: mixing levels here
119 $sub = Subscription::staticGet('token', $rt->tok);
123 common_debug('subscription for request token found', __FILE__);
124 $orig_sub = clone($sub);
125 $sub->token = $at->tok;
126 $sub->secret = $at->secret;
127 if (!$sub->update($orig_sub)) {
130 common_debug('subscription updated to use access token', __FILE__);
131 return new OAuthToken($at->tok, $at->secret);
139 # defined in OAuthDataStore, but not implemented anywhere
141 function fetch_access_token($consumer) {
142 return $this->new_access_token($consumer);