firefly-iii/app/Services/Password/PwndVerifierV2.php

86 lines
2.6 KiB
PHP
Raw Normal View History

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