| 
									
										
										
										
											2017-08-04 15:46:52 +02:00
										 |  |  | <?php | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * PwndVerifier.php | 
					
						
							|  |  |  |  * Copyright (c) 2017 thegrumpydictator@gmail.com | 
					
						
							|  |  |  |  * | 
					
						
							| 
									
										
										
										
											2017-10-21 08:40:00 +02:00
										 |  |  |  * This file is part of Firefly III. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Firefly III is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  |  * it under the terms of the GNU General Public License as published by | 
					
						
							|  |  |  |  * the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  |  * (at your option) any later version. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * Firefly III is distributed in the hope that it will be useful, | 
					
						
							|  |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  |  * GNU General Public License for more details. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You should have received a copy of the GNU General Public License | 
					
						
							| 
									
										
										
										
											2017-12-17 14:44:05 +01:00
										 |  |  |  * along with Firefly III. If not, see <http://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2017-08-04 15:46:52 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | declare(strict_types=1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-08-04 15:48:26 +02:00
										 |  |  | namespace FireflyIII\Services\Password; | 
					
						
							| 
									
										
										
										
											2017-08-04 15:46:52 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | use Log; | 
					
						
							|  |  |  | use Requests; | 
					
						
							|  |  |  | use Requests_Exception; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |  * Class PwndVerifier. | 
					
						
							| 
									
										
										
										
											2017-08-04 15:46:52 +02:00
										 |  |  |  */ | 
					
						
							|  |  |  | class PwndVerifier implements Verifier | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  |     /** | 
					
						
							|  |  |  |      * Verify the given password against (some) service. | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @param string $password | 
					
						
							|  |  |  |      * | 
					
						
							|  |  |  |      * @return bool | 
					
						
							|  |  |  |      */ | 
					
						
							|  |  |  |     public function validPassword(string $password): bool | 
					
						
							|  |  |  |     { | 
					
						
							|  |  |  |         $hash = sha1($password); | 
					
						
							|  |  |  |         $uri  = sprintf('https://haveibeenpwned.com/api/v2/pwnedpassword/%s', $hash); | 
					
						
							|  |  |  |         $opt  = ['useragent' => 'Firefly III v' . config('firefly.version'), 'timeout' => 2]; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try { | 
					
						
							|  |  |  |             $result = Requests::get($uri, ['originalPasswordIsAHash' => 'true'], $opt); | 
					
						
							|  |  |  |         } catch (Requests_Exception $e) { | 
					
						
							|  |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         Log::debug(sprintf('Status code returned is %d', $result->status_code)); | 
					
						
							| 
									
										
										
										
											2017-11-15 12:25:49 +01:00
										 |  |  |         if (404 === $result->status_code) { | 
					
						
							| 
									
										
										
										
											2017-08-04 15:46:52 +02:00
										 |  |  |             return true; | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         return false; | 
					
						
							|  |  |  |     } | 
					
						
							| 
									
										
										
										
											2017-08-12 07:48:39 +02:00
										 |  |  | } |