Delete.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2023 Lucas Azevedo <lhs_azevedo@hotmail.com>
  4. *
  5. * @author Lucas Azevedo <lhs_azevedo@hotmail.com>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Core\Command\User\AuthTokens;
  24. use DateTimeImmutable;
  25. use OC\Authentication\Token\IProvider;
  26. use OC\Core\Command\Base;
  27. use Symfony\Component\Console\Command\Command;
  28. use Symfony\Component\Console\Exception\RuntimeException;
  29. use Symfony\Component\Console\Input\InputArgument;
  30. use Symfony\Component\Console\Input\InputInterface;
  31. use Symfony\Component\Console\Input\InputOption;
  32. use Symfony\Component\Console\Output\OutputInterface;
  33. class Delete extends Base {
  34. public function __construct(
  35. protected IProvider $tokenProvider,
  36. ) {
  37. parent::__construct();
  38. }
  39. protected function configure(): void {
  40. $this
  41. ->setName('user:auth-tokens:delete')
  42. ->setDescription('Deletes an authentication token')
  43. ->addArgument(
  44. 'uid',
  45. InputArgument::REQUIRED,
  46. 'ID of the user to delete tokens for'
  47. )
  48. ->addArgument(
  49. 'id',
  50. InputArgument::OPTIONAL,
  51. 'ID of the auth token to delete'
  52. )
  53. ->addOption(
  54. 'last-used-before',
  55. null,
  56. InputOption::VALUE_REQUIRED,
  57. 'Delete tokens last used before a given date.'
  58. );
  59. }
  60. protected function execute(InputInterface $input, OutputInterface $output): int {
  61. $uid = $input->getArgument('uid');
  62. $id = (int) $input->getArgument('id');
  63. $before = $input->getOption('last-used-before');
  64. if ($before) {
  65. if ($id) {
  66. throw new RuntimeException('Option --last-used-before cannot be used with [<id>]');
  67. }
  68. return $this->deleteLastUsedBefore($uid, $before);
  69. }
  70. if (!$id) {
  71. throw new RuntimeException('Not enough arguments. Specify the token <id> or use the --last-used-before option.');
  72. }
  73. return $this->deleteById($uid, $id);
  74. }
  75. protected function deleteById(string $uid, int $id): int {
  76. $this->tokenProvider->invalidateTokenById($uid, $id);
  77. return Command::SUCCESS;
  78. }
  79. protected function deleteLastUsedBefore(string $uid, string $before): int {
  80. $date = $this->parseDateOption($before);
  81. if (!$date) {
  82. throw new RuntimeException('Invalid date format. Acceptable formats are: ISO8601 (w/o fractions), "YYYY-MM-DD" and Unix time in seconds.');
  83. }
  84. $this->tokenProvider->invalidateLastUsedBefore($uid, $date->getTimestamp());
  85. return Command::SUCCESS;
  86. }
  87. /**
  88. * @return \DateTimeImmutable|false
  89. */
  90. protected function parseDateOption(string $input) {
  91. $date = false;
  92. // Handle Unix timestamp
  93. if (filter_var($input, FILTER_VALIDATE_INT)) {
  94. return new DateTimeImmutable('@' . $input);
  95. }
  96. // ISO8601
  97. $date = DateTimeImmutable::createFromFormat(DateTimeImmutable::ATOM, $input);
  98. if ($date) {
  99. return $date;
  100. }
  101. // YYYY-MM-DD
  102. return DateTimeImmutable::createFromFormat('!Y-m-d', $input);
  103. }
  104. }