Authtokens.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2019, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  7. *
  8. * @license GNU AGPL version 3 or any later version
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as
  12. * published by the Free Software Foundation, either version 3 of the
  13. * License, or (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. */
  24. namespace OCA\Settings\Settings\Personal\Security;
  25. use OC\Authentication\Token\INamedToken;
  26. use OC\Authentication\Token\IProvider as IAuthTokenProvider;
  27. use OC\Authentication\Token\IToken;
  28. use OCP\AppFramework\Http\TemplateResponse;
  29. use OCP\AppFramework\Services\IInitialState;
  30. use OCP\Authentication\Exceptions\InvalidTokenException;
  31. use OCP\ISession;
  32. use OCP\IUserSession;
  33. use OCP\Session\Exceptions\SessionNotAvailableException;
  34. use OCP\Settings\ISettings;
  35. use function array_map;
  36. class Authtokens implements ISettings {
  37. /** @var IAuthTokenProvider */
  38. private $tokenProvider;
  39. /** @var ISession */
  40. private $session;
  41. /** @var IInitialState */
  42. private $initialState;
  43. /** @var string|null */
  44. private $uid;
  45. /** @var IUserSession */
  46. private $userSession;
  47. public function __construct(IAuthTokenProvider $tokenProvider,
  48. ISession $session,
  49. IUserSession $userSession,
  50. IInitialState $initialState,
  51. ?string $UserId) {
  52. $this->tokenProvider = $tokenProvider;
  53. $this->session = $session;
  54. $this->initialState = $initialState;
  55. $this->uid = $UserId;
  56. $this->userSession = $userSession;
  57. }
  58. public function getForm(): TemplateResponse {
  59. $this->initialState->provideInitialState(
  60. 'app_tokens',
  61. $this->getAppTokens()
  62. );
  63. $this->initialState->provideInitialState(
  64. 'can_create_app_token',
  65. $this->userSession->getImpersonatingUserID() === null
  66. );
  67. return new TemplateResponse('settings', 'settings/personal/security/authtokens');
  68. }
  69. public function getSection(): string {
  70. return 'security';
  71. }
  72. public function getPriority(): int {
  73. return 100;
  74. }
  75. private function getAppTokens(): array {
  76. $tokens = $this->tokenProvider->getTokenByUser($this->uid);
  77. try {
  78. $sessionId = $this->session->getId();
  79. } catch (SessionNotAvailableException $ex) {
  80. return [];
  81. }
  82. try {
  83. $sessionToken = $this->tokenProvider->getToken($sessionId);
  84. } catch (InvalidTokenException $ex) {
  85. return [];
  86. }
  87. return array_map(function (IToken $token) use ($sessionToken) {
  88. $data = $token->jsonSerialize();
  89. $data['canDelete'] = true;
  90. $data['canRename'] = $token instanceof INamedToken && $data['type'] !== IToken::WIPE_TOKEN;
  91. if ($sessionToken->getId() === $token->getId()) {
  92. $data['canDelete'] = false;
  93. $data['canRename'] = false;
  94. $data['current'] = true;
  95. }
  96. return $data;
  97. }, $tokens);
  98. }
  99. }