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

98 lines
3.0 KiB
PHP
Raw Normal View History

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