-
-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Two Factor API #49443
base: master
Are you sure you want to change the base?
feat: Two Factor API #49443
Changes from 5 commits
36674f5
5b255be
e13f498
207f5cd
78e280f
643480f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,107 @@ | ||||||
<?php | ||||||
|
||||||
declare(strict_types=1); | ||||||
/** | ||||||
* SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors | ||||||
* SPDX-License-Identifier: AGPL-3.0-or-later | ||||||
*/ | ||||||
|
||||||
namespace OC\Core\Controller; | ||||||
|
||||||
use OC\Authentication\TwoFactorAuth\ProviderManager; | ||||||
use OCP\AppFramework\Http; | ||||||
use OCP\Authentication\TwoFactorAuth\IRegistry; | ||||||
use OCP\AppFramework\Http\Attribute\ApiRoute; | ||||||
use OCP\AppFramework\Http\Attribute\PublicPage; | ||||||
use OCP\AppFramework\Http\DataResponse; | ||||||
use OCP\IRequest; | ||||||
use OCP\IUserManager; | ||||||
|
||||||
class TwoFactorApiController extends \OCP\AppFramework\OCSController { | ||||||
public function __construct( | ||||||
string $appName, | ||||||
IRequest $request, | ||||||
private ProviderManager $tfManager, | ||||||
private IRegistry $tfRegistry, | ||||||
private IUserManager $userManager, | ||||||
) { | ||||||
parent::__construct($appName, $request); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Get two factor authentication provider states | ||||||
* | ||||||
* @param list<string> $users collection of system user ids | ||||||
* | ||||||
* @return DataResponse<Http::STATUS_OK, list{string: list{string: bool}}, array{}> | ||||||
* | ||||||
* 200: user/provider states | ||||||
*/ | ||||||
#[PublicPage] | ||||||
Check failure on line 40 in core/Controller/TwoFactorApiController.php GitHub Actions / static-code-analysisInvalidDocblock
|
||||||
#[ApiRoute(verb: 'POST', url: '/state', root: '/twofactor')] | ||||||
public function state(array $users = []): DataResponse { | ||||||
$states = []; | ||||||
foreach ($users as $userId) { | ||||||
$userObject = $this->userManager->get($userId); | ||||||
if ($userObject !== null) { | ||||||
$states[$userId] = $this->tfRegistry->getProviderStates($userObject); | ||||||
} | ||||||
} | ||||||
return new DataResponse($states); | ||||||
} | ||||||
|
||||||
/** | ||||||
* Enable two factor authentication providers for specific user | ||||||
* | ||||||
* @param string $user system user identifier | ||||||
* @param list<string> $providers collection of TFA provider ids | ||||||
* | ||||||
* @return DataResponse<Http::STATUS_OK|Http::STATUS_NOT_FOUND, list{string: bool}, array{}> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
* | ||||||
* 200: provider states | ||||||
* 404: user not found | ||||||
*/ | ||||||
#[PublicPage] | ||||||
Check failure on line 64 in core/Controller/TwoFactorApiController.php GitHub Actions / static-code-analysisInvalidDocblock
|
||||||
#[ApiRoute(verb: 'POST', url: '/enable', root: '/twofactor')] | ||||||
public function enable(string $user, array $providers = []): DataResponse { | ||||||
$userObject = $this->userManager->get($user); | ||||||
if ($userObject !== null) { | ||||||
if (is_array($providers)) { | ||||||
Check failure on line 69 in core/Controller/TwoFactorApiController.php GitHub Actions / static-code-analysisRedundantCondition
|
||||||
foreach ($providers as $providerId) { | ||||||
$this->tfManager->tryEnableProviderFor($providerId, $userObject); | ||||||
} | ||||||
} | ||||||
$state = $this->tfRegistry->getProviderStates($userObject); | ||||||
return new DataResponse($state); | ||||||
} | ||||||
return new DataResponse([], Http::STATUS_NOT_FOUND); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return empty list is illegal 😿
Suggested change
|
||||||
} | ||||||
|
||||||
/** | ||||||
* Disable two factor authentication providers for specific user | ||||||
* | ||||||
* @param string $user system user identifier | ||||||
* @param list<string> $providers collection of TFA provider ids | ||||||
* | ||||||
* @return DataResponse<Http::STATUS_OK|Http::STATUS_NOT_FOUND, list{string: bool}, array{}> | ||||||
* | ||||||
* 200: provider states | ||||||
* 404: user not found | ||||||
*/ | ||||||
#[PublicPage] | ||||||
Check failure on line 91 in core/Controller/TwoFactorApiController.php GitHub Actions / static-code-analysisInvalidDocblock
|
||||||
#[ApiRoute(verb: 'POST', url: '/disable', root: '/twofactor')] | ||||||
public function disable(string $user, array $providers = []): DataResponse { | ||||||
$userObject = $this->userManager->get($user); | ||||||
if ($userObject !== null) { | ||||||
if (is_array($providers)) { | ||||||
Check failure on line 96 in core/Controller/TwoFactorApiController.php GitHub Actions / static-code-analysisRedundantCondition
|
||||||
foreach ($providers as $providerId) { | ||||||
$this->tfManager->tryDisableProviderFor($providerId, $userObject); | ||||||
} | ||||||
} | ||||||
$state = $this->tfRegistry->getProviderStates($userObject); | ||||||
return new DataResponse($state); | ||||||
} | ||||||
return new DataResponse([], Http::STATUS_NOT_FOUND); | ||||||
} | ||||||
|
||||||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.