| 
									
										
										
										
											2018-03-08 20:44:56 +01:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * 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-04-02 14:50:17 +02:00
										 |  |  | use Exception; | 
					
						
							| 
									
										
										
										
											2018-06-06 21:20:21 +02:00
										 |  |  | use GuzzleHttp\Client; | 
					
						
							|  |  |  | use GuzzleHttp\Exception\GuzzleException; | 
					
						
							| 
									
										
										
										
											2018-03-08 20:44:56 +01:00
										 |  |  | use Log; | 
					
						
							| 
									
										
										
										
											2018-07-28 10:45:16 +02:00
										 |  |  | use RuntimeException; | 
					
						
							| 
									
										
										
										
											2018-03-08 20:44:56 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * Class PwndVerifierV2. | 
					
						
							| 
									
										
										
										
											2019-07-31 16:53:09 +02:00
										 |  |  |  * @codeCoverageIgnore | 
					
						
							| 
									
										
										
										
											2018-03-08 20:44:56 +01:00
										 |  |  |  */ | 
					
						
							|  |  |  | class PwndVerifierV2 implements Verifier | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2018-09-06 12:29:32 +02:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Constructor. | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function __construct() | 
					
						
							|  |  |  |     { | 
					
						
							| 
									
										
										
										
											2018-12-15 07:59:02 +01:00
										 |  |  |         if ('testing' === config('app.env')) { | 
					
						
							| 
									
										
										
										
											2019-06-07 18:20:15 +02:00
										 |  |  |             Log::warning(sprintf('%s should not be instantiated in the TEST environment!', get_class($this))); | 
					
						
							| 
									
										
										
										
											2018-09-06 12:29:32 +02:00
										 |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-03-08 20:44:56 +01:00
										 |  |  |     /** | 
					
						
							|  |  |  |      * Verify the given password against (some) service. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param string $password | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @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); | 
					
						
							|  |  |  |         $uri    = 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' => [ | 
					
						
							|  |  |  |                 'User-Agent' => 'Firefly III v' . config('firefly.version'), | 
					
						
							|  |  |  |                 'Add-Padding' => 'true', | 
					
						
							|  |  |  |             ], | 
					
						
							|  |  |  |             '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(); | 
					
						
							|  |  |  |             $res    = $client->request('GET', $uri, $opt); | 
					
						
							|  |  |  |         } catch (GuzzleException|Exception $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; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											2018-07-28 10:45:16 +02:00
										 |  |  |         try { | 
					
						
							|  |  |  |             $strpos = stripos($res->getBody()->getContents(), $rest); | 
					
						
							| 
									
										
										
										
											2018-08-06 19:14:30 +02:00
										 |  |  |         } catch (RuntimeException $e) { | 
					
						
							| 
									
										
										
										
											2018-07-28 10:45:16 +02:00
										 |  |  |             Log::error(sprintf('Could not get body from Pwnd result: %s', $e->getMessage())); | 
					
						
							|  |  |  |             $strpos = false; | 
					
						
							|  |  |  |         } | 
					
						
							| 
									
										
										
										
											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; | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } |