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 OCP\AppFramework\Services\IInitialState;
  26. use OCP\IUserSession;
  27. use function array_map;
  28. use OC\Authentication\Exceptions\InvalidTokenException;
  29. use OC\Authentication\Token\INamedToken;
  30. use OC\Authentication\Token\IProvider as IAuthTokenProvider;
  31. use OC\Authentication\Token\IToken;
  32. use OCP\AppFramework\Http\TemplateResponse;
  33. use OCP\ISession;
  34. use OCP\Session\Exceptions\SessionNotAvailableException;
  35. use OCP\Settings\ISettings;
  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;
  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. }