DefaultTokenProviderTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421
  1. <?php
  2. /**
  3. * @author Christoph Wurst <christoph@owncloud.com>
  4. *
  5. * @copyright Copyright (c) 2016, Lukas Reschke <lukas@statuscode.ch>
  6. * @copyright Copyright (c) 2016, ownCloud, Inc.
  7. * @license AGPL-3.0
  8. *
  9. * This code is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License, version 3,
  11. * as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Affero General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public License, version 3,
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>
  20. *
  21. */
  22. namespace Test\Authentication\Token;
  23. use OC\Authentication\Exceptions\InvalidTokenException;
  24. use OC\Authentication\Token\DefaultToken;
  25. use OC\Authentication\Token\DefaultTokenProvider;
  26. use OC\Authentication\Token\IToken;
  27. use OCP\AppFramework\Db\DoesNotExistException;
  28. use OCP\AppFramework\Db\Mapper;
  29. use OCP\AppFramework\Utility\ITimeFactory;
  30. use OCP\IConfig;
  31. use OCP\ILogger;
  32. use OCP\IUser;
  33. use OCP\Security\ICrypto;
  34. use Test\TestCase;
  35. class DefaultTokenProviderTest extends TestCase {
  36. /** @var DefaultTokenProvider|\PHPUnit_Framework_MockObject_MockObject */
  37. private $tokenProvider;
  38. /** @var Mapper|\PHPUnit_Framework_MockObject_MockObject */
  39. private $mapper;
  40. /** @var ICrypto|\PHPUnit_Framework_MockObject_MockObject */
  41. private $crypto;
  42. /** @var IConfig|\PHPUnit_Framework_MockObject_MockObject */
  43. private $config;
  44. /** @var ILogger|\PHPUnit_Framework_MockObject_MockObject */
  45. private $logger;
  46. /** @var ITimeFactory|\PHPUnit_Framework_MockObject_MockObject */
  47. private $timeFactory;
  48. /** @var int */
  49. private $time;
  50. protected function setUp() {
  51. parent::setUp();
  52. $this->mapper = $this->getMockBuilder('\OC\Authentication\Token\DefaultTokenMapper')
  53. ->disableOriginalConstructor()
  54. ->getMock();
  55. $this->crypto = $this->createMock(ICrypto::class);
  56. $this->config = $this->createMock(IConfig::class);
  57. $this->logger = $this->createMock(ILogger::class);
  58. $this->timeFactory = $this->createMock(ITimeFactory::class);
  59. $this->time = 1313131;
  60. $this->timeFactory->expects($this->any())
  61. ->method('getTime')
  62. ->will($this->returnValue($this->time));
  63. $this->tokenProvider = new DefaultTokenProvider($this->mapper, $this->crypto, $this->config, $this->logger,
  64. $this->timeFactory);
  65. }
  66. public function testGenerateToken() {
  67. $token = 'token';
  68. $uid = 'user';
  69. $user = 'User';
  70. $password = 'passme';
  71. $name = 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
  72. . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
  73. . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
  74. . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
  75. $type = IToken::PERMANENT_TOKEN;
  76. $toInsert = new DefaultToken();
  77. $toInsert->setUid($uid);
  78. $toInsert->setLoginName($user);
  79. $toInsert->setPassword('encryptedpassword');
  80. $toInsert->setName($name);
  81. $toInsert->setToken(hash('sha512', $token . '1f4h9s'));
  82. $toInsert->setType($type);
  83. $toInsert->setRemember(IToken::DO_NOT_REMEMBER);
  84. $toInsert->setLastActivity($this->time);
  85. $toInsert->setLastCheck($this->time);
  86. $this->config->expects($this->any())
  87. ->method('getSystemValue')
  88. ->with('secret')
  89. ->will($this->returnValue('1f4h9s'));
  90. $this->crypto->expects($this->once())
  91. ->method('encrypt')
  92. ->with($password, $token . '1f4h9s')
  93. ->will($this->returnValue('encryptedpassword'));
  94. $this->mapper->expects($this->once())
  95. ->method('insert')
  96. ->with($this->equalTo($toInsert));
  97. $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
  98. $this->assertEquals($toInsert, $actual);
  99. }
  100. public function testUpdateToken() {
  101. $tk = new DefaultToken();
  102. $tk->setLastActivity($this->time - 200);
  103. $this->mapper->expects($this->once())
  104. ->method('update')
  105. ->with($tk);
  106. $this->tokenProvider->updateTokenActivity($tk);
  107. $this->assertEquals($this->time, $tk->getLastActivity());
  108. }
  109. public function testUpdateTokenDebounce() {
  110. $tk = new DefaultToken();
  111. $tk->setLastActivity($this->time - 30);
  112. $this->mapper->expects($this->never())
  113. ->method('update')
  114. ->with($tk);
  115. $this->tokenProvider->updateTokenActivity($tk);
  116. }
  117. public function testGetTokenByUser() {
  118. $user = $this->createMock(IUser::class);
  119. $this->mapper->expects($this->once())
  120. ->method('getTokenByUser')
  121. ->with($user)
  122. ->will($this->returnValue(['token']));
  123. $this->assertEquals(['token'], $this->tokenProvider->getTokenByUser($user));
  124. }
  125. public function testGetPassword() {
  126. $token = 'token1234';
  127. $tk = new DefaultToken();
  128. $tk->setPassword('someencryptedvalue');
  129. $this->config->expects($this->once())
  130. ->method('getSystemValue')
  131. ->with('secret')
  132. ->will($this->returnValue('1f4h9s'));
  133. $this->crypto->expects($this->once())
  134. ->method('decrypt')
  135. ->with('someencryptedvalue', $token . '1f4h9s')
  136. ->will($this->returnValue('passme'));
  137. $actual = $this->tokenProvider->getPassword($tk, $token);
  138. $this->assertEquals('passme', $actual);
  139. }
  140. /**
  141. * @expectedException \OC\Authentication\Exceptions\PasswordlessTokenException
  142. */
  143. public function testGetPasswordPasswordLessToken() {
  144. $token = 'token1234';
  145. $tk = new DefaultToken();
  146. $tk->setPassword(null);
  147. $this->tokenProvider->getPassword($tk, $token);
  148. }
  149. /**
  150. * @expectedException \OC\Authentication\Exceptions\InvalidTokenException
  151. */
  152. public function testGetPasswordDeletesInvalidToken() {
  153. $token = 'token1234';
  154. $tk = new DefaultToken();
  155. $tk->setPassword('someencryptedvalue');
  156. /* @var $tokenProvider DefaultTokenProvider */
  157. $tokenProvider = $this->getMockBuilder('\OC\Authentication\Token\DefaultTokenProvider')
  158. ->setMethods([
  159. 'invalidateToken'
  160. ])
  161. ->setConstructorArgs([$this->mapper, $this->crypto, $this->config, $this->logger,
  162. $this->timeFactory])
  163. ->getMock();
  164. $this->config->expects($this->once())
  165. ->method('getSystemValue')
  166. ->with('secret')
  167. ->will($this->returnValue('1f4h9s'));
  168. $this->crypto->expects($this->once())
  169. ->method('decrypt')
  170. ->with('someencryptedvalue', $token . '1f4h9s')
  171. ->will($this->throwException(new \Exception('some crypto error occurred')));
  172. $tokenProvider->expects($this->once())
  173. ->method('invalidateToken')
  174. ->with($token);
  175. $tokenProvider->getPassword($tk, $token);
  176. }
  177. public function testSetPassword() {
  178. $token = new DefaultToken();
  179. $tokenId = 'token123';
  180. $password = '123456';
  181. $this->config->expects($this->once())
  182. ->method('getSystemValue')
  183. ->with('secret')
  184. ->will($this->returnValue('ocsecret'));
  185. $this->crypto->expects($this->once())
  186. ->method('encrypt')
  187. ->with($password, $tokenId . 'ocsecret')
  188. ->will($this->returnValue('encryptedpassword'));
  189. $this->mapper->expects($this->once())
  190. ->method('update')
  191. ->with($token);
  192. $this->tokenProvider->setPassword($token, $tokenId, $password);
  193. $this->assertEquals('encryptedpassword', $token->getPassword());
  194. }
  195. /**
  196. * @expectedException \OC\Authentication\Exceptions\InvalidTokenException
  197. */
  198. public function testSetPasswordInvalidToken() {
  199. $token = $this->createMock(IToken::class);
  200. $tokenId = 'token123';
  201. $password = '123456';
  202. $this->tokenProvider->setPassword($token, $tokenId, $password);
  203. }
  204. public function testInvalidateToken() {
  205. $this->mapper->expects($this->once())
  206. ->method('invalidate')
  207. ->with(hash('sha512', 'token7'));
  208. $this->tokenProvider->invalidateToken('token7');
  209. }
  210. public function testInvaildateTokenById() {
  211. $id = 123;
  212. $user = $this->createMock(IUser::class);
  213. $this->mapper->expects($this->once())
  214. ->method('deleteById')
  215. ->with($user, $id);
  216. $this->tokenProvider->invalidateTokenById($user, $id);
  217. }
  218. public function testInvalidateOldTokens() {
  219. $defaultSessionLifetime = 60 * 60 * 24;
  220. $defaultRememberMeLifetime = 60 * 60 * 24 * 15;
  221. $this->config->expects($this->exactly(2))
  222. ->method('getSystemValue')
  223. ->will($this->returnValueMap([
  224. ['session_lifetime', $defaultSessionLifetime, 150],
  225. ['remember_login_cookie_lifetime', $defaultRememberMeLifetime, 300],
  226. ]));
  227. $this->mapper->expects($this->at(0))
  228. ->method('invalidateOld')
  229. ->with($this->time - 150);
  230. $this->mapper->expects($this->at(1))
  231. ->method('invalidateOld')
  232. ->with($this->time - 300);
  233. $this->tokenProvider->invalidateOldTokens();
  234. }
  235. public function testRenewSessionTokenWithoutPassword() {
  236. $token = $this->getMockBuilder(DefaultToken::class)
  237. ->disableOriginalConstructor()
  238. ->setMethods(['getUID', 'getLoginName', 'getPassword', 'getName', 'getRemember'])
  239. ->getMock();
  240. $token
  241. ->expects($this->at(0))
  242. ->method('getUID')
  243. ->willReturn('UserUid');
  244. $token
  245. ->expects($this->at(1))
  246. ->method('getLoginName')
  247. ->willReturn('UserLoginName');
  248. $token
  249. ->expects($this->at(2))
  250. ->method('getPassword')
  251. ->willReturn(null);
  252. $token
  253. ->expects($this->at(3))
  254. ->method('getName')
  255. ->willReturn('MyTokenName');
  256. $token
  257. ->expects($this->at(4))
  258. ->method('getRemember')
  259. ->willReturn(IToken::DO_NOT_REMEMBER);
  260. $this->config
  261. ->expects($this->exactly(2))
  262. ->method('getSystemValue')
  263. ->with('secret')
  264. ->willReturn('MyInstanceSecret');
  265. $this->mapper
  266. ->expects($this->at(0))
  267. ->method('getToken')
  268. ->with(hash('sha512', 'oldId' . 'MyInstanceSecret'))
  269. ->willReturn($token);
  270. $newToken = new DefaultToken();
  271. $newToken->setUid('UserUid');
  272. $newToken->setLoginName('UserLoginName');
  273. $newToken->setName('MyTokenName');
  274. $newToken->setToken(hash('sha512', 'newId' . 'MyInstanceSecret'));
  275. $newToken->setType(IToken::TEMPORARY_TOKEN);
  276. $newToken->setRemember(IToken::DO_NOT_REMEMBER);
  277. $newToken->setLastActivity(1313131);
  278. $this->mapper
  279. ->expects($this->at(1))
  280. ->method('insert')
  281. ->with($newToken);
  282. $this->mapper
  283. ->expects($this->at(2))
  284. ->method('delete')
  285. ->with($token);
  286. $this->tokenProvider->renewSessionToken('oldId', 'newId');
  287. }
  288. public function testRenewSessionTokenWithPassword() {
  289. $token = $this->getMockBuilder(DefaultToken::class)
  290. ->disableOriginalConstructor()
  291. ->setMethods(['getUID', 'getLoginName', 'getPassword', 'getName', 'getRemember'])
  292. ->getMock();
  293. $token
  294. ->expects($this->at(0))
  295. ->method('getUID')
  296. ->willReturn('UserUid');
  297. $token
  298. ->expects($this->at(1))
  299. ->method('getLoginName')
  300. ->willReturn('UserLoginName');
  301. $token
  302. ->expects($this->at(2))
  303. ->method('getPassword')
  304. ->willReturn('EncryptedPassword');
  305. $token
  306. ->expects($this->at(3))
  307. ->method('getPassword')
  308. ->willReturn('EncryptedPassword');
  309. $token
  310. ->expects($this->at(4))
  311. ->method('getName')
  312. ->willReturn('MyTokenName');
  313. $token
  314. ->expects($this->at(5))
  315. ->method('getRemember')
  316. ->willReturn(IToken::REMEMBER);
  317. $this->crypto
  318. ->expects($this->any(0))
  319. ->method('decrypt')
  320. ->with('EncryptedPassword', 'oldIdMyInstanceSecret')
  321. ->willReturn('ClearTextPassword');
  322. $this->crypto
  323. ->expects($this->any(1))
  324. ->method('encrypt')
  325. ->with('ClearTextPassword', 'newIdMyInstanceSecret')
  326. ->willReturn('EncryptedPassword');
  327. $this->config
  328. ->expects($this->exactly(4))
  329. ->method('getSystemValue')
  330. ->with('secret')
  331. ->willReturn('MyInstanceSecret');
  332. $this->mapper
  333. ->expects($this->at(0))
  334. ->method('getToken')
  335. ->with(hash('sha512', 'oldId' . 'MyInstanceSecret'))
  336. ->willReturn($token);
  337. $newToken = new DefaultToken();
  338. $newToken->setUid('UserUid');
  339. $newToken->setLoginName('UserLoginName');
  340. $newToken->setName('MyTokenName');
  341. $newToken->setToken(hash('sha512', 'newId' . 'MyInstanceSecret'));
  342. $newToken->setType(IToken::TEMPORARY_TOKEN);
  343. $newToken->setRemember(IToken::REMEMBER);
  344. $newToken->setLastActivity(1313131);
  345. $newToken->setPassword('EncryptedPassword');
  346. $this->mapper
  347. ->expects($this->at(1))
  348. ->method('insert')
  349. ->with($this->equalTo($newToken));
  350. $this->mapper
  351. ->expects($this->at(2))
  352. ->method('delete')
  353. ->with($token);
  354. $this->tokenProvider->renewSessionToken('oldId', 'newId');
  355. }
  356. public function testGetTokenById() {
  357. $token = $this->createMock(DefaultToken::class);
  358. $this->mapper->expects($this->once())
  359. ->method('getTokenById')
  360. ->with($this->equalTo(42))
  361. ->willReturn($token);
  362. $this->assertSame($token, $this->tokenProvider->getTokenById(42));
  363. }
  364. public function testGetInvalidTokenById() {
  365. $this->expectException(InvalidTokenException::class);
  366. $this->mapper->expects($this->once())
  367. ->method('getTokenById')
  368. ->with($this->equalTo(42))
  369. ->willThrowException(new DoesNotExistException('nope'));
  370. $this->tokenProvider->getTokenById(42);
  371. }
  372. }