Created
October 25, 2023 15:16
-
-
Save garvinhicking/7195d2e0d20a300e7473750756daee5e to your computer and use it in GitHub Desktop.
Simple TYPO3 Authentication service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addService( | |
'your_service_key', | |
'auth', | |
\YourName\YourExtension\Services\MagicLinkAuthenticationService::class, | |
[ | |
'title' => 'Login via POST', | |
'description' => '', | |
'subtype' => 'getUserFE,authUserFE', | |
'available' => true, | |
'priority' => 10, | |
'quality' => 50, | |
'os' => '', | |
'exec' => '', | |
'className' => \YourName\YourExtension\Services\MagicLinkAuthenticationService::class | |
] | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace YourName\YourExtension\Services; | |
use TYPO3\CMS\Core\Session\Backend\Exception\SessionNotCreatedException; | |
use TYPO3\CMS\Core\Database\ConnectionPool; | |
use TYPO3\CMS\Core\Database\Connection; | |
use Psr\Http\Message\RequestInterface; | |
use TYPO3\CMS\Core\Database\Query\QueryBuilder; | |
use TYPO3\CMS\Core\Http\ServerRequest; | |
use TYPO3\CMS\Core\Http\ServerRequestFactory; | |
use TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication; | |
use TYPO3\CMS\Core\Utility\GeneralUtility; | |
class StupidAuthenticationService extends FrontendUserAuthentication | |
{ | |
const SERVICE_KEY = 'YourName\YourExtension\Services\StupidAuthenticationService'; | |
protected $info = []; | |
protected $subType = ''; | |
protected $loginData = ''; | |
protected $authInfo = []; | |
protected $feUserAuth; | |
protected RequestInterface $request; | |
public function getServiceKey(): string | |
{ | |
return self::SERVICE_KEY; | |
} | |
public function init(): bool | |
{ | |
return true; | |
} | |
public function initAuth( | |
string $subType, | |
array $loginData, | |
array $authInfo, | |
FrontendUserAuthentication $feUserAuth | |
): void { | |
$this->subType = $subType; | |
$this->loginData = $loginData; | |
$this->authInfo = $authInfo; | |
$this->feUserAuth = $feUserAuth; | |
} | |
public function getUser() | |
{ | |
$user = null; | |
if (isset($_POST['mySecretParameter'])) { | |
$user = $this->fetchUserBy($_POST['mySecretParameter']); | |
} | |
return ($user) ? $user : false; | |
} | |
public function authUser(array $user): int | |
{ | |
if (isset($_POST['mySecretParameter'])) { | |
$user = $this->fetchUserBy($_POST['mySecretParameter']); | |
if ($user) { | |
return 200; | |
} | |
} | |
// Return 100. User is NOT logged in yet, but further services may still try to authenticate the user. | |
return 100; | |
} | |
protected function fetchUserBy($uriParameter): ?array | |
{ | |
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class) | |
->getQueryBuilderForTable($this->user_table); | |
$uriParameter = | |
$result = $queryBuilder | |
->select('*') | |
->from($this->user_table) | |
->where( | |
$queryBuilder->expr()->eq('uid', $queryBuilder->createNamedParameter($uriParameter, Connection::PARAM_INT)) | |
) | |
->setMaxResults(1) | |
->executeQuery(); | |
return ($result->rowCount() > 0) ? $result->fetchAllAssociative()[0] : null; | |
} | |
protected function getRequest(): ServerRequest | |
{ | |
return $GLOBALS['TYPO3_REQUEST'] ?? ServerRequestFactory::fromGlobals(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment