You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

122 lines
3.3 KiB

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