Repository navigation
Expand file tree
/
Copy pathSessionPendingCeremonyStore.php
More file actions
68 lines (53 loc) · 2.23 KB
/
Copy pathSessionPendingCeremonyStore.php
File metadata and controls
68 lines (53 loc) · 2.23 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
57
58
59
60
61
62
63
64
65
66
67
68
<?php declare(strict_types = 1);
namespace ShipMonk\PasskeysDemo;
use ShipMonk\Passkeys\PendingAuthentication;
use ShipMonk\Passkeys\PendingCeremonyStore;
use ShipMonk\Passkeys\PendingRegistration;
use function array_key_exists;
use function array_shift;
use function count;
/**
* The demo's {@see PendingCeremonyStore}: pending ceremonies live in the PHP session — the right
* scope, since a ceremony belongs to one browser session — keyed by the raw challenge bytes (PHP
* arrays and session serialization are binary-safe) and capped so a page that keeps requesting
* options cannot grow the session unboundedly.
*/
final class SessionPendingCeremonyStore implements PendingCeremonyStore
{
/**
* How many unfinished ceremonies to keep per browser session (oldest dropped first).
*/
private const int MAX_PENDING = 8;
public function rememberPendingAuthentication(PendingAuthentication $pending): void
{
$_SESSION['pending_authentications'][$pending->challenge] = $pending;
while (count($_SESSION['pending_authentications']) > self::MAX_PENDING) {
array_shift($_SESSION['pending_authentications']);
}
}
public function consumePendingAuthentication(string $challenge): ?PendingAuthentication
{
if (!array_key_exists($challenge, $_SESSION['pending_authentications'] ?? [])) {
return null;
}
$pending = $_SESSION['pending_authentications'][$challenge];
unset($_SESSION['pending_authentications'][$challenge]);
return $pending;
}
public function rememberPendingRegistration(PendingRegistration $pending): void
{
$_SESSION['pending_registrations'][$pending->challenge] = $pending;
while (count($_SESSION['pending_registrations']) > self::MAX_PENDING) {
array_shift($_SESSION['pending_registrations']);
}
}
public function consumePendingRegistration(string $challenge): ?PendingRegistration
{
if (!array_key_exists($challenge, $_SESSION['pending_registrations'] ?? [])) {
return null;
}
$pending = $_SESSION['pending_registrations'][$challenge];
unset($_SESSION['pending_registrations'][$challenge]);
return $pending;
}
}