]> git.mxchange.org Git - friendica.git/blob - src/Network/FKOAuthDataStore.php
Add missing non-empty data condition to Protocol\PortableContact
[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\Core\Logger;
14 use Friendica\Database\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 /**
23  * @brief OAuthDataStore class
24  */
25 class FKOAuthDataStore extends OAuthDataStore
26 {
27         /**
28          * @return string
29          */
30         private static function genToken()
31         {
32                 return md5(base64_encode(pack('N6', mt_rand(), mt_rand(), mt_rand(), mt_rand(), mt_rand(), uniqid())));
33         }
34
35         /**
36          * @param string $consumer_key key
37          * @return mixed
38          * @throws \Exception
39          */
40         public function lookup_consumer($consumer_key)
41         {
42                 Logger::log(__function__ . ":" . $consumer_key);
43
44                 $s = DBA::select('clients', ['client_id', 'pw', 'redirect_uri'], ['client_id' => $consumer_key]);
45                 $r = DBA::toArray($s);
46
47                 if (DBA::isResult($r)) {
48                         return new OAuthConsumer($r[0]['client_id'], $r[0]['pw'], $r[0]['redirect_uri']);
49                 }
50
51                 return null;
52         }
53
54         /**
55          * @param string $consumer   consumer
56          * @param string $token_type type
57          * @param string $token      token
58          * @return mixed
59          * @throws \Exception
60          */
61         public function lookup_token($consumer, $token_type, $token)
62         {
63                 Logger::log(__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::toArray($s);
67
68                 if (DBA::isResult($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          * @throws \Exception
86          */
87         public function lookup_nonce($consumer, $token, $nonce, $timestamp)
88         {
89                 $token = DBA::selectFirst('tokens', ['id', 'secret'], ['client_id' => $consumer->key, 'id' => $nonce, 'expires' => $timestamp]);
90                 if (DBA::isResult($token)) {
91                         return new OAuthToken($token['id'], $token['secret']);
92                 }
93
94                 return null;
95         }
96
97         /**
98          * @param string $consumer consumer
99          * @param string $callback optional, default null
100          * @return mixed
101          * @throws \Exception
102          */
103         public function new_request_token($consumer, $callback = null)
104         {
105                 Logger::log(__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                         [
118                                 'id' => $key,
119                                 'secret' => $sec,
120                                 'client_id' => $k,
121                                 'scope' => 'request',
122                                 'expires' => time() + 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          * @throws HTTPException\InternalServerErrorException
138          */
139         public function new_access_token($token, $consumer, $verifier = null)
140         {
141                 Logger::log(__function__ . ":" . $token . ", " . $consumer . ", " . $verifier);
142
143                 // return a new access token attached to this consumer
144                 // for the user associated with this token if the request token
145                 // is authorized
146                 // should also invalidate the request token
147
148                 $ret = null;
149
150                 // get user for this verifier
151                 $uverifier = Config::get("oauth", $verifier);
152                 Logger::log(__function__ . ":" . $verifier . "," . $uverifier);
153
154                 if (is_null($verifier) || ($uverifier !== false)) {
155                         $key = self::genToken();
156                         $sec = self::genToken();
157                         $r = DBA::insert(
158                                 'tokens',
159                                 [
160                                         'id' => $key,
161                                         'secret' => $sec,
162                                         'client_id' => $consumer->key,
163                                         'scope' => 'access',
164                                         'expires' => time() + ACCESS_TOKEN_DURATION,
165                                         'uid' => $uverifier]
166                         );
167
168                         if ($r) {
169                                 $ret = new OAuthToken($key, $sec);
170                         }
171                 }
172
173                 DBA::delete('tokens', ['id' => $token->key]);
174
175                 if (!is_null($ret) && !is_null($uverifier)) {
176                         Config::delete("oauth", $verifier);
177                 }
178
179                 return $ret;
180         }
181 }