IProvider.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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\PasswordlessTokenException;
  30. use OCP\Authentication\Exceptions\ExpiredTokenException;
  31. use OCP\Authentication\Exceptions\InvalidTokenException;
  32. use OCP\Authentication\Exceptions\WipeTokenException;
  33. use OCP\Authentication\Token\IToken as OCPIToken;
  34. interface IProvider {
  35. /**
  36. * Create and persist a new token
  37. *
  38. * @param string $token
  39. * @param string $uid
  40. * @param string $loginName
  41. * @param string|null $password
  42. * @param string $name Name will be trimmed to 120 chars when longer
  43. * @param int $type token type
  44. * @param int $remember whether the session token should be used for remember-me
  45. * @return OCPIToken
  46. * @throws \RuntimeException when OpenSSL reports a problem
  47. */
  48. public function generateToken(string $token,
  49. string $uid,
  50. string $loginName,
  51. ?string $password,
  52. string $name,
  53. int $type = OCPIToken::TEMPORARY_TOKEN,
  54. int $remember = OCPIToken::DO_NOT_REMEMBER): OCPIToken;
  55. /**
  56. * Get a token by token id
  57. *
  58. * @param string $tokenId
  59. * @throws InvalidTokenException
  60. * @throws ExpiredTokenException
  61. * @throws WipeTokenException
  62. * @return OCPIToken
  63. */
  64. public function getToken(string $tokenId): OCPIToken;
  65. /**
  66. * Get a token by token id
  67. *
  68. * @param int $tokenId
  69. * @throws InvalidTokenException
  70. * @throws ExpiredTokenException
  71. * @throws WipeTokenException
  72. * @return OCPIToken
  73. */
  74. public function getTokenById(int $tokenId): OCPIToken;
  75. /**
  76. * Duplicate an existing session token
  77. *
  78. * @param string $oldSessionId
  79. * @param string $sessionId
  80. * @throws InvalidTokenException
  81. * @throws \RuntimeException when OpenSSL reports a problem
  82. * @return OCPIToken The new token
  83. */
  84. public function renewSessionToken(string $oldSessionId, string $sessionId): OCPIToken;
  85. /**
  86. * Invalidate (delete) the given session token
  87. *
  88. * @param string $token
  89. */
  90. public function invalidateToken(string $token);
  91. /**
  92. * Invalidate (delete) the given token
  93. *
  94. * @param string $uid
  95. * @param int $id
  96. */
  97. public function invalidateTokenById(string $uid, int $id);
  98. /**
  99. * Invalidate (delete) old session tokens
  100. */
  101. public function invalidateOldTokens();
  102. /**
  103. * Invalidate (delete) tokens last used before a given date
  104. */
  105. public function invalidateLastUsedBefore(string $uid, int $before): void;
  106. /**
  107. * Save the updated token
  108. *
  109. * @param OCPIToken $token
  110. */
  111. public function updateToken(OCPIToken $token);
  112. /**
  113. * Update token activity timestamp
  114. *
  115. * @param OCPIToken $token
  116. */
  117. public function updateTokenActivity(OCPIToken $token);
  118. /**
  119. * Get all tokens of a user
  120. *
  121. * The provider may limit the number of result rows in case of an abuse
  122. * where a high number of (session) tokens is generated
  123. *
  124. * @param string $uid
  125. * @return OCPIToken[]
  126. */
  127. public function getTokenByUser(string $uid): array;
  128. /**
  129. * Get the (unencrypted) password of the given token
  130. *
  131. * @param OCPIToken $savedToken
  132. * @param string $tokenId
  133. * @throws InvalidTokenException
  134. * @throws PasswordlessTokenException
  135. * @return string
  136. */
  137. public function getPassword(OCPIToken $savedToken, string $tokenId): string;
  138. /**
  139. * Encrypt and set the password of the given token
  140. *
  141. * @param OCPIToken $token
  142. * @param string $tokenId
  143. * @param string $password
  144. * @throws InvalidTokenException
  145. */
  146. public function setPassword(OCPIToken $token, string $tokenId, string $password);
  147. /**
  148. * Rotate the token. Useful for for example oauth tokens
  149. *
  150. * @param OCPIToken $token
  151. * @param string $oldTokenId
  152. * @param string $newTokenId
  153. * @return OCPIToken
  154. * @throws \RuntimeException when OpenSSL reports a problem
  155. */
  156. public function rotate(OCPIToken $token, string $oldTokenId, string $newTokenId): OCPIToken;
  157. /**
  158. * Marks a token as having an invalid password.
  159. *
  160. * @param OCPIToken $token
  161. * @param string $tokenId
  162. */
  163. public function markPasswordInvalid(OCPIToken $token, string $tokenId);
  164. /**
  165. * Update all the passwords of $uid if required
  166. *
  167. * @param string $uid
  168. * @param string $password
  169. */
  170. public function updatePasswords(string $uid, string $password);
  171. }