]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/apioauthstore.php
a92a4d6e49785ae83ee7ea4774f1f6eca01af161
[quix0rs-gnu-social.git] / lib / apioauthstore.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/oauthstore.php';
23
24 class ApiStatusNetOAuthDataStore extends StatusNetOAuthDataStore
25 {
26
27     function lookup_consumer($consumer_key)
28     {
29         $con = Consumer::staticGet('consumer_key', $consumer_key);
30
31         if (!$con) {
32             return null;
33         }
34
35         return new OAuthConsumer($con->consumer_key,
36                                  $con->consumer_secret);
37     }
38
39     function new_access_token($token, $consumer)
40     {
41         common_debug('new_access_token("'.$token->key.'","'.$consumer->key.'")', __FILE__);
42         $rt = new Token();
43         $rt->consumer_key = $consumer->key;
44         $rt->tok = $token->key;
45         $rt->type = 0; // request
46         if ($rt->find(true) && $rt->state == 1) { // authorized
47             common_debug('request token found.', __FILE__);
48             $at = new Token();
49             $at->consumer_key = $consumer->key;
50             $at->tok = common_good_rand(16);
51             $at->secret = common_good_rand(16);
52             $at->type = 1; // access
53             $at->created = DB_DataObject_Cast::dateTime();
54             if (!$at->insert()) {
55                 $e = $at->_lastError;
56                 common_debug('access token "'.$at->tok.'" not inserted: "'.$e->message.'"', __FILE__);
57                 return null;
58             } else {
59                 common_debug('access token "'.$at->tok.'" inserted', __FILE__);
60                 // burn the old one
61                 $orig_rt = clone($rt);
62                 $rt->state = 2; // used
63                 if (!$rt->update($orig_rt)) {
64                     return null;
65                 }
66                 common_debug('request token "'.$rt->tok.'" updated', __FILE__);
67                 // Update subscription
68                 // XXX: mixing levels here
69                 $sub = Subscription::staticGet('token', $rt->tok);
70                 if (!$sub) {
71                     return null;
72                 }
73                 common_debug('subscription for request token found', __FILE__);
74                 $orig_sub = clone($sub);
75                 $sub->token = $at->tok;
76                 $sub->secret = $at->secret;
77                 if (!$sub->update($orig_sub)) {
78                     return null;
79                 } else {
80                     common_debug('subscription updated to use access token', __FILE__);
81                     return new OAuthToken($at->tok, $at->secret);
82                 }
83             }
84         } else {
85             return null;
86         }
87     }
88
89 }
90