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;
19 define('REQUEST_TOKEN_DURATION', 300);
20 define('ACCESS_TOKEN_DURATION', 31536000);
23 * @brief OAuthDataStore class
25 class FKOAuthDataStore extends OAuthDataStore
30 private static function genToken()
32 return md5(base64_encode(pack('N6', mt_rand(), mt_rand(), mt_rand(), mt_rand(), mt_rand(), uniqid())));
36 * @param string $consumer_key key
40 public function lookup_consumer($consumer_key)
42 Logger::log(__function__ . ":" . $consumer_key);
44 $s = DBA::select('clients', ['client_id', 'pw', 'redirect_uri'], ['client_id' => $consumer_key]);
45 $r = DBA::toArray($s);
47 if (DBA::isResult($r)) {
48 return new OAuthConsumer($r[0]['client_id'], $r[0]['pw'], $r[0]['redirect_uri']);
55 * @param string $consumer consumer
56 * @param string $token_type type
57 * @param string $token token
61 public function lookup_token($consumer, $token_type, $token)
63 Logger::log(__function__ . ":" . $consumer . ", " . $token_type . ", " . $token);
65 $s = DBA::select('tokens', ['id', 'secret', 'scope', 'expires', 'uid'], ['client_id' => $consumer->key, 'scope' => $token_type, 'id' => $token]);
66 $r = DBA::toArray($s);
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'];
80 * @param string $consumer consumer
81 * @param string $token token
82 * @param string $nonce nonce
83 * @param string $timestamp timestamp
87 public function lookup_nonce($consumer, $token, $nonce, $timestamp)
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']);
98 * @param string $consumer consumer
99 * @param string $callback optional, default null
103 public function new_request_token($consumer, $callback = null)
105 Logger::log(__function__ . ":" . $consumer . ", " . $callback);
106 $key = self::genToken();
107 $sec = self::genToken();
109 if ($consumer->key) {
121 'scope' => 'request',
122 'expires' => time() + REQUEST_TOKEN_DURATION]
129 return new OAuthToken($key, $sec);
133 * @param string $token token
134 * @param string $consumer consumer
135 * @param string $verifier optional, defult null
137 * @throws HTTPException\InternalServerErrorException
139 public function new_access_token($token, $consumer, $verifier = null)
141 Logger::log(__function__ . ":" . $token . ", " . $consumer . ", " . $verifier);
143 // return a new access token attached to this consumer
144 // for the user associated with this token if the request token
146 // should also invalidate the request token
150 // get user for this verifier
151 $uverifier = Config::get("oauth", $verifier);
152 Logger::log(__function__ . ":" . $verifier . "," . $uverifier);
154 if (is_null($verifier) || ($uverifier !== false)) {
155 $key = self::genToken();
156 $sec = self::genToken();
162 'client_id' => $consumer->key,
164 'expires' => time() + ACCESS_TOKEN_DURATION,
169 $ret = new OAuthToken($key, $sec);
173 DBA::delete('tokens', ['id' => $token->key]);
175 if (!is_null($ret) && !is_null($uverifier)) {
176 Config::delete("oauth", $verifier);