-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path08-offline-mode.php
More file actions
80 lines (63 loc) · 2.08 KB
/
Copy path08-offline-mode.php
File metadata and controls
80 lines (63 loc) · 2.08 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
69
70
71
72
73
74
75
76
77
78
79
80
<?php
/**
* PPS-PHP Example 08: Offline Mode
*
* Demonstrates offline authentication without a server nonce:
* 1. Client creates an offline Pulse
* 2. Server verifies with stricter rate limiting
*
* Usage:
* php examples/08-offline-mode.php
*/
declare(strict_types=1);
require_once __DIR__ . '/../vendor/autoload.php';
use Pps\Client\AuthenticatorClient;
use Pps\Crypto\Base64Url;
use Pps\Key\KeyManager;
use Pps\Server\AuthenticationHandler;
use Pps\Storage\InMemoryStorage;
use Pps\Util\SystemTimeProvider;
echo "=== PPS-PHP Example 08: Offline Mode ===\n\n";
$storage = new InMemoryStorage();
$timeProvider = new SystemTimeProvider();
$rpId = 'example.com';
// Setup: Register device
$clientState = KeyManager::createClientState($rpId);
$kid = $clientState['kid'];
$normalSeed = Base64Url::decode($clientState['current_seed']);
$normalKey = KeyManager::keyPairFromSeed($normalSeed);
$storage->saveDeviceState($kid, [
'kid' => $kid,
'current_pk' => $normalKey->publicKeyB64u(),
'key_seq' => 0,
'last_counter' => 0,
'revoked' => false,
]);
// ---------------------------------------------------------------
// Step 1: Client creates offline Pulse
// ---------------------------------------------------------------
echo "Step 1: Client creates offline Pulse (no nonce)\n";
$client = new AuthenticatorClient($clientState, $timeProvider);
$pulse = $client
->rpId($rpId)
->offline(true)
->createPulse();
echo " Trust Code: " . $pulse['trust_code'] . "\n\n";
// ---------------------------------------------------------------
// Step 2: Server verifies offline Pulse
// ---------------------------------------------------------------
echo "Step 2: Server verifies offline Pulse\n";
$handler = new AuthenticationHandler($storage, $timeProvider);
$result = $handler->verifyOffline(
pulseToken: $pulse['token'],
rpId: $rpId
);
if ($result->ok) {
echo " Verification: SUCCESS\n";
echo " Offline mode: YES\n";
} else {
echo " Verification: FAILED\n";
echo " Error: " . $result->error . "\n";
exit(1);
}
echo "\n=== Example 08 Complete ===\n";