Authtokens.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2019 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OCA\Settings\Settings\Personal\Security;
  8. use OC\Authentication\Token\INamedToken;
  9. use OC\Authentication\Token\IProvider as IAuthTokenProvider;
  10. use OC\Authentication\Token\IToken;
  11. use OCP\AppFramework\Http\TemplateResponse;
  12. use OCP\AppFramework\Services\IInitialState;
  13. use OCP\Authentication\Exceptions\InvalidTokenException;
  14. use OCP\ISession;
  15. use OCP\IUserSession;
  16. use OCP\Session\Exceptions\SessionNotAvailableException;
  17. use OCP\Settings\ISettings;
  18. use function array_map;
  19. class Authtokens implements ISettings {
  20. /** @var IAuthTokenProvider */
  21. private $tokenProvider;
  22. /** @var ISession */
  23. private $session;
  24. /** @var IInitialState */
  25. private $initialState;
  26. /** @var string|null */
  27. private $uid;
  28. /** @var IUserSession */
  29. private $userSession;
  30. public function __construct(IAuthTokenProvider $tokenProvider,
  31. ISession $session,
  32. IUserSession $userSession,
  33. IInitialState $initialState,
  34. ?string $UserId) {
  35. $this->tokenProvider = $tokenProvider;
  36. $this->session = $session;
  37. $this->initialState = $initialState;
  38. $this->uid = $UserId;
  39. $this->userSession = $userSession;
  40. }
  41. public function getForm(): TemplateResponse {
  42. $this->initialState->provideInitialState(
  43. 'app_tokens',
  44. $this->getAppTokens()
  45. );
  46. $this->initialState->provideInitialState(
  47. 'can_create_app_token',
  48. $this->userSession->getImpersonatingUserID() === null
  49. );
  50. return new TemplateResponse('settings', 'settings/personal/security/authtokens');
  51. }
  52. public function getSection(): string {
  53. return 'security';
  54. }
  55. public function getPriority(): int {
  56. return 100;
  57. }
  58. private function getAppTokens(): array {
  59. $tokens = $this->tokenProvider->getTokenByUser($this->uid);
  60. try {
  61. $sessionId = $this->session->getId();
  62. } catch (SessionNotAvailableException $ex) {
  63. return [];
  64. }
  65. try {
  66. $sessionToken = $this->tokenProvider->getToken($sessionId);
  67. } catch (InvalidTokenException $ex) {
  68. return [];
  69. }
  70. return array_map(function (IToken $token) use ($sessionToken) {
  71. $data = $token->jsonSerialize();
  72. $data['canDelete'] = true;
  73. $data['canRename'] = $token instanceof INamedToken && $data['type'] !== IToken::WIPE_TOKEN;
  74. if ($sessionToken->getId() === $token->getId()) {
  75. $data['canDelete'] = false;
  76. $data['canRename'] = false;
  77. $data['current'] = true;
  78. }
  79. return $data;
  80. }, $tokens);
  81. }
  82. }