You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PublicAuthTest.php 13KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Bjoern Schiessle <bjoern@schiessle.org>
  6. * @author Joas Schilling <coding@schilljs.com>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\DAV\Tests\unit\Connector;
  28. use OCP\IRequest;
  29. use OCP\ISession;
  30. use OCP\Security\Bruteforce\IThrottler;
  31. use OCP\Share\Exceptions\ShareNotFound;
  32. use OCP\Share\IManager;
  33. use OCP\Share\IShare;
  34. use Psr\Log\LoggerInterface;
  35. /**
  36. * Class PublicAuthTest
  37. *
  38. * @group DB
  39. *
  40. * @package OCA\DAV\Tests\unit\Connector
  41. */
  42. class PublicAuthTest extends \Test\TestCase {
  43. /** @var ISession|MockObject */
  44. private $session;
  45. /** @var IRequest|MockObject */
  46. private $request;
  47. /** @var IManager|MockObject */
  48. private $shareManager;
  49. /** @var PublicAuth */
  50. private $auth;
  51. /** @var IThrottler|MockObject */
  52. private $throttler;
  53. /** @var LoggerInterface|MockObject */
  54. private $logger;
  55. /** @var string */
  56. private $oldUser;
  57. protected function setUp(): void {
  58. parent::setUp();
  59. $this->session = $this->getMockBuilder(ISession::class)
  60. ->disableOriginalConstructor()
  61. ->getMock();
  62. $this->request = $this->getMockBuilder(IRequest::class)
  63. ->disableOriginalConstructor()
  64. ->getMock();
  65. $this->shareManager = $this->getMockBuilder(IManager::class)
  66. ->disableOriginalConstructor()
  67. ->getMock();
  68. $this->throttler = $this->getMockBuilder(IThrottler::class)
  69. ->disableOriginalConstructor()
  70. ->getMock();
  71. $this->logger = $this->createMock(LoggerInterface::class);
  72. $this->auth = new \OCA\DAV\Connector\Sabre\PublicAuth(
  73. $this->request,
  74. $this->shareManager,
  75. $this->session,
  76. $this->throttler,
  77. $this->logger,
  78. );
  79. // Store current user
  80. $this->oldUser = \OC_User::getUser();
  81. }
  82. protected function tearDown(): void {
  83. \OC_User::setIncognitoMode(false);
  84. // Set old user
  85. \OC_User::setUserId($this->oldUser);
  86. \OC_Util::setupFS($this->oldUser);
  87. parent::tearDown();
  88. }
  89. public function testGetToken(): void {
  90. $this->request->method('getPathInfo')
  91. ->willReturn('/dav/files/GX9HSGQrGE');
  92. $result = $this->invokePrivate($this->auth, 'getToken');
  93. $this->assertSame('GX9HSGQrGE', $result);
  94. }
  95. public function testGetTokenInvalid(): void {
  96. $this->request->method('getPathInfo')
  97. ->willReturn('/dav/files');
  98. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  99. $this->invokePrivate($this->auth, 'getToken');
  100. }
  101. public function testCheckTokenValidShare(): void {
  102. $this->request->method('getPathInfo')
  103. ->willReturn('/dav/files/GX9HSGQrGE');
  104. $share = $this->getMockBuilder(IShare::class)
  105. ->disableOriginalConstructor()
  106. ->getMock();
  107. $share->method('getPassword')->willReturn(null);
  108. $this->shareManager->expects($this->once())
  109. ->method('getShareByToken')
  110. ->with('GX9HSGQrGE')
  111. ->willReturn($share);
  112. $result = $this->invokePrivate($this->auth, 'checkToken');
  113. $this->assertSame([true, 'principals/GX9HSGQrGE'], $result);
  114. }
  115. public function testCheckTokenInvalidShare(): void {
  116. $this->request->method('getPathInfo')
  117. ->willReturn('/dav/files/GX9HSGQrGE');
  118. $this->shareManager
  119. ->expects($this->once())
  120. ->method('getShareByToken')
  121. ->with('GX9HSGQrGE')
  122. ->will($this->throwException(new ShareNotFound()));
  123. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  124. $this->invokePrivate($this->auth, 'checkToken');
  125. }
  126. public function testCheckTokenAlreadyAuthenticated(): void {
  127. $this->request->method('getPathInfo')
  128. ->willReturn('/dav/files/GX9HSGQrGE');
  129. $share = $this->getMockBuilder(IShare::class)
  130. ->disableOriginalConstructor()
  131. ->getMock();
  132. $share->method('getShareType')->willReturn(42);
  133. $this->shareManager->expects($this->once())
  134. ->method('getShareByToken')
  135. ->with('GX9HSGQrGE')
  136. ->willReturn($share);
  137. $this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
  138. $this->session->method('get')->with('public_link_authenticated')->willReturn('42');
  139. $result = $this->invokePrivate($this->auth, 'checkToken');
  140. $this->assertSame([true, 'principals/GX9HSGQrGE'], $result);
  141. }
  142. public function testCheckTokenPasswordNotAuthenticated(): void {
  143. $this->request->method('getPathInfo')
  144. ->willReturn('/dav/files/GX9HSGQrGE');
  145. $share = $this->getMockBuilder(IShare::class)
  146. ->disableOriginalConstructor()
  147. ->getMock();
  148. $share->method('getPassword')->willReturn('password');
  149. $share->method('getShareType')->willReturn(42);
  150. $this->shareManager->expects($this->once())
  151. ->method('getShareByToken')
  152. ->with('GX9HSGQrGE')
  153. ->willReturn($share);
  154. $this->session->method('exists')->with('public_link_authenticated')->willReturn(false);
  155. $this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class);
  156. $this->invokePrivate($this->auth, 'checkToken');
  157. }
  158. public function testCheckTokenPasswordAuthenticatedWrongShare(): void {
  159. $this->request->method('getPathInfo')
  160. ->willReturn('/dav/files/GX9HSGQrGE');
  161. $share = $this->getMockBuilder(IShare::class)
  162. ->disableOriginalConstructor()
  163. ->getMock();
  164. $share->method('getPassword')->willReturn('password');
  165. $share->method('getShareType')->willReturn(42);
  166. $this->shareManager->expects($this->once())
  167. ->method('getShareByToken')
  168. ->with('GX9HSGQrGE')
  169. ->willReturn($share);
  170. $this->session->method('exists')->with('public_link_authenticated')->willReturn(false);
  171. $this->session->method('get')->with('public_link_authenticated')->willReturn('43');
  172. $this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class);
  173. $this->invokePrivate($this->auth, 'checkToken');
  174. }
  175. public function testNoShare(): void {
  176. $this->request->method('getPathInfo')
  177. ->willReturn('/dav/files/GX9HSGQrGE');
  178. $this->shareManager->expects($this->once())
  179. ->method('getShareByToken')
  180. ->with('GX9HSGQrGE')
  181. ->willThrowException(new ShareNotFound());
  182. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  183. $this->assertFalse($result);
  184. }
  185. public function testShareNoPassword(): void {
  186. $this->request->method('getPathInfo')
  187. ->willReturn('/dav/files/GX9HSGQrGE');
  188. $share = $this->getMockBuilder(IShare::class)
  189. ->disableOriginalConstructor()
  190. ->getMock();
  191. $share->method('getPassword')->willReturn(null);
  192. $this->shareManager->expects($this->once())
  193. ->method('getShareByToken')
  194. ->with('GX9HSGQrGE')
  195. ->willReturn($share);
  196. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  197. $this->assertTrue($result);
  198. }
  199. public function testSharePasswordFancyShareType(): void {
  200. $this->request->method('getPathInfo')
  201. ->willReturn('/dav/files/GX9HSGQrGE');
  202. $share = $this->getMockBuilder(IShare::class)
  203. ->disableOriginalConstructor()
  204. ->getMock();
  205. $share->method('getPassword')->willReturn('password');
  206. $share->method('getShareType')->willReturn(42);
  207. $this->shareManager->expects($this->once())
  208. ->method('getShareByToken')
  209. ->with('GX9HSGQrGE')
  210. ->willReturn($share);
  211. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  212. $this->assertFalse($result);
  213. }
  214. public function testSharePasswordRemote(): void {
  215. $this->request->method('getPathInfo')
  216. ->willReturn('/dav/files/GX9HSGQrGE');
  217. $share = $this->getMockBuilder(IShare::class)
  218. ->disableOriginalConstructor()
  219. ->getMock();
  220. $share->method('getPassword')->willReturn('password');
  221. $share->method('getShareType')->willReturn(IShare::TYPE_REMOTE);
  222. $this->shareManager->expects($this->once())
  223. ->method('getShareByToken')
  224. ->with('GX9HSGQrGE')
  225. ->willReturn($share);
  226. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  227. $this->assertTrue($result);
  228. }
  229. public function testSharePasswordLinkValidPassword(): void {
  230. $this->request->method('getPathInfo')
  231. ->willReturn('/dav/files/GX9HSGQrGE');
  232. $share = $this->getMockBuilder(IShare::class)
  233. ->disableOriginalConstructor()
  234. ->getMock();
  235. $share->method('getPassword')->willReturn('password');
  236. $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
  237. $this->shareManager->expects($this->once())
  238. ->method('getShareByToken')
  239. ->with('GX9HSGQrGE')
  240. ->willReturn($share);
  241. $this->shareManager->expects($this->once())
  242. ->method('checkPassword')->with(
  243. $this->equalTo($share),
  244. $this->equalTo('password')
  245. )->willReturn(true);
  246. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  247. $this->assertTrue($result);
  248. }
  249. public function testSharePasswordMailValidPassword(): void {
  250. $this->request->method('getPathInfo')
  251. ->willReturn('/dav/files/GX9HSGQrGE');
  252. $share = $this->getMockBuilder(IShare::class)
  253. ->disableOriginalConstructor()
  254. ->getMock();
  255. $share->method('getPassword')->willReturn('password');
  256. $share->method('getShareType')->willReturn(IShare::TYPE_EMAIL);
  257. $this->shareManager->expects($this->once())
  258. ->method('getShareByToken')
  259. ->with('GX9HSGQrGE')
  260. ->willReturn($share);
  261. $this->shareManager->expects($this->once())
  262. ->method('checkPassword')->with(
  263. $this->equalTo($share),
  264. $this->equalTo('password')
  265. )->willReturn(true);
  266. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  267. $this->assertTrue($result);
  268. }
  269. public function testInvalidSharePasswordLinkValidSession(): void {
  270. $this->request->method('getPathInfo')
  271. ->willReturn('/dav/files/GX9HSGQrGE');
  272. $share = $this->getMockBuilder(IShare::class)
  273. ->disableOriginalConstructor()
  274. ->getMock();
  275. $share->method('getPassword')->willReturn('password');
  276. $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
  277. $share->method('getId')->willReturn('42');
  278. $this->shareManager->expects($this->once())
  279. ->method('getShareByToken')
  280. ->with('GX9HSGQrGE')
  281. ->willReturn($share);
  282. $this->shareManager->expects($this->once())
  283. ->method('checkPassword')
  284. ->with(
  285. $this->equalTo($share),
  286. $this->equalTo('password')
  287. )->willReturn(false);
  288. $this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
  289. $this->session->method('get')->with('public_link_authenticated')->willReturn('42');
  290. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  291. $this->assertTrue($result);
  292. }
  293. public function testSharePasswordLinkInvalidSession(): void {
  294. $this->request->method('getPathInfo')
  295. ->willReturn('/dav/files/GX9HSGQrGE');
  296. $share = $this->getMockBuilder(IShare::class)
  297. ->disableOriginalConstructor()
  298. ->getMock();
  299. $share->method('getPassword')->willReturn('password');
  300. $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
  301. $share->method('getId')->willReturn('42');
  302. $this->shareManager->expects($this->once())
  303. ->method('getShareByToken')
  304. ->with('GX9HSGQrGE')
  305. ->willReturn($share);
  306. $this->shareManager->expects($this->once())
  307. ->method('checkPassword')
  308. ->with(
  309. $this->equalTo($share),
  310. $this->equalTo('password')
  311. )->willReturn(false);
  312. $this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
  313. $this->session->method('get')->with('public_link_authenticated')->willReturn('43');
  314. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  315. $this->assertFalse($result);
  316. }
  317. public function testSharePasswordMailInvalidSession(): void {
  318. $this->request->method('getPathInfo')
  319. ->willReturn('/dav/files/GX9HSGQrGE');
  320. $share = $this->getMockBuilder(IShare::class)
  321. ->disableOriginalConstructor()
  322. ->getMock();
  323. $share->method('getPassword')->willReturn('password');
  324. $share->method('getShareType')->willReturn(IShare::TYPE_EMAIL);
  325. $share->method('getId')->willReturn('42');
  326. $this->shareManager->expects($this->once())
  327. ->method('getShareByToken')
  328. ->with('GX9HSGQrGE')
  329. ->willReturn($share);
  330. $this->shareManager->expects($this->once())
  331. ->method('checkPassword')
  332. ->with(
  333. $this->equalTo($share),
  334. $this->equalTo('password')
  335. )->willReturn(false);
  336. $this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
  337. $this->session->method('get')->with('public_link_authenticated')->willReturn('43');
  338. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  339. $this->assertFalse($result);
  340. }
  341. }