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