@@ -44,218 +44,218 @@ |
||
44 | 44 | |
45 | 45 | class LoginFlowV2Service { |
46 | 46 | |
47 | - /** @var LoginFlowV2Mapper */ |
|
48 | - private $mapper; |
|
49 | - /** @var ISecureRandom */ |
|
50 | - private $random; |
|
51 | - /** @var ITimeFactory */ |
|
52 | - private $time; |
|
53 | - /** @var IConfig */ |
|
54 | - private $config; |
|
55 | - /** @var ICrypto */ |
|
56 | - private $crypto; |
|
57 | - /** @var ILogger */ |
|
58 | - private $logger; |
|
59 | - /** @var IProvider */ |
|
60 | - private $tokenProvider; |
|
61 | - |
|
62 | - public function __construct(LoginFlowV2Mapper $mapper, |
|
63 | - ISecureRandom $random, |
|
64 | - ITimeFactory $time, |
|
65 | - IConfig $config, |
|
66 | - ICrypto $crypto, |
|
67 | - ILogger $logger, |
|
68 | - IProvider $tokenProvider) { |
|
69 | - $this->mapper = $mapper; |
|
70 | - $this->random = $random; |
|
71 | - $this->time = $time; |
|
72 | - $this->config = $config; |
|
73 | - $this->crypto = $crypto; |
|
74 | - $this->logger = $logger; |
|
75 | - $this->tokenProvider = $tokenProvider; |
|
76 | - } |
|
77 | - |
|
78 | - /** |
|
79 | - * @param string $pollToken |
|
80 | - * @return LoginFlowV2Credentials |
|
81 | - * @throws LoginFlowV2NotFoundException |
|
82 | - */ |
|
83 | - public function poll(string $pollToken): LoginFlowV2Credentials { |
|
84 | - try { |
|
85 | - $data = $this->mapper->getByPollToken($this->hashToken($pollToken)); |
|
86 | - } catch (DoesNotExistException $e) { |
|
87 | - throw new LoginFlowV2NotFoundException('Invalid token'); |
|
88 | - } |
|
89 | - |
|
90 | - $loginName = $data->getLoginName(); |
|
91 | - $server = $data->getServer(); |
|
92 | - $appPassword = $data->getAppPassword(); |
|
93 | - |
|
94 | - if ($loginName === null || $server === null || $appPassword === null) { |
|
95 | - throw new LoginFlowV2NotFoundException('Token not yet ready'); |
|
96 | - } |
|
97 | - |
|
98 | - // Remove the data from the DB |
|
99 | - $this->mapper->delete($data); |
|
100 | - |
|
101 | - try { |
|
102 | - // Decrypt the apptoken |
|
103 | - $privateKey = $this->crypto->decrypt($data->getPrivateKey(), $pollToken); |
|
104 | - $appPassword = $this->decryptPassword($data->getAppPassword(), $privateKey); |
|
105 | - } catch (\Exception $e) { |
|
106 | - throw new LoginFlowV2NotFoundException('Apptoken could not be decrypted'); |
|
107 | - } |
|
108 | - |
|
109 | - return new LoginFlowV2Credentials($server, $loginName, $appPassword); |
|
110 | - } |
|
111 | - |
|
112 | - /** |
|
113 | - * @param string $loginToken |
|
114 | - * @return LoginFlowV2 |
|
115 | - * @throws LoginFlowV2NotFoundException |
|
116 | - */ |
|
117 | - public function getByLoginToken(string $loginToken): LoginFlowV2 { |
|
118 | - try { |
|
119 | - return $this->mapper->getByLoginToken($loginToken); |
|
120 | - } catch (DoesNotExistException $e) { |
|
121 | - throw new LoginFlowV2NotFoundException('Login token invalid'); |
|
122 | - } |
|
123 | - } |
|
124 | - |
|
125 | - /** |
|
126 | - * @param string $loginToken |
|
127 | - * @return bool returns true if the start was successfull. False if not. |
|
128 | - */ |
|
129 | - public function startLoginFlow(string $loginToken): bool { |
|
130 | - try { |
|
131 | - $data = $this->mapper->getByLoginToken($loginToken); |
|
132 | - } catch (DoesNotExistException $e) { |
|
133 | - return false; |
|
134 | - } |
|
135 | - |
|
136 | - $data->setStarted(1); |
|
137 | - $this->mapper->update($data); |
|
138 | - |
|
139 | - return true; |
|
140 | - } |
|
141 | - |
|
142 | - /** |
|
143 | - * @param string $loginToken |
|
144 | - * @param string $sessionId |
|
145 | - * @param string $server |
|
146 | - * @param string $userId |
|
147 | - * @return bool true if the flow was successfully completed false otherwise |
|
148 | - */ |
|
149 | - public function flowDone(string $loginToken, string $sessionId, string $server, string $userId): bool { |
|
150 | - try { |
|
151 | - $data = $this->mapper->getByLoginToken($loginToken); |
|
152 | - } catch (DoesNotExistException $e) { |
|
153 | - return false; |
|
154 | - } |
|
155 | - |
|
156 | - try { |
|
157 | - $sessionToken = $this->tokenProvider->getToken($sessionId); |
|
158 | - $loginName = $sessionToken->getLoginName(); |
|
159 | - try { |
|
160 | - $password = $this->tokenProvider->getPassword($sessionToken, $sessionId); |
|
161 | - } catch (PasswordlessTokenException $ex) { |
|
162 | - $password = null; |
|
163 | - } |
|
164 | - } catch (InvalidTokenException $ex) { |
|
165 | - return false; |
|
166 | - } |
|
167 | - |
|
168 | - $appPassword = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS); |
|
169 | - $this->tokenProvider->generateToken( |
|
170 | - $appPassword, |
|
171 | - $userId, |
|
172 | - $loginName, |
|
173 | - $password, |
|
174 | - $data->getClientName(), |
|
175 | - IToken::PERMANENT_TOKEN, |
|
176 | - IToken::DO_NOT_REMEMBER |
|
177 | - ); |
|
178 | - |
|
179 | - $data->setLoginName($loginName); |
|
180 | - $data->setServer($server); |
|
181 | - |
|
182 | - // Properly encrypt |
|
183 | - $data->setAppPassword($this->encryptPassword($appPassword, $data->getPublicKey())); |
|
184 | - |
|
185 | - $this->mapper->update($data); |
|
186 | - return true; |
|
187 | - } |
|
188 | - |
|
189 | - public function createTokens(string $userAgent): LoginFlowV2Tokens { |
|
190 | - $flow = new LoginFlowV2(); |
|
191 | - $pollToken = $this->random->generate(128, ISecureRandom::CHAR_DIGITS.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_UPPER); |
|
192 | - $loginToken = $this->random->generate(128, ISecureRandom::CHAR_DIGITS.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_UPPER); |
|
193 | - $flow->setPollToken($this->hashToken($pollToken)); |
|
194 | - $flow->setLoginToken($loginToken); |
|
195 | - $flow->setStarted(0); |
|
196 | - $flow->setTimestamp($this->time->getTime()); |
|
197 | - $flow->setClientName($userAgent); |
|
198 | - |
|
199 | - [$publicKey, $privateKey] = $this->getKeyPair(); |
|
200 | - $privateKey = $this->crypto->encrypt($privateKey, $pollToken); |
|
201 | - |
|
202 | - $flow->setPublicKey($publicKey); |
|
203 | - $flow->setPrivateKey($privateKey); |
|
204 | - |
|
205 | - $this->mapper->insert($flow); |
|
206 | - |
|
207 | - return new LoginFlowV2Tokens($loginToken, $pollToken); |
|
208 | - } |
|
209 | - |
|
210 | - private function hashToken(string $token): string { |
|
211 | - $secret = $this->config->getSystemValue('secret'); |
|
212 | - return hash('sha512', $token . $secret); |
|
213 | - } |
|
214 | - |
|
215 | - private function getKeyPair(): array { |
|
216 | - $config = array_merge([ |
|
217 | - 'digest_alg' => 'sha512', |
|
218 | - 'private_key_bits' => 2048, |
|
219 | - ], $this->config->getSystemValue('openssl', [])); |
|
220 | - |
|
221 | - // Generate new key |
|
222 | - $res = openssl_pkey_new($config); |
|
223 | - if ($res === false) { |
|
224 | - $this->logOpensslError(); |
|
225 | - throw new \RuntimeException('Could not initialize keys'); |
|
226 | - } |
|
227 | - |
|
228 | - if (openssl_pkey_export($res, $privateKey, null, $config) === false) { |
|
229 | - $this->logOpensslError(); |
|
230 | - throw new \RuntimeException('OpenSSL reported a problem'); |
|
231 | - } |
|
232 | - |
|
233 | - // Extract the public key from $res to $pubKey |
|
234 | - $publicKey = openssl_pkey_get_details($res); |
|
235 | - $publicKey = $publicKey['key']; |
|
236 | - |
|
237 | - return [$publicKey, $privateKey]; |
|
238 | - } |
|
239 | - |
|
240 | - private function logOpensslError(): void { |
|
241 | - $errors = []; |
|
242 | - while ($error = openssl_error_string()) { |
|
243 | - $errors[] = $error; |
|
244 | - } |
|
245 | - $this->logger->critical('Something is wrong with your openssl setup: ' . implode(', ', $errors)); |
|
246 | - } |
|
247 | - |
|
248 | - private function encryptPassword(string $password, string $publicKey): string { |
|
249 | - openssl_public_encrypt($password, $encryptedPassword, $publicKey, OPENSSL_PKCS1_OAEP_PADDING); |
|
250 | - $encryptedPassword = base64_encode($encryptedPassword); |
|
251 | - |
|
252 | - return $encryptedPassword; |
|
253 | - } |
|
254 | - |
|
255 | - private function decryptPassword(string $encryptedPassword, string $privateKey): string { |
|
256 | - $encryptedPassword = base64_decode($encryptedPassword); |
|
257 | - openssl_private_decrypt($encryptedPassword, $password, $privateKey, OPENSSL_PKCS1_OAEP_PADDING); |
|
258 | - |
|
259 | - return $password; |
|
260 | - } |
|
47 | + /** @var LoginFlowV2Mapper */ |
|
48 | + private $mapper; |
|
49 | + /** @var ISecureRandom */ |
|
50 | + private $random; |
|
51 | + /** @var ITimeFactory */ |
|
52 | + private $time; |
|
53 | + /** @var IConfig */ |
|
54 | + private $config; |
|
55 | + /** @var ICrypto */ |
|
56 | + private $crypto; |
|
57 | + /** @var ILogger */ |
|
58 | + private $logger; |
|
59 | + /** @var IProvider */ |
|
60 | + private $tokenProvider; |
|
61 | + |
|
62 | + public function __construct(LoginFlowV2Mapper $mapper, |
|
63 | + ISecureRandom $random, |
|
64 | + ITimeFactory $time, |
|
65 | + IConfig $config, |
|
66 | + ICrypto $crypto, |
|
67 | + ILogger $logger, |
|
68 | + IProvider $tokenProvider) { |
|
69 | + $this->mapper = $mapper; |
|
70 | + $this->random = $random; |
|
71 | + $this->time = $time; |
|
72 | + $this->config = $config; |
|
73 | + $this->crypto = $crypto; |
|
74 | + $this->logger = $logger; |
|
75 | + $this->tokenProvider = $tokenProvider; |
|
76 | + } |
|
77 | + |
|
78 | + /** |
|
79 | + * @param string $pollToken |
|
80 | + * @return LoginFlowV2Credentials |
|
81 | + * @throws LoginFlowV2NotFoundException |
|
82 | + */ |
|
83 | + public function poll(string $pollToken): LoginFlowV2Credentials { |
|
84 | + try { |
|
85 | + $data = $this->mapper->getByPollToken($this->hashToken($pollToken)); |
|
86 | + } catch (DoesNotExistException $e) { |
|
87 | + throw new LoginFlowV2NotFoundException('Invalid token'); |
|
88 | + } |
|
89 | + |
|
90 | + $loginName = $data->getLoginName(); |
|
91 | + $server = $data->getServer(); |
|
92 | + $appPassword = $data->getAppPassword(); |
|
93 | + |
|
94 | + if ($loginName === null || $server === null || $appPassword === null) { |
|
95 | + throw new LoginFlowV2NotFoundException('Token not yet ready'); |
|
96 | + } |
|
97 | + |
|
98 | + // Remove the data from the DB |
|
99 | + $this->mapper->delete($data); |
|
100 | + |
|
101 | + try { |
|
102 | + // Decrypt the apptoken |
|
103 | + $privateKey = $this->crypto->decrypt($data->getPrivateKey(), $pollToken); |
|
104 | + $appPassword = $this->decryptPassword($data->getAppPassword(), $privateKey); |
|
105 | + } catch (\Exception $e) { |
|
106 | + throw new LoginFlowV2NotFoundException('Apptoken could not be decrypted'); |
|
107 | + } |
|
108 | + |
|
109 | + return new LoginFlowV2Credentials($server, $loginName, $appPassword); |
|
110 | + } |
|
111 | + |
|
112 | + /** |
|
113 | + * @param string $loginToken |
|
114 | + * @return LoginFlowV2 |
|
115 | + * @throws LoginFlowV2NotFoundException |
|
116 | + */ |
|
117 | + public function getByLoginToken(string $loginToken): LoginFlowV2 { |
|
118 | + try { |
|
119 | + return $this->mapper->getByLoginToken($loginToken); |
|
120 | + } catch (DoesNotExistException $e) { |
|
121 | + throw new LoginFlowV2NotFoundException('Login token invalid'); |
|
122 | + } |
|
123 | + } |
|
124 | + |
|
125 | + /** |
|
126 | + * @param string $loginToken |
|
127 | + * @return bool returns true if the start was successfull. False if not. |
|
128 | + */ |
|
129 | + public function startLoginFlow(string $loginToken): bool { |
|
130 | + try { |
|
131 | + $data = $this->mapper->getByLoginToken($loginToken); |
|
132 | + } catch (DoesNotExistException $e) { |
|
133 | + return false; |
|
134 | + } |
|
135 | + |
|
136 | + $data->setStarted(1); |
|
137 | + $this->mapper->update($data); |
|
138 | + |
|
139 | + return true; |
|
140 | + } |
|
141 | + |
|
142 | + /** |
|
143 | + * @param string $loginToken |
|
144 | + * @param string $sessionId |
|
145 | + * @param string $server |
|
146 | + * @param string $userId |
|
147 | + * @return bool true if the flow was successfully completed false otherwise |
|
148 | + */ |
|
149 | + public function flowDone(string $loginToken, string $sessionId, string $server, string $userId): bool { |
|
150 | + try { |
|
151 | + $data = $this->mapper->getByLoginToken($loginToken); |
|
152 | + } catch (DoesNotExistException $e) { |
|
153 | + return false; |
|
154 | + } |
|
155 | + |
|
156 | + try { |
|
157 | + $sessionToken = $this->tokenProvider->getToken($sessionId); |
|
158 | + $loginName = $sessionToken->getLoginName(); |
|
159 | + try { |
|
160 | + $password = $this->tokenProvider->getPassword($sessionToken, $sessionId); |
|
161 | + } catch (PasswordlessTokenException $ex) { |
|
162 | + $password = null; |
|
163 | + } |
|
164 | + } catch (InvalidTokenException $ex) { |
|
165 | + return false; |
|
166 | + } |
|
167 | + |
|
168 | + $appPassword = $this->random->generate(72, ISecureRandom::CHAR_UPPER.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_DIGITS); |
|
169 | + $this->tokenProvider->generateToken( |
|
170 | + $appPassword, |
|
171 | + $userId, |
|
172 | + $loginName, |
|
173 | + $password, |
|
174 | + $data->getClientName(), |
|
175 | + IToken::PERMANENT_TOKEN, |
|
176 | + IToken::DO_NOT_REMEMBER |
|
177 | + ); |
|
178 | + |
|
179 | + $data->setLoginName($loginName); |
|
180 | + $data->setServer($server); |
|
181 | + |
|
182 | + // Properly encrypt |
|
183 | + $data->setAppPassword($this->encryptPassword($appPassword, $data->getPublicKey())); |
|
184 | + |
|
185 | + $this->mapper->update($data); |
|
186 | + return true; |
|
187 | + } |
|
188 | + |
|
189 | + public function createTokens(string $userAgent): LoginFlowV2Tokens { |
|
190 | + $flow = new LoginFlowV2(); |
|
191 | + $pollToken = $this->random->generate(128, ISecureRandom::CHAR_DIGITS.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_UPPER); |
|
192 | + $loginToken = $this->random->generate(128, ISecureRandom::CHAR_DIGITS.ISecureRandom::CHAR_LOWER.ISecureRandom::CHAR_UPPER); |
|
193 | + $flow->setPollToken($this->hashToken($pollToken)); |
|
194 | + $flow->setLoginToken($loginToken); |
|
195 | + $flow->setStarted(0); |
|
196 | + $flow->setTimestamp($this->time->getTime()); |
|
197 | + $flow->setClientName($userAgent); |
|
198 | + |
|
199 | + [$publicKey, $privateKey] = $this->getKeyPair(); |
|
200 | + $privateKey = $this->crypto->encrypt($privateKey, $pollToken); |
|
201 | + |
|
202 | + $flow->setPublicKey($publicKey); |
|
203 | + $flow->setPrivateKey($privateKey); |
|
204 | + |
|
205 | + $this->mapper->insert($flow); |
|
206 | + |
|
207 | + return new LoginFlowV2Tokens($loginToken, $pollToken); |
|
208 | + } |
|
209 | + |
|
210 | + private function hashToken(string $token): string { |
|
211 | + $secret = $this->config->getSystemValue('secret'); |
|
212 | + return hash('sha512', $token . $secret); |
|
213 | + } |
|
214 | + |
|
215 | + private function getKeyPair(): array { |
|
216 | + $config = array_merge([ |
|
217 | + 'digest_alg' => 'sha512', |
|
218 | + 'private_key_bits' => 2048, |
|
219 | + ], $this->config->getSystemValue('openssl', [])); |
|
220 | + |
|
221 | + // Generate new key |
|
222 | + $res = openssl_pkey_new($config); |
|
223 | + if ($res === false) { |
|
224 | + $this->logOpensslError(); |
|
225 | + throw new \RuntimeException('Could not initialize keys'); |
|
226 | + } |
|
227 | + |
|
228 | + if (openssl_pkey_export($res, $privateKey, null, $config) === false) { |
|
229 | + $this->logOpensslError(); |
|
230 | + throw new \RuntimeException('OpenSSL reported a problem'); |
|
231 | + } |
|
232 | + |
|
233 | + // Extract the public key from $res to $pubKey |
|
234 | + $publicKey = openssl_pkey_get_details($res); |
|
235 | + $publicKey = $publicKey['key']; |
|
236 | + |
|
237 | + return [$publicKey, $privateKey]; |
|
238 | + } |
|
239 | + |
|
240 | + private function logOpensslError(): void { |
|
241 | + $errors = []; |
|
242 | + while ($error = openssl_error_string()) { |
|
243 | + $errors[] = $error; |
|
244 | + } |
|
245 | + $this->logger->critical('Something is wrong with your openssl setup: ' . implode(', ', $errors)); |
|
246 | + } |
|
247 | + |
|
248 | + private function encryptPassword(string $password, string $publicKey): string { |
|
249 | + openssl_public_encrypt($password, $encryptedPassword, $publicKey, OPENSSL_PKCS1_OAEP_PADDING); |
|
250 | + $encryptedPassword = base64_encode($encryptedPassword); |
|
251 | + |
|
252 | + return $encryptedPassword; |
|
253 | + } |
|
254 | + |
|
255 | + private function decryptPassword(string $encryptedPassword, string $privateKey): string { |
|
256 | + $encryptedPassword = base64_decode($encryptedPassword); |
|
257 | + openssl_private_decrypt($encryptedPassword, $password, $privateKey, OPENSSL_PKCS1_OAEP_PADDING); |
|
258 | + |
|
259 | + return $password; |
|
260 | + } |
|
261 | 261 | } |