IProvider.php 5.1 KB

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