‪TYPO3CMS  11.5
AuthenticationServiceTest.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 Prophecy\PhpUnit\ProphecyTrait;
24 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
25 
29 class ‪AuthenticationServiceTest extends UnitTestCase
30 {
31  use ProphecyTrait;
32 
36  protected ‪$resetSingletonInstances = true;
37 
43  public function ‪processLoginDataProvider(): array
44  {
45  return [
46  'Backend login with securityLevel "normal"' => [
47  'normal',
48  [
49  'status' => 'login',
50  'uname' => 'admin',
51  'uident' => 'password',
52  ],
53  [
54  'status' => 'login',
55  'uname' => 'admin',
56  'uident' => 'password',
57  'uident_text' => 'password',
58  ],
59  ],
60  'Frontend login with securityLevel "normal"' => [
61  'normal',
62  [
63  'status' => 'login',
64  'uname' => 'admin',
65  'uident' => 'password',
66  ],
67  [
68  'status' => 'login',
69  'uname' => 'admin',
70  'uident' => 'password',
71  'uident_text' => 'password',
72  ],
73  ],
74  'Frontend login with securityLevel "normal" and spaced passwords removes spaces' => [
75  'normal',
76  [
77  'status' => 'login',
78  'uname' => 'admin ',
79  'uident' => ' my password ',
80  ],
81  [
82  'status' => 'login',
83  'uname' => 'admin',
84  'uident' => 'my password',
85  'uident_text' => 'my password',
86  ],
87  ],
88  ];
89  }
90 
95  public function ‪processLoginReturnsCorrectData($passwordSubmissionStrategy, $loginData, $expectedProcessedData): void
96  {
97  $subject = new ‪AuthenticationService();
98  // Login data is modified by reference
99  $subject->processLoginData($loginData, $passwordSubmissionStrategy);
100  self::assertEquals($expectedProcessedData, $loginData);
101  }
102 
107  {
108  $subject = new ‪AuthenticationService();
109  $subject->initAuth('mode', ['uident_text' => '', 'uname' => 'user'], [], null);
110  self::assertSame(100, $subject->authUser([]));
111  }
112 
117  {
118  $subject = new ‪AuthenticationService();
119  $subject->initAuth('mode', ['uident_text' => 'foo', 'uname' => ''], [], null);
120  self::assertSame(100, $subject->authUser([]));
121  }
122 
126  public function ‪authUserThrowsExceptionIfUserTableIsNotSet(): void
127  {
128  $subject = new ‪AuthenticationService();
129  $subject->initAuth('mode', ['uident_text' => 'password', 'uname' => 'user'], [], null);
130  $this->expectException(\RuntimeException::class);
131  $this->expectExceptionCode(1533159150);
132  $subject->authUser([]);
133  }
134 
139  {
140  $subject = new ‪AuthenticationService();
141  $userAuthenticationProphecy = $this->prophesize(AbstractUserAuthentication::class);
142  $userAuthentication = $userAuthenticationProphecy->reveal();
143  $userAuthentication->loginType = 'BE';
144  $loggerProphecy = $this->prophesize(Logger::class);
145  $subject->setLogger($loggerProphecy->reveal());
146  $subject->initAuth(
147  'authUserBE',
148  [
149  'uident_text' => 'password',
150  'uname' => 'lolli',
151  ],
152  [
153  'db_user' => ['table' => 'be_users'],
154  'HTTP_HOST' => '',
155  ],
156  $userAuthentication
157  );
158  $dbUser = [
159  'password' => 'aPlainTextPassword',
160  ];
161  self::assertEquals(100, $subject->authUser($dbUser));
162  }
163 
167  public function ‪authUserReturns0IfPasswordDoesNotMatch(): void
168  {
169  $subject = new ‪AuthenticationService();
170  $userAuthenticationProphecy = $this->prophesize(AbstractUserAuthentication::class);
171  $userAuthentication = $userAuthenticationProphecy->reveal();
172  $userAuthentication->loginType = 'BE';
173  $loggerProphecy = $this->prophesize(Logger::class);
174  $subject->setLogger($loggerProphecy->reveal());
175  $subject->initAuth(
176  'authUserBE',
177  [
178  'uident_text' => 'notMyPassword',
179  'uname' => 'lolli',
180  ],
181  [
182  'db_user' => ['table' => 'be_users'],
183  'HTTP_HOST' => '',
184  ],
185  $userAuthentication
186  );
187  $dbUser = [
188  // a phpass hash of 'myPassword'
189  'password' => '$P$C/2Vr3ywuuPo5C7cs75YBnVhgBWpMP1',
190  ];
191  self::assertSame(0, $subject->authUser($dbUser));
192  }
193 
197  public function ‪authUserReturns200IfPasswordMatch(): void
198  {
199  $subject = new ‪AuthenticationService();
200  $userAuthenticationProphecy = $this->prophesize(AbstractUserAuthentication::class);
201  $userAuthentication = $userAuthenticationProphecy->reveal();
202  $userAuthentication->loginType = 'BE';
203  $loggerProphecy = $this->prophesize(Logger::class);
204  $subject->setLogger($loggerProphecy->reveal());
205  $subject->initAuth(
206  'authUserBE',
207  [
208  'uident_text' => 'myPassword',
209  'uname' => 'lolli',
210  ],
211  [
212  'db_user' => ['table' => 'be_users'],
213  'HTTP_HOST' => '',
214  ],
215  $userAuthentication
216  );
217  $dbUser = [
218  // an argon2i hash of 'myPassword'
219  'password' => '$argon2i$v=19$m=65536,t=16,p=1$eGpyelFZbkpRdXN3QVhsUA$rd4abz2fcuksGu3b3fipglQZtHbIy+M3XoIS+sNVSl4',
220  ];
221  self::assertSame(200, $subject->authUser($dbUser));
222  }
223 }
‪TYPO3\CMS\Core\Tests\Unit\Authentication\AuthenticationServiceTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: AuthenticationServiceTest.php:34
‪TYPO3\CMS\Core\Tests\Unit\Authentication\AuthenticationServiceTest\processLoginReturnsCorrectData
‪processLoginReturnsCorrectData($passwordSubmissionStrategy, $loginData, $expectedProcessedData)
Definition: AuthenticationServiceTest.php:93
‪TYPO3\CMS\Core\Tests\Unit\Authentication
Definition: AbstractUserAuthenticationTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Authentication\AuthenticationServiceTest\authUserReturns100IfUserSubmittedUsernameIsEmpty
‪authUserReturns100IfUserSubmittedUsernameIsEmpty()
Definition: AuthenticationServiceTest.php:114
‪TYPO3\CMS\Core\Tests\Unit\Authentication\AuthenticationServiceTest\authUserReturns200IfPasswordMatch
‪authUserReturns200IfPasswordMatch()
Definition: AuthenticationServiceTest.php:195
‪TYPO3\CMS\Core\Tests\Unit\Authentication\AuthenticationServiceTest
Definition: AuthenticationServiceTest.php:30
‪TYPO3\CMS\Core\Tests\Unit\Authentication\AuthenticationServiceTest\authUserReturns0IfPasswordDoesNotMatch
‪authUserReturns0IfPasswordDoesNotMatch()
Definition: AuthenticationServiceTest.php:165
‪TYPO3\CMS\Core\Authentication\AuthenticationService
Definition: AuthenticationService.php:32
‪TYPO3\CMS\Core\Tests\Unit\Authentication\AuthenticationServiceTest\authUserReturns100IfSubmittedPasswordIsEmpty
‪authUserReturns100IfSubmittedPasswordIsEmpty()
Definition: AuthenticationServiceTest.php:104
‪TYPO3\CMS\Core\Tests\Unit\Authentication\AuthenticationServiceTest\authUserThrowsExceptionIfUserTableIsNotSet
‪authUserThrowsExceptionIfUserTableIsNotSet()
Definition: AuthenticationServiceTest.php:124
‪TYPO3\CMS\Core\Tests\Unit\Authentication\AuthenticationServiceTest\processLoginDataProvider
‪array processLoginDataProvider()
Definition: AuthenticationServiceTest.php:41
‪TYPO3\CMS\Core\Log\Logger
Definition: Logger.php:27
‪TYPO3\CMS\Core\Tests\Unit\Authentication\AuthenticationServiceTest\authUserThrowsExceptionIfPasswordInDbDoesNotResolveToAValidHash
‪authUserThrowsExceptionIfPasswordInDbDoesNotResolveToAValidHash()
Definition: AuthenticationServiceTest.php:136
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication
Definition: AbstractUserAuthentication.php:56