DefaultTokenProvider.php 6.4 KB

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