| 
									
										
										
										
											2018-08-12 14:26:11 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * CLIToken.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:56:52 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-08-12 14:26:11 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-08-12 14:26:11 +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. | 
					
						
							| 
									
										
										
										
											2018-08-12 14:26:11 +02:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-08-12 14:26:11 +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. | 
					
						
							| 
									
										
										
										
											2018-08-12 14:26:11 +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/>. | 
					
						
							| 
									
										
										
										
											2018-08-12 14:26:11 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Support\Binder; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-09-18 10:20:19 +02:00
										 |  |  | use FireflyIII\Exceptions\FireflyException; | 
					
						
							| 
									
										
										
										
											2018-08-12 14:26:11 +02:00
										 |  |  | use FireflyIII\Repositories\User\UserRepositoryInterface; | 
					
						
							|  |  |  | use Illuminate\Routing\Route; | 
					
						
							|  |  |  | use Log; | 
					
						
							|  |  |  | use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class CLIToken | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | class CLIToken implements BinderInterface | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * @param string $value | 
					
						
							|  |  |  |      * @param Route  $route | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return mixed | 
					
						
							| 
									
										
										
										
											2021-09-18 10:20:19 +02:00
										 |  |  |      * @throws FireflyException | 
					
						
							| 
									
										
										
										
											2018-08-12 14:26:11 +02:00
										 |  |  |      */ | 
					
						
							|  |  |  |     public static function routeBinder(string $value, Route $route) | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         /** @var UserRepositoryInterface $repository */ | 
					
						
							|  |  |  |         $repository = app(UserRepositoryInterface::class); | 
					
						
							| 
									
										
										
										
											2021-05-13 06:47:11 +02:00
										 |  |  |         $users      = $repository->all(); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         // check for static token
 | 
					
						
							| 
									
										
										
										
											2022-03-29 15:00:29 +02:00
										 |  |  |         if ($value === config('firefly.static_cron_token') && 32 === strlen((string) config('firefly.static_cron_token'))) { | 
					
						
							| 
									
										
										
										
											2021-05-13 06:47:11 +02:00
										 |  |  |             return $value; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-08-12 14:26:11 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  |         foreach ($users as $user) { | 
					
						
							| 
									
										
										
										
											2021-05-24 08:57:02 +02:00
										 |  |  |             $accessToken = app('preferences')->getForUser($user, 'access_token'); | 
					
						
							| 
									
										
										
										
											2019-02-12 21:49:28 +01:00
										 |  |  |             if (null !== $accessToken && $accessToken->data === $value) { | 
					
						
							| 
									
										
										
										
											2018-08-12 14:26:11 +02:00
										 |  |  |                 Log::info(sprintf('Recognized user #%d (%s) from his acccess token.', $user->id, $user->email)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |                 return $value; | 
					
						
							|  |  |  |             } | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-12-29 08:33:25 +01:00
										 |  |  |         Log::error(sprintf('Recognized no users by access token "%s"', $value)); | 
					
						
							| 
									
										
										
										
											2022-10-30 14:24:37 +01:00
										 |  |  |         throw new NotFoundHttpException(); | 
					
						
							| 
									
										
										
										
											2018-08-12 14:26:11 +02:00
										 |  |  |     } | 
					
						
							| 
									
										
										
										
											2018-12-31 07:48:23 +01:00
										 |  |  | } |