feat(cloud): argon2id password + CSRF token helpers
This commit is contained in:
parent
185629a4c4
commit
76ea1b4ecf
3 changed files with 45 additions and 0 deletions
11
cloud/src/Security/Csrf.php
Normal file
11
cloud/src/Security/Csrf.php
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace App\Security;
|
||||
|
||||
final class Csrf
|
||||
{
|
||||
public static function issue(callable $randHex): string { return (string)$randHex(); }
|
||||
public static function check(string $expected, string $given): bool {
|
||||
return $expected !== '' && hash_equals($expected, $given);
|
||||
}
|
||||
}
|
||||
13
cloud/src/Security/Password.php
Normal file
13
cloud/src/Security/Password.php
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace App\Security;
|
||||
|
||||
final class Password
|
||||
{
|
||||
public static function hash(string $plain): string {
|
||||
return password_hash($plain, PASSWORD_ARGON2ID);
|
||||
}
|
||||
public static function verify(string $plain, string $hash): bool {
|
||||
return password_verify($plain, $hash);
|
||||
}
|
||||
}
|
||||
21
cloud/tests/PasswordCsrfTest.php
Normal file
21
cloud/tests/PasswordCsrfTest.php
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
<?php
|
||||
declare(strict_types=1);
|
||||
namespace App\Tests;
|
||||
use App\Security\{Password, Csrf};
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
final class PasswordCsrfTest extends TestCase
|
||||
{
|
||||
public function testHashVerify(): void {
|
||||
$h = Password::hash('s3cret!');
|
||||
$this->assertNotSame('s3cret!', $h);
|
||||
$this->assertTrue(Password::verify('s3cret!', $h));
|
||||
$this->assertFalse(Password::verify('wrong', $h));
|
||||
}
|
||||
public function testCsrf(): void {
|
||||
$t = Csrf::issue(fn() => 'abc123');
|
||||
$this->assertSame('abc123', $t);
|
||||
$this->assertTrue(Csrf::check($t, 'abc123'));
|
||||
$this->assertFalse(Csrf::check($t, 'nope'));
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue