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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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->createMock(ISession::class);
  60. $this->request = $this->createMock(IRequest::class);
  61. $this->shareManager = $this->createMock(IManager::class);
  62. $this->throttler = $this->createMock(IThrottler::class);
  63. $this->logger = $this->createMock(LoggerInterface::class);
  64. $this->auth = new \OCA\DAV\Connector\Sabre\PublicAuth(
  65. $this->request,
  66. $this->shareManager,
  67. $this->session,
  68. $this->throttler,
  69. $this->logger,
  70. );
  71. // Store current user
  72. $this->oldUser = \OC_User::getUser();
  73. }
  74. protected function tearDown(): void {
  75. \OC_User::setIncognitoMode(false);
  76. // Set old user
  77. \OC_User::setUserId($this->oldUser);
  78. \OC_Util::setupFS($this->oldUser);
  79. parent::tearDown();
  80. }
  81. public function testGetToken(): void {
  82. $this->request->method('getPathInfo')
  83. ->willReturn('/dav/files/GX9HSGQrGE');
  84. $result = $this->invokePrivate($this->auth, 'getToken');
  85. $this->assertSame('GX9HSGQrGE', $result);
  86. }
  87. public function testGetTokenInvalid(): void {
  88. $this->request->method('getPathInfo')
  89. ->willReturn('/dav/files');
  90. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  91. $this->invokePrivate($this->auth, 'getToken');
  92. }
  93. public function testCheckTokenValidShare(): void {
  94. $this->request->method('getPathInfo')
  95. ->willReturn('/dav/files/GX9HSGQrGE');
  96. $share = $this->getMockBuilder(IShare::class)
  97. ->disableOriginalConstructor()
  98. ->getMock();
  99. $share->method('getPassword')->willReturn(null);
  100. $this->shareManager->expects($this->once())
  101. ->method('getShareByToken')
  102. ->with('GX9HSGQrGE')
  103. ->willReturn($share);
  104. $result = $this->invokePrivate($this->auth, 'checkToken');
  105. $this->assertSame([true, 'principals/GX9HSGQrGE'], $result);
  106. }
  107. public function testCheckTokenInvalidShare(): void {
  108. $this->request->method('getPathInfo')
  109. ->willReturn('/dav/files/GX9HSGQrGE');
  110. $this->shareManager
  111. ->expects($this->once())
  112. ->method('getShareByToken')
  113. ->with('GX9HSGQrGE')
  114. ->will($this->throwException(new ShareNotFound()));
  115. $this->expectException(\Sabre\DAV\Exception\NotFound::class);
  116. $this->invokePrivate($this->auth, 'checkToken');
  117. }
  118. public function testCheckTokenAlreadyAuthenticated(): void {
  119. $this->request->method('getPathInfo')
  120. ->willReturn('/dav/files/GX9HSGQrGE');
  121. $share = $this->getMockBuilder(IShare::class)
  122. ->disableOriginalConstructor()
  123. ->getMock();
  124. $share->method('getShareType')->willReturn(42);
  125. $this->shareManager->expects($this->once())
  126. ->method('getShareByToken')
  127. ->with('GX9HSGQrGE')
  128. ->willReturn($share);
  129. $this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
  130. $this->session->method('get')->with('public_link_authenticated')->willReturn('42');
  131. $result = $this->invokePrivate($this->auth, 'checkToken');
  132. $this->assertSame([true, 'principals/GX9HSGQrGE'], $result);
  133. }
  134. public function testCheckTokenPasswordNotAuthenticated(): void {
  135. $this->request->method('getPathInfo')
  136. ->willReturn('/dav/files/GX9HSGQrGE');
  137. $share = $this->getMockBuilder(IShare::class)
  138. ->disableOriginalConstructor()
  139. ->getMock();
  140. $share->method('getPassword')->willReturn('password');
  141. $share->method('getShareType')->willReturn(42);
  142. $this->shareManager->expects($this->once())
  143. ->method('getShareByToken')
  144. ->with('GX9HSGQrGE')
  145. ->willReturn($share);
  146. $this->session->method('exists')->with('public_link_authenticated')->willReturn(false);
  147. $this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class);
  148. $this->invokePrivate($this->auth, 'checkToken');
  149. }
  150. public function testCheckTokenPasswordAuthenticatedWrongShare(): void {
  151. $this->request->method('getPathInfo')
  152. ->willReturn('/dav/files/GX9HSGQrGE');
  153. $share = $this->getMockBuilder(IShare::class)
  154. ->disableOriginalConstructor()
  155. ->getMock();
  156. $share->method('getPassword')->willReturn('password');
  157. $share->method('getShareType')->willReturn(42);
  158. $this->shareManager->expects($this->once())
  159. ->method('getShareByToken')
  160. ->with('GX9HSGQrGE')
  161. ->willReturn($share);
  162. $this->session->method('exists')->with('public_link_authenticated')->willReturn(false);
  163. $this->session->method('get')->with('public_link_authenticated')->willReturn('43');
  164. $this->expectException(\Sabre\DAV\Exception\NotAuthenticated::class);
  165. $this->invokePrivate($this->auth, 'checkToken');
  166. }
  167. public function testNoShare(): void {
  168. $this->request->method('getPathInfo')
  169. ->willReturn('/dav/files/GX9HSGQrGE');
  170. $this->shareManager->expects($this->once())
  171. ->method('getShareByToken')
  172. ->with('GX9HSGQrGE')
  173. ->willThrowException(new ShareNotFound());
  174. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  175. $this->assertFalse($result);
  176. }
  177. public function testShareNoPassword(): void {
  178. $this->request->method('getPathInfo')
  179. ->willReturn('/dav/files/GX9HSGQrGE');
  180. $share = $this->getMockBuilder(IShare::class)
  181. ->disableOriginalConstructor()
  182. ->getMock();
  183. $share->method('getPassword')->willReturn(null);
  184. $this->shareManager->expects($this->once())
  185. ->method('getShareByToken')
  186. ->with('GX9HSGQrGE')
  187. ->willReturn($share);
  188. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  189. $this->assertTrue($result);
  190. }
  191. public function testSharePasswordFancyShareType(): void {
  192. $this->request->method('getPathInfo')
  193. ->willReturn('/dav/files/GX9HSGQrGE');
  194. $share = $this->getMockBuilder(IShare::class)
  195. ->disableOriginalConstructor()
  196. ->getMock();
  197. $share->method('getPassword')->willReturn('password');
  198. $share->method('getShareType')->willReturn(42);
  199. $this->shareManager->expects($this->once())
  200. ->method('getShareByToken')
  201. ->with('GX9HSGQrGE')
  202. ->willReturn($share);
  203. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  204. $this->assertFalse($result);
  205. }
  206. public function testSharePasswordRemote(): void {
  207. $this->request->method('getPathInfo')
  208. ->willReturn('/dav/files/GX9HSGQrGE');
  209. $share = $this->getMockBuilder(IShare::class)
  210. ->disableOriginalConstructor()
  211. ->getMock();
  212. $share->method('getPassword')->willReturn('password');
  213. $share->method('getShareType')->willReturn(IShare::TYPE_REMOTE);
  214. $this->shareManager->expects($this->once())
  215. ->method('getShareByToken')
  216. ->with('GX9HSGQrGE')
  217. ->willReturn($share);
  218. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  219. $this->assertTrue($result);
  220. }
  221. public function testSharePasswordLinkValidPassword(): void {
  222. $this->request->method('getPathInfo')
  223. ->willReturn('/dav/files/GX9HSGQrGE');
  224. $share = $this->getMockBuilder(IShare::class)
  225. ->disableOriginalConstructor()
  226. ->getMock();
  227. $share->method('getPassword')->willReturn('password');
  228. $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
  229. $this->shareManager->expects($this->once())
  230. ->method('getShareByToken')
  231. ->with('GX9HSGQrGE')
  232. ->willReturn($share);
  233. $this->shareManager->expects($this->once())
  234. ->method('checkPassword')->with(
  235. $this->equalTo($share),
  236. $this->equalTo('password')
  237. )->willReturn(true);
  238. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  239. $this->assertTrue($result);
  240. }
  241. public function testSharePasswordMailValidPassword(): void {
  242. $this->request->method('getPathInfo')
  243. ->willReturn('/dav/files/GX9HSGQrGE');
  244. $share = $this->getMockBuilder(IShare::class)
  245. ->disableOriginalConstructor()
  246. ->getMock();
  247. $share->method('getPassword')->willReturn('password');
  248. $share->method('getShareType')->willReturn(IShare::TYPE_EMAIL);
  249. $this->shareManager->expects($this->once())
  250. ->method('getShareByToken')
  251. ->with('GX9HSGQrGE')
  252. ->willReturn($share);
  253. $this->shareManager->expects($this->once())
  254. ->method('checkPassword')->with(
  255. $this->equalTo($share),
  256. $this->equalTo('password')
  257. )->willReturn(true);
  258. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  259. $this->assertTrue($result);
  260. }
  261. public function testInvalidSharePasswordLinkValidSession(): void {
  262. $this->request->method('getPathInfo')
  263. ->willReturn('/dav/files/GX9HSGQrGE');
  264. $share = $this->getMockBuilder(IShare::class)
  265. ->disableOriginalConstructor()
  266. ->getMock();
  267. $share->method('getPassword')->willReturn('password');
  268. $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
  269. $share->method('getId')->willReturn('42');
  270. $this->shareManager->expects($this->once())
  271. ->method('getShareByToken')
  272. ->with('GX9HSGQrGE')
  273. ->willReturn($share);
  274. $this->shareManager->expects($this->once())
  275. ->method('checkPassword')
  276. ->with(
  277. $this->equalTo($share),
  278. $this->equalTo('password')
  279. )->willReturn(false);
  280. $this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
  281. $this->session->method('get')->with('public_link_authenticated')->willReturn('42');
  282. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  283. $this->assertTrue($result);
  284. }
  285. public function testSharePasswordLinkInvalidSession(): void {
  286. $this->request->method('getPathInfo')
  287. ->willReturn('/dav/files/GX9HSGQrGE');
  288. $share = $this->getMockBuilder(IShare::class)
  289. ->disableOriginalConstructor()
  290. ->getMock();
  291. $share->method('getPassword')->willReturn('password');
  292. $share->method('getShareType')->willReturn(IShare::TYPE_LINK);
  293. $share->method('getId')->willReturn('42');
  294. $this->shareManager->expects($this->once())
  295. ->method('getShareByToken')
  296. ->with('GX9HSGQrGE')
  297. ->willReturn($share);
  298. $this->shareManager->expects($this->once())
  299. ->method('checkPassword')
  300. ->with(
  301. $this->equalTo($share),
  302. $this->equalTo('password')
  303. )->willReturn(false);
  304. $this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
  305. $this->session->method('get')->with('public_link_authenticated')->willReturn('43');
  306. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  307. $this->assertFalse($result);
  308. }
  309. public function testSharePasswordMailInvalidSession(): void {
  310. $this->request->method('getPathInfo')
  311. ->willReturn('/dav/files/GX9HSGQrGE');
  312. $share = $this->getMockBuilder(IShare::class)
  313. ->disableOriginalConstructor()
  314. ->getMock();
  315. $share->method('getPassword')->willReturn('password');
  316. $share->method('getShareType')->willReturn(IShare::TYPE_EMAIL);
  317. $share->method('getId')->willReturn('42');
  318. $this->shareManager->expects($this->once())
  319. ->method('getShareByToken')
  320. ->with('GX9HSGQrGE')
  321. ->willReturn($share);
  322. $this->shareManager->expects($this->once())
  323. ->method('checkPassword')
  324. ->with(
  325. $this->equalTo($share),
  326. $this->equalTo('password')
  327. )->willReturn(false);
  328. $this->session->method('exists')->with('public_link_authenticated')->willReturn(true);
  329. $this->session->method('get')->with('public_link_authenticated')->willReturn('43');
  330. $result = $this->invokePrivate($this->auth, 'validateUserPass', ['username', 'password']);
  331. $this->assertFalse($result);
  332. }
  333. }