Skip to content
Merged
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
8 changes: 8 additions & 0 deletions config/app_local.example.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,14 @@
// 'clientSecret' => '####',
// ],

/**
* OTP authentication setup
*/
// 'Otp' => [
// 'send' => '/otp',
// 'users_skip_otp' => [], // array of usernames to skip OTP authentication
// ],

/**
* Pagination default settings
*
Expand Down
12 changes: 12 additions & 0 deletions config/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@
['_name' => 'login:oauth2']
);

// OTP.
$routes->connect(
'/otp',
['controller' => 'Login', 'action' => 'otp'],
['_name' => 'otp'],
);
$routes->connect(
'/otp/verify',
['controller' => 'Login', 'action' => 'otpVerify'],
['_name' => 'otp:verify'],
);

// Dashboard.
$routes->connect(
'/',
Expand Down
17 changes: 16 additions & 1 deletion locales/default.pot
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
msgid ""
msgstr ""
"Project-Id-Version: BEdita 4 \n"
"POT-Creation-Date: 2026-03-10 13:13:21 \n"
"POT-Creation-Date: 2026-03-24 09:25:54 \n"
"MIME-Version: 1.0 \n"
"Content-Transfer-Encoding: 8bit \n"
"Language-Team: BEdita I18N & I10N Team \n"
Expand Down Expand Up @@ -322,6 +322,9 @@ msgstr ""
msgid "External Auth"
msgstr ""

msgid "Failed to send OTP code. Please try again later."
msgstr ""

msgid "Failed to write file to disk"
msgstr ""

Expand Down Expand Up @@ -597,6 +600,9 @@ msgstr ""
msgid "Number of updated objects"
msgstr ""

msgid "OTP code is expired or invalid"
msgstr ""

msgid "Object Types"
msgstr ""

Expand Down Expand Up @@ -633,6 +639,9 @@ msgstr ""
msgid "On"
msgstr ""

msgid "One Time Password"
msgstr ""

msgid "Only my contents"
msgstr ""

Expand Down Expand Up @@ -783,6 +792,9 @@ msgstr ""
msgid "Request password"
msgstr ""

msgid "Resend OTP"
msgstr ""

msgid "Reset"
msgstr ""

Expand Down Expand Up @@ -993,6 +1005,9 @@ msgstr ""
msgid "Verified"
msgstr ""

msgid "Verify"
msgstr ""

msgid "Version"
msgstr ""

Expand Down
17 changes: 16 additions & 1 deletion locales/en_US/default.po
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
msgid ""
msgstr ""
"Project-Id-Version: BEdita Manager \n"
"POT-Creation-Date: 2026-03-10 13:13:21 \n"
"POT-Creation-Date: 2026-03-24 09:25:54 \n"
"PO-Revision-Date: \n"
"Last-Translator: \n"
"Language-Team: BEdita I18N & I10N Team \n"
Expand Down Expand Up @@ -325,6 +325,9 @@ msgstr ""
msgid "External Auth"
msgstr ""

msgid "Failed to send OTP code. Please try again later."
msgstr ""

msgid "Failed to write file to disk"
msgstr ""

Expand Down Expand Up @@ -600,6 +603,9 @@ msgstr ""
msgid "Number of updated objects"
msgstr ""

msgid "OTP code is expired or invalid"
msgstr ""

msgid "Object Types"
msgstr ""

Expand Down Expand Up @@ -636,6 +642,9 @@ msgstr ""
msgid "On"
msgstr ""

msgid "One Time Password"
msgstr ""

msgid "Only my contents"
msgstr ""

Expand Down Expand Up @@ -786,6 +795,9 @@ msgstr ""
msgid "Request password"
msgstr ""

msgid "Resend OTP"
msgstr ""

msgid "Reset"
msgstr ""

Expand Down Expand Up @@ -996,6 +1008,9 @@ msgstr ""
msgid "Verified"
msgstr ""

msgid "Verify"
msgstr ""

msgid "Version"
msgstr ""

Expand Down
17 changes: 16 additions & 1 deletion locales/it_IT/default.po
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
msgid ""
msgstr ""
"Project-Id-Version: BEdita Manager \n"
"POT-Creation-Date: 2026-03-10 13:13:21 \n"
"POT-Creation-Date: 2026-03-24 09:25:54 \n"
"PO-Revision-Date: \n"
"Last-Translator: \n"
"Language-Team: BEdita I18N & I10N Team \n"
Expand Down Expand Up @@ -327,6 +327,9 @@ msgstr "Esporta Filtrati"
msgid "External Auth"
msgstr "Autenticazioni esterne"

msgid "Failed to send OTP code. Please try again later."
msgstr "Codice OTP non inviato. Riprova più tardi."

msgid "Failed to write file to disk"
msgstr "Scrittura file su disco fallita"

Expand Down Expand Up @@ -605,6 +608,9 @@ msgstr "Numero di errori di accesso"
msgid "Number of updated objects"
msgstr "Numero di oggetti aggiornati"

msgid "OTP code is expired or invalid"
msgstr "Codice OTP è scaduto o non valido"

msgid "Object Types"
msgstr "Tipi di Oggetto"

Expand Down Expand Up @@ -641,6 +647,9 @@ msgstr "Vecchia Password"
msgid "On"
msgstr ""

msgid "One Time Password"
msgstr "Codice monouso"

msgid "Only my contents"
msgstr "Solo i miei contenuti"

Expand Down Expand Up @@ -793,6 +802,9 @@ msgstr "Rimuovi"
msgid "Request password"
msgstr "Reimposta password"

msgid "Resend OTP"
msgstr "Invia nuovamente OTP"

msgid "Reset"
msgstr "Resetta"

Expand Down Expand Up @@ -1004,6 +1016,9 @@ msgstr "Partita Iva"
msgid "Verified"
msgstr "Verificato"

msgid "Verify"
msgstr "Verifica"

msgid "Version"
msgstr "Versione"

Expand Down
4 changes: 4 additions & 0 deletions src/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use App\Event\TreeCacheEventHandler;
use App\Identifier\ApiIdentifier;
use App\Middleware\ConfigurationMiddleware;
use App\Middleware\OtpMiddleware;
use App\Middleware\ProjectMiddleware;
use App\Middleware\RecoveryMiddleware;
use App\Middleware\StatusMiddleware;
Expand Down Expand Up @@ -150,6 +151,9 @@ public function middleware($middlewareQueue): MiddlewareQueue
// Authentication middleware.
->add(new AuthenticationMiddleware($this))

// Otp middleware.
->add(new OtpMiddleware())

// Authentication middleware.
->add(new OAuth2Middleware())

Expand Down
95 changes: 94 additions & 1 deletion src/Controller/LoginController.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<?php
declare(strict_types=1);

/**
* BEdita, API-first content management framework
* Copyright 2022 ChannelWeb Srl, Chialab Srl
Expand All @@ -13,8 +15,11 @@
namespace App\Controller;

use App\Application;
use Cake\Core\Configure;
use Cake\Core\InstanceConfigTrait;
use Cake\Http\Response;
use Cake\Utility\Hash;
use Throwable;

/**
* Perform basic login and logout operations.
Expand All @@ -27,7 +32,7 @@ class LoginController extends AppController
* @inheritDoc
*/
protected $_defaultConfig = [
// Projects configuration files base path
'otp' => null,
'projectsPath' => CONFIG . 'projects' . DS,
];

