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

Feature/cms 1297 frontend login with mfa #15452

Open
wants to merge 5 commits into
base: 5.6
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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 raw data provided to the template rendered via [[getSetupHtml()]]
*
* @return array
*/
public function getSetupData(): array;

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

public function getSetupData(): array
{
return [];
}

/**
* @inheritdoc
*/
Expand Down
5 changes: 3 additions & 2 deletions src/auth/methods/RecoveryCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use craft\helpers\Json;
use craft\records\RecoveryCodes as RecoveryCodesRecord;
use craft\web\assets\recoverycodes\RecoveryCodesAsset;
use craft\web\View;
use DateTime;
use PragmaRX\Recovery\Recovery;
use yii\base\InvalidArgumentException;
Expand Down Expand Up @@ -62,7 +63,7 @@ public function getSetupHtml(string $containerId): string
new Craft.RecoveryCodesSetup($containerId);
JS, [$containerId]);

return $view->renderTemplate('_components/auth/methods/RecoveryCodes/setup.twig');
return $view->renderTemplate('_components/auth/methods/RecoveryCodes/setup.twig', $this->getSetupData(), View::TEMPLATE_MODE_CP);
}

/**
Expand All @@ -72,7 +73,7 @@ public function getAuthFormHtml(): string
{
$view = Craft::$app->getView();
$view->registerAssetBundle(RecoveryCodesAsset::class);
return $view->renderTemplate('_components/auth/methods/RecoveryCodes/form.twig');
return $view->renderTemplate('_components/auth/methods/RecoveryCodes/form.twig', [], View::TEMPLATE_MODE_CP);
}

/**
Expand Down
21 changes: 14 additions & 7 deletions src/auth/methods/TOTP.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,20 @@ public function isActive(): bool
return self::secretFromDb($this->user->id) !== null;
}

public function getSetupData(): array
{
$secret = $this->secret();
return [
'secret' => trim($secret),
'qrCode' => $this->generateQrCode($secret),
];
}

/**
* @inheritdoc
*/
public function getSetupHtml(string $containerId): string
{
$secret = $this->secret();
$totpFormId = sprintf('totp-form-%s', mt_rand());
$view = Craft::$app->getView();

Expand All @@ -92,12 +100,11 @@ public function getSetupHtml(string $containerId): string
$containerId,
]);

return $view->renderTemplate('_components/auth/methods/TOTP/setup.twig', [
'secret' => $secret,
'user' => $this->user,
'qrCode' => $this->generateQrCode($secret),
$templateData = array_merge($this->getSetupData(), [
'totpFormId' => $totpFormId,
], View::TEMPLATE_MODE_CP);
]);

return $view->renderTemplate('_components/auth/methods/TOTP/setup.twig', $templateData, View::TEMPLATE_MODE_CP);
}

/**
Expand All @@ -107,7 +114,7 @@ public function getAuthFormHtml(): string
{
$view = Craft::$app->getView();
$view->registerAssetBundle(TotpAsset::class);
return $view->renderTemplate('_components/auth/methods/TOTP/form.twig');
return $view->renderTemplate('_components/auth/methods/TOTP/form.twig', [], View::TEMPLATE_MODE_CP);
}

/**
Expand Down
2 changes: 0 additions & 2 deletions src/controllers/AuthController.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public function beforeAction($action): bool
return false;
}

$this->requireCpRequest();
;
return true;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

{% set formId = formId ?? "recovery-codes-form-#{random()}" %}

<form id="{{ formId }}">
<form id="{{ formId }}" method="post">
{{ actionInput('auth/verify-recovery-code') }}

{% if craft.app.config.general.enableCsrfProtection %}
{{ csrfInput() }}
{% endif %}

{% embed '_includes/forms/field.twig' with {
fieldClass: 'first',
label: 'Recovery Code'|t('app'),
Expand All @@ -12,6 +18,7 @@
<div class="flex flex-nowrap">
{{ forms.text({
class: 'code auth-recovery-code',
name: 'code',
maxlength: 13,
}) }}
{{ forms.submitButton({
Expand Down
9 changes: 8 additions & 1 deletion src/templates/_components/auth/methods/TOTP/form.twig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@

{% set formId = formId ?? "totp-form-#{random()}" %}

<form id="{{ formId }}">
<form id="{{ formId }}" method="post">
{{ actionInput('auth/verify-totp') }}

{% if craft.app.config.general.enableCsrfProtection %}
{{ csrfInput() }}
{% endif %}

{% embed '_includes/forms/field.twig' with {
fieldClass: 'first',
label: 'Verification Code'|t('app'),
Expand All @@ -13,6 +19,7 @@
<div class="flex flex-nowrap">
{{ forms.text({
class: 'code auth-totp-code',
name: 'code',
id: 'verification-code',
maxlength: 6,
}) }}
Expand Down
Loading