-
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added some rules for
hyperf/validation
. (#6749)
- Loading branch information
Showing
16 changed files
with
1,949 additions
and
0 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,61 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf. | ||
* | ||
* @link https://www.hyperf.io | ||
* @document https://hyperf.wiki | ||
* @contact [email protected] | ||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Hyperf\Validation; | ||
|
||
use Closure; | ||
use Hyperf\Support\Fluent; | ||
use Hyperf\Validation\Contract\Rule as RuleContract; | ||
|
||
use function Hyperf\Support\value; | ||
|
||
class ConditionalRules | ||
{ | ||
public function __construct( | ||
protected bool|Closure $condition, | ||
protected array|Closure|RuleContract|string $rules, | ||
protected array|Closure|RuleContract|string $defaultRules, | ||
) { | ||
} | ||
|
||
/** | ||
* Determine if the conditional rules should be added. | ||
*/ | ||
public function passes(array $data = []): bool | ||
{ | ||
return is_callable($this->condition) | ||
? call_user_func($this->condition, new Fluent($data)) | ||
: $this->condition; | ||
} | ||
|
||
/** | ||
* Get the rules. | ||
*/ | ||
public function rules(array $data = []) | ||
{ | ||
return is_string($this->rules) | ||
? explode('|', $this->rules) | ||
: value($this->rules, new Fluent($data)); | ||
} | ||
|
||
/** | ||
* Get the default rules. | ||
* | ||
* @return array | ||
*/ | ||
public function defaultRules(array $data = []) | ||
{ | ||
return is_string($this->defaultRules) | ||
? explode('|', $this->defaultRules) | ||
: value($this->defaultRules, new Fluent($data)); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,21 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf. | ||
* | ||
* @link https://www.hyperf.io | ||
* @document https://hyperf.wiki | ||
* @contact [email protected] | ||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Hyperf\Validation\Contract; | ||
|
||
interface UncompromisedVerifier | ||
{ | ||
/** | ||
* Verify that the given data has not been compromised in data leaks. | ||
*/ | ||
public function verify(array $data): bool; | ||
} |
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,88 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf. | ||
* | ||
* @link https://www.hyperf.io | ||
* @document https://hyperf.wiki | ||
* @contact [email protected] | ||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Hyperf\Validation; | ||
|
||
use Hyperf\Collection\Collection; | ||
use Hyperf\Guzzle\ClientFactory; | ||
use Hyperf\Stringable\Str; | ||
use Hyperf\Validation\Contract\UncompromisedVerifier; | ||
|
||
class NotPwnedVerifier implements UncompromisedVerifier | ||
{ | ||
/** | ||
* Create a new uncompromised verifier. | ||
* @param ClientFactory $factory the factory to create the HTTP client | ||
* @param int $timeout the number of seconds the request can run before timing out | ||
*/ | ||
public function __construct( | ||
protected ClientFactory $factory, | ||
protected int $timeout = 30, | ||
) { | ||
} | ||
|
||
public function verify(array $data): bool | ||
{ | ||
$value = $data['value']; | ||
$threshold = $data['threshold']; | ||
|
||
if (empty($value = (string) $value)) { | ||
return false; | ||
} | ||
|
||
[$hash,$hashPrefix] = $this->getHash($value); | ||
return ! $this->search($hashPrefix) | ||
->contains(static function ($line) use ($hash, $hashPrefix, $threshold) { | ||
[$hashSuffix, $count] = explode(':', $line); | ||
|
||
return $hashPrefix . $hashSuffix === $hash && $count > $threshold; | ||
}); | ||
} | ||
|
||
/** | ||
* Get the hash and its first 5 chars. | ||
*/ | ||
protected function getHash(string $value): array | ||
{ | ||
$hash = strtoupper(sha1($value)); | ||
|
||
$hashPrefix = substr($hash, 0, 5); | ||
|
||
return [$hash, $hashPrefix]; | ||
} | ||
|
||
/** | ||
* Search by the given hash prefix and returns all occurrences of leaked passwords. | ||
*/ | ||
protected function search(string $hashPrefix): Collection | ||
{ | ||
$client = $this->factory->create([ | ||
'timeout' => $this->timeout, | ||
]); | ||
$response = $client->get( | ||
'https://api.pwnedpasswords.com/range/' . $hashPrefix, | ||
[ | ||
'headers' => [ | ||
'Add-Padding' => true, | ||
], | ||
] | ||
); | ||
|
||
$body = ($response->getStatusCode() === 200) | ||
? $response->getBody()->getContents() | ||
: ''; | ||
|
||
return Str::of($body)->trim()->explode("\n")->filter(function ($line) { | ||
return str_contains($line, ':'); | ||
}); | ||
} | ||
} |
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 |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
/** | ||
* This file is part of Hyperf. | ||
* | ||
* @link https://www.hyperf.io | ||
* @document https://hyperf.wiki | ||
* @contact [email protected] | ||
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE | ||
*/ | ||
|
||
namespace Hyperf\Validation\Rules; | ||
|
||
use BackedEnum; | ||
use Hyperf\Contract\Arrayable; | ||
use Stringable; | ||
use UnitEnum; | ||
|
||
class ArrayRule implements Stringable | ||
{ | ||
protected $keys; | ||
|
||
public function __construct( | ||
$keys = null, | ||
) { | ||
if ($keys instanceof Arrayable) { | ||
$keys = $keys->toArray(); | ||
} | ||
$this->keys = is_array($keys) ? $keys : func_get_args(); | ||
} | ||
|
||
public function __toString() | ||
{ | ||
if (empty($this->keys)) { | ||
return 'array'; | ||
} | ||
|
||
$keys = array_map( | ||
static fn ($key) => match (true) { | ||
$key instanceof BackedEnum => $key->value, | ||
$key instanceof UnitEnum => $key->name, | ||
default => $key, | ||
}, | ||
$this->keys, | ||
); | ||
|
||
return 'array:' . implode(',', $keys); | ||
} | ||
} |
Oops, something went wrong.