| 
									
										
										
										
											2018-03-08 20:44:56 +01:00
										 |  |  | <?php | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 20:44:56 +01:00
										 |  |  | /** | 
					
						
							|  |  |  |  * PwndVerifierV2.php | 
					
						
							| 
									
										
										
										
											2020-02-16 13:56:35 +01:00
										 |  |  |  * Copyright (c) 2019 james@firefly-iii.org | 
					
						
							| 
									
										
										
										
											2018-03-08 20:44:56 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This file is part of Firefly III (https://github.com/firefly-iii). | 
					
						
							| 
									
										
										
										
											2018-03-08 20:44:56 +01: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-03-08 20:44:56 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2019-10-02 06:37:26 +02:00
										 |  |  |  * This program is distributed in the hope that it will be useful, | 
					
						
							| 
									
										
										
										
											2018-03-08 20:44:56 +01: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-03-08 20:44:56 +01: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-03-08 20:44:56 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | namespace FireflyIII\Services\Password; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-06-06 21:20:21 +02:00
										 |  |  | use GuzzleHttp\Client; | 
					
						
							|  |  |  | use GuzzleHttp\Exception\GuzzleException; | 
					
						
							| 
									
										
										
										
											2021-04-06 17:00:16 +02:00
										 |  |  | use GuzzleHttp\Exception\RequestException; | 
					
						
							| 
									
										
										
										
											2023-04-01 07:04:42 +02:00
										 |  |  | use Illuminate\Support\Facades\Log; | 
					
						
							| 
									
										
										
										
											2018-03-08 20:44:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class PwndVerifierV2. | 
					
						
							| 
									
										
										
										
											2021-03-21 09:15:40 +01:00
										 |  |  |  * | 
					
						
							| 
									
										
										
										
											2023-02-12 07:15:06 +01:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 20:44:56 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | class PwndVerifierV2 implements Verifier | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Verify the given password against (some) service. | 
					
						
							|  |  |  |      * | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |      * @param string $password | 
					
						
							| 
									
										
										
										
											2018-03-08 20:44:56 +01:00
										 |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function validPassword(string $password): bool | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2020-10-01 16:52:01 +02:00
										 |  |  |         // Yes SHA1 is unsafe but in this context its fine.
 | 
					
						
							| 
									
										
										
										
											2018-03-08 20:44:56 +01:00
										 |  |  |         $hash   = sha1($password); | 
					
						
							|  |  |  |         $prefix = substr($hash, 0, 5); | 
					
						
							|  |  |  |         $rest   = substr($hash, 5); | 
					
						
							| 
									
										
										
										
											2022-04-12 18:19:30 +02:00
										 |  |  |         $url    = sprintf('https://api.pwnedpasswords.com/range/%s', $prefix); | 
					
						
							| 
									
										
										
										
											2018-06-06 21:20:21 +02:00
										 |  |  |         $opt    = [ | 
					
						
							| 
									
										
										
										
											2020-04-11 06:42:21 +02:00
										 |  |  |             'headers' => [ | 
					
						
							| 
									
										
										
										
											2021-04-06 17:00:16 +02:00
										 |  |  |                 'User-Agent'  => sprintf('Firefly III v%s', config('firefly.version')), | 
					
						
							| 
									
										
										
										
											2020-04-11 06:42:21 +02:00
										 |  |  |                 'Add-Padding' => 'true', | 
					
						
							|  |  |  |             ], | 
					
						
							| 
									
										
										
										
											2022-12-29 19:42:26 +01:00
										 |  |  |             'timeout' => 3.1415, | 
					
						
							|  |  |  |         ]; | 
					
						
							| 
									
										
										
										
											2018-03-08 20:44:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         Log::debug(sprintf('hash prefix is %s', $prefix)); | 
					
						
							|  |  |  |         Log::debug(sprintf('rest is %s', $rest)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							| 
									
										
										
										
											2018-06-06 21:20:21 +02:00
										 |  |  |             $client = new Client(); | 
					
						
							| 
									
										
										
										
											2022-04-12 18:19:30 +02:00
										 |  |  |             $res    = $client->request('GET', $url, $opt); | 
					
						
							| 
									
										
										
										
											2023-06-21 12:34:58 +02:00
										 |  |  |         } catch (GuzzleException | RequestException $e) { | 
					
						
							| 
									
										
										
										
											2018-07-26 06:27:52 +02:00
										 |  |  |             Log::error(sprintf('Could not verify password security: %s', $e->getMessage())); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 20:44:56 +01:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-06-06 21:20:21 +02:00
										 |  |  |         Log::debug(sprintf('Status code returned is %d', $res->getStatusCode())); | 
					
						
							|  |  |  |         if (404 === $res->getStatusCode()) { | 
					
						
							| 
									
										
										
										
											2018-03-08 20:44:56 +01:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2022-12-30 20:25:04 +01:00
										 |  |  |         $strpos = stripos($res->getBody()->getContents(), $rest); | 
					
						
							| 
									
										
										
										
											2018-07-26 06:27:52 +02:00
										 |  |  |         if (false === $strpos) { | 
					
						
							| 
									
										
										
										
											2018-03-08 20:44:56 +01:00
										 |  |  |             Log::debug(sprintf('%s was not found in result body. Return true.', $rest)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2020-04-11 06:42:21 +02:00
										 |  |  |         Log::debug(sprintf('Found %s, return FALSE.', $rest)); | 
					
						
							| 
									
										
										
										
											2018-03-08 20:44:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |