Store.php 3.6 KB

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