‪TYPO3CMS  ‪main
FrontendUserAuthenticationTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use GuzzleHttp\Cookie\SetCookie;
21 use PHPUnit\Framework\Attributes\Test;
22 use Psr\Log\NullLogger;
30 use TYPO3\TestingFramework\Core\Functional\Framework\Frontend\InternalRequest;
31 use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;
32 
33 final class ‪FrontendUserAuthenticationTest extends FunctionalTestCase
34 {
36 
37  private const ‪ROOT_PAGE_ID = 1;
38  protected const ‪LANGUAGE_PRESETS = [
39  'EN' => ['id' => 0, 'title' => 'English', 'locale' => 'en_US.UTF8'],
40  ];
41 
42  public function ‪setUp(): void
43  {
44  parent::setUp();
45  $this->importCSVDataSet(__DIR__ . '/../Fixtures/pages.csv');
47  'frontend_authentication',
48  $this->‪buildSiteConfiguration(self::ROOT_PAGE_ID, '/'),
49  );
50  $this->setUpFrontendRootPage(self::ROOT_PAGE_ID, ['EXT:frontend/Tests/Functional/Fixtures/TypoScript/page.typoscript']);
51  }
52 
53  #[Test]
54  public function ‪feSessionsAreNotStoredForAnonymousSessions(): void
55  {
56  $response = $this->executeFrontendSubRequest((new InternalRequest())->withPageId(self::ROOT_PAGE_ID));
57 
58  self::assertStringNotContainsString('fe_typo_user', $response->getHeaderLine('Set-Cookie'));
59  $this->assertCSVDataSet(__DIR__ . '/Fixtures/fe_sessions_empty.csv');
60  }
61 
62  #[Test]
64  {
65  $this->importCSVDataSet(__DIR__ . '/Fixtures/fe_users.csv');
66 
67  $normalizedParams = new ‪NormalizedParams(
68  [
69  'REQUEST_URI' => '/',
70  'HTTP_HOST' => 'localhost',
71  'DOCUMENT_ROOT' => ‪Environment::getPublicPath(),
72  'SCRIPT_FILENAME' => ‪Environment::getPublicPath() . '/index.php',
73  'SCRIPT_NAME' => '/index.php',
74  ],
75  ‪$GLOBALS['TYPO3_CONF_VARS']['SYS'],
76  ‪Environment::getPublicPath() . '/index.php',
78  );
79 
80  $nonce = ‪Nonce::create();
81  $requestToken = ‪RequestToken::create('core/user-auth/fe')->toHashSignedJwt($nonce);
82  $request = (new InternalRequest())
83  ->withPageId(self::ROOT_PAGE_ID)
84  ->withMethod('POST')
85  ->withParsedBody(
86  [
87  'user' => 'testuser',
88  'pass' => 'test',
89  'logintype' => 'login',
90  '__RequestToken' => $requestToken,
91  ]
92  )
93  ->withAttribute('normalizedParams', $normalizedParams)
94  ->withCookieParams([123 => 'bogus', 'typo3nonce_' . $nonce->getSigningIdentifier()->name => $nonce->toHashSignedJwt()]);
95 
96  $response = $this->executeFrontendSubRequest($request);
97 
98  self::assertStringContainsString('fe_typo_user', $response->getHeaderLine('Set-Cookie'));
99  $this->assertCSVDataSet(__DIR__ . '/Fixtures/fe_sessions_filled.csv');
100 
101  // Now check whether the existing session is retrieved by providing the retrieved JWT token in the cookie params.
102  $cookie = SetCookie::fromString($response->getHeaderLine('Set-Cookie'));
103  $request = (new ‪ServerRequest('http://localhost/'))
104  ->withAttribute('normalizedParams', $normalizedParams)
105  ->withCookieParams([$cookie->getName() => $cookie->getValue()]);
106 
107  $frontendUserAuthentication = new ‪FrontendUserAuthentication();
108  $frontendUserAuthentication->setLogger(new NullLogger());
109  $frontendUserAuthentication->start($request);
110 
111  self::assertNotNull($frontendUserAuthentication->user);
112  self::assertEquals('testuser', $frontendUserAuthentication->user['username']);
113  }
114 }
‪TYPO3\CMS\Frontend\Tests\Functional\Authentication\FrontendUserAuthenticationTest\LANGUAGE_PRESETS
‪const LANGUAGE_PRESETS
Definition: FrontendUserAuthenticationTest.php:37
‪TYPO3\CMS\Frontend\Tests\Functional\Authentication\FrontendUserAuthenticationTest\feSessionsAreNotStoredForAnonymousSessions
‪feSessionsAreNotStoredForAnonymousSessions()
Definition: FrontendUserAuthenticationTest.php:53
‪TYPO3\CMS\Frontend\Tests\Functional\Authentication\FrontendUserAuthenticationTest
Definition: FrontendUserAuthenticationTest.php:34
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait
Definition: SiteBasedTestTrait.php:37
‪TYPO3\CMS\Core\Core\Environment\getPublicPath
‪static getPublicPath()
Definition: Environment.php:187
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\writeSiteConfiguration
‪writeSiteConfiguration(string $identifier, array $site=[], array $languages=[], array $errorHandling=[])
Definition: SiteBasedTestTrait.php:50
‪TYPO3\CMS\Core\Tests\Functional\SiteHandling\SiteBasedTestTrait\buildSiteConfiguration
‪buildSiteConfiguration(int $rootPageId, string $base='')
Definition: SiteBasedTestTrait.php:88
‪TYPO3\CMS\Core\Security\RequestToken
Definition: RequestToken.php:26
‪TYPO3\CMS\Core\Security\Nonce\create
‪static create(int $length=self::MIN_BYTES)
Definition: Nonce.php:37
‪TYPO3\CMS\Frontend\Tests\Functional\Authentication\FrontendUserAuthenticationTest\setUp
‪setUp()
Definition: FrontendUserAuthenticationTest.php:41
‪TYPO3\CMS\Frontend\Tests\Functional\Authentication
Definition: FrontendUserAuthenticationTest.php:18
‪TYPO3\CMS\Core\Http\ServerRequest
Definition: ServerRequest.php:39
‪TYPO3\CMS\Core\Security\Nonce
Definition: Nonce.php:29
‪$GLOBALS
‪$GLOBALS['TYPO3_CONF_VARS']['EXTCONF']['adminpanel']['modules']
Definition: ext_localconf.php:25
‪TYPO3\CMS\Core\Core\Environment
Definition: Environment.php:41
‪TYPO3\CMS\Frontend\Authentication\FrontendUserAuthentication
Definition: FrontendUserAuthentication.php:33
‪TYPO3\CMS\Core\Security\RequestToken\create
‪static create(string $scope)
Definition: RequestToken.php:43
‪TYPO3\CMS\Frontend\Tests\Functional\Authentication\FrontendUserAuthenticationTest\ROOT_PAGE_ID
‪const ROOT_PAGE_ID
Definition: FrontendUserAuthenticationTest.php:36
‪TYPO3\CMS\Frontend\Tests\Functional\Authentication\FrontendUserAuthenticationTest\canCreateNewAndExistingSessionWithValidRequestToken
‪canCreateNewAndExistingSessionWithValidRequestToken()
Definition: FrontendUserAuthenticationTest.php:62
‪TYPO3\CMS\Core\Http\NormalizedParams
Definition: NormalizedParams.php:38