SimpleOAuthDataStore.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /* A very naive dbm-based oauth storage
  3. *
  4. * NOTE: This is for reference ONLY,
  5. * and contains, amongst others, a hole
  6. * where you can get the token secret
  7. * easily..
  8. */
  9. class SimpleOAuthDataStore extends OAuthDataStore {/*{{{*/
  10. private $dbh;
  11. function __construct($path = "oauth.gdbm") {/*{{{*/
  12. $this->dbh = dba_popen($path, 'c', 'gdbm');
  13. }/*}}}*/
  14. function __destruct() {/*{{{*/
  15. dba_close($this->dbh);
  16. }/*}}}*/
  17. function lookup_consumer($consumer_key) {/*{{{*/
  18. $rv = dba_fetch("consumer_$consumer_key", $this->dbh);
  19. if ($rv === FALSE) {
  20. return NULL;
  21. }
  22. $obj = unserialize($rv);
  23. if (!($obj instanceof OAuthConsumer)) {
  24. return NULL;
  25. }
  26. return $obj;
  27. }/*}}}*/
  28. function lookup_token($consumer, $token_type, $token) {/*{{{*/
  29. $rv = dba_fetch("${token_type}_${token}", $this->dbh);
  30. if ($rv === FALSE) {
  31. return NULL;
  32. }
  33. $obj = unserialize($rv);
  34. if (!($obj instanceof OAuthToken)) {
  35. return NULL;
  36. }
  37. return $obj;
  38. }/*}}}*/
  39. function lookup_nonce($consumer, $token, $nonce, $timestamp) {/*{{{*/
  40. if (dba_exists("nonce_$nonce", $this->dbh)) {
  41. return TRUE;
  42. } else {
  43. dba_insert("nonce_$nonce", "1", $this->dbh);
  44. return FALSE;
  45. }
  46. }/*}}}*/
  47. function new_token($consumer, $type="request") {/*{{{*/
  48. $key = md5(time());
  49. $secret = time() + time();
  50. $token = new OAuthToken($key, md5(md5($secret)));
  51. if (!dba_insert("${type}_$key", serialize($token), $this->dbh)) {
  52. throw new OAuthException("doooom!");
  53. }
  54. return $token;
  55. }/*}}}*/
  56. function new_request_token($consumer) {/*{{{*/
  57. return $this->new_token($consumer, "request");
  58. }/*}}}*/
  59. function new_access_token($token, $consumer) {/*{{{*/
  60. $token = $this->new_token($consumer, 'access');
  61. dba_delete("request_" . $token->key, $this->dbh);
  62. return $token;
  63. }/*}}}*/
  64. }/*}}}*/