Skip to content
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

Draft
wants to merge 8 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 107 additions & 0 deletions core/Controller/TwoFactorApiController.php
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{}>
SebastianKrupinski marked this conversation as resolved.
Show resolved Hide resolved
*
* 200: user/provider states
*/
#[PublicPage]

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

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidDocblock

core/Controller/TwoFactorApiController.php:40:2: InvalidDocblock: A list shape cannot describe a non-list in docblock for OC\Core\Controller\TwoFactorApiController::state (see https://psalm.dev/008)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[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 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{}>
SebastianKrupinski marked this conversation as resolved.
Show resolved Hide resolved
*
* 200: provider states
* 404: user not found
*/
#[PublicPage]

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

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidDocblock

core/Controller/TwoFactorApiController.php:64:2: InvalidDocblock: A list shape cannot describe a non-list in docblock for OC\Core\Controller\TwoFactorApiController::enable (see https://psalm.dev/008)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[PublicPage]

#[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

View workflow job for this annotation

GitHub Actions / static-code-analysis

RedundantCondition

core/Controller/TwoFactorApiController.php:69:8: RedundantCondition: Type list<string> for $providers is always array<array-key, mixed> (see https://psalm.dev/122)
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);
SebastianKrupinski marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* 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

View workflow job for this annotation

GitHub Actions / static-code-analysis

InvalidDocblock

core/Controller/TwoFactorApiController.php:91:2: InvalidDocblock: A list shape cannot describe a non-list in docblock for OC\Core\Controller\TwoFactorApiController::disable (see https://psalm.dev/008)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[PublicPage]

#[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

View workflow job for this annotation

GitHub Actions / static-code-analysis

RedundantCondition

core/Controller/TwoFactorApiController.php:96:8: RedundantCondition: Type list<string> for $providers is always array<array-key, mixed> (see https://psalm.dev/122)
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);
}

}
245 changes: 245 additions & 0 deletions core/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -7071,6 +7071,251 @@
}
}
},
"/ocs/v2.php/twofactor/state": {
"post": {
"operationId": "twofactor-state",
"summary": "Get Two Factor state of specific users",
"tags": [
"twofactor"
],
"security": [
{
"bearer_auth": []
},
{
"basic_auth": []
}
],
"requestBody": {
"required": true,
"description": "State Request",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"users"
],
"properties": {
"users": {
"type": "array",
"description": "Collection of user identifiers",
"items": {
"type": "string"
},
"example": ["user1", "user2", "user3"]
}
}
}
}
}
},
"responses": {
"200": {
"description": "State Response",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {
"type": "object",
"example": {
"user1": {
"backup_codes": true,
"totp": false
},
"user2": {
"backup_codes": false,
"totp": true
}
}
}
}
}
}
}
}
}
}
}
}
},
"/ocs/v2.php/twofactor/enable": {
"post": {
"operationId": "twofactor-enable",
"summary": "Enable Two Factor Authentication for a specific user and specific TFA provider. Not all TFA providers can be enabled this way, some require user interaction",
"tags": [
"twofactor"
],
"security": [
{
"bearer_auth": []
},
{
"basic_auth": []
}
],
"requestBody": {
"required": true,
"description": "Enable Request",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"users"
],
"properties": {
"user": {
"type": "string",
"description": "System user identifier"
},
"providers": {
"type": "array",
"description": "Collection of provider identifiers",
"items": {
"type": "string"
},
"example": ["backup_codes", "totp"]
}
}
}
}
}
},
"responses": {
"200": {
"description": "Enable Response",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {
"type": "object",
"example": {
"backup_codes": true,
"totp": false
}
}
}
}
}
}
}
}
}
}
}
},
"/ocs/v2.php/twofactor/disable": {
"post": {
"operationId": "twofactor-disable",
"summary": "Disable Two Factor Authentication for a specific user and specific TFA provider",
"tags": [
"twofactor"
],
"security": [
{
"bearer_auth": []
},
{
"basic_auth": []
}
],
"requestBody": {
"required": true,
"description": "Disable Request",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"users"
],
"properties": {
"user": {
"type": "string",
"description": "System user identifier"
},
"providers": {
"type": "array",
"description": "Collection of provider identifiers",
"items": {
"type": "string"
},
"example": ["backup_codes", "totp"]
}
}
}
}
}
},
"responses": {
"200": {
"description": "Disable Response",
"content": {
"application/json": {
"schema": {
"type": "object",
"required": [
"ocs"
],
"properties": {
"ocs": {
"type": "object",
"required": [
"meta",
"data"
],
"properties": {
"meta": {
"$ref": "#/components/schemas/OCSMeta"
},
"data": {
"type": "object",
"example": {
"backup_codes": true,
"totp": false
}
}
}
}
}
}
}
}
}
}
}
},
"/ocs/v2.php/search/providers": {
"get": {
"operationId": "unified_search-get-providers",
Expand Down
1 change: 1 addition & 0 deletions lib/composer/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -1296,6 +1296,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 @@ -1337,6 +1337,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
Loading