-
Notifications
You must be signed in to change notification settings - Fork 1
/
index.php
201 lines (196 loc) · 7.93 KB
/
index.php
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
<?php
/**
* Copyright (c) 2014 Yubico AB
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* * Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* * Redistributions in binary form must reproduce the above
* copyright notice, this list of conditions and the following
* disclaimer in the documentation and/or other materials provided
* with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
/**
* This is a minimal example of U2F registration and authentication.
* The data that has to be stored between registration and authentication
* is stored in browser localStorage, so there's nothing real-world
* about this.
*/
require_once('U2F.php');
$scheme = 'https://';
$u2f = new u2flib_server\U2F($scheme . $_SERVER['HTTP_HOST']);
?>
<html>
<head>
<title>PHP U2F Demo</title>
<!--
<script src="/u2f-api-pp.js"></script>
-->
<script>
function addRegistration(reg) {
var existing = localStorage.getItem('u2fregistration');
var regobj = JSON.parse(reg);
var data = null;
if(existing) {
data = JSON.parse(existing);
if(Array.isArray(data)) {
for (var i = 0; i < data.length; i++) {
console.log( data[i], regobj );
if(data[i].keyHandle === regobj.keyHandle) {
data.splice(i,1);
break;
}
}
data.push(regobj);
} else {
data = null;
}
}
if(data == null) {
data = [regobj];
}
localStorage.setItem('u2fregistration', JSON.stringify(data));
}
<?php
function fixupArray($data) {
$ret = array();
$decoded = json_decode($data);
foreach ($decoded as $d) {
$ret[] = json_encode($d);
}
return $ret;
}
if($_SERVER['REQUEST_METHOD'] === 'POST') {
if(isset($_POST['startRegister'])) {
$regs = json_decode($_POST['registrations']) ? : array();
list($request, $signs) = $u2f->getRegisterData($regs);
echo "var request = " . json_encode($request) . ";\n";
echo "var signs = " . json_encode($signs) . ";\n";
?>
setTimeout(function() {
var appId = request.appId;
var registerRequests = [ { version: request.version, challenge: request.challenge } ];
console.log("Register: ", registerRequests, signs, appId);
u2f.register(appId, registerRequests, signs, function(data) {
var form = document.getElementById('form');
var reg = document.getElementById('doRegister');
var req = document.getElementById('request');
console.log("Register callback", data);
if(data.errorCode && data.errorCode != 0) {
alert("registration failed with errror: " + data.errorCode);
return;
}
reg.value=JSON.stringify(data);
req.value=JSON.stringify(request);
form.submit();
});
}, 1000);
<?php
} else if($_POST['doRegister']) {
try {
$data = $u2f->doRegister(json_decode($_POST['request']), json_decode($_POST['doRegister']));
echo "var registration = '" . json_encode($data) . "';\n";
?>
addRegistration(registration);
alert("registration successful!");
<?php
} catch(u2flib_server\Error $e) {
echo "alert('error:" . $e->getMessage() . "');\n";
}
} else if(isset($_POST['startAuthenticate'])) {
$regs = json_decode($_POST['registrations']);
$data = $u2f->getAuthenticateData($regs);
$registeredKeys = array();
foreach ($data as $d )
{
$key = array();
$key['version'] = $d->version;
$key['keyHandle'] = $d->keyHandle;
$registeredKeys[] = $key;
}
echo "var request = " . json_encode($data) . ";\n";
echo "var registrations = " . $_POST['registrations'] . ";\n";
echo "var appId = '". $data[0]->appId . "';\n";
echo "var challenge = ". json_encode($data[0]->challenge ) . ";\n";
echo "var registeredKeys = ". json_encode($registeredKeys) . ";\n";
?>
setTimeout(function() {
console.log( appId, challenge, registeredKeys );
u2f.sign(appId, challenge, registeredKeys, function(data) {
var form = document.getElementById('form');
var reg = document.getElementById('doAuthenticate');
var req = document.getElementById('request');
var regs = document.getElementById('registrations');
console.log("Authenticate callback", data);
reg.value=JSON.stringify(data);
req.value=JSON.stringify(request);
regs.value=JSON.stringify(registrations);
form.submit();
});
}, 1000);
<?php
} else if($_POST['doAuthenticate']) {
$reqs = json_decode($_POST['request']);
$regs = json_decode($_POST['registrations']);
try {
$data = $u2f->doAuthenticate($reqs, $regs, json_decode($_POST['doAuthenticate']));
echo "var registration = '" . json_encode($data) . "';\n";
echo "addRegistration(registration);\n";
echo "alert('Authentication successful, counter:" . $data->counter . "');\n";
} catch(u2flib_server\Error $e) {
echo "alert('error:" . $e->getMessage() . "');\n";
}
}
}
?>
</script>
</head>
<body>
<form method="POST" id="form">
<button name="startRegister" type="submit">Register</button>
<input type="hidden" name="doRegister" id="doRegister"/>
<button name="startAuthenticate" type="submit" id="startAuthenticate">Authenticate</button>
<input type="hidden" name="doAuthenticate" id="doAuthenticate"/>
<input type="hidden" name="request" id="request"/>
<input type="hidden" name="registrations" id="registrations"/>
</form>
<p>
<span id="registered">0</span> Authenticators currently registered.
</p>
<script>
var reg = localStorage.getItem('u2fregistration');
var auth = document.getElementById('startAuthenticate');
if(reg == null) {
auth.disabled = true;
} else {
var regs = document.getElementById('registrations');
decoded = JSON.parse(reg);
if(!Array.isArray(decoded)) {
auth.disabled = true;
} else {
regs.value = reg;
console.log("set the registrations to : ", reg);
var regged = document.getElementById('registered');
regged.innerHTML = decoded.length;
}
}
</script>
</body>
</html>