diff options
author | Lucas Azevedo <lhs_azevedo@hotmail.com> | 2023-08-25 02:07:57 -0300 |
---|---|---|
committer | Lucas Azevedo <lhs_azevedo@hotmail.com> | 2023-08-25 02:07:57 -0300 |
commit | fe9b9c1955cb33c5026928a9f753bb6bde6e65ab (patch) | |
tree | 923b13c0b4dc10426720bd734083ee4b9d569b72 /core/Command | |
parent | a49a220fca751ba946da0a1439429933ad56a93b (diff) | |
download | nextcloud-server-fe9b9c1955cb33c5026928a9f753bb6bde6e65ab.tar.gz nextcloud-server-fe9b9c1955cb33c5026928a9f753bb6bde6e65ab.zip |
Add last-used-before option
Signed-off-by: Lucas Azevedo <lhs_azevedo@hotmail.com>
Diffstat (limited to 'core/Command')
-rw-r--r-- | core/Command/User/AuthTokens/Delete.php | 72 |
1 files changed, 68 insertions, 4 deletions
diff --git a/core/Command/User/AuthTokens/Delete.php b/core/Command/User/AuthTokens/Delete.php index 928387f1cc6..830050c1bb9 100644 --- a/core/Command/User/AuthTokens/Delete.php +++ b/core/Command/User/AuthTokens/Delete.php @@ -22,10 +22,14 @@ */ namespace OC\Core\Command\User\AuthTokens; +use DateTimeImmutable; use OC\Core\Command\Base; use OC\Authentication\Token\IProvider; +use Symfony\Component\Console\Command\Command; +use Symfony\Component\Console\Exception\RuntimeException; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class Delete extends Base { @@ -40,17 +44,77 @@ class Delete extends Base { ->setName('user:auth-tokens:delete') ->setDescription('Deletes an authentication token') ->addArgument( - 'id', + 'uid', InputArgument::REQUIRED, + 'ID of the user to delete tokens for' + ) + ->addArgument( + 'id', + InputArgument::OPTIONAL, 'ID of the auth token to delete' + ) + ->addOption( + 'last-used-before', + null, + InputOption::VALUE_REQUIRED, + 'Delete tokens last used before a given date.' ); } protected function execute(InputInterface $input, OutputInterface $output): int { - $token = $this->tokenProvider->getTokenById($input->getArgument('id')); + $uid = $input->getArgument('uid'); + $id = $input->getArgument('id'); + $before = $input->getOption('last-used-before'); + + if ($before) { + if ($id) { + throw new RuntimeException('Option --last-used-before cannot be used with [<id>]'); + } + + return $this->deleteLastUsedBefore($uid, $before); + } + + if (!$id) { + throw new RuntimeException('Not enough arguments. Specify the token <id> or use the --last-used-before option.'); + } + return $this->deleteById($uid, $id); + } + + protected function deleteById(string $uid, string $id) { + $this->tokenProvider->invalidateTokenById($uid, $id); + + return Command::SUCCESS; + } + + protected function deleteLastUsedBefore(string $uid, string $before) { + $date = $this->parseDateOption($before); + if (!$date) { + throw new RuntimeException('Invalid date format. Acceptable formats are: ISO8601 (w/o fractions), "YYYY-MM-DD" and Unix time in seconds.'); + } + + $this->tokenProvider->invalidateLastUsedBefore($uid, $date->getTimestamp()); + + return Command::SUCCESS; + } + + /** + * @return \DateTimeImmutable|false + */ + protected function parseDateOption(string $input) { + $date = false; + + // Handle Unix timestamp + if (filter_var($input, FILTER_VALIDATE_INT)) { + return new DateTimeImmutable('@' . $input); + } - $this->tokenProvider->invalidateTokenById($token->getUID(), $token->getId()); + // ISO8601 + $date = DateTimeImmutable::createFromFormat(DateTimeImmutable::ATOM, $input); + if ($date) { + return $date; + } - return 0; + // YYYY-MM-DD + return DateTimeImmutable::createFromFormat('!Y-m-d', $input); } } |