Skip to content

Commit

Permalink
Add support for TYPO3 v10
Browse files Browse the repository at this point in the history
Resolves: codeFareith#183
MohsinQK authored and xperseguers committed Feb 28, 2022

Unverified

This user has not yet uploaded their public signing key.
1 parent 6e34453 commit e54bc01
Showing 11 changed files with 65 additions and 75 deletions.
4 changes: 1 addition & 3 deletions Classes/Controller/Frontend/SetupController.php
Original file line number Diff line number Diff line change
@@ -33,7 +33,7 @@
use TYPO3\CMS\Extbase\Object\Exception as ObjectException;
use TYPO3\CMS\Extbase\Persistence\Exception\IllegalObjectTypeException;
use TYPO3\CMS\Extbase\Persistence\Exception\UnknownObjectException;
use TYPO3\CMS\Lang\LanguageService;
use TYPO3\CMS\Core\Localization\LanguageService;
use function get_class;
use function vsprintf;

@@ -93,8 +93,6 @@ public function __construct(
Context $context
)
{
parent::__construct();

$this->frontendUserRepository = $frontendUserRepository;
$this->qrCodeGenerator = $qrCodeGenerator;
$this->setupFormValidator = $setupFormValidator;
2 changes: 1 addition & 1 deletion Classes/Hook/FeLogin.php
Original file line number Diff line number Diff line change
@@ -18,7 +18,7 @@
use CodeFareith\CfGoogleAuthenticator\Utility\ExtensionBasicDataUtility;
use CodeFareith\CfGoogleAuthenticator\Utility\PathUtility;
use TYPO3\CMS\Core\Service\MarkerBasedTemplateService;
use TYPO3\CMS\Lang\LanguageService;
use TYPO3\CMS\Core\Localization\LanguageService;

/**
* Hook for the TYPO3 CMS extension 'felogin'
11 changes: 9 additions & 2 deletions Classes/Hook/TCEMain.php
Original file line number Diff line number Diff line change
@@ -54,7 +54,7 @@ class TCEMain
/**
* @noinspection MoreThanThreeArgumentsInspection
*
* @param mixed $fieldArray
* @param mixed $fieldArray
* @param string|int $id
*
* @throws MissingRequiredField
@@ -75,7 +75,14 @@ public function processDatamap_preProcessFieldArray(
$otpInFieldArray = $otpInPostData;
}

$preProcessFieldArrayDTO = $this->getPreProcessFieldArrayDTO($fieldArray, $table, (int) $id, $dataHandler);
$otpInFieldArray2 = &$fieldArray['tx_cfgoogleauthenticator_secret'];
$otpInPostData2 = $_POST['data']['be_users']['tx_cfgoogleauthenticator_secret'];

if ($otpInFieldArray2 === null && $otpInPostData2 !== null) {
$otpInFieldArray2 = $otpInPostData2;
}

$preProcessFieldArrayDTO = $this->getPreProcessFieldArrayDTO($fieldArray, $table, (int)$id, $dataHandler);
$result = $this->getGoogleAuthenticatorSetupHandler()->process($preProcessFieldArrayDTO);

$fieldArray = array_merge($fieldArray, $result);
36 changes: 19 additions & 17 deletions Classes/Hook/UserSettings.php
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@
use TYPO3\CMS\Fluid\View\StandaloneView;
use function sprintf;
use function vsprintf;
use TYPO3\CMS\Backend\Form\Element\AbstractFormElement;

/**
* Hook for the user settings
@@ -37,7 +38,7 @@
* @package CodeFareith\CfGoogleAuthenticator\Hook
* @since 1.0.0
*/
class UserSettings
class UserSettings extends AbstractFormElement
{
/*─────────────────────────────────────────────────────────────────────────────*\
Traits
@@ -71,21 +72,20 @@ class UserSettings
* @return string
* @throws Exception
*/
public function createSecretField(array $data): string
public function render(): array
{
$this->data = $data;

$result = $this->initializeResultArray();
$authenticationSecret = $this->getAuthenticationSecret();
$templateView = $this->initializeTemplateView();
$isEnabled = $this->isGoogleAuthenticatorEnabled();
$qrCodeUri = $this->getQrCodeGenerator()->generateUri($authenticationSecret);

$prefix = '';
if ($data['table'] !== null) {
$prefix .= sprintf('[%s]', $data['table']);
if ($this->data['tableName'] !== null) {
$prefix .= sprintf('[%s]', $this->data['tableName']);
}
if ($data['row']['uid'] !== null) {
$prefix .= sprintf('[%s]', (string)$data['row']['uid']);
if ($data['databaseRow']['uid'] !== null) {
$prefix .= sprintf('[%s]', (string)$this->data['databaseRow']['uid']);
}

$templateView->assignMultiple(
@@ -97,7 +97,9 @@ public function createSecretField(array $data): string
]
);

return $templateView->render();
$result['html'] = $templateView->render();

return $result;
}

private function initializeTemplateView(): StandaloneView
@@ -146,15 +148,15 @@ private function getLayer(): string
{
$layer = '';

if ($this->data['table'] === 'fe_users') {
if ($this->data['tableName'] === 'fe_users') {
$layer = 'Frontend';
} elseif ($this->data['table'] === 'be_users') {
} elseif ($this->data['tableName'] === 'be_users') {
$layer = 'Backend';
}

$dispatcher = GeneralUtility::makeInstance(Dispatcher::class);
$signalArguments = [
'table' => $this->data['table'],
'table' => $this->data['tableName'],
'layer' => $layer,
'caller' => $this,
];
@@ -169,7 +171,7 @@ private function getLayer(): string

private function getUsername(): string
{
return $this->data['row']['username'] ?? '';
return $this->data['databaseRow']['username'] ?? '';
}

/**
@@ -195,7 +197,7 @@ private function getAuthenticationSecret(): AuthenticationSecret
private function getSecretKey(): string
{
if ($this->isGoogleAuthenticatorEnabled()) {
$secretKey = (string) $this->data['row']['tx_cfgoogleauthenticator_secret'];
$secretKey = (string) $this->data['databaseRow']['tx_cfgoogleauthenticator_secret'];
} else {
$secretKey = Base32Utility::generateRandomString(16);
}
@@ -205,10 +207,10 @@ private function getSecretKey(): string

private function isGoogleAuthenticatorEnabled(): bool
{
if ($this->data['type'] === 'user' && !is_array($this->data['row'])) {
$this->data['row'] = $GLOBALS['BE_USER']->user;
if ($this->data['parameterArray']['fieldConf']['config']['type'] === 'user' && !is_array($this->data['databaseRow'])) {
$this->data['databaseRow'] = $GLOBALS['BE_USER']->user;
}
return (bool) $this->data['row']['tx_cfgoogleauthenticator_enabled'];
return (bool) $this->data['databaseRow']['tx_cfgoogleauthenticator_enabled'];
}

private function getQrCodeGenerator(): QrCodeGeneratorInterface
10 changes: 10 additions & 0 deletions Configuration/Extbase/Persistence/Classes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

return [
\CodeFareith\CfGoogleAuthenticator\Domain\Model\FrontendUser::class => [
'tableName' => 'fe_users',
],
\CodeFareith\CfGoogleAuthenticator\Domain\Model\BackendUser::class => [
'tableName' => 'be_users',
],
];
2 changes: 1 addition & 1 deletion Configuration/TCA/Overrides/be_users.php
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@ static function () {
),
'config' => [
'type' => 'user',
'userFunc' => \CodeFareith\CfGoogleAuthenticator\Hook\UserSettings::class . '->createSecretField',
'renderType' => 'TwoFactorAuth',
],
],
]
2 changes: 1 addition & 1 deletion Configuration/TCA/Overrides/fe_users.php
Original file line number Diff line number Diff line change
@@ -41,7 +41,7 @@ static function () {
),
'config' => [
'type' => 'user',
'userFunc' => \CodeFareith\CfGoogleAuthenticator\Hook\UserSettings::class . '->createSecretField',
'renderType' => 'TwoFactorAuth',
],
],
]
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
@@ -46,9 +46,9 @@
"rss": "https://github.com/codeFareith/cf_google_authenticator/commits/master.atom"
},
"require": {
"typo3/cms-core": "^8.7 || ^9.5",
"typo3/cms-reports": "^8.7 || ^9.5",
"typo3/cms-setup": "^8.7 || ^9.5",
"typo3/cms-core": "^9.5 || ^10.4",
"typo3/cms-reports": "^9.5 || ^10.4",
"typo3/cms-setup": "^9.5 || ^10.4",
"ext-json": "*"
},
"require-dev": {
32 changes: 11 additions & 21 deletions ext_emconf.php
Original file line number Diff line number Diff line change
@@ -1,47 +1,36 @@
<?php
/**
* Declaration file for TYPO3 CMS Extension 'cf_google_authenticator'
*
* This file contains a declaration of what this extension is and does for the
* Extension Manager.

/***************************************************************
* Extension Manager/Repository config file for ext "cf_google_authenticator".
*
* @author Robin 'codeFareith' von den Bergen <[email protected]>
* @copyright (c) 2018-2019 by Robin von den Bergen
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
* @version 1.0.0
* Auto generated 23-11-2020 17:42
*
* @link https://github.com/codeFareith/cf_google_authenticator
* @see https://www.fareith.de
* @see https://typo3.org
*/

/** @var string $_EXTKEY */
* Manual updates:
* Only the data in the array - everything else is removed by next
* writing. "version" and "dependencies" must not be touched!
***************************************************************/

$EM_CONF[$_EXTKEY] = [
'title' => '[codeFareith] Google Authenticator',
'description' => 'Enable Google 2FA (two factor authentication) for both, frontend- and backend accounts.',
'category' => 'misc',

'author' => 'Robin "codeFareith" von den Bergen',
'author_email' => '[email protected]',
'author_company' => '',

'state' => 'stable',
'version' => '1.2.4',

'uploadFolders' => false,
'createDirs' => '',
'clearCacheOnLoad' => true,

'constraints' => [
'depends' => [
'php' => '7.1-',
'typo3' => '8.7.0-9.5.99',
'typo3' => '9.5.0-10.4.99',
],
'conflicts' => [
],
'suggests' => [
'felogin' => '8.7.0-9.5.99',
'felogin' => '9.5.0-10.4.99',
],
],

@@ -56,3 +45,4 @@
],
],
];

17 changes: 9 additions & 8 deletions ext_localconf.php
Original file line number Diff line number Diff line change
@@ -26,12 +26,6 @@ static function ($_EXTKEY) {

$extConf = \CodeFareith\CfGoogleAuthenticator\Utility\ExtensionBasicDataUtility::getExtensionConfiguration();

$objectManager = \TYPO3\CMS\Core\Utility\GeneralUtility::makeInstance(
\TYPO3\CMS\Extbase\Object\ObjectManager::class
);
$adapterFactory = $objectManager->get(\CodeFareith\CfGoogleAuthenticator\Service\GoogleAuthenticationServiceAdapterFactory::class);
$adapter = $adapterFactory->create();

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addService(
$_EXTKEY,
'auth',
@@ -45,7 +39,7 @@ static function ($_EXTKEY) {
'quality' => 80,
'os' => '',
'exec' => '',
'className' => get_class($adapter),
'className' => \CodeFareith\CfGoogleAuthenticator\Service\CoreAuthenticationServiceAdapter::class,
]
);

@@ -95,7 +89,14 @@ static function ($_EXTKEY) {
['felogin']
['postProcContent']
[$_EXTKEY] = \CodeFareith\CfGoogleAuthenticator\Hook\FeLogin::class . '->createOneTimePasswordField';

// Register a node in ext_localconf.php
$GLOBALS['TYPO3_CONF_VARS']['SYS']['formEngine']['nodeRegistry'][1606376982] = [
'nodeName' => 'TwoFactorAuth',
'priority' => 40,
'class' => \CodeFareith\CfGoogleAuthenticator\Hook\UserSettings::class,
];
},
/** @var string $_EXTKEY */
$_EXTKEY
'cf_google_authenticator'
);
18 changes: 0 additions & 18 deletions ext_typoscript_setup.typoscript

This file was deleted.

0 comments on commit e54bc01

Please sign in to comment.