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

90 lines
2.8 KiB
PHP
Raw Normal View History

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
*
* This file is part of Firefly III (https://github.com/firefly-iii).
2018-03-08 20:44:56 +01: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
*
* 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
* GNU Affero General Public License for more details.
2018-03-08 20:44:56 +01: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;
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.
2021-03-21 09:15:40 +01:00
*
2019-07-31 16:53:09 +02:00
* @codeCoverageIgnore
2018-03-08 20:44:56 +01:00
*/
class PwndVerifierV2 implements Verifier
{
/**
* 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' => [
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',
],
'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);
2021-04-06 17:00:16 +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;
}
2018-07-28 10:45:16 +02:00
try {
$strpos = stripos($res->getBody()->getContents(), $rest);
2021-04-06 17:00:16 +02:00
} catch (RuntimeException $e) { // @phpstan-ignore-line
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;
}
}