‪TYPO3CMS  10.4
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 
23 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
24 
28 class ‪AuthenticationServiceTest extends UnitTestCase
29 {
33  protected ‪$resetSingletonInstances = true;
34 
40  public function ‪processLoginDataProvider(): array
41  {
42  return [
43  'Backend login with securityLevel "normal"' => [
44  'normal',
45  [
46  'status' => 'login',
47  'uname' => 'admin',
48  'uident' => 'password',
49  ],
50  [
51  'status' => 'login',
52  'uname' => 'admin',
53  'uident' => 'password',
54  'uident_text' => 'password',
55  ]
56  ],
57  'Frontend login with securityLevel "normal"' => [
58  'normal',
59  [
60  'status' => 'login',
61  'uname' => 'admin',
62  'uident' => 'password',
63  ],
64  [
65  'status' => 'login',
66  'uname' => 'admin',
67  'uident' => 'password',
68  'uident_text' => 'password',
69  ]
70  ],
71  'Frontend login with securityLevel "normal" and spaced passwords removes spaces' => [
72  'normal',
73  [
74  'status' => 'login',
75  'uname' => 'admin ',
76  'uident' => ' my password ',
77  ],
78  [
79  'status' => 'login',
80  'uname' => 'admin',
81  'uident' => 'my password',
82  'uident_text' => 'my password',
83  ]
84  ],
85  ];
86  }
87 
92  public function ‪processLoginReturnsCorrectData($passwordSubmissionStrategy, $loginData, $expectedProcessedData): void
93  {
94  $subject = new ‪AuthenticationService();
95  // Login data is modified by reference
96  $subject->processLoginData($loginData, $passwordSubmissionStrategy);
97  self::assertEquals($expectedProcessedData, $loginData);
98  }
99 
104  {
105  $subject = new ‪AuthenticationService();
106  $subject->initAuth('mode', ['uident_text' => '', 'uname' => 'user'], [], null);
107  self::assertSame(100, $subject->authUser([]));
108  }
109 
114  {
115  $subject = new ‪AuthenticationService();
116  $subject->initAuth('mode', ['uident_text' => 'foo', 'uname' => ''], [], null);
117  self::assertSame(100, $subject->authUser([]));
118  }
119 
123  public function ‪authUserThrowsExceptionIfUserTableIsNotSet(): void
124  {
125  $subject = new ‪AuthenticationService();
126  $subject->initAuth('mode', ['uident_text' => 'password', 'uname' => 'user'], [], null);
127  $this->expectException(\RuntimeException::class);
128  $this->expectExceptionCode(1533159150);
129  $subject->authUser([]);
130  }
131 
136  {
137  $subject = new ‪AuthenticationService();
138  $pObjProphecy = $this->prophesize(AbstractUserAuthentication::class);
139  $pObjProphecy->loginType = 'BE';
140  $loggerProphecy = $this->prophesize(Logger::class);
141  $subject->setLogger($loggerProphecy->reveal());
142  $subject->initAuth(
143  'authUserBE',
144  [
145  'uident_text' => 'password',
146  'uname' => 'lolli'
147  ],
148  [
149  'db_user' => ['table' => 'be_users'],
150  'HTTP_HOST' => ''
151  ],
152  $pObjProphecy->reveal()
153  );
154  $dbUser = [
155  'password' => 'aPlainTextPassword',
156  'lockToDomain' => ''
157  ];
158  self::assertEquals(100, $subject->authUser($dbUser));
159  }
160 
164  public function ‪authUserReturns0IfPasswordDoesNotMatch(): void
165  {
166  $subject = new ‪AuthenticationService();
167  $pObjProphecy = $this->prophesize(AbstractUserAuthentication::class);
168  $pObjProphecy->loginType = 'BE';
169  $loggerProphecy = $this->prophesize(Logger::class);
170  $subject->setLogger($loggerProphecy->reveal());
171  $subject->initAuth(
172  'authUserBE',
173  [
174  'uident_text' => 'notMyPassword',
175  'uname' => 'lolli'
176  ],
177  [
178  'db_user' => ['table' => 'be_users'],
179  'HTTP_HOST' => '',
180  ],
181  $pObjProphecy->reveal()
182  );
183  $dbUser = [
184  // a phpass hash of 'myPassword'
185  'password' => '$P$C/2Vr3ywuuPo5C7cs75YBnVhgBWpMP1',
186  'lockToDomain' => ''
187  ];
188  self::assertSame(0, $subject->authUser($dbUser));
189  }
190 
194  public function ‪authUserReturns200IfPasswordMatch(): void
195  {
196  $subject = new ‪AuthenticationService();
197  $pObjProphecy = $this->prophesize(AbstractUserAuthentication::class);
198  $pObjProphecy->loginType = 'BE';
199  $loggerProphecy = $this->prophesize(Logger::class);
200  $subject->setLogger($loggerProphecy->reveal());
201  $subject->initAuth(
202  'authUserBE',
203  [
204  'uident_text' => 'myPassword',
205  'uname' => 'lolli'
206  ],
207  [
208  'db_user' => ['table' => 'be_users'],
209  'HTTP_HOST' => ''
210  ],
211  $pObjProphecy->reveal()
212  );
213  $dbUser = [
214  // an argon2i hash of 'myPassword'
215  'password' => '$argon2i$v=19$m=65536,t=16,p=1$eGpyelFZbkpRdXN3QVhsUA$rd4abz2fcuksGu3b3fipglQZtHbIy+M3XoIS+sNVSl4',
216  'lockToDomain' => ''
217  ];
218  self::assertSame(200, $subject->authUser($dbUser));
219  }
220 
225  {
226  $subject = new ‪AuthenticationService();
227  $pObjProphecy = $this->prophesize(AbstractUserAuthentication::class);
228  $pObjProphecy->loginType = 'BE';
229  $loggerProphecy = $this->prophesize(Logger::class);
230  $subject->setLogger($loggerProphecy->reveal());
231  $subject->initAuth(
232  'authUserBE',
233  [
234  'uident_text' => 'myPassword',
235  'uname' => 'lolli'
236  ],
237  [
238  'db_user' => [
239  'table' => 'be_users',
240  'username_column' => 'username',
241  ],
242  'REMOTE_HOST' => '',
243  'HTTP_HOST' => 'example.com',
244  ],
245  $pObjProphecy->reveal()
246  );
247  $dbUser = [
248  // an argon2i hash of 'myPassword'
249  'password' => '$argon2i$v=19$m=65536,t=16,p=2$LnUzc3ZISWJwQWlSbmpkYw$qD1sRsJFzkUmjcEaKzDeg6LtflwdTpo49VbH3tMeMXU',
250  'username' => 'lolli',
251  'lockToDomain' => 'not.example.com'
252  ];
253  self::assertSame(0, $subject->authUser($dbUser));
254  }
255 }
‪TYPO3\CMS\Core\Tests\Unit\Authentication\AuthenticationServiceTest\$resetSingletonInstances
‪bool $resetSingletonInstances
Definition: AuthenticationServiceTest.php:32
‪TYPO3\CMS\Core\Tests\Unit\Authentication\AuthenticationServiceTest\processLoginReturnsCorrectData
‪processLoginReturnsCorrectData($passwordSubmissionStrategy, $loginData, $expectedProcessedData)
Definition: AuthenticationServiceTest.php:91
‪TYPO3\CMS\Core\Tests\Unit\Authentication
Definition: AbstractUserAuthenticationTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Authentication\AuthenticationServiceTest\authUserReturns100IfUserSubmittedUsernameIsEmpty
‪authUserReturns100IfUserSubmittedUsernameIsEmpty()
Definition: AuthenticationServiceTest.php:112
‪TYPO3\CMS\Core\Tests\Unit\Authentication\AuthenticationServiceTest\authUserReturns200IfPasswordMatch
‪authUserReturns200IfPasswordMatch()
Definition: AuthenticationServiceTest.php:193
‪TYPO3\CMS\Core\Tests\Unit\Authentication\AuthenticationServiceTest
Definition: AuthenticationServiceTest.php:29
‪TYPO3\CMS\Core\Tests\Unit\Authentication\AuthenticationServiceTest\authUserReturns0IfPasswordDoesNotMatch
‪authUserReturns0IfPasswordDoesNotMatch()
Definition: AuthenticationServiceTest.php:163
‪TYPO3\CMS\Core\Tests\Unit\Authentication\AuthenticationServiceTest\authUserReturns0IfPasswordMatchButDomainLockDoesNotMatch
‪authUserReturns0IfPasswordMatchButDomainLockDoesNotMatch()
Definition: AuthenticationServiceTest.php:223
‪TYPO3\CMS\Core\Authentication\AuthenticationService
Definition: AuthenticationService.php:33
‪TYPO3\CMS\Core\Tests\Unit\Authentication\AuthenticationServiceTest\authUserReturns100IfSubmittedPasswordIsEmpty
‪authUserReturns100IfSubmittedPasswordIsEmpty()
Definition: AuthenticationServiceTest.php:102
‪TYPO3\CMS\Core\Tests\Unit\Authentication\AuthenticationServiceTest\authUserThrowsExceptionIfUserTableIsNotSet
‪authUserThrowsExceptionIfUserTableIsNotSet()
Definition: AuthenticationServiceTest.php:122
‪TYPO3\CMS\Core\Tests\Unit\Authentication\AuthenticationServiceTest\processLoginDataProvider
‪array processLoginDataProvider()
Definition: AuthenticationServiceTest.php:39
‪TYPO3\CMS\Core\Log\Logger
Definition: Logger.php:27
‪TYPO3\CMS\Core\Tests\Unit\Authentication\AuthenticationServiceTest\authUserThrowsExceptionIfPasswordInDbDoesNotResolveToAValidHash
‪authUserThrowsExceptionIfPasswordInDbDoesNotResolveToAValidHash()
Definition: AuthenticationServiceTest.php:134
‪TYPO3\CMS\Core\Authentication\AbstractUserAuthentication
Definition: AbstractUserAuthentication.php:51