]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/apioauthstore.php
Localisation updates from http://translatewiki.net
[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)
75     {
76         common_debug('new_access_token("'.$token->key.'","'.$consumer->key.'")', __FILE__);
77
78         $rt = new Token();
79         $rt->consumer_key = $consumer->key;
80         $rt->tok = $token->key;
81         $rt->type = 0; // request
82
83         $app = Oauth_application::getByConsumerKey($consumer->key);
84
85         if (empty($app)) {
86             common_debug("empty app!");
87         }
88
89         if ($rt->find(true) && $rt->state == 1) { // authorized
90             common_debug('request token found.', __FILE__);
91
92             // find the associated user of the app
93
94             $appUser = new Oauth_application_user();
95             $appUser->application_id = $app->id;
96             $appUser->token = $rt->tok;
97             $result = $appUser->find(true);
98
99             if (!empty($result)) {
100                 common_debug("Oath app user found.");
101             } else {
102                 common_debug("Oauth app user not found. app id $app->id token $rt->tok");
103                 return null;
104             }
105
106             // go ahead and make the access token
107
108             $at = new Token();
109             $at->consumer_key = $consumer->key;
110             $at->tok = common_good_rand(16);
111             $at->secret = common_good_rand(16);
112             $at->type = 1; // access
113             $at->created = DB_DataObject_Cast::dateTime();
114
115             if (!$at->insert()) {
116                 $e = $at->_lastError;
117                 common_debug('access token "'.$at->tok.'" not inserted: "'.$e->message.'"', __FILE__);
118                 return null;
119             } else {
120                 common_debug('access token "'.$at->tok.'" inserted', __FILE__);
121                 // burn the old one
122                 $orig_rt = clone($rt);
123                 $rt->state = 2; // used
124                 if (!$rt->update($orig_rt)) {
125                     return null;
126                 }
127                 common_debug('request token "'.$rt->tok.'" updated', __FILE__);
128
129                 // update the token from req to access for the user
130
131                 $orig = clone($appUser);
132                 $appUser->token = $at->tok;
133
134                 // It's at this point that we change the access type
135                 // to whatever the application's access is.  Request
136                 // tokens should always have an access type of 0, and
137                 // therefore be unuseable for making requests for
138                 // protected resources.
139
140                 $appUser->access_type = $app->access_type;
141
142                 $result = $appUser->update($orig);
143
144                 if (empty($result)) {
145                     common_debug('couldn\'t update OAuth app user.');
146                     return null;
147                 }
148
149                 // Okay, good
150                 return new OAuthToken($at->tok, $at->secret);
151             }
152         } else {
153             return null;
154         }
155     }
156
157     /**
158      * Revoke specified access token
159      *
160      * Revokes the token specified by $token_key.
161      * Throws exceptions in case of error.
162      *
163      * @param string $token_key the token to be revoked
164      * @param int    $type      type of token (0 = req, 1 = access)
165      *
166      * @access public
167      *
168      * @return void
169      */
170     public function revoke_token($token_key, $type = 0) {
171         $rt = new Token();
172         $rt->tok = $token_key;
173         $rt->type = $type;
174         $rt->state = 0;
175
176         if (!$rt->find(true)) {
177             // TRANS: Exception thrown when an attempt is made to revoke an unknown token.
178             throw new Exception(_('Tried to revoke unknown token.'));
179         }
180
181         if (!$rt->delete()) {
182             // TRANS: Exception thrown when an attempt is made to remove a revoked token.
183             throw new Exception(_('Failed to delete revoked token.'));
184         }
185     }
186 }