/www/include/lib_api_oauth2_access_tokens.php

https://github.com/straup/parallel-flickr · PHP · 381 lines · 222 code · 129 blank · 30 comment · 18 complexity · 98b3e65ee00e1a8da9cf0891feaa9011 MD5 · raw file

  1. <?php
  2. #################################################################
  3. # TO DO: put me in the config?
  4. # (20121103/straup)
  5. function api_oauth2_access_tokens_permissions_map($string_keys=0){
  6. $map = array(
  7. '0' => 'login',
  8. '1' => 'read',
  9. '2' => 'write',
  10. );
  11. if ($string_keys){
  12. $map = array_flip($map);
  13. }
  14. return $map;
  15. }
  16. #################################################################
  17. function api_oauth2_access_tokens_ttl_map($string_keys=0){
  18. $map = array(
  19. '0' => 'until I revoke it',
  20. '3600' => 'for one hour',
  21. '21600' => 'for six hours',
  22. '86400' => 'for a day',
  23. '604800' => 'for one week',
  24. '2592000' => 'for a month',
  25. );
  26. if ($string_keys){
  27. $map = array_flip($map);
  28. }
  29. return $map;
  30. }
  31. #################################################################
  32. function api_oauth2_access_tokens_is_valid_permission($perm, $str_perm=0){
  33. $map = api_oauth2_access_tokens_permissions_map($str_perm);
  34. return (isset($map[$perm])) ? 1 : 0;
  35. }
  36. #################################################################
  37. function api_oauth2_access_tokens_get_by_token($token){
  38. $cache_key = "oauth2_access_token_{$token}";
  39. $cache = cache_get($cache_key);
  40. if ($cache['ok']){
  41. return $cache['data'];
  42. }
  43. $enc_token = AddSlashes($token);
  44. $sql = "SELECT * FROM OAuth2AccessTokens WHERE access_token='{$enc_token}'";
  45. $rsp = db_fetch($sql);
  46. $row = db_single($rsp);
  47. if ($rsp['ok']){
  48. cache_set($cache_key, $row);
  49. }
  50. return $row;
  51. }
  52. #################################################################
  53. function api_oauth2_access_tokens_for_user(&$user, $more=array()){
  54. $enc_user = AddSlashes($user['id']);
  55. $sql = "SELECT * FROM OAuth2AccessTokens WHERE user_id='{$enc_user}' AND (expires=0 OR expires > UNIX_TIMESTAMP(NOW()))";
  56. if (features_is_enabled(array("api_site_keys", "api_site_tokens"))){
  57. $sql .= " AND api_key_role_id=0";
  58. }
  59. $sql .= " ORDER BY created DESC";
  60. $rsp = db_fetch_paginated($sql, $more);
  61. return $rsp;
  62. }
  63. #################################################################
  64. function api_oauth2_access_tokens_for_key(&$key, $more=array()){
  65. $enc_key = AddSlashes($key['id']);
  66. $sql = "SELECT * FROM OAuth2AccessTokens WHERE api_key_id='{$enc_key}' AND (expires=0 OR expires > UNIX_TIMESTAMP(NOW()))";
  67. if (features_is_enabled(array("api_site_keys", "api_site_tokens"))){
  68. # pretty sure we don't want to filter on this
  69. # but just in case... (20130711/straup)
  70. # $sql .= " AND api_key_role_id=0";
  71. }
  72. $sql .= " ORDER BY created DESC";
  73. $rsp = db_fetch_paginated($sql, $more);
  74. return $rsp;
  75. }
  76. #################################################################
  77. function api_oauth2_access_tokens_count_for_key(&$key){
  78. $more = array(
  79. 'per_page' => 1,
  80. );
  81. $rsp = api_oauth2_access_tokens_for_key($key, $more);
  82. return $rsp['pagination']['total_count'];
  83. }
  84. #################################################################
  85. function api_oauth2_access_tokens_get_for_user_and_key(&$user, &$key){
  86. $cache_key = "oauth2_access_token_uk_{$user['id']}_{$key['id']}";
  87. $cache = cache_get($cache_key);
  88. if ($cache['ok']){
  89. # return $cache['data'];
  90. }
  91. $enc_user = AddSlashes($user['id']);
  92. $enc_key = AddSlashes($key['id']);
  93. $sql = "SELECT * FROM OAuth2AccessTokens WHERE user_id='{$enc_user}' AND api_key_id='{$enc_key}' AND (expires=0 OR expires > UNIX_TIMESTAMP(NOW()))";
  94. $rsp = db_fetch($sql);
  95. $row = db_single($rsp);
  96. if ($rsp['ok']){
  97. cache_set($cache_key, $row);
  98. }
  99. return $row;
  100. }
  101. #################################################################
  102. function api_oauth2_access_tokens_create(&$key, &$user, $perms, $ttl=0){
  103. $id = dbtickets_create(64);
  104. $token = api_oauth2_access_tokens_generate_token();
  105. $now = time();
  106. $row = array(
  107. 'id' => $id,
  108. 'perms' => $perms,
  109. 'api_key_id' => $key['id'],
  110. 'user_id' => $user['id'],
  111. 'access_token' => $token,
  112. 'created' => $now,
  113. 'last_modified' => $now,
  114. );
  115. if (intval($ttl) > 0){
  116. $row['expires'] = $now + $ttl;
  117. }
  118. $insert = array();
  119. foreach ($row as $k => $v){
  120. $insert[$k] = AddSlashes($v);
  121. }
  122. $rsp = db_insert('OAuth2AccessTokens', $insert);
  123. if ($rsp['ok']){
  124. $rsp['token'] = $row;
  125. }
  126. return $rsp;
  127. }
  128. #################################################################
  129. function api_oauth2_access_tokens_update(&$token, $update){
  130. $update['last_modified'] = time();
  131. $insert = array();
  132. foreach ($update as $k => $v){
  133. $insert[$k] = AddSlashes($v);
  134. }
  135. $enc_id = AddSlashes($token['id']);
  136. $where = "id='{$enc_id}'";
  137. $rsp = db_update('OAuth2AccessTokens', $update, $where);
  138. if ($rsp['ok']){
  139. api_oauth2_access_tokens_purge_cache($token);
  140. $token = array_merge($token, $update);
  141. $rsp['token'] = $token;
  142. }
  143. return $rsp;
  144. }
  145. #################################################################
  146. # THERE IS NO UNDO...
  147. function api_oauth2_access_tokens_delete(&$token){
  148. $enc_id = AddSlashes($token['id']);
  149. $sql = "DELETE FROM OAuth2AccessTokens WHERE id='{$enc_id}'";
  150. $rsp = db_write($sql);
  151. if ($rsp['ok']){
  152. api_oauth2_access_tokens_purge_cache($token);
  153. }
  154. return $rsp;
  155. }
  156. #################################################################
  157. function api_oauth2_access_tokens_delete_for_key(&$key){
  158. $enc_key = AddSlashes($key['id']);
  159. $sql = "DELETE FROM OAuth2AccessTokens WHERE api_key_id='{$enc_key}'";
  160. # TO DO: purge caches - iterate over all the things?
  161. # (20121103/straup)
  162. $rsp = db_write($sql);
  163. return $rsp;
  164. }
  165. #################################################################
  166. function api_oauth2_access_tokens_purge_cache(&$token){
  167. $cache_keys = array(
  168. "oauth2_access_token_{$token['access_token']}",
  169. "oauth2_access_token_uk_{$token['user_id']}_{$token['api_key_id']}",
  170. );
  171. foreach ($cache_keys as $key){
  172. cache_unset($key);
  173. }
  174. }
  175. #################################################################
  176. function api_oauth2_access_tokens_generate_token(){
  177. $token = md5(random_string(100) . time());
  178. return $token;
  179. }
  180. #################################################################
  181. function api_oauth2_access_tokens_fetch_site_token($user=null){
  182. $now = time();
  183. $site_token = api_oauth2_access_tokens_get_site_token($user);
  184. if ($site_token['expires'] <= $now){
  185. $rsp = api_oauth2_access_tokens_delete($site_token);
  186. if ($rsp['ok']){
  187. $user_id = ($user) ? $user['id'] : 0;
  188. $cache_key = "oauth2_access_token_site_{$user_id}";
  189. cache_unset($cache_key);
  190. }
  191. $site_token = null;
  192. }
  193. # TO DO: error handling / reporting
  194. if (! $site_token){
  195. $rsp = api_oauth2_access_tokens_create_site_token($user);
  196. $site_token = $rsp['token'];
  197. }
  198. return $site_token;
  199. }
  200. #################################################################
  201. function api_oauth2_access_tokens_get_site_token($user=null){
  202. $user_id = ($user) ? $user['id'] : 0;
  203. $cache_key = "oauth2_access_token_site_{$user_id}";
  204. $cache = cache_get($cache_key);
  205. if ($cache['ok']){
  206. # return $cache['data'];
  207. }
  208. $site_key = api_keys_fetch_site_key();
  209. $enc_user = AddSlashes($user_id);
  210. $enc_key = AddSlashes($site_key['id']);
  211. $sql = "SELECT * FROM OAuth2AccessTokens WHERE user_id='{$enc_user}' AND api_key_id='{$enc_key}' AND (expires=0 OR expires > UNIX_TIMESTAMP(NOW()))";
  212. $rsp = db_fetch($sql);
  213. $row = db_single($rsp);
  214. if ($rsp['ok']){
  215. cache_set($cache_key, $row);
  216. }
  217. return $row;
  218. }
  219. #################################################################
  220. function api_oauth2_access_tokens_create_site_token($user=null){
  221. $site_key = api_keys_fetch_site_key();
  222. $id = dbtickets_create(64);
  223. $user_id = ($user) ? $user['id'] : 0;
  224. $token = api_oauth2_access_tokens_generate_token();
  225. $ttl = ($user) ? $GLOBALS['cfg']['api_site_tokens_user_ttl'] : $GLOBALS['cfg']['api_site_tokens_ttl'];
  226. $now = time();
  227. $expires = $now + $ttl;
  228. $perms_map = api_oauth2_access_tokens_permissions_map('string keys');
  229. $perms = ($user_id) ? $perms_map['write'] : $perms_map['login'];
  230. $row = array(
  231. 'id' => $id,
  232. 'perms' => $perms,
  233. 'api_key_id' => $site_key['id'],
  234. 'api_key_role_id' => $site_key['role_id'],
  235. 'user_id' => $user_id,
  236. 'access_token' => $token,
  237. 'created' => $now,
  238. 'last_modified' => $now,
  239. 'expires' => $expires,
  240. );
  241. $insert = array();
  242. foreach ($row as $k => $v){
  243. $insert[$k] = AddSlashes($v);
  244. }
  245. $rsp = db_insert('OAuth2AccessTokens', $insert);
  246. if ($rsp['ok']){
  247. $rsp['token'] = $row;
  248. }
  249. return $rsp;
  250. }
  251. #################################################################
  252. # the end