]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/apioauthstore.php
OAuth 1.0 working now
[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
43         $rt = new Token();
44         $rt->consumer_key = $consumer->key;
45         $rt->tok = $token->key;
46         $rt->type = 0; // request
47
48         $app = Oauth_application::getByConsumerKey($consumer->key);
49
50         if (empty($app)) {
51             common_debug("empty app!");
52         }
53
54         if ($rt->find(true) && $rt->state == 1) { // authorized
55             common_debug('request token found.', __FILE__);
56
57             // find the associated user of the app
58
59             $appUser = new Oauth_application_user();
60             $appUser->application_id = $app->id;
61             $appUser->token = $rt->tok;
62             $result = $appUser->find(true);
63
64             if (!empty($result)) {
65                 common_debug("Oath app user found.");
66             } else {
67                 common_debug("Oauth app user not found.");
68                 return null;
69             }
70
71             // go ahead and make the access token
72
73             $at = new Token();
74             $at->consumer_key = $consumer->key;
75             $at->tok = common_good_rand(16);
76             $at->secret = common_good_rand(16);
77             $at->type = 1; // access
78             $at->created = DB_DataObject_Cast::dateTime();
79
80             if (!$at->insert()) {
81                 $e = $at->_lastError;
82                 common_debug('access token "'.$at->tok.'" not inserted: "'.$e->message.'"', __FILE__);
83                 return null;
84             } else {
85                 common_debug('access token "'.$at->tok.'" inserted', __FILE__);
86                 // burn the old one
87                 $orig_rt = clone($rt);
88                 $rt->state = 2; // used
89                 if (!$rt->update($orig_rt)) {
90                     return null;
91                 }
92                 common_debug('request token "'.$rt->tok.'" updated', __FILE__);
93
94                 // update the token from req to access for the user
95
96                 $orig = clone($appUser);
97                 $appUser->token = $at->tok;
98
99                 // It's at this point that we change the access type
100                 // to whatever the application's access is.  Request
101                 // tokens should always have an access type of 0, and
102                 // therefore be unuseable for making requests for
103                 // protected resources.
104
105                 $appUser->access_type = $app->access_type;
106
107                 $result = $appUser->update($orig);
108
109                 if (empty($result)) {
110                     common_debug('couldn\'t update OAuth app user.');
111                     return null;
112                 }
113
114                 // Okay, good
115
116                 return new OAuthToken($at->tok, $at->secret);
117             }
118
119         } else {
120             return null;
121         }
122     }
123
124 }
125