Manager.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Morris Jobke <hey@morrisjobke.de>
  8. * @author Roeland Jago Douma <roeland@famdouma.nl>
  9. *
  10. * @license GNU AGPL version 3 or any later version
  11. *
  12. * This program is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License as
  14. * published by the Free Software Foundation, either version 3 of the
  15. * License, or (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU Affero General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU Affero General Public License
  23. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. *
  25. */
  26. namespace OC\Authentication\Token;
  27. use Doctrine\DBAL\Exception\UniqueConstraintViolationException;
  28. use OC\Authentication\Exceptions\ExpiredTokenException;
  29. use OC\Authentication\Exceptions\InvalidTokenException;
  30. use OC\Authentication\Exceptions\PasswordlessTokenException;
  31. use OC\Authentication\Exceptions\WipeTokenException;
  32. class Manager implements IProvider {
  33. /** @var PublicKeyTokenProvider */
  34. private $publicKeyTokenProvider;
  35. public function __construct(PublicKeyTokenProvider $publicKeyTokenProvider) {
  36. $this->publicKeyTokenProvider = $publicKeyTokenProvider;
  37. }
  38. /**
  39. * Create and persist a new token
  40. *
  41. * @param string $token
  42. * @param string $uid
  43. * @param string $loginName
  44. * @param string|null $password
  45. * @param string $name Name will be trimmed to 120 chars when longer
  46. * @param int $type token type
  47. * @param int $remember whether the session token should be used for remember-me
  48. * @return IToken
  49. */
  50. public function generateToken(string $token,
  51. string $uid,
  52. string $loginName,
  53. $password,
  54. string $name,
  55. int $type = IToken::TEMPORARY_TOKEN,
  56. int $remember = IToken::DO_NOT_REMEMBER): IToken {
  57. if (mb_strlen($name) > 128) {
  58. $name = mb_substr($name, 0, 120) . '…';
  59. }
  60. try {
  61. return $this->publicKeyTokenProvider->generateToken(
  62. $token,
  63. $uid,
  64. $loginName,
  65. $password,
  66. $name,
  67. $type,
  68. $remember
  69. );
  70. } catch (UniqueConstraintViolationException $e) {
  71. // It's rare, but if two requests of the same session (e.g. env-based SAML)
  72. // try to create the session token they might end up here at the same time
  73. // because we use the session ID as token and the db token is created anew
  74. // with every request.
  75. //
  76. // If the UIDs match, then this should be fine.
  77. $existing = $this->getToken($token);
  78. if ($existing->getUID() !== $uid) {
  79. throw new \Exception('Token conflict handled, but UIDs do not match. This should not happen', 0, $e);
  80. }
  81. return $existing;
  82. }
  83. }
  84. /**
  85. * Save the updated token
  86. *
  87. * @param IToken $token
  88. * @throws InvalidTokenException
  89. */
  90. public function updateToken(IToken $token) {
  91. $provider = $this->getProvider($token);
  92. $provider->updateToken($token);
  93. }
  94. /**
  95. * Update token activity timestamp
  96. *
  97. * @throws InvalidTokenException
  98. * @param IToken $token
  99. */
  100. public function updateTokenActivity(IToken $token) {
  101. $provider = $this->getProvider($token);
  102. $provider->updateTokenActivity($token);
  103. }
  104. /**
  105. * @param string $uid
  106. * @return IToken[]
  107. */
  108. public function getTokenByUser(string $uid): array {
  109. return $this->publicKeyTokenProvider->getTokenByUser($uid);
  110. }
  111. /**
  112. * Get a token by token
  113. *
  114. * @param string $tokenId
  115. * @throws InvalidTokenException
  116. * @throws \RuntimeException when OpenSSL reports a problem
  117. * @return IToken
  118. */
  119. public function getToken(string $tokenId): IToken {
  120. try {
  121. return $this->publicKeyTokenProvider->getToken($tokenId);
  122. } catch (WipeTokenException $e) {
  123. throw $e;
  124. } catch (ExpiredTokenException $e) {
  125. throw $e;
  126. } catch (InvalidTokenException $e) {
  127. throw $e;
  128. }
  129. }
  130. /**
  131. * Get a token by token id
  132. *
  133. * @param int $tokenId
  134. * @throws InvalidTokenException
  135. * @return IToken
  136. */
  137. public function getTokenById(int $tokenId): IToken {
  138. try {
  139. return $this->publicKeyTokenProvider->getTokenById($tokenId);
  140. } catch (ExpiredTokenException $e) {
  141. throw $e;
  142. } catch (WipeTokenException $e) {
  143. throw $e;
  144. } catch (InvalidTokenException $e) {
  145. throw $e;
  146. }
  147. }
  148. /**
  149. * @param string $oldSessionId
  150. * @param string $sessionId
  151. * @throws InvalidTokenException
  152. * @return IToken
  153. */
  154. public function renewSessionToken(string $oldSessionId, string $sessionId): IToken {
  155. try {
  156. return $this->publicKeyTokenProvider->renewSessionToken($oldSessionId, $sessionId);
  157. } catch (ExpiredTokenException $e) {
  158. throw $e;
  159. } catch (InvalidTokenException $e) {
  160. throw $e;
  161. }
  162. }
  163. /**
  164. * @param IToken $savedToken
  165. * @param string $tokenId session token
  166. * @throws InvalidTokenException
  167. * @throws PasswordlessTokenException
  168. * @return string
  169. */
  170. public function getPassword(IToken $savedToken, string $tokenId): string {
  171. $provider = $this->getProvider($savedToken);
  172. return $provider->getPassword($savedToken, $tokenId);
  173. }
  174. public function setPassword(IToken $token, string $tokenId, string $password) {
  175. $provider = $this->getProvider($token);
  176. $provider->setPassword($token, $tokenId, $password);
  177. }
  178. public function invalidateToken(string $token) {
  179. $this->publicKeyTokenProvider->invalidateToken($token);
  180. }
  181. public function invalidateTokenById(string $uid, int $id) {
  182. $this->publicKeyTokenProvider->invalidateTokenById($uid, $id);
  183. }
  184. public function invalidateOldTokens() {
  185. $this->publicKeyTokenProvider->invalidateOldTokens();
  186. }
  187. /**
  188. * @param IToken $token
  189. * @param string $oldTokenId
  190. * @param string $newTokenId
  191. * @return IToken
  192. * @throws InvalidTokenException
  193. * @throws \RuntimeException when OpenSSL reports a problem
  194. */
  195. public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken {
  196. if ($token instanceof PublicKeyToken) {
  197. return $this->publicKeyTokenProvider->rotate($token, $oldTokenId, $newTokenId);
  198. }
  199. throw new InvalidTokenException();
  200. }
  201. /**
  202. * @param IToken $token
  203. * @return IProvider
  204. * @throws InvalidTokenException
  205. */
  206. private function getProvider(IToken $token): IProvider {
  207. if ($token instanceof PublicKeyToken) {
  208. return $this->publicKeyTokenProvider;
  209. }
  210. throw new InvalidTokenException();
  211. }
  212. public function markPasswordInvalid(IToken $token, string $tokenId) {
  213. $this->getProvider($token)->markPasswordInvalid($token, $tokenId);
  214. }
  215. public function updatePasswords(string $uid, string $password) {
  216. $this->publicKeyTokenProvider->updatePasswords($uid, $password);
  217. }
  218. }