You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Store.php 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * @copyright 2016 Christoph Wurst <christoph@winzerhof-wurst.at>
  5. *
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Lionel Elie Mamane <lionel@mamane.lu>
  8. *
  9. * @license GNU AGPL version 3 or any later version
  10. *
  11. * This program is free software: you can redistribute it and/or modify
  12. * it under the terms of the GNU Affero General Public License as
  13. * published by the Free Software Foundation, either version 3 of the
  14. * License, or (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. *
  24. */
  25. namespace OC\Authentication\LoginCredentials;
  26. use OC\Authentication\Exceptions\InvalidTokenException;
  27. use OC\Authentication\Exceptions\PasswordlessTokenException;
  28. use OC\Authentication\Token\IProvider;
  29. use OCP\Authentication\Exceptions\CredentialsUnavailableException;
  30. use OCP\Authentication\LoginCredentials\ICredentials;
  31. use OCP\Authentication\LoginCredentials\IStore;
  32. use OCP\ILogger;
  33. use OCP\ISession;
  34. use OCP\Session\Exceptions\SessionNotAvailableException;
  35. use OCP\Util;
  36. class Store implements IStore {
  37. /** @var ISession */
  38. private $session;
  39. /** @var ILogger */
  40. private $logger;
  41. /** @var IProvider|null */
  42. private $tokenProvider;
  43. /**
  44. * @param ISession $session
  45. * @param ILogger $logger
  46. * @param IProvider $tokenProvider
  47. */
  48. public function __construct(ISession $session, ILogger $logger, IProvider $tokenProvider = null) {
  49. $this->session = $session;
  50. $this->logger = $logger;
  51. $this->tokenProvider = $tokenProvider;
  52. Util::connectHook('OC_User', 'post_login', $this, 'authenticate');
  53. }
  54. /**
  55. * Hook listener on post login
  56. *
  57. * @param array $params
  58. */
  59. public function authenticate(array $params) {
  60. $this->session->set('login_credentials', json_encode($params));
  61. }
  62. /**
  63. * Replace the session implementation
  64. *
  65. * @param ISession $session
  66. */
  67. public function setSession(ISession $session) {
  68. $this->session = $session;
  69. }
  70. /**
  71. * @since 12
  72. *
  73. * @return ICredentials the login credentials of the current user
  74. * @throws CredentialsUnavailableException
  75. */
  76. public function getLoginCredentials(): ICredentials {
  77. if ($this->tokenProvider === null) {
  78. throw new CredentialsUnavailableException();
  79. }
  80. $trySession = false;
  81. try {
  82. $sessionId = $this->session->getId();
  83. $token = $this->tokenProvider->getToken($sessionId);
  84. $uid = $token->getUID();
  85. $user = $token->getLoginName();
  86. $password = $this->tokenProvider->getPassword($token, $sessionId);
  87. return new Credentials($uid, $user, $password);
  88. } catch (SessionNotAvailableException $ex) {
  89. $this->logger->debug('could not get login credentials because session is unavailable', ['app' => 'core']);
  90. } catch (InvalidTokenException $ex) {
  91. $this->logger->debug('could not get login credentials because the token is invalid', ['app' => 'core']);
  92. $trySession = true;
  93. } catch (PasswordlessTokenException $ex) {
  94. $this->logger->debug('could not get login credentials because the token has no password', ['app' => 'core']);
  95. $trySession = true;
  96. }
  97. if ($trySession && $this->session->exists('login_credentials')) {
  98. $creds = json_decode($this->session->get('login_credentials'));
  99. return new Credentials($creds->uid, $creds->loginName, $creds->password);
  100. }
  101. // If we reach this line, an exception was thrown.
  102. throw new CredentialsUnavailableException();
  103. }
  104. }