4 * @file src/Network/FKOAuthDataStore.php
6 * Based on oauth2-php <http://code.google.com/p/oauth2-php/>
10 namespace Friendica\Network;
12 use Friendica\Core\Config;
13 use Friendica\Core\Logger;
14 use Friendica\Database\DBA;
16 use Friendica\Util\Strings;
21 define('REQUEST_TOKEN_DURATION', 300);
22 define('ACCESS_TOKEN_DURATION', 31536000);
25 * OAuthDataStore class
27 class FKOAuthDataStore extends OAuthDataStore
33 private static function genToken()
35 return Strings::getRandomHex(32);
39 * @param string $consumer_key key
40 * @return OAuthConsumer|null
43 public function lookup_consumer($consumer_key)
45 Logger::log(__function__ . ":" . $consumer_key);
47 $s = DBA::select('clients', ['client_id', 'pw', 'redirect_uri'], ['client_id' => $consumer_key]);
48 $r = DBA::toArray($s);
50 if (DBA::isResult($r)) {
51 return new OAuthConsumer($r[0]['client_id'], $r[0]['pw'], $r[0]['redirect_uri']);
58 * @param OAuthConsumer $consumer
59 * @param string $token_type
60 * @param string $token_id
61 * @return OAuthToken|null
64 public function lookup_token(OAuthConsumer $consumer, $token_type, $token_id)
66 Logger::log(__function__ . ":" . $consumer . ", " . $token_type . ", " . $token_id);
68 $s = DBA::select('tokens', ['id', 'secret', 'scope', 'expires', 'uid'], ['client_id' => $consumer->key, 'scope' => $token_type, 'id' => $token_id]);
69 $r = DBA::toArray($s);
71 if (DBA::isResult($r)) {
72 $ot = new OAuthToken($r[0]['id'], $r[0]['secret']);
73 $ot->scope = $r[0]['scope'];
74 $ot->expires = $r[0]['expires'];
75 $ot->uid = $r[0]['uid'];
83 * @param OAuthConsumer $consumer
84 * @param OAuthToken $token
85 * @param string $nonce
86 * @param int $timestamp
90 public function lookup_nonce(OAuthConsumer $consumer, OAuthToken $token, $nonce, int $timestamp)
92 $token = DBA::selectFirst('tokens', ['id', 'secret'], ['client_id' => $consumer->key, 'id' => $nonce, 'expires' => $timestamp]);
93 if (DBA::isResult($token)) {
94 return new OAuthToken($token['id'], $token['secret']);
101 * @param OAuthConsumer $consumer
102 * @param string $callback
103 * @return OAuthToken|null
106 public function new_request_token(OAuthConsumer $consumer, $callback = null)
108 Logger::log(__function__ . ":" . $consumer . ", " . $callback);
109 $key = self::genToken();
110 $sec = self::genToken();
112 if ($consumer->key) {
124 'scope' => 'request',
125 'expires' => time() + REQUEST_TOKEN_DURATION
133 return new OAuthToken($key, $sec);
137 * @param OAuthToken $token token
138 * @param OAuthConsumer $consumer consumer
139 * @param string $verifier optional, defult null
143 public function new_access_token(OAuthToken $token, OAuthConsumer $consumer, $verifier = null)
145 Logger::log(__function__ . ":" . $token . ", " . $consumer . ", " . $verifier);
147 // return a new access token attached to this consumer
148 // for the user associated with this token if the request token
150 // should also invalidate the request token
154 // get user for this verifier
155 $uverifier = DI::config()->get("oauth", $verifier);
156 Logger::log(__function__ . ":" . $verifier . "," . $uverifier);
158 if (is_null($verifier) || ($uverifier !== false)) {
159 $key = self::genToken();
160 $sec = self::genToken();
166 'client_id' => $consumer->key,
168 'expires' => time() + ACCESS_TOKEN_DURATION,
174 $ret = new OAuthToken($key, $sec);
178 DBA::delete('tokens', ['id' => $token->key]);
180 if (!is_null($ret) && !is_null($uverifier)) {
181 DI::config()->delete("oauth", $verifier);