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