DefaultTokenProvider.php 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. * @copyright Copyright (c) 2016, Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@owncloud.com>
  7. *
  8. * @license AGPL-3.0
  9. *
  10. * This code is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License, version 3,
  12. * as published by the Free Software Foundation.
  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, version 3,
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>
  21. *
  22. */
  23. namespace OC\Authentication\Token;
  24. use Exception;
  25. use OC\Authentication\Exceptions\InvalidTokenException;
  26. use OC\Authentication\Exceptions\PasswordlessTokenException;
  27. use OCP\AppFramework\Db\DoesNotExistException;
  28. use OCP\AppFramework\Utility\ITimeFactory;
  29. use OCP\IConfig;
  30. use OCP\ILogger;
  31. use OCP\IUser;
  32. use OCP\Security\ICrypto;
  33. class DefaultTokenProvider implements IProvider {
  34. /** @var DefaultTokenMapper */
  35. private $mapper;
  36. /** @var ICrypto */
  37. private $crypto;
  38. /** @var IConfig */
  39. private $config;
  40. /** @var ILogger $logger */
  41. private $logger;
  42. /** @var ITimeFactory $time */
  43. private $time;
  44. /**
  45. * @param DefaultTokenMapper $mapper
  46. * @param ICrypto $crypto
  47. * @param IConfig $config
  48. * @param ILogger $logger
  49. * @param ITimeFactory $time
  50. */
  51. public function __construct(DefaultTokenMapper $mapper,
  52. ICrypto $crypto,
  53. IConfig $config,
  54. ILogger $logger,
  55. ITimeFactory $time) {
  56. $this->mapper = $mapper;
  57. $this->crypto = $crypto;
  58. $this->config = $config;
  59. $this->logger = $logger;
  60. $this->time = $time;
  61. }
  62. /**
  63. * Create and persist a new token
  64. *
  65. * @param string $token
  66. * @param string $uid
  67. * @param string $loginName
  68. * @param string|null $password
  69. * @param string $name
  70. * @param int $type token type
  71. * @param int $remember whether the session token should be used for remember-me
  72. * @return IToken
  73. */
  74. public function generateToken($token, $uid, $loginName, $password, $name, $type = IToken::TEMPORARY_TOKEN, $remember = IToken::DO_NOT_REMEMBER) {
  75. $dbToken = new DefaultToken();
  76. $dbToken->setUid($uid);
  77. $dbToken->setLoginName($loginName);
  78. if (!is_null($password)) {
  79. $dbToken->setPassword($this->encryptPassword($password, $token));
  80. }
  81. $dbToken->setName($name);
  82. $dbToken->setToken($this->hashToken($token));
  83. $dbToken->setType($type);
  84. $dbToken->setRemember($remember);
  85. $dbToken->setLastActivity($this->time->getTime());
  86. $this->mapper->insert($dbToken);
  87. return $dbToken;
  88. }
  89. /**
  90. * Save the updated token
  91. *
  92. * @param IToken $token
  93. * @throws InvalidTokenException
  94. */
  95. public function updateToken(IToken $token) {
  96. if (!($token instanceof DefaultToken)) {
  97. throw new InvalidTokenException();
  98. }
  99. $this->mapper->update($token);
  100. }
  101. /**
  102. * Update token activity timestamp
  103. *
  104. * @throws InvalidTokenException
  105. * @param IToken $token
  106. */
  107. public function updateTokenActivity(IToken $token) {
  108. if (!($token instanceof DefaultToken)) {
  109. throw new InvalidTokenException();
  110. }
  111. /** @var DefaultToken $token */
  112. $now = $this->time->getTime();
  113. if ($token->getLastActivity() < ($now - 60)) {
  114. // Update token only once per minute
  115. $token->setLastActivity($now);
  116. $this->mapper->update($token);
  117. }
  118. }
  119. /**
  120. * Get all token of a user
  121. *
  122. * The provider may limit the number of result rows in case of an abuse
  123. * where a high number of (session) tokens is generated
  124. *
  125. * @param IUser $user
  126. * @return IToken[]
  127. */
  128. public function getTokenByUser(IUser $user) {
  129. return $this->mapper->getTokenByUser($user);
  130. }
  131. /**
  132. * Get a token by token
  133. *
  134. * @param string $tokenId
  135. * @throws InvalidTokenException
  136. * @return DefaultToken
  137. */
  138. public function getToken($tokenId) {
  139. try {
  140. return $this->mapper->getToken($this->hashToken($tokenId));
  141. } catch (DoesNotExistException $ex) {
  142. throw new InvalidTokenException();
  143. }
  144. }
  145. /**
  146. * Get a token by token id
  147. *
  148. * @param string $tokenId
  149. * @throws InvalidTokenException
  150. * @return DefaultToken
  151. */
  152. public function getTokenById($tokenId) {
  153. try {
  154. return $this->mapper->getTokenById($tokenId);
  155. } catch (DoesNotExistException $ex) {
  156. throw new InvalidTokenException();
  157. }
  158. }
  159. /**
  160. * @param string $oldSessionId
  161. * @param string $sessionId
  162. * @throws InvalidTokenException
  163. */
  164. public function renewSessionToken($oldSessionId, $sessionId) {
  165. $token = $this->getToken($oldSessionId);
  166. $newToken = new DefaultToken();
  167. $newToken->setUid($token->getUID());
  168. $newToken->setLoginName($token->getLoginName());
  169. if (!is_null($token->getPassword())) {
  170. $password = $this->decryptPassword($token->getPassword(), $oldSessionId);
  171. $newToken->setPassword($this->encryptPassword($password, $sessionId));
  172. }
  173. $newToken->setName($token->getName());
  174. $newToken->setToken($this->hashToken($sessionId));
  175. $newToken->setType(IToken::TEMPORARY_TOKEN);
  176. $newToken->setRemember($token->getRemember());
  177. $newToken->setLastActivity($this->time->getTime());
  178. $this->mapper->insert($newToken);
  179. }
  180. /**
  181. * @param IToken $savedToken
  182. * @param string $tokenId session token
  183. * @throws InvalidTokenException
  184. * @throws PasswordlessTokenException
  185. * @return string
  186. */
  187. public function getPassword(IToken $savedToken, $tokenId) {
  188. $password = $savedToken->getPassword();
  189. if (is_null($password)) {
  190. throw new PasswordlessTokenException();
  191. }
  192. return $this->decryptPassword($password, $tokenId);
  193. }
  194. /**
  195. * Encrypt and set the password of the given token
  196. *
  197. * @param IToken $token
  198. * @param string $tokenId
  199. * @param string $password
  200. * @throws InvalidTokenException
  201. */
  202. public function setPassword(IToken $token, $tokenId, $password) {
  203. if (!($token instanceof DefaultToken)) {
  204. throw new InvalidTokenException();
  205. }
  206. /** @var DefaultToken $token */
  207. $token->setPassword($this->encryptPassword($password, $tokenId));
  208. $this->mapper->update($token);
  209. }
  210. /**
  211. * Invalidate (delete) the given session token
  212. *
  213. * @param string $token
  214. */
  215. public function invalidateToken($token) {
  216. $this->mapper->invalidate($this->hashToken($token));
  217. }
  218. /**
  219. * Invalidate (delete) the given token
  220. *
  221. * @param IUser $user
  222. * @param int $id
  223. */
  224. public function invalidateTokenById(IUser $user, $id) {
  225. $this->mapper->deleteById($user, $id);
  226. }
  227. /**
  228. * Invalidate (delete) old session tokens
  229. */
  230. public function invalidateOldTokens() {
  231. $olderThan = $this->time->getTime() - (int) $this->config->getSystemValue('session_lifetime', 60 * 60 * 24);
  232. $this->logger->debug('Invalidating session tokens older than ' . date('c', $olderThan), ['app' => 'cron']);
  233. $this->mapper->invalidateOld($olderThan, IToken::DO_NOT_REMEMBER);
  234. $rememberThreshold = $this->time->getTime() - (int) $this->config->getSystemValue('remember_login_cookie_lifetime', 60 * 60 * 24 * 15);
  235. $this->logger->debug('Invalidating remembered session tokens older than ' . date('c', $rememberThreshold), ['app' => 'cron']);
  236. $this->mapper->invalidateOld($rememberThreshold, IToken::REMEMBER);
  237. }
  238. /**
  239. * @param string $token
  240. * @return string
  241. */
  242. private function hashToken($token) {
  243. $secret = $this->config->getSystemValue('secret');
  244. return hash('sha512', $token . $secret);
  245. }
  246. /**
  247. * Encrypt the given password
  248. *
  249. * The token is used as key
  250. *
  251. * @param string $password
  252. * @param string $token
  253. * @return string encrypted password
  254. */
  255. private function encryptPassword($password, $token) {
  256. $secret = $this->config->getSystemValue('secret');
  257. return $this->crypto->encrypt($password, $token . $secret);
  258. }
  259. /**
  260. * Decrypt the given password
  261. *
  262. * The token is used as key
  263. *
  264. * @param string $password
  265. * @param string $token
  266. * @throws InvalidTokenException
  267. * @return string the decrypted key
  268. */
  269. private function decryptPassword($password, $token) {
  270. $secret = $this->config->getSystemValue('secret');
  271. try {
  272. return $this->crypto->decrypt($password, $token . $secret);
  273. } catch (Exception $ex) {
  274. // Delete the invalid token
  275. $this->invalidateToken($token);
  276. throw new InvalidTokenException();
  277. }
  278. }
  279. }