4 * Based on oauth2-php <http://code.google.com/p/oauth2-php/>
8 define('REQUEST_TOKEN_DURATION', 300);
9 define('ACCESS_TOKEN_DURATION', 31536000);
11 require_once("library/OAuth1.php");
12 require_once("library/oauth2-php/lib/OAuth2.inc");
14 class FKOAuthDataStore extends OAuthDataStore {
16 return md5(base64_encode(pack('N6', mt_rand(), mt_rand(), mt_rand(), mt_rand(), mt_rand(), uniqid())));
19 function lookup_consumer($consumer_key) {
20 logger(__function__.":".$consumer_key);
21 //echo "<pre>"; var_dump($consumer_key); killme();
23 $r = q("SELECT client_id, pw, redirect_uri FROM clients WHERE client_id='%s'",
26 if (dbm::is_result($r))
27 return new OAuthConsumer($r[0]['client_id'],$r[0]['pw'],$r[0]['redirect_uri']);
31 function lookup_token($consumer, $token_type, $token) {
32 logger(__function__.":".$consumer.", ". $token_type.", ".$token);
33 $r = q("SELECT id, secret,scope, expires, uid FROM tokens WHERE client_id='%s' AND scope='%s' AND id='%s'",
34 dbesc($consumer->key),
38 if (dbm::is_result($r)){
39 $ot=new OAuthToken($r[0]['id'],$r[0]['secret']);
40 $ot->scope=$r[0]['scope'];
41 $ot->expires = $r[0]['expires'];
42 $ot->uid = $r[0]['uid'];
48 function lookup_nonce($consumer, $token, $nonce, $timestamp) {
49 //echo __file__.":".__line__."<pre>"; var_dump($consumer,$key); killme();
50 $r = q("SELECT id, secret FROM tokens WHERE client_id='%s' AND id='%s' AND expires=%d",
51 dbesc($consumer->key),
55 if (dbm::is_result($r))
56 return new OAuthToken($r[0]['id'],$r[0]['secret']);
60 function new_request_token($consumer, $callback = null) {
61 logger(__function__.":".$consumer.", ". $callback);
62 $key = $this->gen_token();
63 $sec = $this->gen_token();
71 $r = q("INSERT INTO tokens (id, secret, client_id, scope, expires) VALUES ('%s','%s','%s','%s', UNIX_TIMESTAMP()+%d)",
76 intval(REQUEST_TOKEN_DURATION));
78 return new OAuthToken($key,$sec);
81 function new_access_token($token, $consumer, $verifier = null) {
82 logger(__function__.":".$token.", ". $consumer.", ". $verifier);
84 // return a new access token attached to this consumer
85 // for the user associated with this token if the request token
87 // should also invalidate the request token
91 // get user for this verifier
92 $uverifier = get_config("oauth", $verifier);
93 logger(__function__.":".$verifier.",".$uverifier);
94 if (is_null($verifier) || ($uverifier!==false)){
96 $key = $this->gen_token();
97 $sec = $this->gen_token();
98 $r = q("INSERT INTO tokens (id, secret, client_id, scope, expires, uid) VALUES ('%s','%s','%s','%s', UNIX_TIMESTAMP()+%d, %d)",
101 dbesc($consumer->key),
103 intval(ACCESS_TOKEN_DURATION),
106 $ret = new OAuthToken($key,$sec);
110 q("DELETE FROM tokens WHERE id='%s'", $token->key);
113 if (!is_null($ret) && $uverifier!==false){
114 del_config("oauth", $verifier);
115 /* $apps = get_pconfig($uverifier, "oauth", "apps");
116 if ($apps===false) $apps=array();
117 $apps[] = $consumer->key;
118 set_pconfig($uverifier, "oauth", "apps", $apps);*/
126 class FKOAuth1 extends OAuthServer {
127 function __construct() {
128 parent::__construct(new FKOAuthDataStore());
129 $this->add_signature_method(new OAuthSignatureMethod_PLAINTEXT());
130 $this->add_signature_method(new OAuthSignatureMethod_HMAC_SHA1());
133 function loginUser($uid){
134 logger("FKOAuth1::loginUser $uid");
136 $r = q("SELECT * FROM `user` WHERE uid=%d AND `blocked` = 0 AND `account_expired` = 0 AND `account_removed` = 0 AND `verified` = 1 LIMIT 1",
139 if (dbm::is_result($r)){
142 logger('FKOAuth1::loginUser failure: ' . print_r($_SERVER,true), LOGGER_DEBUG);
143 header('HTTP/1.0 401 Unauthorized');
144 die('This api requires login');
146 $_SESSION['uid'] = $record['uid'];
147 $_SESSION['theme'] = $record['theme'];
148 $_SESSION['mobile-theme'] = get_pconfig($record['uid'], 'system', 'mobile_theme');
149 $_SESSION['authenticated'] = 1;
150 $_SESSION['page_flags'] = $record['page-flags'];
151 $_SESSION['my_url'] = App::get_baseurl() . '/profile/' . $record['nickname'];
152 $_SESSION['addr'] = $_SERVER['REMOTE_ADDR'];
153 $_SESSION["allow_api"] = true;
155 //notice( t("Welcome back ") . $record['username'] . EOL);
158 if(strlen($a->user['timezone'])) {
159 date_default_timezone_set($a->user['timezone']);
160 $a->timezone = $a->user['timezone'];
163 $r = q("SELECT * FROM `contact` WHERE `uid` = %s AND `self` = 1 LIMIT 1",
164 intval($_SESSION['uid']));
165 if (dbm::is_result($r)) {
167 $a->cid = $r[0]['id'];
168 $_SESSION['cid'] = $a->cid;
170 q("UPDATE `user` SET `login_date` = '%s' WHERE `uid` = %d",
171 dbesc(datetime_convert()),
172 intval($_SESSION['uid'])
175 call_hooks('logged_in', $a->user);
180 class FKOAuth2 extends OAuth2 {
182 private function db_secret($client_secret){
183 return hash('whirlpool',$client_secret);
186 public function addClient($client_id, $client_secret, $redirect_uri) {
187 $client_secret = $this->db_secret($client_secret);
188 $r = q("INSERT INTO clients (client_id, pw, redirect_uri) VALUES ('%s', '%s', '%s')",
190 dbesc($client_secret),
197 protected function checkClientCredentials($client_id, $client_secret = NULL) {
198 $client_secret = $this->db_secret($client_secret);
200 $r = q("SELECT pw FROM clients WHERE client_id = '%s'",
203 if ($client_secret === NULL)
204 return $result !== FALSE;
206 return $result["client_secret"] == $client_secret;
209 protected function getRedirectUri($client_id) {
210 $r = q("SELECT redirect_uri FROM clients WHERE client_id = '%s'",
215 return isset($r[0]["redirect_uri"]) && $r[0]["redirect_uri"] ? $r[0]["redirect_uri"] : NULL;
218 protected function getAccessToken($oauth_token) {
219 $r = q("SELECT client_id, expires, scope FROM tokens WHERE id = '%s'",
220 dbesc($oauth_token));
222 if (dbm::is_result($r))
229 protected function setAccessToken($oauth_token, $client_id, $expires, $scope = NULL) {
230 $r = q("INSERT INTO tokens (id, client_id, expires, scope) VALUES ('%s', '%s', %d, '%s')",
239 protected function getSupportedGrantTypes() {
241 OAUTH2_GRANT_TYPE_AUTH_CODE,
246 protected function getAuthCode($code) {
247 $r = q("SELECT id, client_id, redirect_uri, expires, scope FROM auth_codes WHERE id = '%s'",
250 if (dbm::is_result($r))
255 protected function setAuthCode($code, $client_id, $redirect_uri, $expires, $scope = NULL) {
256 $r = q("INSERT INTO auth_codes
257 (id, client_id, redirect_uri, expires, scope) VALUES
258 ('%s', '%s', '%s', %d, '%s')",
261 dbesc($redirect_uri),