]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/oauthstore.php
replace all tabs with four spaces
[quix0rs-gnu-social.git] / lib / oauthstore.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, Inc.
5  *
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.
10  *
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.
15  *
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/>.
18  */
19
20 if (!defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/omb.php');
23
24 class LaconicaOAuthDataStore extends OAuthDataStore {
25
26     # We keep a record of who's contacted us
27
28     function lookup_consumer($consumer_key) {
29         $con = Consumer::staticGet('consumer_key', $consumer_key);
30         if (!$con) {
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()) {
36                 return NULL;
37             }
38         }
39         return new OAuthConsumer($con->consumer_key, '');
40     }
41
42     function lookup_token($consumer, $token_type, $token_key) {
43         $t = new Token();
44         $t->consumer_key = $consumer->key;
45         $t->tok = $token_key;
46         $t->type = ($token_type == 'access') ? 1 : 0;
47         if ($t->find(true)) {
48             return new OAuthToken($t->tok, $t->secret);
49         } else {
50             return NULL;
51         }
52     }
53
54     function lookup_nonce($consumer, $token, $nonce, $timestamp) {
55         $n = new Nonce();
56         $n->consumer_key = $consumer->key;
57         $n->tok = $token->key;
58         $n->nonce = $nonce;
59         if ($n->find(TRUE)) {
60             return TRUE;
61         } else {
62             $n->timestamp = $timestamp;
63             $n->created = DB_DataObject_Cast::dateTime();
64             $n->insert();
65             return FALSE;
66         }
67     }
68
69     function new_request_token($consumer) {
70         $t = new Token();
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();
77         if (!$t->insert()) {
78             return NULL;
79         } else {
80             return new OAuthToken($t->tok, $t->secret);
81         }
82     }
83
84     # defined in OAuthDataStore, but not implemented anywhere
85
86     function fetch_request_token($consumer) {
87         return $this->new_request_token($consumer);
88     }
89
90     function new_access_token($token, $consumer) {
91         common_debug('new_access_token("'.$token->key.'","'.$consumer->key.'")', __FILE__);
92         $rt = new Token();
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__);
98             $at = new Token();
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__);
107                 return NULL;
108             } else {
109                 common_debug('access token "'.$at->tok.'" inserted', __FILE__);
110                 # burn the old one
111                 $orig_rt = clone($rt);
112                 $rt->state = 2; # used
113                 if (!$rt->update($orig_rt)) {
114                     return NULL;
115                 }
116                 common_debug('request token "'.$rt->tok.'" updated', __FILE__);
117                 # Update subscription
118                 # XXX: mixing levels here
119                 $sub = Subscription::staticGet('token', $rt->tok);
120                 if (!$sub) {
121                     return NULL;
122                 }
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)) {
128                     return NULL;
129                 } else {
130                     common_debug('subscription updated to use access token', __FILE__);
131                     return new OAuthToken($at->tok, $at->secret);
132                 }
133             }
134         } else {
135             return NULL;
136         }
137     }
138
139     # defined in OAuthDataStore, but not implemented anywhere
140
141     function fetch_access_token($consumer) {
142         return $this->new_access_token($consumer);
143     }
144 }