Expand All @@ -38,6 +43,10 @@ public function initialize(): void
{
parent::initialize();
$this->Authentication->allowUnauthenticated(['login']);
$otpConfig = (array)Configure::read('Otp');
if (!empty($otpConfig)) {
$this->setConfig('otp', $otpConfig);
}
}

/**
Expand Down Expand Up @@ -82,6 +91,14 @@ protected function authRequest(): ?Response
if ($result->isValid()) {
// Setup current project name.
$this->setupCurrentProject();

$provider = $this->request->getParam('provider');
$username = $result->getData()['attributes']['username'];

if (empty($provider) && $this->otpEnabled($username)) {
return $this->redirect('/otp');
}

// Redirect.
$target = $this->Authentication->getLoginRedirect() ?? ['_name' => 'dashboard'];

Expand Down Expand Up @@ -160,4 +177,80 @@ public function handleFlashMessages(array $query): void
$this->getRequest()->getSession()->delete('Flash');
}
}

/**
* Otp verification page.
*
* @return \Cake\Http\Response|null
*/
public function otp(): ?Response
{
$this->getRequest()->allowMethod(['get']);
if (!$this->otpEnabled()) {
return $this->redirect('/login');
}
try {
$otpSession = (array)$this->getRequest()->getSession()->read('Otp');
$pending = (string)Hash::get($otpSession, 'pending');
$force = $this->getRequest()->getQuery('force', false);
if (!$pending || $force) {
// Send OTP code via API
$response = $this->apiClient->post(
$this->getConfig('otp.send'),
null,
['Content-Type' => 'application/json'],
);
$this->getRequest()->getSession()->write('Otp', [
'otp_code' => $response['data']['otp_code'],
'expires_at' => $response['data']['expires_at'],
'pending' => true,
]);
}
} catch (Throwable $e) {
$this->Flash->error(__('Failed to send OTP code. Please try again later.'));
}

return null;
}

/**
* Otp verification request.
*
* @return \Cake\Http\Response|null
*/
public function otpVerify(): ?Response
{
$this->getRequest()->allowMethod(['post']);
if (!$this->otpEnabled()) {
return $this->redirect('/login');
}
$requestOtpCode = (string)$this->getRequest()->getData('otp_code');
$sessionOtpData = $this->getRequest()->getSession()->read('Otp');
$sessionOtpCode = (string)Hash::get($sessionOtpData, 'otp_code');
$sessionExpiresAt = (string)Hash::get($sessionOtpData, 'expires_at');
if (empty($sessionOtpData) || $sessionOtpCode !== $requestOtpCode || strtotime($sessionExpiresAt) < time()) {
$reason = __('OTP code is expired or invalid');
$this->getRequest()->getSession()->delete('Otp');
$this->Flash->error(__($reason));

return $this->redirect('/otp');
}
$this->getRequest()->getSession()->delete('Otp');

return $this->redirect($this->Authentication->getLoginRedirect() ?? ['_name' => 'dashboard']);
}

/**
* Check otp is enabled
*
* @return bool
*/
protected function otpEnabled(?string $username = null): bool
{
$otpConfig = (array)$this->getConfig('otp');
$usersSkipOtp = (array)Hash::get($otpConfig, 'users_skip_otp', []);
$skip = $username !== null && in_array($username, $usersSkipOtp);

return !empty($otpConfig) && !$skip;
}
}
Loading
Loading