Skip to content

Commit

Permalink
feat: Two Factor API
Browse files Browse the repository at this point in the history
Signed-off-by: SebastianKrupinski <[email protected]>
  • Loading branch information
SebastianKrupinski committed Nov 23, 2024
1 parent 4740c18 commit 36674f5
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 0 deletions.
109 changes: 109 additions & 0 deletions core/Controller/TwoFactorApiController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?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\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 provider states
*
* @param array<string> $users collection of system user ids
*
* @return DataResponse<Http::STATUS_OK, array{userId: array{providerId: bool}}>

Check failure on line 35 in core/Controller/TwoFactorApiController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

MissingTemplateParam

core/Controller/TwoFactorApiController.php:35:13: MissingTemplateParam: OCP\AppFramework\Http\DataResponse has missing template params, expecting 3 (see https://psalm.dev/182)

Check failure on line 35 in core/Controller/TwoFactorApiController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidTemplateParam

core/Controller/TwoFactorApiController.php:35:13: InvalidTemplateParam: Extended template param S of OCP\AppFramework\Http\DataResponse<OC\Core\Controller\Http::STATUS_OK, array{userId: array{providerId: bool}}> expects type int, type OC\Core\Controller\Http::STATUS_OK given (see https://psalm.dev/183)

Check failure on line 35 in core/Controller/TwoFactorApiController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedDocblockClass

core/Controller/TwoFactorApiController.php:35:13: UndefinedDocblockClass: Docblock-defined class, interface or enum named OC\Core\Controller\Http does not exist (see https://psalm.dev/200)
*
* 200: user/provider states
*/
#[PublicPage]
#[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 providers
*
* @param array<string:array<string>> $users collection of system user ids and provider ids

Check failure on line 56 in core/Controller/TwoFactorApiController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidDocblock

core/Controller/TwoFactorApiController.php:56:12: InvalidDocblock: Saw : outside of object-like array in docblock for OC\Core\Controller\TwoFactorApiController::enable (see https://psalm.dev/008)
*
* @return DataResponse<Http::STATUS_OK, array{userId: array{providerId: bool}}>

Check failure on line 58 in core/Controller/TwoFactorApiController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

MissingTemplateParam

core/Controller/TwoFactorApiController.php:58:13: MissingTemplateParam: OCP\AppFramework\Http\DataResponse has missing template params, expecting 3 (see https://psalm.dev/182)

Check failure on line 58 in core/Controller/TwoFactorApiController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidTemplateParam

core/Controller/TwoFactorApiController.php:58:13: InvalidTemplateParam: Extended template param S of OCP\AppFramework\Http\DataResponse<OC\Core\Controller\Http::STATUS_OK, array{userId: array{providerId: bool}}> expects type int, type OC\Core\Controller\Http::STATUS_OK given (see https://psalm.dev/183)

Check failure on line 58 in core/Controller/TwoFactorApiController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

UndefinedDocblockClass

core/Controller/TwoFactorApiController.php:58:13: UndefinedDocblockClass: Docblock-defined class, interface or enum named OC\Core\Controller\Http does not exist (see https://psalm.dev/200)
*
* 200: user/provider states
*/
#[PublicPage]
#[ApiRoute(verb: 'POST', url: '/enable', root: '/twofactor')]
public function enable(array $users = []): DataResponse {
$states = [];
foreach ($users as $userId => $providers) {
$userObject = $this->userManager->get($userId);
if ($userObject !== null) {
if (is_array($providers)) {
foreach ($providers as $providerId) {
$this->tfManager->tryEnableProviderFor($providerId, $userObject);
}
}
$states[$userId] = $this->tfRegistry->getProviderStates($userObject);
}
}

return new DataResponse($states);
}

/**
* Disable two factor providers
*
* @param array<string:array<string>> $users collection of system user ids and provider ids

Check failure on line 84 in core/Controller/TwoFactorApiController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidDocblock

core/Controller/TwoFactorApiController.php:84:12: InvalidDocblock: Saw : outside of object-like array in docblock for OC\Core\Controller\TwoFactorApiController::disable (see https://psalm.dev/008)
*
* @return DataResponse<Http::STATUS_OK, array{userId: array{providerId: bool}}>

Check failure on line 86 in core/Controller/TwoFactorApiController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

MissingTemplateParam

core/Controller/TwoFactorApiController.php:86:13: MissingTemplateParam: OCP\AppFramework\Http\DataResponse has missing template params, expecting 3 (see https://psalm.dev/182)

Check failure on line 86 in core/Controller/TwoFactorApiController.php

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidTemplateParam

core/Controller/TwoFactorApiController.php:86:13: InvalidTemplateParam: Extended template param S of OCP\AppFramework\Http\DataResponse<OC\Core\Controller\Http::STATUS_OK, array{userId: array{providerId: bool}}> expects type int, type OC\Core\Controller\Http::STATUS_OK given (see https://psalm.dev/183)
*
* 200: user/provider states
*/
#[PublicPage]
#[ApiRoute(verb: 'POST', url: '/disable', root: '/twofactor')]
public function disable(array $users = []): DataResponse {
$states = [];
foreach ($users as $userId => $providers) {
$userObject = $this->userManager->get($userId);
if ($userObject !== null) {
if (is_array($providers)) {
foreach ($providers as $providerId) {
$this->tfManager->tryDisableProviderFor($providerId, $userObject);
}
}
$states[$userId] = $this->tfRegistry->getProviderStates($userObject);
}
}

return new DataResponse($states);
}

}
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1295,6 +1295,7 @@
'OC\\Core\\Controller\\TextProcessingApiController' => $baseDir . '/core/Controller/TextProcessingApiController.php',
'OC\\Core\\Controller\\TextToImageApiController' => $baseDir . '/core/Controller/TextToImageApiController.php',
'OC\\Core\\Controller\\TranslationApiController' => $baseDir . '/core/Controller/TranslationApiController.php',
'OC\\Core\\Controller\\TwoFactorApiController' => $baseDir . '/core/Controller/TwoFactorApiController.php',
'OC\\Core\\Controller\\TwoFactorChallengeController' => $baseDir . '/core/Controller/TwoFactorChallengeController.php',
'OC\\Core\\Controller\\UnifiedSearchController' => $baseDir . '/core/Controller/UnifiedSearchController.php',
'OC\\Core\\Controller\\UnsupportedBrowserController' => $baseDir . '/core/Controller/UnsupportedBrowserController.php',
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -1336,6 +1336,7 @@ class ComposerStaticInit749170dad3f5e7f9ca158f5a9f04f6a2
'OC\\Core\\Controller\\TextProcessingApiController' => __DIR__ . '/../../..' . '/core/Controller/TextProcessingApiController.php',
'OC\\Core\\Controller\\TextToImageApiController' => __DIR__ . '/../../..' . '/core/Controller/TextToImageApiController.php',
'OC\\Core\\Controller\\TranslationApiController' => __DIR__ . '/../../..' . '/core/Controller/TranslationApiController.php',
'OC\\Core\\Controller\\TwoFactorApiController' => __DIR__ . '/../../..' . '/core/Controller/TwoFactorApiController.php',
'OC\\Core\\Controller\\TwoFactorChallengeController' => __DIR__ . '/../../..' . '/core/Controller/TwoFactorChallengeController.php',
'OC\\Core\\Controller\\UnifiedSearchController' => __DIR__ . '/../../..' . '/core/Controller/UnifiedSearchController.php',
'OC\\Core\\Controller\\UnsupportedBrowserController' => __DIR__ . '/../../..' . '/core/Controller/UnsupportedBrowserController.php',
Expand Down

0 comments on commit 36674f5

Please sign in to comment.