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;
16 define('REQUEST_TOKEN_DURATION', 300);
17 define('ACCESS_TOKEN_DURATION', 31536000);
19 require_once "library/OAuth1.php";
20 require_once "library/oauth2-php/lib/OAuth2.inc";
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
39 public static function lookup_consumer($consumer_key)
41 logger(__function__.":".$consumer_key);
43 $s = dba::select('clients', array('client_id', 'pw', 'redirect_uri'), array('client_id' => $consumer_key));
44 $r = dba::inArray($r);
46 if (DBM::is_result($r)) {
47 return new OAuthConsumer($r[0]['client_id'], $r[0]['pw'], $r[0]['redirect_uri']);
54 * @param string $consumer consumer
55 * @param string $token_type type
56 * @param string $token token
59 public static function lookup_token($consumer, $token_type, $token)
61 logger(__function__.":".$consumer.", ". $token_type.", ".$token);
63 $s = dba::select('tokens', array('id', 'secret', 'scope', 'expires', 'uid'), array('client_id' => $consumer->key, 'scope' => $token_type, 'id' => $token));
64 $r = dba::inArray($s);
66 if (DBM::is_result($r)) {
67 $ot=new OAuthToken($r[0]['id'], $r[0]['secret']);
68 $ot->scope = $r[0]['scope'];
69 $ot->expires = $r[0]['expires'];
70 $ot->uid = $r[0]['uid'];
78 * @param string $consumer consumer
79 * @param string $token token
80 * @param string $nonce nonce
81 * @param string $timestamp timestamp
84 public static function lookup_nonce($consumer, $token, $nonce, $timestamp)
86 $s = dba::select('tokens', array('id', 'secret'), array('client_id' => $consumer->key, 'id' => $nonce, 'expires' => $timestamp));
87 $r = dba::inArray($s);
89 if (DBM::is_result($r)) {
90 return new OAuthToken($r[0]['id'], $r[0]['secret']);
97 * @param string $consumer consumer
98 * @param string $callback optional, default null
101 public static 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 static 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) && $uverifier !== false) {
175 Config::delete("oauth", $verifier);