]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/apioauthstore.php
* i18n/L10n fixes.
[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     function lookup_consumer($consumerKey)
27     {
28         $con = Consumer::staticGet('consumer_key', $consumerKey);
29
30         if (!$con) {
31
32             // Create an anon consumer and anon application if one
33             // doesn't exist already
34             if ($consumerKey == 'anonymous') {
35                 $con = new Consumer();
36                 $con->consumer_key    = $consumerKey;
37                 $con->consumer_secret = $consumerKey;
38                 $result = $con->insert();
39                 if (!$result) {
40                     // TRANS: Server error displayed when trying to create an anynymous OAuth consumer.
41                     $this->serverError(_('Could not create anonymous consumer.'));
42                 }
43                 $app               = new OAuth_application();
44                 $app->consumer_key = $con->consumer_key;
45                 $app->name         = 'anonymous';
46
47                 // XXX: allow the user to set the access type when
48                 // authorizing? Currently we default to r+w for anonymous
49                 // OAuth client applications
50                 $app->access_type  = 3; // read + write
51                 $id = $app->insert();
52                 if (!$id) {
53                     // TRANS: Server error displayed when trying to create an anynymous OAuth application.
54                     $this->serverError(_('Could not create anonymous OAuth application.'));
55                 }
56             } else {
57                 return null;
58             }
59         }
60
61         return new OAuthConsumer(
62             $con->consumer_key,
63             $con->consumer_secret
64         );
65     }
66
67     function getAppByRequestToken($token_key)
68     {
69         // Look up the full req tokenx
70         $req_token = $this->lookup_token(null,
71                                          'request',
72                                          $token_key);
73
74         if (empty($req_token)) {
75             common_debug("couldn't get request token from oauth datastore");
76             return null;
77         }
78
79         // Look up the full Token
80         $token = new Token();
81         $token->tok = $req_token->key;
82         $result = $token->find(true);
83
84         if (empty($result)) {
85             common_debug('Couldn\'t find req token in the token table.');
86             return null;
87         }
88
89         // Look up the app
90
91         $app = new Oauth_application();
92         $app->consumer_key = $token->consumer_key;
93         $result = $app->find(true);
94
95         if (!empty($result)) {
96             return $app;
97         } else {
98             common_debug("Couldn't find the app!");
99             return null;
100         }
101     }
102
103     function new_access_token($token, $consumer, $verifier)
104     {
105         common_debug(
106             sprintf(
107                 "%s - New access token from request token %s, consumer %s and verifier %s ",
108                 __FILE__,
109                 $token,
110                 $consumer,
111                 $verifier
112             )
113         );
114
115         $rt = new Token();
116
117         $rt->consumer_key = $consumer->key;
118         $rt->tok          = $token->key;
119         $rt->type         = 0; // request
120
121         $app = Oauth_application::getByConsumerKey($consumer->key);
122         assert(!empty($app));
123
124         if ($rt->find(true) && $rt->state == 1 && $rt->verifier == $verifier) { // authorized
125
126             common_debug('request token found.');
127
128             // find the associated user of the app
129
130             $appUser = new Oauth_application_user();
131
132             $appUser->application_id = $app->id;
133             $appUser->token          = $rt->tok;
134
135             $result = $appUser->find(true);
136
137             if (!empty($result)) {
138                 common_debug("Ouath app user found.");
139             } else {
140                 common_debug("Oauth app user not found. app id $app->id token $rt->tok");
141                 return null;
142             }
143
144             // go ahead and make the access token
145
146             $at = new Token();
147             $at->consumer_key      = $consumer->key;
148             $at->tok               = common_good_rand(16);
149             $at->secret            = common_good_rand(16);
150             $at->type              = 1; // access
151             $at->verifier          = $verifier;
152             $at->verified_callback = $rt->verified_callback; // 1.0a
153             $at->created = DB_DataObject_Cast::dateTime();
154
155             if (!$at->insert()) {
156                 $e = $at->_lastError;
157                 common_debug('access token "'.$at->tok.'" not inserted: "'.$e->message.'"', __FILE__);
158                 return null;
159             } else {
160                 common_debug('access token "'.$at->tok.'" inserted', __FILE__);
161                 // burn the old one
162                 $orig_rt = clone($rt);
163                 $rt->state = 2; // used
164                 if (!$rt->update($orig_rt)) {
165                     return null;
166                 }
167                 common_debug('request token "'.$rt->tok.'" updated', __FILE__);
168
169                 // update the token from req to access for the user
170
171                 $orig = clone($appUser);
172
173                 $appUser->token = $at->tok;
174
175                 // It's at this point that we change the access type
176                 // to whatever the application's access is.  Request
177                 // tokens should always have an access type of 0, and
178                 // therefore be unuseable for making requests for
179                 // protected resources.
180
181                 $appUser->access_type = $app->access_type;
182
183                 $result = $appUser->updateKeys($orig);
184
185                 if (!$result) {
186                     throw new Exception('Couldn\'t update OAuth app user.');
187                 }
188
189                 // Okay, good
190                 return new OAuthToken($at->tok, $at->secret);
191             }
192         } else {
193             return null;
194         }
195     }
196
197     /**
198      * Revoke specified access token
199      *
200      * Revokes the token specified by $token_key.
201      * Throws exceptions in case of error.
202      *
203      * @param string $token_key the token to be revoked
204      * @param int    $type      type of token (0 = req, 1 = access)
205      *
206      * @access public
207      *
208      * @return void
209      */
210     public function revoke_token($token_key, $type = 0) {
211         $rt        = new Token();
212         $rt->tok   = $token_key;
213         $rt->type  = $type;
214         $rt->state = 0;
215
216         if (!$rt->find(true)) {
217             // TRANS: Exception thrown when an attempt is made to revoke an unknown token.
218             throw new Exception(_('Tried to revoke unknown token.'));
219         }
220
221         if (!$rt->delete()) {
222             // TRANS: Exception thrown when an attempt is made to remove a revoked token.
223             throw new Exception(_('Failed to delete revoked token.'));
224         }
225     }
226
227     /*
228      * Create a new request token. Overrided to support OAuth 1.0a callback
229      *
230      * @param OAuthConsumer $consumer the OAuth Consumer for this token
231      * @param string        $callback the verified OAuth callback URL
232      *
233      * @return OAuthToken   $token a new unauthorized OAuth request token
234      */
235     function new_request_token($consumer, $callback)
236     {
237         $t = new Token();
238         $t->consumer_key = $consumer->key;
239         $t->tok = common_good_rand(16);
240         $t->secret = common_good_rand(16);
241         $t->type = 0; // request
242         $t->state = 0; // unauthorized
243         $t->verified_callback = $callback;
244
245         if ($callback === 'oob') {
246             // six digit pin
247             $t->verifier = mt_rand(0, 9999999);
248         } else {
249             $t->verifier = common_good_rand(8);
250         }
251
252         $t->created = DB_DataObject_Cast::dateTime();
253         if (!$t->insert()) {
254             return null;
255         } else {
256             return new OAuthToken($t->tok, $t->secret);
257         }
258     }
259 }