Manager.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright 2018, Roeland Jago Douma <roeland@famdouma.nl>
  5. *
  6. * @author Roeland Jago Douma <roeland@famdouma.nl>
  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 OC\Authentication\Exceptions\ExpiredTokenException;
  25. use OC\Authentication\Exceptions\InvalidTokenException;
  26. use OC\Authentication\Exceptions\PasswordlessTokenException;
  27. class Manager implements IProvider {
  28. /** @var DefaultTokenProvider */
  29. private $defaultTokenProvider;
  30. /** @var PublicKeyTokenProvider */
  31. private $publicKeyTokenProvider;
  32. public function __construct(DefaultTokenProvider $defaultTokenProvider, PublicKeyTokenProvider $publicKeyTokenProvider) {
  33. $this->defaultTokenProvider = $defaultTokenProvider;
  34. $this->publicKeyTokenProvider = $publicKeyTokenProvider;
  35. }
  36. /**
  37. * Create and persist a new token
  38. *
  39. * @param string $token
  40. * @param string $uid
  41. * @param string $loginName
  42. * @param string|null $password
  43. * @param string $name
  44. * @param int $type token type
  45. * @param int $remember whether the session token should be used for remember-me
  46. * @return IToken
  47. */
  48. public function generateToken(string $token,
  49. string $uid,
  50. string $loginName,
  51. $password,
  52. string $name,
  53. int $type = IToken::TEMPORARY_TOKEN,
  54. int $remember = IToken::DO_NOT_REMEMBER): IToken {
  55. return $this->publicKeyTokenProvider->generateToken(
  56. $token,
  57. $uid,
  58. $loginName,
  59. $password,
  60. $name,
  61. $type,
  62. $remember
  63. );
  64. }
  65. /**
  66. * Save the updated token
  67. *
  68. * @param IToken $token
  69. * @throws InvalidTokenException
  70. */
  71. public function updateToken(IToken $token) {
  72. $provider = $this->getProvider($token);
  73. $provider->updateToken($token);
  74. }
  75. /**
  76. * Update token activity timestamp
  77. *
  78. * @throws InvalidTokenException
  79. * @param IToken $token
  80. */
  81. public function updateTokenActivity(IToken $token) {
  82. $provider = $this->getProvider($token);
  83. $provider->updateTokenActivity($token);
  84. }
  85. /**
  86. * @param string $uid
  87. * @return IToken[]
  88. */
  89. public function getTokenByUser(string $uid): array {
  90. $old = $this->defaultTokenProvider->getTokenByUser($uid);
  91. $new = $this->publicKeyTokenProvider->getTokenByUser($uid);
  92. return array_merge($old, $new);
  93. }
  94. /**
  95. * Get a token by token
  96. *
  97. * @param string $tokenId
  98. * @throws InvalidTokenException
  99. * @return IToken
  100. */
  101. public function getToken(string $tokenId): IToken {
  102. try {
  103. return $this->publicKeyTokenProvider->getToken($tokenId);
  104. } catch (ExpiredTokenException $e) {
  105. throw $e;
  106. } catch(InvalidTokenException $e) {
  107. // No worries we try to convert it to a PublicKey Token
  108. }
  109. //Convert!
  110. $token = $this->defaultTokenProvider->getToken($tokenId);
  111. try {
  112. $password = $this->defaultTokenProvider->getPassword($token, $tokenId);
  113. } catch (PasswordlessTokenException $e) {
  114. $password = null;
  115. }
  116. return $this->publicKeyTokenProvider->convertToken($token, $tokenId, $password);
  117. }
  118. /**
  119. * Get a token by token id
  120. *
  121. * @param int $tokenId
  122. * @throws InvalidTokenException
  123. * @return IToken
  124. */
  125. public function getTokenById(int $tokenId): IToken {
  126. try {
  127. return $this->publicKeyTokenProvider->getTokenById($tokenId);
  128. } catch (ExpiredTokenException $e) {
  129. throw $e;
  130. } catch (InvalidTokenException $e) {
  131. return $this->defaultTokenProvider->getTokenById($tokenId);
  132. }
  133. }
  134. /**
  135. * @param string $oldSessionId
  136. * @param string $sessionId
  137. * @throws InvalidTokenException
  138. */
  139. public function renewSessionToken(string $oldSessionId, string $sessionId) {
  140. try {
  141. $this->publicKeyTokenProvider->renewSessionToken($oldSessionId, $sessionId);
  142. } catch (ExpiredTokenException $e) {
  143. throw $e;
  144. } catch (InvalidTokenException $e) {
  145. $this->defaultTokenProvider->renewSessionToken($oldSessionId, $sessionId);
  146. }
  147. }
  148. /**
  149. * @param IToken $savedToken
  150. * @param string $tokenId session token
  151. * @throws InvalidTokenException
  152. * @throws PasswordlessTokenException
  153. * @return string
  154. */
  155. public function getPassword(IToken $savedToken, string $tokenId): string {
  156. $provider = $this->getProvider($savedToken);
  157. return $provider->getPassword($savedToken, $tokenId);
  158. }
  159. public function setPassword(IToken $token, string $tokenId, string $password) {
  160. $provider = $this->getProvider($token);
  161. $provider->setPassword($token, $tokenId, $password);
  162. }
  163. public function invalidateToken(string $token) {
  164. $this->defaultTokenProvider->invalidateToken($token);
  165. $this->publicKeyTokenProvider->invalidateToken($token);
  166. }
  167. public function invalidateTokenById(string $uid, int $id) {
  168. $this->defaultTokenProvider->invalidateTokenById($uid, $id);
  169. $this->publicKeyTokenProvider->invalidateTokenById($uid, $id);
  170. }
  171. public function invalidateOldTokens() {
  172. $this->defaultTokenProvider->invalidateOldTokens();
  173. $this->publicKeyTokenProvider->invalidateOldTokens();
  174. }
  175. /**
  176. * @param IToken $token
  177. * @param string $oldTokenId
  178. * @param string $newTokenId
  179. * @return IToken
  180. * @throws InvalidTokenException
  181. */
  182. public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken {
  183. if ($token instanceof DefaultToken) {
  184. try {
  185. $password = $this->defaultTokenProvider->getPassword($token, $oldTokenId);
  186. } catch (PasswordlessTokenException $e) {
  187. $password = null;
  188. }
  189. return $this->publicKeyTokenProvider->convertToken($token, $newTokenId, $password);
  190. }
  191. if ($token instanceof PublicKeyToken) {
  192. return $this->publicKeyTokenProvider->rotate($token, $oldTokenId, $newTokenId);
  193. }
  194. throw new InvalidTokenException();
  195. }
  196. /**
  197. * @param IToken $token
  198. * @return IProvider
  199. * @throws InvalidTokenException
  200. */
  201. private function getProvider(IToken $token): IProvider {
  202. if ($token instanceof DefaultToken) {
  203. return $this->defaultTokenProvider;
  204. }
  205. if ($token instanceof PublicKeyToken) {
  206. return $this->publicKeyTokenProvider;
  207. }
  208. throw new InvalidTokenException();
  209. }
  210. public function markPasswordInvalid(IToken $token, string $tokenId) {
  211. $this->getProvider($token)->markPasswordInvalid($token, $tokenId);
  212. }
  213. public function updatePasswords(string $uid, string $password) {
  214. $this->defaultTokenProvider->updatePasswords($uid, $password);
  215. $this->publicKeyTokenProvider->updatePasswords($uid, $password);
  216. }
  217. }