Consumer.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 default_0000-00-00%2000%3A00%3A00
  15. public $modified; // datetime() 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, 'default' => '0000-00-00 00:00:00', 'description' => 'date this record was created'),
  27. 'modified' => array('type' => 'datetime', 'not null' => true, 'default' => 'CURRENT_TIMESTAMP', '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. }