0

The situation:
I build an authentication service that uses Basic Authentication to check if the user exists on an external database and fetches some data. The users in question only exist on the external database.

The problem:
Typo3 needs to have an user entry in the fe_user table to login the user. So whenever this entry does not exist, the user cannot login.

What I want to do:
Create the user in the authentication service to avoid using a sql dump from the external database and ensure that synchronisation is possible.

The relevant code:

public function authUser(array $user) {
    $a_user = $this->login['uname'];
    $a_pwd = $this->login['uident_text'];
    $url = 'https://soliday.fluchtpunkt.at/api/queryMediaItems';
    $data = json_decode('{"language":"de-at"}');
    $basicAuth = base64_encode("$a_user:$a_pwd");

    // use key 'http' even if you send the request to https://...
    $options = array (
            'http' => array (
                    'header' => array(
                                "Content-Type: application/json",
                                "Accept: application/json",
                                "Authorization: Basic {$basicAuth}"
                            ),
                    'method' => 'POST',
                    'content' => '{"language":"de-at"}'
            ) 
    );
    $context = stream_context_create ( $options );
    $result = file_get_contents ($url, false, $context);
    $response = gzdecode($result);
    $checkUser = $this->fetchUserRecord ( $this->login ['uname'] );
    if (!is_array($checkUser)&& $result!== FALSE) {
        $this->createUser();
    }
    // failure
    if ($result === FALSE) {
        return static::STATUS_AUTHENTICATION_FAILURE_BREAK;
    }
    $this->processData($response);

    // success
    return static::STATUS_AUTHENTICATION_SUCCESS_BREAK;
}

public function createUser() {
    $username = $this->login ['uname'];
    $password = $this->login ['uident_text'];
    $record = $GLOBALS ['TYPO3_DB']->exec_SELECTgetSingleRow ( '*', 'fe_users', "username = '" . $username . "' AND disable = 0 AND deleted = 0" );
    if (! $record) {
        // user has no DB record (yet), create one using defaults registered in extension config
        // password is not important, username is set to the user's input
        $record = array (
                'username' => $username,
                'password' => $password,
                'name' => '',
                'email' => '',
                'disable' => '0',
                'deleted' => '0',
                'pid' => $this->config ['storagePid'],
                'usergroup' => $this->config ['addUsersToGroups'],
                'tstamp' => time () 
        );
        if (t3lib_extMgm::isLoaded ( 'extbase' )) {
            $record ['tx_extbase_type'] = $this->config ['recordType'];
        }
        $GLOBALS ['TYPO3_DB']->exec_INSERTquery ( 'fe_users', $record );
        $uid = $GLOBALS ['TYPO3_DB']->sql_insert_id ();
        $record = $GLOBALS ['TYPO3_DB']->exec_SELECTgetSingleRow ( '*', 'fe_users', 'uid = ' . intval ( $uid ) );
    }
    $_SESSION [$this->sessionKey] ['user'] ['fe'] = $record;
}

the ext_localconf.php file:

   <?php
if (!defined('TYPO3_MODE')) {
    die ('Access denied.');
}

\TYPO3\CMS\Core\Utility\ExtensionManagementUtility::addService(
    $_EXTKEY,
    'auth' /* sv type */,
    'AuthService' /* sv key */,
    array(
        'title' => 'GET Authentication service',
        'description' => 'Authenticates users with GET request.',

        'subtype' => 'getUserFE, authUserFE',

        'available' => true,
        'priority' => 90,
        'quality' => 90,

        'os' => '',
        'exec' => '',

        'className' => Plaspack\professionalZoneLogin\Service\AuthService::class,
    )
);
4

2 回答 2

0

不确定它是否相关,但除非您使用的是 TYPO3 6 ,否则t3lib_extMgm应该是相关的。\TYPO3\CMS\Core\Utility\ExtensionManagementUtility

您还可以通过调用来查看是否收到任何 SQL 错误$GLOBALS['TYPO3_DB']->sql_error()

于 2018-07-10T13:58:27.737 回答
0

您应该使用自己的代码扩展 AuthenticationService,这里描述了这样做的方式https://docs.typo3.org/typo3cms/CoreApiReference/ApiOverview/Xclasses/Index.html

于 2018-07-10T11:42:13.860 回答