]> git.mxchange.org Git - friendica.git/blob - src/Network/FKOAuthDataStore.php
Move Cookie to own class (with tests)
[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 Friendica\Util\Strings::getRandomHex(32);
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
126                 if (!$r) {
127                         return null;
128                 }
129
130                 return new OAuthToken($key, $sec);
131         }
132
133         /**
134          * @param string $token    token
135          * @param string $consumer consumer
136          * @param string $verifier optional, defult null
137          * @return object
138          * @throws HTTPException\InternalServerErrorException
139          */
140         public function new_access_token($token, $consumer, $verifier = null)
141         {
142                 Logger::log(__function__ . ":" . $token . ", " . $consumer . ", " . $verifier);
143
144                 // return a new access token attached to this consumer
145                 // for the user associated with this token if the request token
146                 // is authorized
147                 // should also invalidate the request token
148
149                 $ret = null;
150
151                 // get user for this verifier
152                 $uverifier = Config::get("oauth", $verifier);
153                 Logger::log(__function__ . ":" . $verifier . "," . $uverifier);
154
155                 if (is_null($verifier) || ($uverifier !== false)) {
156                         $key = self::genToken();
157                         $sec = self::genToken();
158                         $r = DBA::insert(
159                                 'tokens',
160                                 [
161                                         'id' => $key,
162                                         'secret' => $sec,
163                                         'client_id' => $consumer->key,
164                                         'scope' => 'access',
165                                         'expires' => time() + ACCESS_TOKEN_DURATION,
166                                         'uid' => $uverifier
167                                 ]
168                         );
169
170                         if ($r) {
171                                 $ret = new OAuthToken($key, $sec);
172                         }
173                 }
174
175                 DBA::delete('tokens', ['id' => $token->key]);
176
177                 if (!is_null($ret) && !is_null($uverifier)) {
178                         Config::delete("oauth", $verifier);
179                 }
180
181                 return $ret;
182         }
183 }