Skip to content

EspoCRM – Open Source CRM Application

2FA message, code max length, numeric input

Yuriibc10020ec3ff
diff --git a/application/Espo/Core/Authentication/Result/Data.php b/application/Espo/Core/Authentication/Result/Data.php
index 75014cd0fd..604a987bd0 100644
--- a/application/Espo/Core/Authentication/Result/Data.php
+++ b/application/Espo/Core/Authentication/Result/Data.php
@@ -36,6 +36,11 @@ use stdClass;
*/
class Data
{
+ /**
+ * @since 10.1.0
+ */
+ public const string PARAM_CODE_LENGTH = 'codeLength';
+
/** @var array<string, mixed> */
private array $data = [];
@@ -43,7 +48,7 @@ class Data
private function __construct(
private ?string $message = null,
private ?string $failReason = null,
- private ?string $view = null
+ private ?string $view = null,
) {}
public static function create(): self
diff --git a/application/Espo/Core/Authentication/TwoFactor/Totp/TotpLogin.php b/application/Espo/Core/Authentication/TwoFactor/Totp/TotpLogin.php
index ce07de4a09..a26a4270c5 100644
--- a/application/Espo/Core/Authentication/TwoFactor/Totp/TotpLogin.php
+++ b/application/Espo/Core/Authentication/TwoFactor/Totp/TotpLogin.php
@@ -47,7 +47,9 @@ use RuntimeException;
*/
class TotpLogin implements Login
{
- public const NAME = 'Totp';
+ public const string NAME = 'Totp';
+
+ private const int CODE_LENGTH = 6;
public function __construct(
private EntityManager $entityManager,
@@ -77,7 +79,8 @@ class TotpLogin implements Login
private function getResultData(): ResultData
{
- return ResultData::createWithMessage('enterTotpCode');
+ return ResultData::createWithMessage('enterTotpCode')
+ ->withDataItem(Result\Data::PARAM_CODE_LENGTH, self::CODE_LENGTH);
}
private function verifyCode(User $user, string $code): bool
diff --git a/client/res/templates/login-second-step.tpl b/client/res/templates/login-second-step.tpl
index b18b35c19e..8fbb7cb62a 100644
--- a/client/res/templates/login-second-step.tpl
+++ b/client/res/templates/login-second-step.tpl
@@ -3,7 +3,12 @@
<div id="login" class="panel panel-default block-center-sm">
<div class="panel-body">
<div>
- <p>{{message}}</p>
+ {{#if message}}
+ <div
+ class="complex-text margin-bottom-2x center-align small text-soft"
+ >{{complexText message}}</div>
+ {{/if}}
+
<form id="login-form">
<div class="form-group cell">
<label for="field-code">{{translate 'Code' scope='User'}}</label>
@@ -16,8 +21,9 @@
spellcheck="false"
tabindex="1"
autocomplete="one-time-code"
- maxlength="7"
+ maxlength="{{codeLength}}"
inputmode="numeric"
+ pattern="[0-9]*"
>
</div>
<div class="margin-top-2x">
diff --git a/client/src/views/login-second-step.js b/client/src/views/login-second-step.js
index 2cfc40a998..975d54eb2b 100644
--- a/client/src/views/login-second-step.js
+++ b/client/src/views/login-second-step.js
@@ -50,14 +50,6 @@ class LoginSecondStepView extends View {
*/
anotherUser = null
- /**
- * Response from the first step.
- *
- * @type {Object.<string, *>}
- * @private
- */
- loginData = null
-
/**
* Headers composed in the first step.
*
@@ -81,17 +73,41 @@ class LoginSecondStepView extends View {
*/
submitElement
+ /**
+ * @private
+ * @type {number}
+ */
+ static CODE_LENGTH = 7
+
+ /**
+ * @private
+ * @type {number}
+ */
+ codeLength
+
data() {
return {
message: this.message,
+ codeLength: this.codeLength,
};
}
setup() {
- this.message = this.translate(this.options.loginData.message, 'messages', 'User');
+ /**
+ * @type {{
+ * message?: string,
+ * data?: {
+ * codeLength?: number,
+ * },
+ * }}
+ */
+ const loginData = this.options.loginData ?? {};
+
+ this.message = this.translate(loginData.message, 'messages', 'User');
this.anotherUser = this.options.anotherUser || null;
this.headers = this.options.headers || {};
- this.loginData = this.options.loginData;
+
+ this.codeLength = loginData.data?.codeLength ?? LoginSecondStepView.CODE_LENGTH;
this.addHandler('submit', '#login-form', e => {
e.preventDefault();
@@ -99,6 +115,10 @@ class LoginSecondStepView extends View {
this.send();
});
+ this.addHandler('input', 'input[data-name="field-code"]', (_, /** HTMLInputElement */target) => {
+ target.value = target.value.replace(/[^0-9]/g, '');
+ });
+
this.addHandler('keydown', '', e => {
if (Utils.getKeyFromKeyEvent(e) === 'Control+Enter') {
e.preventDefault();
diff --git a/client/src/views/login.js b/client/src/views/login.js
index 03d736c030..83e1c34b38 100644
--- a/client/src/views/login.js
+++ b/client/src/views/login.js
@@ -292,7 +292,8 @@ class LoginView extends View {
this.enableForm();
if (xhr.status === 401) {
- const data = xhr.responseJSON || {};
+ const data = JSON.parse(xhr.responseText);
+
const statusReason = xhr.getResponseHeader('X-Status-Reason');
if (statusReason === 'second-step-required') {