IProvider.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright Copyright (c) 2016, ownCloud, Inc.
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Lukas Reschke <lukas@statuscode.ch>
  8. * @author Marcel Waldvogel <marcel.waldvogel@uni-konstanz.de>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OC\Authentication\Token;
  29. use OC\Authentication\Exceptions\ExpiredTokenException;
  30. use OC\Authentication\Exceptions\InvalidTokenException;
  31. use OC\Authentication\Exceptions\PasswordlessTokenException;
  32. use OC\Authentication\Exceptions\WipeTokenException;
  33. interface IProvider {
  34. /**
  35. * Create and persist a new token
  36. *
  37. * @param string $token
  38. * @param string $uid
  39. * @param string $loginName
  40. * @param string|null $password
  41. * @param string $name Name will be trimmed to 120 chars when longer
  42. * @param int $type token type
  43. * @param int $remember whether the session token should be used for remember-me
  44. * @return IToken
  45. * @throws \RuntimeException when OpenSSL reports a problem
  46. */
  47. public function generateToken(string $token,
  48. string $uid,
  49. string $loginName,
  50. ?string $password,
  51. string $name,
  52. int $type = IToken::TEMPORARY_TOKEN,
  53. int $remember = IToken::DO_NOT_REMEMBER): IToken;
  54. /**
  55. * Get a token by token id
  56. *
  57. * @param string $tokenId
  58. * @throws InvalidTokenException
  59. * @throws ExpiredTokenException
  60. * @throws WipeTokenException
  61. * @return IToken
  62. */
  63. public function getToken(string $tokenId): IToken;
  64. /**
  65. * Get a token by token id
  66. *
  67. * @param int $tokenId
  68. * @throws InvalidTokenException
  69. * @throws ExpiredTokenException
  70. * @throws WipeTokenException
  71. * @return IToken
  72. */
  73. public function getTokenById(int $tokenId): IToken;
  74. /**
  75. * Duplicate an existing session token
  76. *
  77. * @param string $oldSessionId
  78. * @param string $sessionId
  79. * @throws InvalidTokenException
  80. * @throws \RuntimeException when OpenSSL reports a problem
  81. * @return IToken The new token
  82. */
  83. public function renewSessionToken(string $oldSessionId, string $sessionId): IToken;
  84. /**
  85. * Invalidate (delete) the given session token
  86. *
  87. * @param string $token
  88. */
  89. public function invalidateToken(string $token);
  90. /**
  91. * Invalidate (delete) the given token
  92. *
  93. * @param string $uid
  94. * @param int $id
  95. */
  96. public function invalidateTokenById(string $uid, int $id);
  97. /**
  98. * Invalidate (delete) old session tokens
  99. */
  100. public function invalidateOldTokens();
  101. /**
  102. * Invalidate (delete) tokens last used before a given date
  103. */
  104. public function invalidateLastUsedBefore(string $uid, int $before): void;
  105. /**
  106. * Save the updated token
  107. *
  108. * @param IToken $token
  109. */
  110. public function updateToken(IToken $token);
  111. /**
  112. * Update token activity timestamp
  113. *
  114. * @param IToken $token
  115. */
  116. public function updateTokenActivity(IToken $token);
  117. /**
  118. * Get all tokens of a user
  119. *
  120. * The provider may limit the number of result rows in case of an abuse
  121. * where a high number of (session) tokens is generated
  122. *
  123. * @param string $uid
  124. * @return IToken[]
  125. */
  126. public function getTokenByUser(string $uid): array;
  127. /**
  128. * Get the (unencrypted) password of the given token
  129. *
  130. * @param IToken $savedToken
  131. * @param string $tokenId
  132. * @throws InvalidTokenException
  133. * @throws PasswordlessTokenException
  134. * @return string
  135. */
  136. public function getPassword(IToken $savedToken, string $tokenId): string;
  137. /**
  138. * Encrypt and set the password of the given token
  139. *
  140. * @param IToken $token
  141. * @param string $tokenId
  142. * @param string $password
  143. * @throws InvalidTokenException
  144. */
  145. public function setPassword(IToken $token, string $tokenId, string $password);
  146. /**
  147. * Rotate the token. Useful for for example oauth tokens
  148. *
  149. * @param IToken $token
  150. * @param string $oldTokenId
  151. * @param string $newTokenId
  152. * @return IToken
  153. * @throws \RuntimeException when OpenSSL reports a problem
  154. */
  155. public function rotate(IToken $token, string $oldTokenId, string $newTokenId): IToken;
  156. /**
  157. * Marks a token as having an invalid password.
  158. *
  159. * @param IToken $token
  160. * @param string $tokenId
  161. */
  162. public function markPasswordInvalid(IToken $token, string $tokenId);
  163. /**
  164. * Update all the passwords of $uid if required
  165. *
  166. * @param string $uid
  167. * @param string $password
  168. */
  169. public function updatePasswords(string $uid, string $password);
  170. }