3 * @file src/Protocol/FKOAuthDataStore.php
5 * Based on oauth2-php <http://code.google.com/p/oauth2-php/>
8 namespace Friendica\Network;
11 use Friendica\Core\Config;
12 use Friendica\Core\System;
13 use Friendica\Database\DBM;
17 define('REQUEST_TOKEN_DURATION', 300);
18 define('ACCESS_TOKEN_DURATION', 31536000);
20 require_once "library/OAuth1.php";
21 require_once "library/oauth2-php/lib/OAuth2.inc";
24 * @brief OAuthDataStore class
26 class FKOAuthDataStore extends OAuthDataStore
31 private static function genToken()
33 return md5(base64_encode(pack('N6', mt_rand(), mt_rand(), mt_rand(), mt_rand(), mt_rand(), uniqid())));
37 * @param string $consumer_key key
40 public function lookup_consumer($consumer_key)
42 logger(__function__.":".$consumer_key);
44 $s = dba::select('clients', array('client_id', 'pw', 'redirect_uri'), array('client_id' => $consumer_key));
45 $r = dba::inArray($r);
47 if (DBM::is_result($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
60 public function lookup_token($consumer, $token_type, $token)
62 logger(__function__.":".$consumer.", ". $token_type.", ".$token);
64 $s = dba::select('tokens', array('id', 'secret', 'scope', 'expires', 'uid'), array('client_id' => $consumer->key, 'scope' => $token_type, 'id' => $token));
65 $r = dba::inArray($s);
67 if (DBM::is_result($r)) {
68 $ot=new OAuthToken($r[0]['id'], $r[0]['secret']);
69 $ot->scope = $r[0]['scope'];
70 $ot->expires = $r[0]['expires'];
71 $ot->uid = $r[0]['uid'];
79 * @param string $consumer consumer
80 * @param string $token token
81 * @param string $nonce nonce
82 * @param string $timestamp timestamp
85 public function lookup_nonce($consumer, $token, $nonce, $timestamp)
87 $r = dba::select('tokens', ['id', 'secret'], ['client_id' => $consumer->key, 'id' => $nonce, 'expires' => $timestamp], ['limit' => 1]);
89 if (DBM::is_result($r)) {
90 return new OAuthToken($r['id'], $r['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' => UNIX_TIMESTAMP() + 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' => UNIX_TIMESTAMP() + ACCESS_TOKEN_DURATION,
166 $ret = new OAuthToken($key, $sec);
171 dba::delete('tokens', array('id' => $token->key));
174 if (!is_null($ret) && !is_null($uverifier)) {
175 Config::delete("oauth", $verifier);