Skip to content

Commit

Permalink
Merge 26d4eea into 908ed8e
Browse files Browse the repository at this point in the history
  • Loading branch information
DivineOmega authored Jan 30, 2019
2 parents 908ed8e + 26d4eea commit dca519d
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 8 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"php": ">=5.6",
"guzzlehttp/guzzle": "^6.3",
"paragonie/certainty": "^1|^2",
"divineomega/do-file-cache-psr-6": "^2.0"
"divineomega/do-file-cache-psr-6": "^2.0",
"psr/cache": "^1.0"
}
}
46 changes: 39 additions & 7 deletions src/PasswordExposedChecker.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use ParagonIE\Certainty\Bundle;
use ParagonIE\Certainty\Fetch;
use ParagonIE\Certainty\RemoteFetch;
use Psr\Cache\CacheItemPoolInterface;
use Psr\Http\Message\ResponseInterface;

class PasswordExposedChecker
Expand All @@ -18,12 +19,22 @@ class PasswordExposedChecker
/** @var Client $client */
private $client;

/** @var CacheItemPool $cache */
/** @var CacheItemPoolInterface $cache */
private $cache;

const CACHE_EXPIRY_SECONDS = 60 * 60 * 24 * 30;

public function __construct(Client $client = null, CacheItemPool $cache = null, Bundle $bundle = null)
/**
* PasswordExposedChecker constructor.
*
* @param Client|null $client
* @param CacheItemPoolInterface|null $cache
* @param Bundle|null $bundle
*
* @throws \ParagonIE\Certainty\Exception\CertaintyException
* @throws \SodiumException
*/
public function __construct(Client $client = null, CacheItemPoolInterface $cache = null, Bundle $bundle = null)
{
if (!$client) {
$client = new Client([
Expand All @@ -48,6 +59,11 @@ public function __construct(Client $client = null, CacheItemPool $cache = null,
}

/**
* Get secure bundle from Certainty.
*
* @throws \ParagonIE\Certainty\Exception\CertaintyException
* @throws \SodiumException
*
* @return Bundle
*/
private function getBundleFromCertainty()
Expand Down Expand Up @@ -75,8 +91,13 @@ private function getBundleFromCertainty()
}

/**
* Check if password has been exposed.
*
* @param string $password
*
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Psr\Cache\InvalidArgumentException
*
* @return string (see PasswordStatus)
*/
public function passwordExposed($password)
Expand All @@ -85,8 +106,13 @@ public function passwordExposed($password)
}

/**
* Check if password has been exposed (using SHA1 hash).
*
* @param string $hash Hexadecimal SHA-1 hash of the password
*
* @throws \GuzzleHttp\Exception\GuzzleException
* @throws \Psr\Cache\InvalidArgumentException
*
* @return string (see PasswordStatus)
*/
public function passwordExposedByHash($hash)
Expand All @@ -112,18 +138,22 @@ public function passwordExposedByHash($hash)

/** @var string $responseBody */
$responseBody = (string) $response->getBody();
}

$cacheItem->set($responseBody);
$cacheItem->expiresAfter(self::CACHE_EXPIRY_SECONDS);
$this->cache->save($cacheItem);
$cacheItem->set($responseBody);
$cacheItem->expiresAfter(self::CACHE_EXPIRY_SECONDS);
$this->cache->save($cacheItem);
}

return $this->getPasswordStatus($hash, $responseBody);
}

/**
* Perform request to HIBP Passwords API.
*
* @param string $hash
*
* @throws \GuzzleHttp\Exception\GuzzleException
*
* @return \Psr\Http\Message\ResponseInterface
*/
private function makeRequest($hash)
Expand All @@ -140,10 +170,12 @@ private function makeRequest($hash)
}

/**
* Convert response body to PasswordStatus constant.
*
* @param string $hash
* @param string $responseBody
*
* @return string
* @return string (see PasswordStatus)
*/
private function getPasswordStatus($hash, $responseBody)
{
Expand Down

0 comments on commit dca519d

Please sign in to comment.