Consumer.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. /**
  3. * Table Definition for consumer
  4. */
  5. require_once INSTALLDIR.'/classes/Memcached_DataObject.php';
  6. class Consumer extends Managed_DataObject
  7. {
  8. ###START_AUTOCODE
  9. /* the code below is auto generated do not remove the above tag */
  10. public $__table = 'consumer'; // table name
  11. public $consumer_key; // varchar(191) primary_key not_null not 255 because utf8mb4 takes more space
  12. public $consumer_secret; // varchar(191) not_null not 255 because utf8mb4 takes more space
  13. public $seed; // char(32) not_null
  14. public $created; // datetime not_null
  15. public $modified; // timestamp not_null default_CURRENT_TIMESTAMP
  16. /* the code above is auto generated do not remove the tag below */
  17. ###END_AUTOCODE
  18. public static function schemaDef()
  19. {
  20. return array(
  21. 'description' => 'OAuth consumer record',
  22. 'fields' => array(
  23. 'consumer_key' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'unique identifier, root URL'),
  24. 'consumer_secret' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'secret value'),
  25. 'seed' => array('type' => 'char', 'length' => 32, 'not null' => true, 'description' => 'seed for new tokens by this consumer'),
  26. 'created' => array('type' => 'datetime', 'not null' => true, 'description' => 'date this record was created'),
  27. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  28. ),
  29. 'primary key' => array('consumer_key'),
  30. );
  31. }
  32. static function generateNew()
  33. {
  34. $cons = new Consumer();
  35. $rand = common_random_hexstr(16);
  36. $cons->seed = $rand;
  37. $cons->consumer_key = md5(time() + $rand);
  38. $cons->consumer_secret = md5(md5(time() + time() + $rand));
  39. $cons->created = common_sql_now();
  40. return $cons;
  41. }
  42. /**
  43. * Delete a Consumer and related tokens and nonces
  44. *
  45. * XXX: Should this happen in an OAuthDataStore instead?
  46. *
  47. */
  48. function delete($useWhere=false)
  49. {
  50. // XXX: Is there any reason NOT to do this kind of cleanup?
  51. $this->_deleteTokens();
  52. $this->_deleteNonces();
  53. return parent::delete($useWhere);
  54. }
  55. function _deleteTokens()
  56. {
  57. $token = new Token();
  58. $token->consumer_key = $this->consumer_key;
  59. $token->delete();
  60. }
  61. function _deleteNonces()
  62. {
  63. $nonce = new Nonce();
  64. $nonce->consumer_key = $this->consumer_key;
  65. $nonce->delete();
  66. }
  67. }