@@ -40,88 +40,88 @@ |
||
40 | 40 | |
41 | 41 | class Store implements IStore { |
42 | 42 | |
43 | - /** @var ISession */ |
|
44 | - private $session; |
|
45 | - |
|
46 | - /** @var ILogger */ |
|
47 | - private $logger; |
|
48 | - |
|
49 | - /** @var IProvider|null */ |
|
50 | - private $tokenProvider; |
|
51 | - |
|
52 | - /** |
|
53 | - * @param ISession $session |
|
54 | - * @param ILogger $logger |
|
55 | - * @param IProvider $tokenProvider |
|
56 | - */ |
|
57 | - public function __construct(ISession $session, ILogger $logger, IProvider $tokenProvider = null) { |
|
58 | - $this->session = $session; |
|
59 | - $this->logger = $logger; |
|
60 | - $this->tokenProvider = $tokenProvider; |
|
61 | - |
|
62 | - Util::connectHook('OC_User', 'post_login', $this, 'authenticate'); |
|
63 | - } |
|
64 | - |
|
65 | - /** |
|
66 | - * Hook listener on post login |
|
67 | - * |
|
68 | - * @param array $params |
|
69 | - */ |
|
70 | - public function authenticate(array $params) { |
|
71 | - $this->session->set('login_credentials', json_encode($params)); |
|
72 | - } |
|
73 | - |
|
74 | - /** |
|
75 | - * Replace the session implementation |
|
76 | - * |
|
77 | - * @param ISession $session |
|
78 | - */ |
|
79 | - public function setSession(ISession $session) { |
|
80 | - $this->session = $session; |
|
81 | - } |
|
82 | - |
|
83 | - /** |
|
84 | - * @since 12 |
|
85 | - * |
|
86 | - * @return ICredentials the login credentials of the current user |
|
87 | - * @throws CredentialsUnavailableException |
|
88 | - */ |
|
89 | - public function getLoginCredentials(): ICredentials { |
|
90 | - if ($this->tokenProvider === null) { |
|
91 | - throw new CredentialsUnavailableException(); |
|
92 | - } |
|
93 | - |
|
94 | - $trySession = false; |
|
95 | - try { |
|
96 | - $sessionId = $this->session->getId(); |
|
97 | - $token = $this->tokenProvider->getToken($sessionId); |
|
98 | - |
|
99 | - $uid = $token->getUID(); |
|
100 | - $user = $token->getLoginName(); |
|
101 | - $password = $this->tokenProvider->getPassword($token, $sessionId); |
|
102 | - |
|
103 | - return new Credentials($uid, $user, $password); |
|
104 | - } catch (SessionNotAvailableException $ex) { |
|
105 | - $this->logger->debug('could not get login credentials because session is unavailable', ['app' => 'core']); |
|
106 | - } catch (InvalidTokenException $ex) { |
|
107 | - $this->logger->debug('could not get login credentials because the token is invalid', ['app' => 'core']); |
|
108 | - $trySession = true; |
|
109 | - } catch (PasswordlessTokenException $ex) { |
|
110 | - $this->logger->debug('could not get login credentials because the token has no password', ['app' => 'core']); |
|
111 | - $trySession = true; |
|
112 | - } |
|
113 | - |
|
114 | - if ($trySession && $this->session->exists('login_credentials')) { |
|
115 | - /** @var array $creds */ |
|
116 | - $creds = json_decode($this->session->get('login_credentials'), true); |
|
117 | - return new Credentials( |
|
118 | - $creds['uid'], |
|
119 | - $creds['loginName'] ?? $this->session->get('loginname') ?? $creds['uid'], // Pre 20 didn't have a loginName property, hence fall back to the session value and then to the UID |
|
120 | - $creds['password'] |
|
121 | - ); |
|
122 | - } |
|
123 | - |
|
124 | - // If we reach this line, an exception was thrown. |
|
125 | - throw new CredentialsUnavailableException(); |
|
126 | - } |
|
43 | + /** @var ISession */ |
|
44 | + private $session; |
|
45 | + |
|
46 | + /** @var ILogger */ |
|
47 | + private $logger; |
|
48 | + |
|
49 | + /** @var IProvider|null */ |
|
50 | + private $tokenProvider; |
|
51 | + |
|
52 | + /** |
|
53 | + * @param ISession $session |
|
54 | + * @param ILogger $logger |
|
55 | + * @param IProvider $tokenProvider |
|
56 | + */ |
|
57 | + public function __construct(ISession $session, ILogger $logger, IProvider $tokenProvider = null) { |
|
58 | + $this->session = $session; |
|
59 | + $this->logger = $logger; |
|
60 | + $this->tokenProvider = $tokenProvider; |
|
61 | + |
|
62 | + Util::connectHook('OC_User', 'post_login', $this, 'authenticate'); |
|
63 | + } |
|
64 | + |
|
65 | + /** |
|
66 | + * Hook listener on post login |
|
67 | + * |
|
68 | + * @param array $params |
|
69 | + */ |
|
70 | + public function authenticate(array $params) { |
|
71 | + $this->session->set('login_credentials', json_encode($params)); |
|
72 | + } |
|
73 | + |
|
74 | + /** |
|
75 | + * Replace the session implementation |
|
76 | + * |
|
77 | + * @param ISession $session |
|
78 | + */ |
|
79 | + public function setSession(ISession $session) { |
|
80 | + $this->session = $session; |
|
81 | + } |
|
82 | + |
|
83 | + /** |
|
84 | + * @since 12 |
|
85 | + * |
|
86 | + * @return ICredentials the login credentials of the current user |
|
87 | + * @throws CredentialsUnavailableException |
|
88 | + */ |
|
89 | + public function getLoginCredentials(): ICredentials { |
|
90 | + if ($this->tokenProvider === null) { |
|
91 | + throw new CredentialsUnavailableException(); |
|
92 | + } |
|
93 | + |
|
94 | + $trySession = false; |
|
95 | + try { |
|
96 | + $sessionId = $this->session->getId(); |
|
97 | + $token = $this->tokenProvider->getToken($sessionId); |
|
98 | + |
|
99 | + $uid = $token->getUID(); |
|
100 | + $user = $token->getLoginName(); |
|
101 | + $password = $this->tokenProvider->getPassword($token, $sessionId); |
|
102 | + |
|
103 | + return new Credentials($uid, $user, $password); |
|
104 | + } catch (SessionNotAvailableException $ex) { |
|
105 | + $this->logger->debug('could not get login credentials because session is unavailable', ['app' => 'core']); |
|
106 | + } catch (InvalidTokenException $ex) { |
|
107 | + $this->logger->debug('could not get login credentials because the token is invalid', ['app' => 'core']); |
|
108 | + $trySession = true; |
|
109 | + } catch (PasswordlessTokenException $ex) { |
|
110 | + $this->logger->debug('could not get login credentials because the token has no password', ['app' => 'core']); |
|
111 | + $trySession = true; |
|
112 | + } |
|
113 | + |
|
114 | + if ($trySession && $this->session->exists('login_credentials')) { |
|
115 | + /** @var array $creds */ |
|
116 | + $creds = json_decode($this->session->get('login_credentials'), true); |
|
117 | + return new Credentials( |
|
118 | + $creds['uid'], |
|
119 | + $creds['loginName'] ?? $this->session->get('loginname') ?? $creds['uid'], // Pre 20 didn't have a loginName property, hence fall back to the session value and then to the UID |
|
120 | + $creds['password'] |
|
121 | + ); |
|
122 | + } |
|
123 | + |
|
124 | + // If we reach this line, an exception was thrown. |
|
125 | + throw new CredentialsUnavailableException(); |
|
126 | + } |
|
127 | 127 | } |