]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/apioauthstore.php
Merge branch 'testing' of git@gitorious.org:statusnet/mainline into testing
[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 getAppByRequestToken($token_key)
40     {
41         // Look up the full req tokenx
42
43         $req_token = $this->lookup_token(null,
44                                          'request',
45                                          $token_key);
46
47         if (empty($req_token)) {
48             common_debug("couldn't get request token from oauth datastore");
49             return null;
50         }
51
52         // Look up the full Token
53
54         $token = new Token();
55         $token->tok = $req_token->key;
56         $result = $token->find(true);
57
58         if (empty($result)) {
59             common_debug('Couldn\'t find req token in the token table.');
60             return null;
61         }
62
63         // Look up the app
64
65         $app = new Oauth_application();
66         $app->consumer_key = $token->consumer_key;
67         $result = $app->find(true);
68
69         if (!empty($result)) {
70             return $app;
71         } else {
72             common_debug("Couldn't find the app!");
73             return null;
74         }
75     }
76
77     function new_access_token($token, $consumer)
78     {
79         common_debug('new_access_token("'.$token->key.'","'.$consumer->key.'")', __FILE__);
80
81         $rt = new Token();
82         $rt->consumer_key = $consumer->key;
83         $rt->tok = $token->key;
84         $rt->type = 0; // request
85
86         $app = Oauth_application::getByConsumerKey($consumer->key);
87
88         if (empty($app)) {
89             common_debug("empty app!");
90         }
91
92         if ($rt->find(true) && $rt->state == 1) { // authorized
93             common_debug('request token found.', __FILE__);
94
95             // find the associated user of the app
96
97             $appUser = new Oauth_application_user();
98             $appUser->application_id = $app->id;
99             $appUser->token = $rt->tok;
100             $result = $appUser->find(true);
101
102             if (!empty($result)) {
103                 common_debug("Oath app user found.");
104             } else {
105                 common_debug("Oauth app user not found. app id $app->id token $rt->tok");
106                 return null;
107             }
108
109             // go ahead and make the access token
110
111             $at = new Token();
112             $at->consumer_key = $consumer->key;
113             $at->tok = common_good_rand(16);
114             $at->secret = common_good_rand(16);
115             $at->type = 1; // access
116             $at->created = DB_DataObject_Cast::dateTime();
117
118             if (!$at->insert()) {
119                 $e = $at->_lastError;
120                 common_debug('access token "'.$at->tok.'" not inserted: "'.$e->message.'"', __FILE__);
121                 return null;
122             } else {
123                 common_debug('access token "'.$at->tok.'" inserted', __FILE__);
124                 // burn the old one
125                 $orig_rt = clone($rt);
126                 $rt->state = 2; // used
127                 if (!$rt->update($orig_rt)) {
128                     return null;
129                 }
130                 common_debug('request token "'.$rt->tok.'" updated', __FILE__);
131
132                 // update the token from req to access for the user
133
134                 $orig = clone($appUser);
135                 $appUser->token = $at->tok;
136
137                 // It's at this point that we change the access type
138                 // to whatever the application's access is.  Request
139                 // tokens should always have an access type of 0, and
140                 // therefore be unuseable for making requests for
141                 // protected resources.
142
143                 $appUser->access_type = $app->access_type;
144
145                 $result = $appUser->update($orig);
146
147                 if (empty($result)) {
148                     common_debug('couldn\'t update OAuth app user.');
149                     return null;
150                 }
151
152                 // Okay, good
153
154                 return new OAuthToken($at->tok, $at->secret);
155             }
156
157         } else {
158             return null;
159         }
160     }
161
162 }
163