TwoFactorApiController.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace OC\Core\Controller;
  8. use OC\Authentication\TwoFactorAuth\ProviderManager;
  9. use OCP\AppFramework\Http;
  10. use OCP\Authentication\TwoFactorAuth\IRegistry;
  11. use OCP\AppFramework\Http\Attribute\ApiRoute;
  12. use OCP\AppFramework\Http\Attribute\PublicPage;
  13. use OCP\AppFramework\Http\DataResponse;
  14. use OCP\IRequest;
  15. use OCP\IUserManager;
  16. class TwoFactorApiController extends \OCP\AppFramework\OCSController {
  17. public function __construct(
  18. string $appName,
  19. IRequest $request,
  20. private ProviderManager $tfManager,
  21. private IRegistry $tfRegistry,
  22. private IUserManager $userManager,
  23. ) {
  24. parent::__construct($appName, $request);
  25. }
  26. /**
  27. * Get two factor authentication provider states
  28. *
  29. * @param list<string> $users collection of system user ids
  30. *
  31. * @return DataResponse<Http::STATUS_OK, list{string: list{string: bool}}, array{}>
  32. *
  33. * 200: user/provider states
  34. */
  35. #[PublicPage]
  36. #[ApiRoute(verb: 'POST', url: '/state', root: '/twofactor')]
  37. public function state(array $users = []): DataResponse {
  38. $states = [];
  39. foreach ($users as $userId) {
  40. $userObject = $this->userManager->get($userId);
  41. if ($userObject !== null) {
  42. $states[$userId] = $this->tfRegistry->getProviderStates($userObject);
  43. }
  44. }
  45. return new DataResponse($states);
  46. }
  47. /**
  48. * Enable two factor authentication providers for specific user
  49. *
  50. * @param string $user system user identifier
  51. * @param list<string> $providers collection of TFA provider ids
  52. *
  53. * @return DataResponse<Http::STATUS_OK|Http::STATUS_NOT_FOUND, list{string: bool}, array{}>
  54. *
  55. * 200: provider states
  56. * 404: user not found
  57. */
  58. #[PublicPage]
  59. #[ApiRoute(verb: 'POST', url: '/enable', root: '/twofactor')]
  60. public function enable(string $user, array $providers = []): DataResponse {
  61. $userObject = $this->userManager->get($user);
  62. if ($userObject !== null) {
  63. if (is_array($providers)) {
  64. foreach ($providers as $providerId) {
  65. $this->tfManager->tryEnableProviderFor($providerId, $userObject);
  66. }
  67. }
  68. $state = $this->tfRegistry->getProviderStates($userObject);
  69. return new DataResponse($state);
  70. }
  71. return new DataResponse([], Http::STATUS_NOT_FOUND);
  72. }
  73. /**
  74. * Disable two factor authentication providers for specific user
  75. *
  76. * @param string $user system user identifier
  77. * @param list<string> $providers collection of TFA provider ids
  78. *
  79. * @return DataResponse<Http::STATUS_OK|Http::STATUS_NOT_FOUND, list{string: bool}, array{}>
  80. *
  81. * 200: provider states
  82. * 404: user not found
  83. */
  84. #[PublicPage]
  85. #[ApiRoute(verb: 'POST', url: '/disable', root: '/twofactor')]
  86. public function disable(string $user, array $providers = []): DataResponse {
  87. $userObject = $this->userManager->get($user);
  88. if ($userObject !== null) {
  89. if (is_array($providers)) {
  90. foreach ($providers as $providerId) {
  91. $this->tfManager->tryDisableProviderFor($providerId, $userObject);
  92. }
  93. }
  94. $state = $this->tfRegistry->getProviderStates($userObject);
  95. return new DataResponse($state);
  96. }
  97. return new DataResponse([], Http::STATUS_NOT_FOUND);
  98. }
  99. }