Manager.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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
  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. try {
  58. return $this->publicKeyTokenProvider->generateToken(
  59. $token,
  60. $uid,
  61. $loginName,
  62. $password,
  63. $name,
  64. $type,
  65. $remember
  66. );
  67. } catch (UniqueConstraintViolationException $e) {
  68. // It's rare, but if two requests of the same session (e.g. env-based SAML)
  69. // try to create the session token they might end up here at the same time
  70. // because we use the session ID as token and the db token is created anew
  71. // with every request.
  72. //
  73. // If the UIDs match, then this should be fine.
  74. $existing = $this->getToken($token);
  75. if ($existing->getUID() !== $uid) {
  76. throw new \Exception('Token conflict handled, but UIDs do not match. This should not happen', 0, $e);
  77. }
  78. return $existing;
  79. }
  80. }
  81. /**
  82. * Save the updated token
  83. *
  84. * @param IToken $token
  85. * @throws InvalidTokenException
  86. */
  87. public function updateToken(IToken $token) {
  88. $provider = $this->getProvider($token);
  89. $provider->updateToken($token);
  90. }
  91. /**
  92. * Update token activity timestamp
  93. *
  94. * @throws InvalidTokenException
  95. * @param IToken $token
  96. */
  97. public function updateTokenActivity(IToken $token) {
  98. $provider = $this->getProvider($token);
  99. $provider->updateTokenActivity($token);
  100. }
  101. /**
  102. * @param string $uid
  103. * @return IToken[]
  104. */
  105. public function getTokenByUser(string $uid): array {
  106. return $this->publicKeyTokenProvider->getTokenByUser($uid);
  107. }
  108. /**
  109. * Get a token by token
  110. *
  111. * @param string $tokenId
  112. * @throws InvalidTokenException
  113. * @throws \RuntimeException when OpenSSL reports a problem
  114. * @return IToken
  115. */
  116. public function getToken(string $tokenId): IToken {
  117. try {
  118. return $this->publicKeyTokenProvider->getToken($tokenId);
  119. } catch (WipeTokenException $e) {
  120. throw $e;
  121. } catch (ExpiredTokenException $e) {
  122. throw $e;
  123. } catch (InvalidTokenException $e) {
  124. throw $e;
  125. }
  126. }
  127. /**
  128. * Get a token by token id
  129. *
  130. * @param int $tokenId
  131. * @throws InvalidTokenException
  132. * @return IToken
  133. */
  134. public function getTokenById(int $tokenId): IToken {
  135. try {
  136. return $this->publicKeyTokenProvider->getTokenById($tokenId);
  137. } catch (ExpiredTokenException $e) {
  138. throw $e;
  139. } catch (WipeTokenException $e) {
  140. throw $e;
  141. } catch (InvalidTokenException $e) {
  142. throw $e;
  143. }
  144. }
  145. /**
  146. * @param string $oldSessionId
  147. * @param string $sessionId
  148. * @throws InvalidTokenException
  149. * @return IToken
  150. */
  151. public function renewSessionToken(string $oldSessionId, string $sessionId): IToken {
  152. try {
  153. return $this->publicKeyTokenProvider->renewSessionToken($oldSessionId, $sessionId);
  154. } catch (ExpiredTokenException $e) {
  155. throw $e;
  156. } catch (InvalidTokenException $e) {
  157. throw $e;
  158. }
  159. }
  160. /**
  161. * @param IToken $savedToken
  162. * @param string $tokenId session token
  163. * @throws InvalidTokenException
  164. * @throws PasswordlessTokenException
  165. * @return string
  166. */
  167. public function getPassword(IToken $savedToken, string $tokenId): string {
  168. $provider = $this->getProvider($savedToken);
  169. return $provider->getPassword($savedToken, $tokenId);
  170. }
  171. public function setPassword(IToken $token, string $tokenId, string $password) {
  172. $provider = $this->getProvider($token);
  173. $provider->setPassword($token, $tokenId, $password);
  174. }
  175. public function invalidateToken(string $token) {
  176. $this->publicKeyTokenProvider->invalidateToken($token);
  177. }
  178. public function invalidateTokenById(string $uid, int $id) {
  179. $this->publicKeyTokenProvider->invalidateTokenById($uid, $id);
  180. }
  181. public function invalidateOldTokens() {
  182. $this->publicKeyTokenProvider->invalidateOldTokens();
  183. }
  184. /**
  185. * @param IToken $token
  186. * @param string $oldTokenId
  187. * @param string $newTokenId
  188. * @return IToken
  189. * @throws InvalidTokenException
  190. * @throws \RuntimeException when OpenSSL reports a problem
  191. */
  192. public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken {
  193. if ($token instanceof PublicKeyToken) {
  194. return $this->publicKeyTokenProvider->rotate($token, $oldTokenId, $newTokenId);
  195. }
  196. throw new InvalidTokenException();
  197. }
  198. /**
  199. * @param IToken $token
  200. * @return IProvider
  201. * @throws InvalidTokenException
  202. */
  203. private function getProvider(IToken $token): IProvider {
  204. if ($token instanceof PublicKeyToken) {
  205. return $this->publicKeyTokenProvider;
  206. }
  207. throw new InvalidTokenException();
  208. }
  209. public function markPasswordInvalid(IToken $token, string $tokenId) {
  210. $this->getProvider($token)->markPasswordInvalid($token, $tokenId);
  211. }
  212. public function updatePasswords(string $uid, string $password) {
  213. $this->publicKeyTokenProvider->updatePasswords($uid, $password);
  214. }
  215. }