]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - lib/oauthstore.php
fix consumer fetching, nonce making
[quix0rs-gnu-social.git] / lib / oauthstore.php
1 <?php
2 /*
3  * Laconica - a distributed open-source microblogging tool
4  * Copyright (C) 2008, Controlez-Vous, 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('LACONICA')) { exit(1); }
21
22 require_once(INSTALLDIR.'/lib/omb.php');
23
24 class LaconicaOAuthDataStore extends OAuthDataStore {
25
26         # We keep a record of who's contacted us
27         
28         function lookup_consumer($consumer_key) {
29                 $con = Consumer::staticGet('consumer_key', $consumer_key);
30                 if (!$con) {
31                         $con = new Consumer();
32                         $con->consumer_key = $consumer_key;
33                         $con->seed = common_good_rand(16);
34                         $con->created = DB_DataObject_Cast::dateTime();
35                         if (!$con->insert()) {
36                                 return NULL;
37                         }
38                 }
39                 return new OAuthConsumer($con->consumer_key, '');
40         }
41         
42         function lookup_token($consumer, $token_type, $token) {
43                 $t = new Token();
44                 $t->consumer_key = $consumer->consumer_key;
45                 $t->tok = $token;
46                 $t->type = ($token_type == 'access') ? 1 : 0;
47                 if ($t->find(true)) {
48                         return new OAuthToken($t->tok, $t->secret);
49                 } else {
50                         return NULL;
51                 }
52         }
53         
54         function lookup_nonce($consumer, $token, $nonce, $timestamp) {
55                 $n = new Nonce();
56                 $n->consumer_key = $consumer->consumer_key;
57                 $n->tok = $token;
58                 $n->nonce = $nonce;
59                 if ($n->find(TRUE)) {
60                         return TRUE;
61                 } else {
62                         $n->timestamp = $timestamp;
63                         $n->created = DB_DataObject_Cast::dateTime();
64                         $n->insert();
65                         return FALSE;
66                 }
67         }
68         
69         function new_request_token($consumer) {
70                 $t = new Token();
71                 $t->consumer_key = $consumer->consumer_key;
72                 $t->tok = common_good_rand(16);
73                 $t->secret = common_good_rand(16);
74                 $t->type = 0; # request
75                 $t->state = 0;
76                 $t->created = DB_DataObject_Cast::dateTime();
77                 if (!$t->insert()) {
78                         return NULL;
79                 } else {
80                         return new OAuthToken($t->tok, $t->secret);
81                 }
82         }
83
84         # defined in OAuthDataStore, but not implemented anywhere
85         
86         function fetch_request_token($consumer) {
87                 return $this->new_request_token($consumer);
88         }
89         
90         function new_access_token($token, $consumer) {
91                 $rt = new Token();
92                 $rt->consumer_key = $consumer->consumer_key;
93                 $rt->tok = $token;
94                 if ($rt->find(TRUE) && $rt->state == 1) {
95                         $at = new Token();
96                         $at->consumer_key = $consumer->consumer_key;
97                         $at->tok = common_good_rand(16);
98                         $at->secret = common_good_rand(16);
99                         $at->type = 1; # access
100                         $at->created = DB_DataObject_Cast::dateTime();
101                         if (!$at->insert()) {
102                                 return NULL;
103                         } else {
104                                 # burn the old one
105                                 $orig_rt = clone($rt);
106                                 $rt->state = 2; # used
107                                 if (!$rt->update($orig_rt)) {
108                                         return NULL;
109                                 } else {
110                                         return new OAuthToken($at->tok, $at->secret);
111                                 }
112                         }
113                 } else {
114                         return NULL;
115                 }
116         }
117         
118         # defined in OAuthDataStore, but not implemented anywhere
119         
120         function fetch_access_token($consumer) {
121                 return $this->new_access_token($consumer);
122         }
123 }