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