-
-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
588 additions
and
129 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
# top-most EditorConfig file | ||
root = true | ||
|
||
# Unix-style newlines with a newline ending every file | ||
[*] | ||
charset = utf-8 | ||
end_of_line = lf | ||
insert_final_newline = true | ||
indent_style = space | ||
indent_size = 4 | ||
|
||
[*.yml] | ||
indent_size = 2 | ||
|
||
# Ignore paths | ||
[/{vendor, cache, bundles, assets}/**] | ||
charset = none | ||
end_of_line = none | ||
insert_final_newline = none | ||
trim_trailing_whitespace = none | ||
indent_style = none | ||
indent_size = none |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,7 @@ | ||
composer.lock | ||
vendor | ||
/composer.lock | ||
/vendor/ | ||
/cache/ | ||
/tests/Logs | ||
bundles/* | ||
!bundles/. | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,6 +23,6 @@ | |
</whitelist> | ||
</filter> | ||
<php> | ||
|
||
</php> | ||
</phpunit> | ||
</phpunit> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,6 @@ | |
totallyTyped="false" | ||
> | ||
<projectFiles> | ||
<directory name="src" /> | ||
<directory name="src"/> | ||
</projectFiles> | ||
</psalm> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,178 @@ | ||
<?php | ||
|
||
namespace DivineOmega\PasswordExposed; | ||
|
||
use Psr\Cache\CacheItemPoolInterface; | ||
use Psr\Http\Client\ClientExceptionInterface; | ||
use Psr\Http\Client\ClientInterface; | ||
use Psr\Http\Message\RequestFactoryInterface; | ||
use Psr\Http\Message\ResponseInterface; | ||
use Psr\Http\Message\UriFactoryInterface; | ||
|
||
/** | ||
* Class AbstractPasswordExposedChecker. | ||
*/ | ||
abstract class AbstractPasswordExposedChecker implements PasswordExposedCheckerInterface | ||
{ | ||
/** @var int */ | ||
protected const CACHE_EXPIRY_SECONDS = 2592000; | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function passwordExposed(string $password): string | ||
{ | ||
return $this->passwordExposedByHash($this->getHash($password)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function passwordExposedByHash(string $hash): string | ||
{ | ||
$cache = $this->getCache(); | ||
$cacheKey = substr($hash, 0, 2).'_'.substr($hash, 2, 3); | ||
$body = null; | ||
|
||
try { | ||
$cacheItem = $cache->getItem($cacheKey); | ||
|
||
// try to get status from cache | ||
if ($cacheItem->isHit()) { | ||
$body = $cacheItem->get(); | ||
} | ||
} catch (\Exception $e) { | ||
$cacheItem = null; | ||
} | ||
|
||
// get status from api | ||
if ($body === null) { | ||
try { | ||
/** @var ResponseInterface $response */ | ||
$response = $this->makeRequest($hash); | ||
|
||
/** @var string $responseBody */ | ||
$body = $response->getBody()->getContents(); | ||
|
||
// cache status | ||
if ($cacheItem !== null) { | ||
$cacheLifeTime = $this->getCacheLifeTime(); | ||
|
||
if ($cacheLifeTime <= 0) { | ||
$cacheLifeTime = self::CACHE_EXPIRY_SECONDS; | ||
} | ||
|
||
$cacheItem->set($body); | ||
$cacheItem->expiresAfter($cacheLifeTime); | ||
$cache->save($cacheItem); | ||
} | ||
} catch (ClientExceptionInterface $e) { | ||
} | ||
} | ||
|
||
if ($body === null) { | ||
return PasswordExposedCheckerInterface::UNKNOWN; | ||
} | ||
|
||
return $this->getPasswordStatus($hash, $body); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isExposed(string $password): ?bool | ||
{ | ||
return $this->isExposedByHash($this->getHash($password)); | ||
} | ||
|
||
/** | ||
* {@inheritdoc} | ||
*/ | ||
public function isExposedByHash(string $hash): ?bool | ||
{ | ||
$status = $this->passwordExposedByHash($hash); | ||
|
||
if ($status === PasswordExposedCheckerInterface::EXPOSED) { | ||
return true; | ||
} | ||
|
||
if ($status === PasswordExposedCheckerInterface::NOT_EXPOSED) { | ||
return false; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
/** | ||
* @param $hash | ||
* | ||
* @throws \Psr\Http\Client\ClientExceptionInterface | ||
* | ||
* @return ResponseInterface | ||
*/ | ||
protected function makeRequest(string $hash): ResponseInterface | ||
{ | ||
$uri = $this->getUriFactory()->createUri('https://api.pwnedpasswords.com/range/'.substr($hash, 0, 5)); | ||
$request = $this->getRequestFactory()->createRequest('GET', $uri); | ||
|
||
return $this->getClient()->sendRequest($request); | ||
} | ||
|
||
/** | ||
* @param $string | ||
* | ||
* @return string | ||
*/ | ||
protected function getHash(string $string): string | ||
{ | ||
return sha1($string); | ||
} | ||
|
||
/** | ||
* @param string $hash | ||
* @param string $responseBody | ||
* | ||
* @return string | ||
*/ | ||
protected function getPasswordStatus($hash, $responseBody): string | ||
{ | ||
$hash = strtoupper($hash); | ||
$hashSuffix = substr($hash, 5); | ||
|
||
$lines = explode("\r\n", $responseBody); | ||
|
||
foreach ($lines as $line) { | ||
list($exposedHashSuffix, $occurrences) = explode(':', $line); | ||
if (hash_equals($hashSuffix, $exposedHashSuffix)) { | ||
return PasswordStatus::EXPOSED; | ||
} | ||
} | ||
|
||
return PasswordStatus::NOT_EXPOSED; | ||
} | ||
|
||
/** | ||
* @return ClientInterface | ||
*/ | ||
abstract protected function getClient(): ClientInterface; | ||
|
||
/** | ||
* @return CacheItemPoolInterface | ||
*/ | ||
abstract protected function getCache(): CacheItemPoolInterface; | ||
|
||
/** | ||
* @return int | ||
*/ | ||
abstract protected function getCacheLifeTime(): int; | ||
|
||
/** | ||
* @return RequestFactoryInterface | ||
*/ | ||
abstract protected function getRequestFactory(): RequestFactoryInterface; | ||
|
||
/** | ||
* @return UriFactoryInterface | ||
*/ | ||
abstract protected function getUriFactory(): UriFactoryInterface; | ||
} |
Oops, something went wrong.