]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/oauthstore.php
define LACONICA and accept LACONICA for backwards compatibility
[quix0rs-gnu-social.git] / lib / oauthstore.php
1 <?php
2 /*
3  * StatusNet - the distributed open-source microblogging tool
4  * Copyright (C) 2008, 2009, StatusNet, 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('STATUSNET') && !defined('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/omb.php');
23
24 class StatusNetOAuthDataStore extends OAuthDataStore
25 {
26
27     // We keep a record of who's contacted us
28
29     function lookup_consumer($consumer_key)
30     {
31         $con = Consumer::staticGet('consumer_key', $consumer_key);
32         if (!$con) {
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()) {
38                 return null;
39             }
40         }
41         return new OAuthConsumer($con->consumer_key, '');
42     }
43
44     function lookup_token($consumer, $token_type, $token_key)
45     {
46         $t = new Token();
47         $t->consumer_key = $consumer->key;
48         $t->tok = $token_key;
49         $t->type = ($token_type == 'access') ? 1 : 0;
50         if ($t->find(true)) {
51             return new OAuthToken($t->tok, $t->secret);
52         } else {
53             return null;
54         }
55     }
56
57     // http://oauth.net/core/1.0/#nonce
58     // "The Consumer SHALL then generate a Nonce value that is unique for
59     // all requests with that timestamp."
60
61     // XXX: It's not clear why the token is here
62
63     function lookup_nonce($consumer, $token, $nonce, $timestamp)
64     {
65         $n = new Nonce();
66         $n->consumer_key = $consumer->key;
67         $n->ts = $timestamp;
68         $n->nonce = $nonce;
69         if ($n->find(true)) {
70             return true;
71         } else {
72             $n->created = DB_DataObject_Cast::dateTime();
73             $n->insert();
74             return false;
75         }
76     }
77
78     function new_request_token($consumer)
79     {
80         $t = new Token();
81         $t->consumer_key = $consumer->key;
82         $t->tok = common_good_rand(16);
83         $t->secret = common_good_rand(16);
84         $t->type = 0; // request
85         $t->state = 0; // unauthorized
86         $t->created = DB_DataObject_Cast::dateTime();
87         if (!$t->insert()) {
88             return null;
89         } else {
90             return new OAuthToken($t->tok, $t->secret);
91         }
92     }
93
94     // defined in OAuthDataStore, but not implemented anywhere
95
96     function fetch_request_token($consumer)
97     {
98         return $this->new_request_token($consumer);
99     }
100
101     function new_access_token($token, $consumer)
102     {
103         common_debug('new_access_token("'.$token->key.'","'.$consumer->key.'")', __FILE__);
104         $rt = new Token();
105         $rt->consumer_key = $consumer->key;
106         $rt->tok = $token->key;
107         $rt->type = 0; // request
108         if ($rt->find(true) && $rt->state == 1) { // authorized
109             common_debug('request token found.', __FILE__);
110             $at = new Token();
111             $at->consumer_key = $consumer->key;
112             $at->tok = common_good_rand(16);
113             $at->secret = common_good_rand(16);
114             $at->type = 1; // access
115             $at->created = DB_DataObject_Cast::dateTime();
116             if (!$at->insert()) {
117                 $e = $at->_lastError;
118                 common_debug('access token "'.$at->tok.'" not inserted: "'.$e->message.'"', __FILE__);
119                 return null;
120             } else {
121                 common_debug('access token "'.$at->tok.'" inserted', __FILE__);
122                 // burn the old one
123                 $orig_rt = clone($rt);
124                 $rt->state = 2; // used
125                 if (!$rt->update($orig_rt)) {
126                     return null;
127                 }
128                 common_debug('request token "'.$rt->tok.'" updated', __FILE__);
129                 // Update subscription
130                 // XXX: mixing levels here
131                 $sub = Subscription::staticGet('token', $rt->tok);
132                 if (!$sub) {
133                     return null;
134                 }
135                 common_debug('subscription for request token found', __FILE__);
136                 $orig_sub = clone($sub);
137                 $sub->token = $at->tok;
138                 $sub->secret = $at->secret;
139                 if (!$sub->update($orig_sub)) {
140                     return null;
141                 } else {
142                     common_debug('subscription updated to use access token', __FILE__);
143                     return new OAuthToken($at->tok, $at->secret);
144                 }
145             }
146         } else {
147             return null;
148         }
149     }
150
151     // defined in OAuthDataStore, but not implemented anywhere
152
153     function fetch_access_token($consumer)
154     {
155         return $this->new_access_token($consumer);
156     }
157 }