Repository navigation
Expand file tree
/
Copy pathOneTimeCode.php
More file actions
56 lines (51 loc) · 1.99 KB
/
Copy pathOneTimeCode.php
File metadata and controls
56 lines (51 loc) · 1.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
declare(strict_types=1);
namespace NyonCode\WireModuleAuth\ValueObjects;
use Carbon\CarbonInterface;
use NyonCode\WireModuleAuth\Actions\MailResetCode;
use NyonCode\WireModuleAuth\Contracts\OneTimeCodes;
use NyonCode\WireModuleAuth\Enums\CodePurpose;
/**
* A code, in the one moment it is readable.
*
* Issuing returns this; verifying returns it again with `$code` empty, because
* by then the plain digits are the thing the caller typed and the row holds only
* a hash. What survives the round trip is the `payload` — for the e-mail
* verification flow, the address the code was asked for, so a code requested for
* one address cannot confirm another.
*
* **Nothing secret belongs in it.** The row stores it as plain JSON, so a
* payload is readable by anybody who can read the table — which is the whole
* reason the reset flow stopped carrying the broker's token there
* ({@see MailResetCode}).
*
* Readonly because a code that can be edited after it is issued is a code the
* notification and the row can disagree about.
*/
final readonly class OneTimeCode
{
/**
* @param string $code The digits, as the person will read them. Empty on
* the way back out of {@see OneTimeCodes::verify()}.
* @param array<string, mixed> $payload Whatever the flow has to carry
* from issuing to verifying.
*/
public function __construct(
public CodePurpose $purpose,
public string $identifier,
public string $code,
public CarbonInterface $expiresAt,
public array $payload = [],
) {}
/**
* One value out of the payload.
*
* A method rather than array access at every call site: the reset flow asks
* for `token` in one place and gets `null` rather than a notice if the row
* was written by an older release that did not carry one.
*/
public function payload(string $key): mixed
{
return $this->payload[$key] ?? null;
}
}