]> git.mxchange.org Git - friendica.git/blob - src/Network/FKOAuthDataStore.php
e8f580c58a2d508ec284dbc70ac5c40ee6a483f8
[friendica.git] / src / Network / FKOAuthDataStore.php
1 <?php
2 /**
3  * @file src/Protocol/FKOAuthDataStore.php
4  * OAuth server
5  * Based on oauth2-php <http://code.google.com/p/oauth2-php/>
6  *
7  */
8 namespace Friendica\Network;
9
10 use Friendica\App;
11 use Friendica\Core\Config;
12 use Friendica\Core\System;
13 use Friendica\Database\DBM;
14 use dba;
15 use OAuthDataStore;
16
17 define('REQUEST_TOKEN_DURATION', 300);
18 define('ACCESS_TOKEN_DURATION', 31536000);
19
20 require_once 'include/dba.php';
21
22 require_once "library/OAuth1.php";
23 require_once "library/oauth2-php/lib/OAuth2.inc";
24
25 /**
26  * @brief OAuthDataStore class
27  */
28 class FKOAuthDataStore extends OAuthDataStore
29 {
30         /**
31          * @return string
32          */
33         private static function genToken()
34         {
35                 return md5(base64_encode(pack('N6', mt_rand(), mt_rand(), mt_rand(), mt_rand(), mt_rand(), uniqid())));
36         }
37
38         /**
39          * @param string $consumer_key key
40          * @return mixed
41          */
42         public function lookup_consumer($consumer_key)
43         {
44                 logger(__function__.":".$consumer_key);
45                 
46                 $s = dba::select('clients', array('client_id', 'pw', 'redirect_uri'), array('client_id' => $consumer_key));
47                 $r = dba::inArray($r);
48
49                 if (DBM::is_result($r)) {
50                         return new \OAuthConsumer($r[0]['client_id'], $r[0]['pw'], $r[0]['redirect_uri']);
51                 }
52
53                 return null;
54         }
55
56         /**
57          * @param string $consumer   consumer
58          * @param string $token_type type
59          * @param string $token      token
60          * @return mixed
61          */
62         public function lookup_token($consumer, $token_type, $token)
63         {
64                 logger(__function__.":".$consumer.", ". $token_type.", ".$token);
65                 
66                 $s = dba::select('tokens', array('id', 'secret', 'scope', 'expires', 'uid'), array('client_id' => $consumer->key, 'scope' => $token_type, 'id' => $token));
67                 $r = dba::inArray($s);
68
69                 if (DBM::is_result($r)) {
70                         $ot = new \OAuthToken($r[0]['id'], $r[0]['secret']);
71                         $ot->scope = $r[0]['scope'];
72                         $ot->expires = $r[0]['expires'];
73                         $ot->uid = $r[0]['uid'];
74                         return $ot;
75                 }
76
77                 return null;
78         }
79
80         /**
81          * @param string $consumer  consumer
82          * @param string $token     token
83          * @param string $nonce     nonce
84          * @param string $timestamp timestamp
85          * @return mixed
86          */
87         public function lookup_nonce($consumer, $token, $nonce, $timestamp)
88         {
89                 $r = dba::select('tokens', ['id', 'secret'], ['client_id' => $consumer->key, 'id' => $nonce, 'expires' => $timestamp], ['limit' => 1]);
90                                 
91                 if (DBM::is_result($r)) {
92                         return new \OAuthToken($r['id'], $r['secret']);
93                 }
94
95                 return null;
96         }
97
98         /**
99          * @param string $consumer consumer
100          * @param string $callback optional, default null
101          * @return mixed
102          */
103         public function new_request_token($consumer, $callback = null)
104         {
105                 logger(__function__.":".$consumer.", ". $callback);
106                 $key = self::genToken();
107                 $sec = self::genToken();
108
109                 if ($consumer->key) {
110                         $k = $consumer->key;
111                 } else {
112                         $k = $consumer;
113                 }
114
115                 $r = dba::insert(
116                         'tokens',
117                         array(
118                                 'id' => $key,
119                                 'secret' => $sec,
120                                 'client_id' => $k,
121                                 'scope' => 'request',
122                                 'expires' => UNIX_TIMESTAMP() + REQUEST_TOKEN_DURATION)
123                 );
124
125                 if (!$r) {
126                         return null;
127                 }
128
129                 return new \OAuthToken($key, $sec);
130         }
131
132         /**
133          * @param string $token    token
134          * @param string $consumer consumer
135          * @param string $verifier optional, defult null
136          * @return object
137          */
138         public function new_access_token($token, $consumer, $verifier = null)
139         {
140                 logger(__function__.":".$token.", ". $consumer.", ". $verifier);
141
142                 // return a new access token attached to this consumer
143                 // for the user associated with this token if the request token
144                 // is authorized
145                 // should also invalidate the request token
146
147                 $ret = null;
148
149                 // get user for this verifier
150                 $uverifier = Config::get("oauth", $verifier);
151                 logger(__function__.":".$verifier.",".$uverifier);
152
153                 if (is_null($verifier) || ($uverifier!==false)) {
154                         $key = self::genToken();
155                         $sec = self::genToken();
156                         $r = dba::insert(
157                                 'tokens',
158                                 array(
159                                         'id' => $key,
160                                         'secret' => $sec,
161                                         'client_id' => $consumer->key,
162                                         'scope' => 'access',
163                                         'expires' => UNIX_TIMESTAMP() + ACCESS_TOKEN_DURATION,
164                                         'uid' => $uverifier)
165                         );
166
167                         if ($r) {
168                                 $ret = new \OAuthToken($key, $sec);
169                         }
170                 }
171
172
173                 dba::delete('tokens', array('id' => $token->key));
174
175
176                 if (!is_null($ret) && !is_null($uverifier)) {
177                         Config::delete("oauth", $verifier);
178                 }
179
180                 return $ret;
181         }
182 }