AuthTest.php 21 KB

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