Friendica Communications Platform (please note that this is a clone of the repository at github, issues are handled there) https://friendi.ca
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

oauth.php 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. <?php
  2. /**
  3. * OAuth server
  4. * Based on oauth2-php <http://code.google.com/p/oauth2-php/>
  5. *
  6. */
  7. define('REQUEST_TOKEN_DURATION', 300);
  8. define('ACCESS_TOKEN_DURATION', 31536000);
  9. require_once("library/OAuth1.php");
  10. require_once("library/oauth2-php/lib/OAuth2.inc");
  11. class FKOAuthDataStore extends OAuthDataStore {
  12. function gen_token(){
  13. return md5(base64_encode(pack('N6', mt_rand(), mt_rand(), mt_rand(), mt_rand(), mt_rand(), uniqid())));
  14. }
  15. function lookup_consumer($consumer_key) {
  16. logger(__function__.":".$consumer_key);
  17. //echo "<pre>"; var_dump($consumer_key); killme();
  18. $r = q("SELECT client_id, pw, redirect_uri FROM clients WHERE client_id='%s'",
  19. dbesc($consumer_key)
  20. );
  21. if (dbm::is_result($r))
  22. return new OAuthConsumer($r[0]['client_id'],$r[0]['pw'],$r[0]['redirect_uri']);
  23. return null;
  24. }
  25. function lookup_token($consumer, $token_type, $token) {
  26. logger(__function__.":".$consumer.", ". $token_type.", ".$token);
  27. $r = q("SELECT id, secret,scope, expires, uid FROM tokens WHERE client_id='%s' AND scope='%s' AND id='%s'",
  28. dbesc($consumer->key),
  29. dbesc($token_type),
  30. dbesc($token)
  31. );
  32. if (dbm::is_result($r)){
  33. $ot=new OAuthToken($r[0]['id'],$r[0]['secret']);
  34. $ot->scope=$r[0]['scope'];
  35. $ot->expires = $r[0]['expires'];
  36. $ot->uid = $r[0]['uid'];
  37. return $ot;
  38. }
  39. return null;
  40. }
  41. function lookup_nonce($consumer, $token, $nonce, $timestamp) {
  42. //echo __file__.":".__line__."<pre>"; var_dump($consumer,$key); killme();
  43. $r = q("SELECT id, secret FROM tokens WHERE client_id='%s' AND id='%s' AND expires=%d",
  44. dbesc($consumer->key),
  45. dbesc($nonce),
  46. intval($timestamp)
  47. );
  48. if (dbm::is_result($r))
  49. return new OAuthToken($r[0]['id'],$r[0]['secret']);
  50. return null;
  51. }
  52. function new_request_token($consumer, $callback = null) {
  53. logger(__function__.":".$consumer.", ". $callback);
  54. $key = $this->gen_token();
  55. $sec = $this->gen_token();
  56. if ($consumer->key){
  57. $k = $consumer->key;
  58. } else {
  59. $k = $consumer;
  60. }
  61. $r = q("INSERT INTO tokens (id, secret, client_id, scope, expires) VALUES ('%s','%s','%s','%s', UNIX_TIMESTAMP()+%d)",
  62. dbesc($key),
  63. dbesc($sec),
  64. dbesc($k),
  65. 'request',
  66. intval(REQUEST_TOKEN_DURATION));
  67. if (!$r) return null;
  68. return new OAuthToken($key,$sec);
  69. }
  70. function new_access_token($token, $consumer, $verifier = null) {
  71. logger(__function__.":".$token.", ". $consumer.", ". $verifier);
  72. // return a new access token attached to this consumer
  73. // for the user associated with this token if the request token
  74. // is authorized
  75. // should also invalidate the request token
  76. $ret=Null;
  77. // get user for this verifier
  78. $uverifier = get_config("oauth", $verifier);
  79. logger(__function__.":".$verifier.",".$uverifier);
  80. if (is_null($verifier) || ($uverifier!==false)){
  81. $key = $this->gen_token();
  82. $sec = $this->gen_token();
  83. $r = q("INSERT INTO tokens (id, secret, client_id, scope, expires, uid) VALUES ('%s','%s','%s','%s', UNIX_TIMESTAMP()+%d, %d)",
  84. dbesc($key),
  85. dbesc($sec),
  86. dbesc($consumer->key),
  87. 'access',
  88. intval(ACCESS_TOKEN_DURATION),
  89. intval($uverifier));
  90. if ($r)
  91. $ret = new OAuthToken($key,$sec);
  92. }
  93. q("DELETE FROM tokens WHERE id='%s'", $token->key);
  94. if (!is_null($ret) && $uverifier!==false){
  95. del_config("oauth", $verifier);
  96. /* $apps = get_pconfig($uverifier, "oauth", "apps");
  97. if ($apps===false) $apps=array();
  98. $apps[] = $consumer->key;
  99. set_pconfig($uverifier, "oauth", "apps", $apps);*/
  100. }
  101. return $ret;
  102. }
  103. }
  104. class FKOAuth1 extends OAuthServer {
  105. function __construct() {
  106. parent::__construct(new FKOAuthDataStore());
  107. $this->add_signature_method(new OAuthSignatureMethod_PLAINTEXT());
  108. $this->add_signature_method(new OAuthSignatureMethod_HMAC_SHA1());
  109. }
  110. function loginUser($uid){
  111. logger("FKOAuth1::loginUser $uid");
  112. $a = get_app();
  113. $r = q("SELECT * FROM `user` WHERE uid=%d AND `blocked` = 0 AND `account_expired` = 0 AND `account_removed` = 0 AND `verified` = 1 LIMIT 1",
  114. intval($uid)
  115. );
  116. if (dbm::is_result($r)){
  117. $record = $r[0];
  118. } else {
  119. logger('FKOAuth1::loginUser failure: ' . print_r($_SERVER,true), LOGGER_DEBUG);
  120. header('HTTP/1.0 401 Unauthorized');
  121. die('This api requires login');
  122. }
  123. $_SESSION['uid'] = $record['uid'];
  124. $_SESSION['theme'] = $record['theme'];
  125. $_SESSION['mobile-theme'] = get_pconfig($record['uid'], 'system', 'mobile_theme');
  126. $_SESSION['authenticated'] = 1;
  127. $_SESSION['page_flags'] = $record['page-flags'];
  128. $_SESSION['my_url'] = App::get_baseurl() . '/profile/' . $record['nickname'];
  129. $_SESSION['addr'] = $_SERVER['REMOTE_ADDR'];
  130. $_SESSION["allow_api"] = true;
  131. //notice( t("Welcome back ") . $record['username'] . EOL);
  132. $a->user = $record;
  133. if(strlen($a->user['timezone'])) {
  134. date_default_timezone_set($a->user['timezone']);
  135. $a->timezone = $a->user['timezone'];
  136. }
  137. $r = q("SELECT * FROM `contact` WHERE `uid` = %s AND `self` = 1 LIMIT 1",
  138. intval($_SESSION['uid']));
  139. if (dbm::is_result($r)) {
  140. $a->contact = $r[0];
  141. $a->cid = $r[0]['id'];
  142. $_SESSION['cid'] = $a->cid;
  143. }
  144. q("UPDATE `user` SET `login_date` = '%s' WHERE `uid` = %d",
  145. dbesc(datetime_convert()),
  146. intval($_SESSION['uid'])
  147. );
  148. call_hooks('logged_in', $a->user);
  149. }
  150. }
  151. /*
  152. class FKOAuth2 extends OAuth2 {
  153. private function db_secret($client_secret){
  154. return hash('whirlpool',$client_secret);
  155. }
  156. public function addClient($client_id, $client_secret, $redirect_uri) {
  157. $client_secret = $this->db_secret($client_secret);
  158. $r = q("INSERT INTO clients (client_id, pw, redirect_uri) VALUES ('%s', '%s', '%s')",
  159. dbesc($client_id),
  160. dbesc($client_secret),
  161. dbesc($redirect_uri)
  162. );
  163. return $r;
  164. }
  165. protected function checkClientCredentials($client_id, $client_secret = NULL) {
  166. $client_secret = $this->db_secret($client_secret);
  167. $r = q("SELECT pw FROM clients WHERE client_id = '%s'",
  168. dbesc($client_id));
  169. if ($client_secret === NULL)
  170. return $result !== FALSE;
  171. return $result["client_secret"] == $client_secret;
  172. }
  173. protected function getRedirectUri($client_id) {
  174. $r = q("SELECT redirect_uri FROM clients WHERE client_id = '%s'",
  175. dbesc($client_id));
  176. if ($r === FALSE)
  177. return FALSE;
  178. return isset($r[0]["redirect_uri"]) && $r[0]["redirect_uri"] ? $r[0]["redirect_uri"] : NULL;
  179. }
  180. protected function getAccessToken($oauth_token) {
  181. $r = q("SELECT client_id, expires, scope FROM tokens WHERE id = '%s'",
  182. dbesc($oauth_token));
  183. if (dbm::is_result($r))
  184. return $r[0];
  185. return null;
  186. }
  187. protected function setAccessToken($oauth_token, $client_id, $expires, $scope = NULL) {
  188. $r = q("INSERT INTO tokens (id, client_id, expires, scope) VALUES ('%s', '%s', %d, '%s')",
  189. dbesc($oauth_token),
  190. dbesc($client_id),
  191. intval($expires),
  192. dbesc($scope));
  193. return $r;
  194. }
  195. protected function getSupportedGrantTypes() {
  196. return array(
  197. OAUTH2_GRANT_TYPE_AUTH_CODE,
  198. );
  199. }
  200. protected function getAuthCode($code) {
  201. $r = q("SELECT id, client_id, redirect_uri, expires, scope FROM auth_codes WHERE id = '%s'",
  202. dbesc($code));
  203. if (dbm::is_result($r))
  204. return $r[0];
  205. return null;
  206. }
  207. protected function setAuthCode($code, $client_id, $redirect_uri, $expires, $scope = NULL) {
  208. $r = q("INSERT INTO auth_codes
  209. (id, client_id, redirect_uri, expires, scope) VALUES
  210. ('%s', '%s', '%s', %d, '%s')",
  211. dbesc($code),
  212. dbesc($client_id),
  213. dbesc($redirect_uri),
  214. intval($expires),
  215. dbesc($scope));
  216. return $r;
  217. }
  218. }
  219. */