Skip to content
Open
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
7 changes: 7 additions & 0 deletions src/auth/methods/AuthMethodInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ public function isActive(): bool;
*/
public function getSetupHtml(string $containerId): string;

/**
* Returns the data needed to render the authentication method’s setup form.
*
* @return array
*/
public function getSetupData(): array;
Comment thread
timkelty marked this conversation as resolved.
Outdated

/**
* Returns the HTML for the authentication method’s authentication form.
*
Expand Down
8 changes: 8 additions & 0 deletions src/auth/methods/BaseAuthMethod.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,14 @@ abstract class BaseAuthMethod extends Component implements AuthMethodInterface
*/
protected User $user;

/**
* @inheritdoc
*/
public function getSetupData(): array
{
return [];
}

/**
* @inheritdoc
*/
Expand Down
13 changes: 13 additions & 0 deletions src/auth/methods/TOTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,19 @@ public function isActive(): bool
return self::secretFromDb($this->user->id) !== null;
}

/**
* @inheritdoc
*/
public function getSetupData(): array
{
$secret = $this->secret();

return [
'secret' => trim($secret),
'qrCode' => $this->generateQrCode($secret),
];
}
Comment thread
Copilot marked this conversation as resolved.

/**
* @inheritdoc
*/
Expand Down
47 changes: 46 additions & 1 deletion src/controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace craft\controllers;

use Craft;
use craft\auth\methods\AuthMethodInterface;
use craft\auth\methods\RecoveryCodes;
use craft\auth\methods\TOTP;
use craft\helpers\Html;
Expand Down Expand Up @@ -96,6 +97,40 @@ public function actionMethodListingHtml(): ?Response
]);
}

/**
* Returns the data for available authentication methods.
*
* @return Response
*/
public function actionGetMethods(): Response
{
$this->requireAcceptsJson();

Comment thread
timkelty marked this conversation as resolved.
return $this->asJson([
Comment thread
timkelty marked this conversation as resolved.
'methods' => array_map(
fn(AuthMethodInterface $method) => $this->authMethodData($method),
Craft::$app->getAuth()->getAvailableMethods(),
),
]);
}

/**
* Returns the data needed to set up an authentication method.
*
* @return Response
*/
public function actionMethodSetupData(): Response
{
$this->requirePostRequest();
$this->requireAcceptsJson();
$this->requireElevatedSession();

$class = $this->request->getRequiredBodyParam('method');
$method = Craft::$app->getAuth()->getMethod($class);

return $this->asJson($method->getSetupData());
}

/**
* Remove auth type setup (for 2FA or Passkeys) from the database
*
Expand All @@ -105,7 +140,6 @@ public function actionMethodListingHtml(): ?Response
*/
public function actionRemoveMethod(): ?Response
{
$this->requireCpRequest();
$this->requirePostRequest();
$this->requireElevatedSession();

Comment thread
timkelty marked this conversation as resolved.
Expand Down Expand Up @@ -263,6 +297,17 @@ private function passkeyTableHtml(): string
]);
}

private function authMethodData(AuthMethodInterface $method): array
{
return [
'class' => $method::class,
'name' => $method::displayName(),
'description' => $method::description(),
'actionMenuItems' => $method->getActionMenuItems(),
'isActive' => $method->isActive(),
];
}

/**
* Generates new recovery codes.
*
Expand Down
Loading