AuthTest.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Lukas Reschke <lukas@statuscode.ch>
  10. * @author Morris Jobke <hey@morrisjobke.de>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. * @author Thomas Müller <thomas.mueller@tmit.eu>
  13. * @author Vincent Petry <vincent@nextcloud.com>
  14. *
  15. * @license AGPL-3.0
  16. *
  17. * This code is free software: you can redistribute it and/or modify
  18. * it under the terms of the GNU Affero General Public License, version 3,
  19. * as published by the Free Software Foundation.
  20. *
  21. * This program is distributed in the hope that it will be useful,
  22. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  23. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  24. * GNU Affero General Public License for more details.
  25. *
  26. * You should have received a copy of the GNU Affero General Public License, version 3,
  27. * along with this program. If not, see <http://www.gnu.org/licenses/>
  28. *
  29. */
  30. namespace OCA\DAV\Tests\unit\Connector\Sabre;
  31. use OC\Authentication\TwoFactorAuth\Manager;
  32. use OC\User\Session;
  33. use OCP\IRequest;
  34. use OCP\ISession;
  35. use OCP\IUser;
  36. use OCP\Security\Bruteforce\IThrottler;
  37. use Sabre\DAV\Server;
  38. use Sabre\HTTP\RequestInterface;
  39. use Sabre\HTTP\ResponseInterface;
  40. use Test\TestCase;
  41. /**
  42. * Class AuthTest
  43. *
  44. * @package OCA\DAV\Tests\unit\Connector\Sabre
  45. * @group DB
  46. */
  47. class AuthTest extends TestCase {
  48. /** @var ISession */
  49. private $session;
  50. /** @var \OCA\DAV\Connector\Sabre\Auth */
  51. private $auth;
  52. /** @var Session */
  53. private $userSession;
  54. /** @var IRequest */
  55. private $request;
  56. /** @var Manager */
  57. private $twoFactorManager;
  58. /** @var IThrottler */
  59. private $throttler;
  60. protected function setUp(): void {
  61. parent::setUp();
  62. $this->session = $this->getMockBuilder(ISession::class)
  63. ->disableOriginalConstructor()->getMock();
  64. $this->userSession = $this->getMockBuilder(Session::class)
  65. ->disableOriginalConstructor()->getMock();
  66. $this->request = $this->getMockBuilder(IRequest::class)
  67. ->disableOriginalConstructor()->getMock();
  68. $this->twoFactorManager = $this->getMockBuilder(Manager::class)
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. $this->throttler = $this->getMockBuilder(IThrottler::class)
  72. ->disableOriginalConstructor()
  73. ->getMock();
  74. $this->auth = new \OCA\DAV\Connector\Sabre\Auth(
  75. $this->session,
  76. $this->userSession,
  77. $this->request,
  78. $this->twoFactorManager,
  79. $this->throttler
  80. );
  81. }
  82. public function testIsDavAuthenticatedWithoutDavSession(): void {
  83. $this->session
  84. ->expects($this->once())
  85. ->method('get')
  86. ->with('AUTHENTICATED_TO_DAV_BACKEND')
  87. ->willReturn(null);
  88. $this->assertFalse($this->invokePrivate($this->auth, 'isDavAuthenticated', ['MyTestUser']));
  89. }
  90. public function testIsDavAuthenticatedWithWrongDavSession(): void {
  91. $this->session
  92. ->expects($this->exactly(2))
  93. ->method('get')
  94. ->with('AUTHENTICATED_TO_DAV_BACKEND')
  95. ->willReturn('AnotherUser');
  96. $this->assertFalse($this->invokePrivate($this->auth, 'isDavAuthenticated', ['MyTestUser']));
  97. }
  98. public function testIsDavAuthenticatedWithCorrectDavSession(): void {
  99. $this->session
  100. ->expects($this->exactly(2))
  101. ->method('get')
  102. ->with('AUTHENTICATED_TO_DAV_BACKEND')
  103. ->willReturn('MyTestUser');
  104. $this->assertTrue($this->invokePrivate($this->auth, 'isDavAuthenticated', ['MyTestUser']));
  105. }
  106. public function testValidateUserPassOfAlreadyDAVAuthenticatedUser(): void {
  107. $user = $this->getMockBuilder(IUser::class)
  108. ->disableOriginalConstructor()
  109. ->getMock();
  110. $user->expects($this->exactly(1))
  111. ->method('getUID')
  112. ->willReturn('MyTestUser');
  113. $this->userSession
  114. ->expects($this->once())
  115. ->method('isLoggedIn')
  116. ->willReturn(true);
  117. $this->userSession
  118. ->expects($this->exactly(1))
  119. ->method('getUser')
  120. ->willReturn($user);
  121. $this->session
  122. ->expects($this->exactly(2))
  123. ->method('get')
  124. ->with('AUTHENTICATED_TO_DAV_BACKEND')
  125. ->willReturn('MyTestUser');
  126. $this->session
  127. ->expects($this->once())
  128. ->method('close');
  129. $this->assertTrue($this->invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword']));
  130. }
  131. public function testValidateUserPassOfInvalidDAVAuthenticatedUser(): void {
  132. $user = $this->getMockBuilder(IUser::class)
  133. ->disableOriginalConstructor()
  134. ->getMock();
  135. $user->expects($this->once())
  136. ->method('getUID')
  137. ->willReturn('MyTestUser');
  138. $this->userSession
  139. ->expects($this->once())
  140. ->method('isLoggedIn')
  141. ->willReturn(true);
  142. $this->userSession
  143. ->expects($this->once())
  144. ->method('getUser')
  145. ->willReturn($user);
  146. $this->session
  147. ->expects($this->exactly(2))
  148. ->method('get')
  149. ->with('AUTHENTICATED_TO_DAV_BACKEND')
  150. ->willReturn('AnotherUser');
  151. $this->session
  152. ->expects($this->once())
  153. ->method('close');
  154. $this->assertFalse($this->invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword']));
  155. }
  156. public function testValidateUserPassOfInvalidDAVAuthenticatedUserWithValidPassword(): void {
  157. $user = $this->getMockBuilder(IUser::class)
  158. ->disableOriginalConstructor()
  159. ->getMock();
  160. $user->expects($this->exactly(2))
  161. ->method('getUID')
  162. ->willReturn('MyTestUser');
  163. $this->userSession
  164. ->expects($this->once())
  165. ->method('isLoggedIn')
  166. ->willReturn(true);
  167. $this->userSession
  168. ->expects($this->exactly(2))
  169. ->method('getUser')
  170. ->willReturn($user);
  171. $this->session
  172. ->expects($this->exactly(2))
  173. ->method('get')
  174. ->with('AUTHENTICATED_TO_DAV_BACKEND')
  175. ->willReturn('AnotherUser');
  176. $this->userSession
  177. ->expects($this->once())
  178. ->method('logClientIn')
  179. ->with('MyTestUser', 'MyTestPassword', $this->request)
  180. ->willReturn(true);
  181. $this->session
  182. ->expects($this->once())
  183. ->method('set')
  184. ->with('AUTHENTICATED_TO_DAV_BACKEND', 'MyTestUser');
  185. $this->session
  186. ->expects($this->once())
  187. ->method('close');
  188. $this->assertTrue($this->invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword']));
  189. }
  190. public function testValidateUserPassWithInvalidPassword(): void {
  191. $this->userSession
  192. ->expects($this->once())
  193. ->method('isLoggedIn')
  194. ->willReturn(false);
  195. $this->userSession
  196. ->expects($this->once())
  197. ->method('logClientIn')
  198. ->with('MyTestUser', 'MyTestPassword')
  199. ->willReturn(false);
  200. $this->session
  201. ->expects($this->once())
  202. ->method('close');
  203. $this->assertFalse($this->invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword']));
  204. }
  205. public function testValidateUserPassWithPasswordLoginForbidden(): void {
  206. $this->expectException(\OCA\DAV\Connector\Sabre\Exception\PasswordLoginForbidden::class);
  207. $this->userSession
  208. ->expects($this->once())
  209. ->method('isLoggedIn')
  210. ->willReturn(false);
  211. $this->userSession
  212. ->expects($this->once())
  213. ->method('logClientIn')
  214. ->with('MyTestUser', 'MyTestPassword')
  215. ->will($this->throwException(new \OC\Authentication\Exceptions\PasswordLoginForbiddenException()));
  216. $this->session
  217. ->expects($this->once())
  218. ->method('close');
  219. $this->invokePrivate($this->auth, 'validateUserPass', ['MyTestUser', 'MyTestPassword']);
  220. }
  221. public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenForNonGet(): void {
  222. $request = $this->getMockBuilder(RequestInterface::class)
  223. ->disableOriginalConstructor()
  224. ->getMock();
  225. $response = $this->getMockBuilder(ResponseInterface::class)
  226. ->disableOriginalConstructor()
  227. ->getMock();
  228. $this->userSession
  229. ->expects($this->any())
  230. ->method('isLoggedIn')
  231. ->willReturn(true);
  232. $this->request
  233. ->expects($this->any())
  234. ->method('getMethod')
  235. ->willReturn('POST');
  236. $this->session
  237. ->expects($this->any())
  238. ->method('get')
  239. ->with('AUTHENTICATED_TO_DAV_BACKEND')
  240. ->willReturn(null);
  241. $user = $this->getMockBuilder(IUser::class)
  242. ->disableOriginalConstructor()
  243. ->getMock();
  244. $user->expects($this->any())
  245. ->method('getUID')
  246. ->willReturn('MyWrongDavUser');
  247. $this->userSession
  248. ->expects($this->any())
  249. ->method('getUser')
  250. ->willReturn($user);
  251. $this->request
  252. ->expects($this->once())
  253. ->method('passesCSRFCheck')
  254. ->willReturn(false);
  255. $expectedResponse = [
  256. false,
  257. "No 'Authorization: Basic' header found. Either the client didn't send one, or the server is misconfigured",
  258. ];
  259. $response = $this->auth->check($request, $response);
  260. $this->assertSame($expectedResponse, $response);
  261. }
  262. public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenAndCorrectlyDavAuthenticated(): void {
  263. $request = $this->getMockBuilder(RequestInterface::class)
  264. ->disableOriginalConstructor()
  265. ->getMock();
  266. $response = $this->getMockBuilder(ResponseInterface::class)
  267. ->disableOriginalConstructor()
  268. ->getMock();
  269. $this->userSession
  270. ->expects($this->any())
  271. ->method('isLoggedIn')
  272. ->willReturn(true);
  273. $this->request
  274. ->expects($this->any())
  275. ->method('getMethod')
  276. ->willReturn('PROPFIND');
  277. $this->request
  278. ->expects($this->any())
  279. ->method('isUserAgent')
  280. ->willReturn(false);
  281. $this->session
  282. ->expects($this->any())
  283. ->method('get')
  284. ->with('AUTHENTICATED_TO_DAV_BACKEND')
  285. ->willReturn('LoggedInUser');
  286. $user = $this->getMockBuilder(IUser::class)
  287. ->disableOriginalConstructor()
  288. ->getMock();
  289. $user->expects($this->any())
  290. ->method('getUID')
  291. ->willReturn('LoggedInUser');
  292. $this->userSession
  293. ->expects($this->any())
  294. ->method('getUser')
  295. ->willReturn($user);
  296. $this->request
  297. ->expects($this->once())
  298. ->method('passesCSRFCheck')
  299. ->willReturn(false);
  300. $this->auth->check($request, $response);
  301. }
  302. public function testAuthenticateAlreadyLoggedInWithoutTwoFactorChallengePassed(): void {
  303. $this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class);
  304. $this->expectExceptionMessage('2FA challenge not passed.');
  305. $request = $this->getMockBuilder(RequestInterface::class)
  306. ->disableOriginalConstructor()
  307. ->getMock();
  308. $response = $this->getMockBuilder(ResponseInterface::class)
  309. ->disableOriginalConstructor()
  310. ->getMock();
  311. $this->userSession
  312. ->expects($this->any())
  313. ->method('isLoggedIn')
  314. ->willReturn(true);
  315. $this->request
  316. ->expects($this->any())
  317. ->method('getMethod')
  318. ->willReturn('PROPFIND');
  319. $this->request
  320. ->expects($this->any())
  321. ->method('isUserAgent')
  322. ->willReturn(false);
  323. $this->session
  324. ->expects($this->any())
  325. ->method('get')
  326. ->with('AUTHENTICATED_TO_DAV_BACKEND')
  327. ->willReturn('LoggedInUser');
  328. $user = $this->getMockBuilder(IUser::class)
  329. ->disableOriginalConstructor()
  330. ->getMock();
  331. $user->expects($this->any())
  332. ->method('getUID')
  333. ->willReturn('LoggedInUser');
  334. $this->userSession
  335. ->expects($this->any())
  336. ->method('getUser')
  337. ->willReturn($user);
  338. $this->request
  339. ->expects($this->once())
  340. ->method('passesCSRFCheck')
  341. ->willReturn(true);
  342. $this->twoFactorManager->expects($this->once())
  343. ->method('needsSecondFactor')
  344. ->with($user)
  345. ->willReturn(true);
  346. $this->auth->check($request, $response);
  347. }
  348. public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenAndIncorrectlyDavAuthenticated(): void {
  349. $this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class);
  350. $this->expectExceptionMessage('CSRF check not passed.');
  351. $request = $this->getMockBuilder(RequestInterface::class)
  352. ->disableOriginalConstructor()
  353. ->getMock();
  354. $response = $this->getMockBuilder(ResponseInterface::class)
  355. ->disableOriginalConstructor()
  356. ->getMock();
  357. $this->userSession
  358. ->expects($this->any())
  359. ->method('isLoggedIn')
  360. ->willReturn(true);
  361. $this->request
  362. ->expects($this->any())
  363. ->method('getMethod')
  364. ->willReturn('PROPFIND');
  365. $this->request
  366. ->expects($this->any())
  367. ->method('isUserAgent')
  368. ->willReturn(false);
  369. $this->session
  370. ->expects($this->any())
  371. ->method('get')
  372. ->with('AUTHENTICATED_TO_DAV_BACKEND')
  373. ->willReturn('AnotherUser');
  374. $user = $this->getMockBuilder(IUser::class)
  375. ->disableOriginalConstructor()
  376. ->getMock();
  377. $user->expects($this->any())
  378. ->method('getUID')
  379. ->willReturn('LoggedInUser');
  380. $this->userSession
  381. ->expects($this->any())
  382. ->method('getUser')
  383. ->willReturn($user);
  384. $this->request
  385. ->expects($this->once())
  386. ->method('passesCSRFCheck')
  387. ->willReturn(false);
  388. $this->auth->check($request, $response);
  389. }
  390. public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenForNonGetAndDesktopClient(): void {
  391. $request = $this->getMockBuilder(RequestInterface::class)
  392. ->disableOriginalConstructor()
  393. ->getMock();
  394. $response = $this->getMockBuilder(ResponseInterface::class)
  395. ->disableOriginalConstructor()
  396. ->getMock();
  397. $this->userSession
  398. ->expects($this->any())
  399. ->method('isLoggedIn')
  400. ->willReturn(true);
  401. $this->request
  402. ->expects($this->any())
  403. ->method('getMethod')
  404. ->willReturn('POST');
  405. $this->request
  406. ->expects($this->any())
  407. ->method('isUserAgent')
  408. ->willReturn(true);
  409. $this->session
  410. ->expects($this->any())
  411. ->method('get')
  412. ->with('AUTHENTICATED_TO_DAV_BACKEND')
  413. ->willReturn(null);
  414. $user = $this->getMockBuilder(IUser::class)
  415. ->disableOriginalConstructor()
  416. ->getMock();
  417. $user->expects($this->any())
  418. ->method('getUID')
  419. ->willReturn('MyWrongDavUser');
  420. $this->userSession
  421. ->expects($this->any())
  422. ->method('getUser')
  423. ->willReturn($user);
  424. $this->request
  425. ->expects($this->once())
  426. ->method('passesCSRFCheck')
  427. ->willReturn(false);
  428. $this->auth->check($request, $response);
  429. }
  430. public function testAuthenticateAlreadyLoggedInWithoutCsrfTokenForGet(): void {
  431. $request = $this->getMockBuilder(RequestInterface::class)
  432. ->disableOriginalConstructor()
  433. ->getMock();
  434. $response = $this->getMockBuilder(ResponseInterface::class)
  435. ->disableOriginalConstructor()
  436. ->getMock();
  437. $this->userSession
  438. ->expects($this->any())
  439. ->method('isLoggedIn')
  440. ->willReturn(true);
  441. $this->session
  442. ->expects($this->any())
  443. ->method('get')
  444. ->with('AUTHENTICATED_TO_DAV_BACKEND')
  445. ->willReturn(null);
  446. $user = $this->getMockBuilder(IUser::class)
  447. ->disableOriginalConstructor()
  448. ->getMock();
  449. $user->expects($this->any())
  450. ->method('getUID')
  451. ->willReturn('MyWrongDavUser');
  452. $this->userSession
  453. ->expects($this->any())
  454. ->method('getUser')
  455. ->willReturn($user);
  456. $this->request
  457. ->expects($this->any())
  458. ->method('getMethod')
  459. ->willReturn('GET');
  460. $response = $this->auth->check($request, $response);
  461. $this->assertEquals([true, 'principals/users/MyWrongDavUser'], $response);
  462. }
  463. public function testAuthenticateAlreadyLoggedInWithCsrfTokenForGet(): void {
  464. $request = $this->getMockBuilder(RequestInterface::class)
  465. ->disableOriginalConstructor()
  466. ->getMock();
  467. $response = $this->getMockBuilder(ResponseInterface::class)
  468. ->disableOriginalConstructor()
  469. ->getMock();
  470. $this->userSession
  471. ->expects($this->any())
  472. ->method('isLoggedIn')
  473. ->willReturn(true);
  474. $this->session
  475. ->expects($this->any())
  476. ->method('get')
  477. ->with('AUTHENTICATED_TO_DAV_BACKEND')
  478. ->willReturn(null);
  479. $user = $this->getMockBuilder(IUser::class)
  480. ->disableOriginalConstructor()
  481. ->getMock();
  482. $user->expects($this->any())
  483. ->method('getUID')
  484. ->willReturn('MyWrongDavUser');
  485. $this->userSession
  486. ->expects($this->any())
  487. ->method('getUser')
  488. ->willReturn($user);
  489. $this->request
  490. ->expects($this->once())
  491. ->method('passesCSRFCheck')
  492. ->willReturn(true);
  493. $response = $this->auth->check($request, $response);
  494. $this->assertEquals([true, 'principals/users/MyWrongDavUser'], $response);
  495. }
  496. public function testAuthenticateNoBasicAuthenticateHeadersProvided(): void {
  497. $server = $this->getMockBuilder(Server::class)
  498. ->disableOriginalConstructor()
  499. ->getMock();
  500. $server->httpRequest = $this->getMockBuilder(RequestInterface::class)
  501. ->disableOriginalConstructor()
  502. ->getMock();
  503. $server->httpResponse = $this->getMockBuilder(ResponseInterface::class)
  504. ->disableOriginalConstructor()
  505. ->getMock();
  506. $response = $this->auth->check($server->httpRequest, $server->httpResponse);
  507. $this->assertEquals([false, 'No \'Authorization: Basic\' header found. Either the client didn\'t send one, or the server is misconfigured'], $response);
  508. }
  509. public function testAuthenticateNoBasicAuthenticateHeadersProvidedWithAjax(): void {
  510. $this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class);
  511. $this->expectExceptionMessage('Cannot authenticate over ajax calls');
  512. /** @var \Sabre\HTTP\RequestInterface $httpRequest */
  513. $httpRequest = $this->getMockBuilder(RequestInterface::class)
  514. ->disableOriginalConstructor()
  515. ->getMock();
  516. /** @var \Sabre\HTTP\ResponseInterface $httpResponse */
  517. $httpResponse = $this->getMockBuilder(ResponseInterface::class)
  518. ->disableOriginalConstructor()
  519. ->getMock();
  520. $this->userSession
  521. ->expects($this->any())
  522. ->method('isLoggedIn')
  523. ->willReturn(false);
  524. $httpRequest
  525. ->expects($this->once())
  526. ->method('getHeader')
  527. ->with('X-Requested-With')
  528. ->willReturn('XMLHttpRequest');
  529. $this->auth->check($httpRequest, $httpResponse);
  530. }
  531. public function testAuthenticateNoBasicAuthenticateHeadersProvidedWithAjaxButUserIsStillLoggedIn(): void {
  532. /** @var \Sabre\HTTP\RequestInterface $httpRequest */
  533. $httpRequest = $this->getMockBuilder(RequestInterface::class)
  534. ->disableOriginalConstructor()
  535. ->getMock();
  536. /** @var \Sabre\HTTP\ResponseInterface $httpResponse */
  537. $httpResponse = $this->getMockBuilder(ResponseInterface::class)
  538. ->disableOriginalConstructor()
  539. ->getMock();
  540. /** @var IUser */
  541. $user = $this->getMockBuilder(IUser::class)
  542. ->disableOriginalConstructor()
  543. ->getMock();
  544. $user->method('getUID')->willReturn('MyTestUser');
  545. $this->userSession
  546. ->expects($this->any())
  547. ->method('isLoggedIn')
  548. ->willReturn(true);
  549. $this->userSession
  550. ->expects($this->any())
  551. ->method('getUser')
  552. ->willReturn($user);
  553. $this->session
  554. ->expects($this->atLeastOnce())
  555. ->method('get')
  556. ->with('AUTHENTICATED_TO_DAV_BACKEND')
  557. ->willReturn('MyTestUser');
  558. $this->request
  559. ->expects($this->once())
  560. ->method('getMethod')
  561. ->willReturn('GET');
  562. $httpRequest
  563. ->expects($this->atLeastOnce())
  564. ->method('getHeader')
  565. ->with('Authorization')
  566. ->willReturn(null);
  567. $this->assertEquals(
  568. [true, 'principals/users/MyTestUser'],
  569. $this->auth->check($httpRequest, $httpResponse)
  570. );
  571. }
  572. public function testAuthenticateValidCredentials(): void {
  573. $server = $this->getMockBuilder(Server::class)
  574. ->disableOriginalConstructor()
  575. ->getMock();
  576. $server->httpRequest = $this->getMockBuilder(RequestInterface::class)
  577. ->disableOriginalConstructor()
  578. ->getMock();
  579. $server->httpRequest
  580. ->expects($this->exactly(2))
  581. ->method('getHeader')
  582. ->withConsecutive(
  583. ['X-Requested-With'],
  584. ['Authorization'],
  585. )
  586. ->willReturnOnConsecutiveCalls(
  587. null,
  588. 'basic dXNlcm5hbWU6cGFzc3dvcmQ=',
  589. );
  590. $server->httpResponse = $this->getMockBuilder(ResponseInterface::class)
  591. ->disableOriginalConstructor()
  592. ->getMock();
  593. $this->userSession
  594. ->expects($this->once())
  595. ->method('logClientIn')
  596. ->with('username', 'password')
  597. ->willReturn(true);
  598. $user = $this->getMockBuilder(IUser::class)
  599. ->disableOriginalConstructor()
  600. ->getMock();
  601. $user->expects($this->exactly(2))
  602. ->method('getUID')
  603. ->willReturn('MyTestUser');
  604. $this->userSession
  605. ->expects($this->exactly(3))
  606. ->method('getUser')
  607. ->willReturn($user);
  608. $response = $this->auth->check($server->httpRequest, $server->httpResponse);
  609. $this->assertEquals([true, 'principals/users/MyTestUser'], $response);
  610. }
  611. public function testAuthenticateInvalidCredentials(): void {
  612. $server = $this->getMockBuilder(Server::class)
  613. ->disableOriginalConstructor()
  614. ->getMock();
  615. $server->httpRequest = $this->getMockBuilder(RequestInterface::class)
  616. ->disableOriginalConstructor()
  617. ->getMock();
  618. $server->httpRequest
  619. ->expects($this->exactly(2))
  620. ->method('getHeader')
  621. ->withConsecutive(
  622. ['X-Requested-With'],
  623. ['Authorization'],
  624. )
  625. ->willReturnOnConsecutiveCalls(
  626. null,
  627. 'basic dXNlcm5hbWU6cGFzc3dvcmQ=',
  628. );
  629. $server->httpResponse = $this->getMockBuilder(ResponseInterface::class)
  630. ->disableOriginalConstructor()
  631. ->getMock();
  632. $this->userSession
  633. ->expects($this->once())
  634. ->method('logClientIn')
  635. ->with('username', 'password')
  636. ->willReturn(false);
  637. $response = $this->auth->check($server->httpRequest, $server->httpResponse);
  638. $this->assertEquals([false, 'Username or password was incorrect'], $response);
  639. }
  640. }