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\Database\DBM;
19 define('REQUEST_TOKEN_DURATION', 300);
20 define('ACCESS_TOKEN_DURATION', 31536000);
22 require_once 'include/dba.php';
25 * @brief OAuthDataStore class
27 class FKOAuthDataStore extends OAuthDataStore
32 private static function genToken()
34 return md5(base64_encode(pack('N6', mt_rand(), mt_rand(), mt_rand(), mt_rand(), mt_rand(), uniqid())));
38 * @param string $consumer_key key
41 public function lookup_consumer($consumer_key)
43 logger(__function__ . ":" . $consumer_key);
45 $s = dba::select('clients', ['client_id', 'pw', 'redirect_uri'], ['client_id' => $consumer_key]);
46 $r = dba::inArray($s);
48 if (DBM::is_result($r)) {
49 return new OAuthConsumer($r[0]['client_id'], $r[0]['pw'], $r[0]['redirect_uri']);
56 * @param string $consumer consumer
57 * @param string $token_type type
58 * @param string $token token
61 public function lookup_token($consumer, $token_type, $token)
63 logger(__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::inArray($s);
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'];
80 * @param string $consumer consumer
81 * @param string $token token
82 * @param string $nonce nonce
83 * @param string $timestamp timestamp
86 public function lookup_nonce($consumer, $token, $nonce, $timestamp)
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']);
97 * @param string $consumer consumer
98 * @param string $callback optional, default null
101 public function new_request_token($consumer, $callback = null)
103 logger(__function__ . ":" . $consumer . ", " . $callback);
104 $key = self::genToken();
105 $sec = self::genToken();
107 if ($consumer->key) {
119 'scope' => 'request',
120 'expires' => time() + REQUEST_TOKEN_DURATION]
127 return new OAuthToken($key, $sec);
131 * @param string $token token
132 * @param string $consumer consumer
133 * @param string $verifier optional, defult null
136 public function new_access_token($token, $consumer, $verifier = null)
138 logger(__function__ . ":" . $token . ", " . $consumer . ", " . $verifier);
140 // return a new access token attached to this consumer
141 // for the user associated with this token if the request token
143 // should also invalidate the request token
147 // get user for this verifier
148 $uverifier = Config::get("oauth", $verifier);
149 logger(__function__ . ":" . $verifier . "," . $uverifier);
151 if (is_null($verifier) || ($uverifier !== false)) {
152 $key = self::genToken();
153 $sec = self::genToken();
159 'client_id' => $consumer->key,
161 'expires' => time() + ACCESS_TOKEN_DURATION,
166 $ret = new OAuthToken($key, $sec);
170 dba::delete('tokens', ['id' => $token->key]);
172 if (!is_null($ret) && !is_null($uverifier)) {
173 Config::delete("oauth", $verifier);