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.

526 lines
16 KiB

  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2018 Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\Authentication\Token;
  24. use OC\Authentication\Exceptions\ExpiredTokenException;
  25. use OC\Authentication\Exceptions\InvalidTokenException;
  26. use OC\Authentication\Token\IToken;
  27. use OC\Authentication\Token\PublicKeyToken;
  28. use OC\Authentication\Token\PublicKeyTokenMapper;
  29. use OC\Authentication\Token\PublicKeyTokenProvider;
  30. use OCP\AppFramework\Db\DoesNotExistException;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\IConfig;
  33. use OCP\Security\ICrypto;
  34. use Psr\Log\LoggerInterface;
  35. use Test\TestCase;
  36. class PublicKeyTokenProviderTest extends TestCase {
  37. /** @var PublicKeyTokenProvider|\PHPUnit\Framework\MockObject\MockObject */
  38. private $tokenProvider;
  39. /** @var PublicKeyTokenMapper|\PHPUnit\Framework\MockObject\MockObject */
  40. private $mapper;
  41. /** @var ICrypto */
  42. private $crypto;
  43. /** @var IConfig|\PHPUnit\Framework\MockObject\MockObject */
  44. private $config;
  45. /** @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject */
  46. private $logger;
  47. /** @var ITimeFactory|\PHPUnit\Framework\MockObject\MockObject */
  48. private $timeFactory;
  49. /** @var int */
  50. private $time;
  51. protected function setUp(): void {
  52. parent::setUp();
  53. $this->mapper = $this->createMock(PublicKeyTokenMapper::class);
  54. $this->crypto = \OC::$server->getCrypto();
  55. $this->config = $this->createMock(IConfig::class);
  56. $this->config->method('getSystemValue')
  57. ->willReturnMap([
  58. ['session_lifetime', 60 * 60 * 24, 150],
  59. ['remember_login_cookie_lifetime', 60 * 60 * 24 * 15, 300],
  60. ['secret', '', '1f4h9s'],
  61. ['openssl', [], []],
  62. ]);
  63. $this->logger = $this->createMock(LoggerInterface::class);
  64. $this->timeFactory = $this->createMock(ITimeFactory::class);
  65. $this->time = 1313131;
  66. $this->timeFactory->method('getTime')
  67. ->willReturn($this->time);
  68. $this->tokenProvider = new PublicKeyTokenProvider($this->mapper, $this->crypto, $this->config, $this->logger,
  69. $this->timeFactory);
  70. }
  71. public function testGenerateToken() {
  72. $token = 'token';
  73. $uid = 'user';
  74. $user = 'User';
  75. $password = 'passme';
  76. $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';
  77. $type = IToken::PERMANENT_TOKEN;
  78. $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
  79. $this->assertInstanceOf(PublicKeyToken::class, $actual);
  80. $this->assertSame($uid, $actual->getUID());
  81. $this->assertSame($user, $actual->getLoginName());
  82. $this->assertSame($name, $actual->getName());
  83. $this->assertSame(IToken::DO_NOT_REMEMBER, $actual->getRemember());
  84. $this->assertSame($password, $this->tokenProvider->getPassword($actual, $token));
  85. }
  86. public function testGenerateTokenInvalidName() {
  87. $token = 'token';
  88. $uid = 'user';
  89. $user = 'User';
  90. $password = 'passme';
  91. $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'
  92. . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
  93. . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12'
  94. . 'User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12';
  95. $type = IToken::PERMANENT_TOKEN;
  96. $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
  97. $this->assertInstanceOf(PublicKeyToken::class, $actual);
  98. $this->assertSame($uid, $actual->getUID());
  99. $this->assertSame($user, $actual->getLoginName());
  100. $this->assertSame('User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.2.12) Gecko/20101026 Firefox/3.6.12User-Agent: Mozill…', $actual->getName());
  101. $this->assertSame(IToken::DO_NOT_REMEMBER, $actual->getRemember());
  102. $this->assertSame($password, $this->tokenProvider->getPassword($actual, $token));
  103. }
  104. public function testUpdateToken() {
  105. $tk = new PublicKeyToken();
  106. $this->mapper->expects($this->once())
  107. ->method('updateActivity')
  108. ->with($tk, $this->time);
  109. $tk->setLastActivity($this->time - 200);
  110. $this->tokenProvider->updateTokenActivity($tk);
  111. $this->assertEquals($this->time, $tk->getLastActivity());
  112. }
  113. public function testUpdateTokenDebounce() {
  114. $tk = new PublicKeyToken();
  115. $this->config->method('getSystemValueInt')
  116. ->willReturnCallback(function ($value, $default) {
  117. return $default;
  118. });
  119. $tk->setLastActivity($this->time - 30);
  120. $this->mapper->expects($this->never())
  121. ->method('updateActivity')
  122. ->with($tk, $this->time);
  123. $this->tokenProvider->updateTokenActivity($tk);
  124. }
  125. public function testGetTokenByUser() {
  126. $this->mapper->expects($this->once())
  127. ->method('getTokenByUser')
  128. ->with('uid')
  129. ->willReturn(['token']);
  130. $this->assertEquals(['token'], $this->tokenProvider->getTokenByUser('uid'));
  131. }
  132. public function testGetPassword() {
  133. $token = 'token';
  134. $uid = 'user';
  135. $user = 'User';
  136. $password = 'passme';
  137. $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';
  138. $type = IToken::PERMANENT_TOKEN;
  139. $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
  140. $this->assertSame($password, $this->tokenProvider->getPassword($actual, $token));
  141. }
  142. public function testGetPasswordPasswordLessToken() {
  143. $this->expectException(\OC\Authentication\Exceptions\PasswordlessTokenException::class);
  144. $token = 'token1234';
  145. $tk = new PublicKeyToken();
  146. $tk->setPassword(null);
  147. $this->tokenProvider->getPassword($tk, $token);
  148. }
  149. public function testGetPasswordInvalidToken() {
  150. $this->expectException(\OC\Authentication\Exceptions\InvalidTokenException::class);
  151. $token = 'token';
  152. $uid = 'user';
  153. $user = 'User';
  154. $password = 'passme';
  155. $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';
  156. $type = IToken::PERMANENT_TOKEN;
  157. $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
  158. $this->tokenProvider->getPassword($actual, 'wrongtoken');
  159. }
  160. public function testSetPassword() {
  161. $token = 'token';
  162. $uid = 'user';
  163. $user = 'User';
  164. $password = 'passme';
  165. $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';
  166. $type = IToken::PERMANENT_TOKEN;
  167. $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
  168. $this->mapper->method('getTokenByUser')
  169. ->with('user')
  170. ->willReturn([$actual]);
  171. $newpass = 'newpass';
  172. $this->mapper->expects($this->once())
  173. ->method('update')
  174. ->with($this->callback(function ($token) use ($newpass) {
  175. return $newpass === $this->tokenProvider->getPassword($token, 'token');
  176. }));
  177. $this->tokenProvider->setPassword($actual, $token, $newpass);
  178. $this->assertSame($newpass, $this->tokenProvider->getPassword($actual, 'token'));
  179. }
  180. public function testSetPasswordInvalidToken() {
  181. $this->expectException(\OC\Authentication\Exceptions\InvalidTokenException::class);
  182. $token = $this->createMock(IToken::class);
  183. $tokenId = 'token123';
  184. $password = '123456';
  185. $this->tokenProvider->setPassword($token, $tokenId, $password);
  186. }
  187. public function testInvalidateToken() {
  188. $this->mapper->expects($this->once())
  189. ->method('invalidate')
  190. ->with(hash('sha512', 'token7'.'1f4h9s'));
  191. $this->tokenProvider->invalidateToken('token7');
  192. }
  193. public function testInvaildateTokenById() {
  194. $id = 123;
  195. $this->mapper->expects($this->once())
  196. ->method('deleteById')
  197. ->with('uid', $id);
  198. $this->tokenProvider->invalidateTokenById('uid', $id);
  199. }
  200. public function testInvalidateOldTokens() {
  201. $defaultSessionLifetime = 60 * 60 * 24;
  202. $defaultRememberMeLifetime = 60 * 60 * 24 * 15;
  203. $this->config->expects($this->exactly(2))
  204. ->method('getSystemValue')
  205. ->willReturnMap([
  206. ['session_lifetime', $defaultSessionLifetime, 150],
  207. ['remember_login_cookie_lifetime', $defaultRememberMeLifetime, 300],
  208. ]);
  209. $this->mapper->expects($this->exactly(2))
  210. ->method('invalidateOld')
  211. ->withConsecutive(
  212. [$this->time - 150],
  213. [$this->time - 300]
  214. );
  215. $this->tokenProvider->invalidateOldTokens();
  216. }
  217. public function testRenewSessionTokenWithoutPassword() {
  218. $token = 'oldId';
  219. $uid = 'user';
  220. $user = 'User';
  221. $password = null;
  222. $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';
  223. $type = IToken::PERMANENT_TOKEN;
  224. $oldToken = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
  225. $this->mapper
  226. ->expects($this->once())
  227. ->method('getToken')
  228. ->with(hash('sha512', 'oldId' . '1f4h9s'))
  229. ->willReturn($oldToken);
  230. $this->mapper
  231. ->expects($this->once())
  232. ->method('insert')
  233. ->with($this->callback(function (PublicKeyToken $token) use ($user, $uid, $name) {
  234. return $token->getUID() === $uid &&
  235. $token->getLoginName() === $user &&
  236. $token->getName() === $name &&
  237. $token->getType() === IToken::DO_NOT_REMEMBER &&
  238. $token->getLastActivity() === $this->time &&
  239. $token->getPassword() === null;
  240. }));
  241. $this->mapper
  242. ->expects($this->once())
  243. ->method('delete')
  244. ->with($this->callback(function ($token) use ($oldToken) {
  245. return $token === $oldToken;
  246. }));
  247. $this->tokenProvider->renewSessionToken('oldId', 'newId');
  248. }
  249. public function testRenewSessionTokenWithPassword() {
  250. $token = 'oldId';
  251. $uid = 'user';
  252. $user = 'User';
  253. $password = 'password';
  254. $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';
  255. $type = IToken::PERMANENT_TOKEN;
  256. $oldToken = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
  257. $this->mapper
  258. ->expects($this->once())
  259. ->method('getToken')
  260. ->with(hash('sha512', 'oldId' . '1f4h9s'))
  261. ->willReturn($oldToken);
  262. $this->mapper
  263. ->expects($this->once())
  264. ->method('insert')
  265. ->with($this->callback(function (PublicKeyToken $token) use ($user, $uid, $name) {
  266. return $token->getUID() === $uid &&
  267. $token->getLoginName() === $user &&
  268. $token->getName() === $name &&
  269. $token->getType() === IToken::DO_NOT_REMEMBER &&
  270. $token->getLastActivity() === $this->time &&
  271. $token->getPassword() !== null &&
  272. $this->tokenProvider->getPassword($token, 'newId') === 'password';
  273. }));
  274. $this->mapper
  275. ->expects($this->once())
  276. ->method('delete')
  277. ->with($this->callback(function ($token) use ($oldToken) {
  278. return $token === $oldToken;
  279. }));
  280. $this->tokenProvider->renewSessionToken('oldId', 'newId');
  281. }
  282. public function testGetToken() {
  283. $token = new PublicKeyToken();
  284. $this->config->method('getSystemValue')
  285. ->with('secret')
  286. ->willReturn('mysecret');
  287. $this->mapper->method('getToken')
  288. ->with(
  289. $this->callback(function (string $token) {
  290. return hash('sha512', 'unhashedToken'.'1f4h9s') === $token;
  291. })
  292. )->willReturn($token);
  293. $this->assertSame($token, $this->tokenProvider->getToken('unhashedToken'));
  294. }
  295. public function testGetInvalidToken() {
  296. $this->expectException(InvalidTokenException::class);
  297. $this->mapper->method('getToken')
  298. ->with(
  299. $this->callback(function (string $token) {
  300. return hash('sha512', 'unhashedToken'.'1f4h9s') === $token;
  301. })
  302. )->willThrowException(new DoesNotExistException('nope'));
  303. $this->tokenProvider->getToken('unhashedToken');
  304. }
  305. public function testGetExpiredToken() {
  306. $token = 'token';
  307. $uid = 'user';
  308. $user = 'User';
  309. $password = 'passme';
  310. $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';
  311. $type = IToken::PERMANENT_TOKEN;
  312. $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
  313. $actual->setExpires(42);
  314. $this->mapper->method('getToken')
  315. ->with(
  316. $this->callback(function (string $token) {
  317. return hash('sha512', 'token'.'1f4h9s') === $token;
  318. })
  319. )->willReturn($actual);
  320. try {
  321. $this->tokenProvider->getToken('token');
  322. $this->fail();
  323. } catch (ExpiredTokenException $e) {
  324. $this->assertSame($actual, $e->getToken());
  325. }
  326. }
  327. public function testGetTokenById() {
  328. $token = $this->createMock(PublicKeyToken::class);
  329. $this->mapper->expects($this->once())
  330. ->method('getTokenById')
  331. ->with($this->equalTo(42))
  332. ->willReturn($token);
  333. $this->assertSame($token, $this->tokenProvider->getTokenById(42));
  334. }
  335. public function testGetInvalidTokenById() {
  336. $this->expectException(InvalidTokenException::class);
  337. $this->mapper->expects($this->once())
  338. ->method('getTokenById')
  339. ->with($this->equalTo(42))
  340. ->willThrowException(new DoesNotExistException('nope'));
  341. $this->tokenProvider->getTokenById(42);
  342. }
  343. public function testGetExpiredTokenById() {
  344. $token = new PublicKeyToken();
  345. $token->setExpires(42);
  346. $this->mapper->expects($this->once())
  347. ->method('getTokenById')
  348. ->with($this->equalTo(42))
  349. ->willReturn($token);
  350. try {
  351. $this->tokenProvider->getTokenById(42);
  352. $this->fail();
  353. } catch (ExpiredTokenException $e) {
  354. $this->assertSame($token, $e->getToken());
  355. }
  356. }
  357. public function testRotate() {
  358. $token = 'oldtoken';
  359. $uid = 'user';
  360. $user = 'User';
  361. $password = 'password';
  362. $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';
  363. $type = IToken::PERMANENT_TOKEN;
  364. $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
  365. $new = $this->tokenProvider->rotate($actual, 'oldtoken', 'newtoken');
  366. $this->assertSame('password', $this->tokenProvider->getPassword($new, 'newtoken'));
  367. }
  368. public function testRotateNoPassword() {
  369. $token = 'oldtoken';
  370. $uid = 'user';
  371. $user = 'User';
  372. $password = null;
  373. $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';
  374. $type = IToken::PERMANENT_TOKEN;
  375. $actual = $this->tokenProvider->generateToken($token, $uid, $user, $password, $name, $type, IToken::DO_NOT_REMEMBER);
  376. $oldPrivate = $actual->getPrivateKey();
  377. $new = $this->tokenProvider->rotate($actual, 'oldtoken', 'newtoken');
  378. $newPrivate = $new->getPrivateKey();
  379. $this->assertNotSame($newPrivate, $oldPrivate);
  380. $this->assertNull($new->getPassword());
  381. }
  382. public function testMarkPasswordInvalidInvalidToken() {
  383. $token = $this->createMock(IToken::class);
  384. $this->expectException(InvalidTokenException::class);
  385. $this->tokenProvider->markPasswordInvalid($token, 'tokenId');
  386. }
  387. public function testMarkPasswordInvalid() {
  388. $token = $this->createMock(PublicKeyToken::class);
  389. $token->expects($this->once())
  390. ->method('setPasswordInvalid')
  391. ->with(true);
  392. $this->mapper->expects($this->once())
  393. ->method('update')
  394. ->with($token);
  395. $this->tokenProvider->markPasswordInvalid($token, 'tokenId');
  396. }
  397. public function testUpdatePasswords() {
  398. $uid = 'myUID';
  399. $token1 = $this->tokenProvider->generateToken(
  400. 'foo',
  401. $uid,
  402. $uid,
  403. 'bar',
  404. 'random1',
  405. IToken::PERMANENT_TOKEN,
  406. IToken::REMEMBER);
  407. $token2 = $this->tokenProvider->generateToken(
  408. 'foobar',
  409. $uid,
  410. $uid,
  411. 'bar',
  412. 'random2',
  413. IToken::PERMANENT_TOKEN,
  414. IToken::REMEMBER);
  415. $this->mapper->method('hasExpiredTokens')
  416. ->with($uid)
  417. ->willReturn(true);
  418. $this->mapper->expects($this->once())
  419. ->method('getTokenByUser')
  420. ->with($uid)
  421. ->willReturn([$token1, $token2]);
  422. $this->mapper->expects($this->exactly(2))
  423. ->method('update')
  424. ->with($this->callback(function (PublicKeyToken $t) use ($token1, $token2) {
  425. return $t === $token1 || $t === $token2;
  426. }));
  427. $this->tokenProvider->updatePasswords($uid, 'bar2');
  428. }
  429. }