| 
									
										
										
										
											2017-09-14 21:17:19 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * VerifiesAccessToken.php | 
					
						
							| 
									
										
										
										
											2020-01-23 20:35:02 +01:00
										 |  |  |  * Copyright (c) 2020 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2017-09-14 21:17:19 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU Affero General Public License as | 
					
						
							|  |  |  |  * published by the Free Software Foundation, either version 3 of the | 
					
						
							|  |  |  |  * License, or (at your option) any later version. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * GNU Affero General Public License for more details. | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  |  * along with this program.  If not, see <https://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2017-09-14 21:17:19 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-11 10:08:34 +02:00
										 |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 21:17:19 +02:00
										 |  |  | namespace FireflyIII\Console\Commands; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-13 21:32:20 +02:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2017-09-14 21:17:19 +02:00
										 |  |  | use FireflyIII\Repositories\User\UserRepositoryInterface; | 
					
						
							| 
									
										
										
										
											2018-10-13 21:32:20 +02:00
										 |  |  | use FireflyIII\User; | 
					
						
							| 
									
										
										
										
											2017-09-14 21:17:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-16 07:41:03 +02:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |  * Trait VerifiesAccessToken. | 
					
						
							| 
									
										
										
										
											2017-09-16 07:41:03 +02:00
										 |  |  |  * | 
					
						
							|  |  |  |  * Verifies user access token for sensitive commands. | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2017-09-14 21:17:19 +02:00
										 |  |  | trait VerifiesAccessToken | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-10-13 21:32:20 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2018-10-13 21:32:20 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public function getUser(): User | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $userId     = (int)$this->option('user'); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-10-13 21:32:20 +02:00
										 |  |  |         /** @var UserRepositoryInterface $repository */ | 
					
						
							|  |  |  |         $repository = app(UserRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2021-06-30 06:17:38 +02:00
										 |  |  |         $user       = $repository->find($userId); | 
					
						
							| 
									
										
										
										
											2018-10-13 21:32:20 +02:00
										 |  |  |         if (null === $user) { | 
					
						
							| 
									
										
										
										
											2023-01-21 12:21:06 +01:00
										 |  |  |             throw new FireflyException('300000: User is unexpectedly NULL'); | 
					
						
							| 
									
										
										
										
											2018-10-13 21:32:20 +02:00
										 |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return $user; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-16 07:17:58 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2017-09-16 07:41:03 +02:00
										 |  |  |      * Abstract method to make sure trait knows about method "option". | 
					
						
							| 
									
										
										
										
											2017-10-05 11:49:06 +02:00
										 |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  |      * @param null|string $key | 
					
						
							| 
									
										
										
										
											2017-09-16 07:17:58 +02:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return mixed | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     abstract public function option($key = null); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 21:17:19 +02:00
										 |  |  |     /** | 
					
						
							| 
									
										
										
										
											2017-09-16 07:41:03 +02:00
										 |  |  |      * Returns false when given token does not match given user token. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2021-05-24 08:50:17 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2017-09-14 21:17:19 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     protected function verifyAccessToken(): bool | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $userId      = (int)$this->option('user'); | 
					
						
							|  |  |  |         $token       = (string)$this->option('token'); | 
					
						
							| 
									
										
										
										
											2023-12-20 19:35:52 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-09-14 21:17:19 +02:00
										 |  |  |         /** @var UserRepositoryInterface $repository */ | 
					
						
							| 
									
										
										
										
											2024-01-01 14:43:56 +01:00
										 |  |  |         $repository  = app(UserRepositoryInterface::class); | 
					
						
							|  |  |  |         $user        = $repository->find($userId); | 
					
						
							| 
									
										
										
										
											2017-09-14 21:17:19 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-01-25 18:41:27 +01:00
										 |  |  |         if (null === $user) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error(sprintf('verifyAccessToken(): no such user for input "%d"', $userId)); | 
					
						
							| 
									
										
										
										
											2017-09-14 21:17:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2021-05-24 08:50:17 +02:00
										 |  |  |         $accessToken = app('preferences')->getForUser($user, 'access_token'); | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (null === $accessToken) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error(sprintf('User #%d has no access token, so cannot access command line options.', $userId)); | 
					
						
							| 
									
										
										
										
											2017-09-14 21:17:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-10-24 16:59:56 +02:00
										 |  |  |         if ($accessToken->data !== $token) { | 
					
						
							| 
									
										
										
										
											2023-10-29 06:32:00 +01:00
										 |  |  |             app('log')->error(sprintf('Invalid access token for user #%d.', $userId)); | 
					
						
							|  |  |  |             app('log')->error(sprintf('Token given is "%s", expected something else.', $token)); | 
					
						
							| 
									
										
										
										
											2017-09-14 21:17:19 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |             return false; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return true; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-11-08 09:05:10 +01:00
										 |  |  | } |