Consumer.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. // This file is part of GNU social - https://www.gnu.org/software/social
  3. //
  4. // GNU social is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // GNU social is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with GNU social. If not, see <http://www.gnu.org/licenses/>.
  16. /**
  17. * Table Definition for consumer
  18. */
  19. defined('GNUSOCIAL') || die();
  20. class Consumer extends Managed_DataObject
  21. {
  22. ###START_AUTOCODE
  23. /* the code below is auto generated do not remove the above tag */
  24. public $__table = 'consumer'; // table name
  25. public $consumer_key; // varchar(191) primary_key not_null not 255 because utf8mb4 takes more space
  26. public $consumer_secret; // varchar(191) not_null not 255 because utf8mb4 takes more space
  27. public $seed; // char(32) not_null
  28. public $created; // datetime()
  29. public $modified; // timestamp() not_null default_CURRENT_TIMESTAMP
  30. /* the code above is auto generated do not remove the tag below */
  31. ###END_AUTOCODE
  32. public static function schemaDef()
  33. {
  34. return array(
  35. 'description' => 'OAuth consumer record',
  36. 'fields' => array(
  37. 'consumer_key' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'unique identifier, root URL'),
  38. 'consumer_secret' => array('type' => 'varchar', 'length' => 191, 'not null' => true, 'description' => 'secret value'),
  39. 'seed' => array('type' => 'char', 'length' => 32, 'not null' => true, 'description' => 'seed for new tokens by this consumer'),
  40. 'created' => array('type' => 'datetime', 'description' => 'date this record was created'),
  41. 'modified' => array('type' => 'timestamp', 'not null' => true, 'description' => 'date this record was modified'),
  42. ),
  43. 'primary key' => array('consumer_key'),
  44. );
  45. }
  46. public static function generateNew()
  47. {
  48. $cons = new Consumer();
  49. $rand = common_random_hexstr(16);
  50. $cons->seed = $rand;
  51. $cons->consumer_key = md5(time() + $rand);
  52. $cons->consumer_secret = md5(md5(time() + time() + $rand));
  53. $cons->created = common_sql_now();
  54. return $cons;
  55. }
  56. /**
  57. * Delete a Consumer and related tokens and nonces
  58. *
  59. * XXX: Should this happen in an OAuthDataStore instead?
  60. *
  61. */
  62. public function delete($useWhere = false)
  63. {
  64. // XXX: Is there any reason NOT to do this kind of cleanup?
  65. $this->deleteTokens();
  66. $this->deleteNonces();
  67. return parent::delete($useWhere);
  68. }
  69. private function deleteTokens()
  70. {
  71. $token = new Token();
  72. $token->consumer_key = $this->consumer_key;
  73. $token->delete();
  74. }
  75. private function deleteNonces()
  76. {
  77. $nonce = new Nonce();
  78. $nonce->consumer_key = $this->consumer_key;
  79. $nonce->delete();
  80. }
  81. }