@@ -408,7 +408,7 @@ |
||
408 | 408 | * Fixes a certificate where the signature contains unused bits. |
409 | 409 | * |
410 | 410 | * @param string $cert |
411 | - * @return mixed |
|
411 | + * @return string |
|
412 | 412 | */ |
413 | 413 | private function fixSignatureUnusedBits($cert) |
414 | 414 | { |
@@ -97,7 +97,7 @@ discard block |
||
97 | 97 | */ |
98 | 98 | public function __construct($appId, $attestDir = null) |
99 | 99 | { |
100 | - if(OPENSSL_VERSION_NUMBER < 0x10000000) { |
|
100 | + if (OPENSSL_VERSION_NUMBER < 0x10000000) { |
|
101 | 101 | throw new Error('OpenSSL has to be at least version 1.0.0, this is ' . OPENSSL_VERSION_TEXT, ERR_OLD_OPENSSL); |
102 | 102 | } |
103 | 103 | $this->appId = $appId; |
@@ -135,19 +135,19 @@ discard block |
||
135 | 135 | */ |
136 | 136 | public function doRegister($request, $response, $includeCert = true) |
137 | 137 | { |
138 | - if( !is_object( $request ) ) { |
|
138 | + if (!is_object($request)) { |
|
139 | 139 | throw new \InvalidArgumentException('$request of doRegister() method only accepts object.'); |
140 | 140 | } |
141 | 141 | |
142 | - if( !is_object( $response ) ) { |
|
142 | + if (!is_object($response)) { |
|
143 | 143 | throw new \InvalidArgumentException('$response of doRegister() method only accepts object.'); |
144 | 144 | } |
145 | 145 | |
146 | - if( property_exists( $response, 'errorCode') ) { |
|
147 | - throw new Error('User-agent returned error. Error code: ' . $response->errorCode, ERR_BAD_UA_RETURNING ); |
|
146 | + if (property_exists($response, 'errorCode')) { |
|
147 | + throw new Error('User-agent returned error. Error code: ' . $response->errorCode, ERR_BAD_UA_RETURNING); |
|
148 | 148 | } |
149 | 149 | |
150 | - if( !is_bool( $includeCert ) ) { |
|
150 | + if (!is_bool($includeCert)) { |
|
151 | 151 | throw new \InvalidArgumentException('$include_cert of doRegister() method only accepts boolean.'); |
152 | 152 | } |
153 | 153 | |
@@ -156,8 +156,8 @@ discard block |
||
156 | 156 | $clientData = $this->base64u_decode($response->clientData); |
157 | 157 | $cli = json_decode($clientData); |
158 | 158 | |
159 | - if($cli->challenge !== $request->challenge) { |
|
160 | - throw new Error('Registration challenge does not match', ERR_UNMATCHED_CHALLENGE ); |
|
159 | + if ($cli->challenge !== $request->challenge) { |
|
160 | + throw new Error('Registration challenge does not match', ERR_UNMATCHED_CHALLENGE); |
|
161 | 161 | } |
162 | 162 | |
163 | 163 | $registration = new Registration(); |
@@ -166,8 +166,8 @@ discard block |
||
166 | 166 | $offs += PUBKEY_LEN; |
167 | 167 | // decode the pubKey to make sure it's good |
168 | 168 | $tmpKey = $this->pubkey_to_pem($pubKey); |
169 | - if($tmpKey === null) { |
|
170 | - throw new Error('Decoding of public key failed', ERR_PUBKEY_DECODE ); |
|
169 | + if ($tmpKey === null) { |
|
170 | + throw new Error('Decoding of public key failed', ERR_PUBKEY_DECODE); |
|
171 | 171 | } |
172 | 172 | $registration->publicKey = base64_encode($pubKey); |
173 | 173 | $khLen = $regData[$offs++]; |
@@ -185,17 +185,17 @@ discard block |
||
185 | 185 | $pemCert = "-----BEGIN CERTIFICATE-----\r\n"; |
186 | 186 | $pemCert .= chunk_split(base64_encode($rawCert), 64); |
187 | 187 | $pemCert .= "-----END CERTIFICATE-----"; |
188 | - if($includeCert) { |
|
188 | + if ($includeCert) { |
|
189 | 189 | $registration->certificate = base64_encode($rawCert); |
190 | 190 | } |
191 | - if($this->attestDir) { |
|
192 | - if(openssl_x509_checkpurpose($pemCert, -1, $this->get_certs()) !== true) { |
|
193 | - throw new Error('Attestation certificate can not be validated', ERR_ATTESTATION_VERIFICATION ); |
|
191 | + if ($this->attestDir) { |
|
192 | + if (openssl_x509_checkpurpose($pemCert, -1, $this->get_certs()) !== true) { |
|
193 | + throw new Error('Attestation certificate can not be validated', ERR_ATTESTATION_VERIFICATION); |
|
194 | 194 | } |
195 | 195 | } |
196 | 196 | |
197 | - if(!openssl_pkey_get_public($pemCert)) { |
|
198 | - throw new Error('Decoding of public key failed', ERR_PUBKEY_DECODE ); |
|
197 | + if (!openssl_pkey_get_public($pemCert)) { |
|
198 | + throw new Error('Decoding of public key failed', ERR_PUBKEY_DECODE); |
|
199 | 199 | } |
200 | 200 | $signature = substr($rawReg, $offs); |
201 | 201 | |
@@ -205,10 +205,10 @@ discard block |
||
205 | 205 | $dataToVerify .= $kh; |
206 | 206 | $dataToVerify .= $pubKey; |
207 | 207 | |
208 | - if(openssl_verify($dataToVerify, $signature, $pemCert, 'sha256') === 1) { |
|
208 | + if (openssl_verify($dataToVerify, $signature, $pemCert, 'sha256') === 1) { |
|
209 | 209 | return $registration; |
210 | 210 | } else { |
211 | - throw new Error('Attestation signature does not match', ERR_ATTESTATION_SIGNATURE ); |
|
211 | + throw new Error('Attestation signature does not match', ERR_ATTESTATION_SIGNATURE); |
|
212 | 212 | } |
213 | 213 | } |
214 | 214 | |
@@ -223,7 +223,7 @@ discard block |
||
223 | 223 | { |
224 | 224 | $sigs = array(); |
225 | 225 | foreach ($registrations as $reg) { |
226 | - if( !is_object( $reg ) ) { |
|
226 | + if (!is_object($reg)) { |
|
227 | 227 | throw new \InvalidArgumentException('$registrations of getAuthenticateData() method only accepts array of object.'); |
228 | 228 | } |
229 | 229 | |
@@ -252,12 +252,12 @@ discard block |
||
252 | 252 | */ |
253 | 253 | public function doAuthenticate(array $requests, array $registrations, $response) |
254 | 254 | { |
255 | - if( !is_object( $response ) ) { |
|
255 | + if (!is_object($response)) { |
|
256 | 256 | throw new \InvalidArgumentException('$response of doAuthenticate() method only accepts object.'); |
257 | 257 | } |
258 | 258 | |
259 | - if( property_exists( $response, 'errorCode') ) { |
|
260 | - throw new Error('User-agent returned error. Error code: ' . $response->errorCode, ERR_BAD_UA_RETURNING ); |
|
259 | + if (property_exists($response, 'errorCode')) { |
|
260 | + throw new Error('User-agent returned error. Error code: ' . $response->errorCode, ERR_BAD_UA_RETURNING); |
|
261 | 261 | } |
262 | 262 | |
263 | 263 | /** @var object|null $req */ |
@@ -269,35 +269,35 @@ discard block |
||
269 | 269 | $clientData = $this->base64u_decode($response->clientData); |
270 | 270 | $decodedClient = json_decode($clientData); |
271 | 271 | foreach ($requests as $req) { |
272 | - if( !is_object( $req ) ) { |
|
272 | + if (!is_object($req)) { |
|
273 | 273 | throw new \InvalidArgumentException('$requests of doAuthenticate() method only accepts array of object.'); |
274 | 274 | } |
275 | 275 | |
276 | - if($req->keyHandle === $response->keyHandle && $req->challenge === $decodedClient->challenge) { |
|
276 | + if ($req->keyHandle === $response->keyHandle && $req->challenge === $decodedClient->challenge) { |
|
277 | 277 | break; |
278 | 278 | } |
279 | 279 | |
280 | 280 | $req = null; |
281 | 281 | } |
282 | - if($req === null) { |
|
283 | - throw new Error('No matching request found', ERR_NO_MATCHING_REQUEST ); |
|
282 | + if ($req === null) { |
|
283 | + throw new Error('No matching request found', ERR_NO_MATCHING_REQUEST); |
|
284 | 284 | } |
285 | 285 | foreach ($registrations as $reg) { |
286 | - if( !is_object( $reg ) ) { |
|
286 | + if (!is_object($reg)) { |
|
287 | 287 | throw new \InvalidArgumentException('$registrations of doAuthenticate() method only accepts array of object.'); |
288 | 288 | } |
289 | 289 | |
290 | - if($reg->keyHandle === $response->keyHandle) { |
|
290 | + if ($reg->keyHandle === $response->keyHandle) { |
|
291 | 291 | break; |
292 | 292 | } |
293 | 293 | $reg = null; |
294 | 294 | } |
295 | - if($reg === null) { |
|
296 | - throw new Error('No matching registration found', ERR_NO_MATCHING_REGISTRATION ); |
|
295 | + if ($reg === null) { |
|
296 | + throw new Error('No matching registration found', ERR_NO_MATCHING_REGISTRATION); |
|
297 | 297 | } |
298 | 298 | $pemKey = $this->pubkey_to_pem($this->base64u_decode($reg->publicKey)); |
299 | - if($pemKey === null) { |
|
300 | - throw new Error('Decoding of public key failed', ERR_PUBKEY_DECODE ); |
|
299 | + if ($pemKey === null) { |
|
300 | + throw new Error('Decoding of public key failed', ERR_PUBKEY_DECODE); |
|
301 | 301 | } |
302 | 302 | |
303 | 303 | $signData = $this->base64u_decode($response->signatureData); |
@@ -306,18 +306,18 @@ discard block |
||
306 | 306 | $dataToVerify .= hash('sha256', $clientData, true); |
307 | 307 | $signature = substr($signData, 5); |
308 | 308 | |
309 | - if(openssl_verify($dataToVerify, $signature, $pemKey, 'sha256') === 1) { |
|
309 | + if (openssl_verify($dataToVerify, $signature, $pemKey, 'sha256') === 1) { |
|
310 | 310 | $ctr = unpack("Nctr", substr($signData, 1, 4)); |
311 | 311 | $counter = $ctr['ctr']; |
312 | 312 | /* TODO: wrap-around should be handled somehow.. */ |
313 | - if($counter > $reg->counter) { |
|
313 | + if ($counter > $reg->counter) { |
|
314 | 314 | $reg->counter = $counter; |
315 | 315 | return $reg; |
316 | 316 | } else { |
317 | - throw new Error('Counter too low.', ERR_COUNTER_TOO_LOW ); |
|
317 | + throw new Error('Counter too low.', ERR_COUNTER_TOO_LOW); |
|
318 | 318 | } |
319 | 319 | } else { |
320 | - throw new Error('Authentication failed', ERR_AUTHENTICATION_FAILURE ); |
|
320 | + throw new Error('Authentication failed', ERR_AUTHENTICATION_FAILURE); |
|
321 | 321 | } |
322 | 322 | } |
323 | 323 | |
@@ -328,9 +328,9 @@ discard block |
||
328 | 328 | { |
329 | 329 | $files = array(); |
330 | 330 | $dir = $this->attestDir; |
331 | - if($dir && $handle = opendir($dir)) { |
|
332 | - while(false !== ($entry = readdir($handle))) { |
|
333 | - if(is_file("$dir/$entry")) { |
|
331 | + if ($dir && $handle = opendir($dir)) { |
|
332 | + while (false !== ($entry = readdir($handle))) { |
|
333 | + if (is_file("$dir/$entry")) { |
|
334 | 334 | $files[] = "$dir/$entry"; |
335 | 335 | } |
336 | 336 | } |
@@ -363,7 +363,7 @@ discard block |
||
363 | 363 | */ |
364 | 364 | private function pubkey_to_pem($key) |
365 | 365 | { |
366 | - if(strlen($key) !== PUBKEY_LEN || $key[0] !== "\x04") { |
|
366 | + if (strlen($key) !== PUBKEY_LEN || $key[0] !== "\x04") { |
|
367 | 367 | return null; |
368 | 368 | } |
369 | 369 | |
@@ -379,7 +379,7 @@ discard block |
||
379 | 379 | */ |
380 | 380 | $der = "\x30\x59\x30\x13\x06\x07\x2a\x86\x48\xce\x3d\x02\x01"; |
381 | 381 | $der .= "\x06\x08\x2a\x86\x48\xce\x3d\x03\x01\x07\x03\x42"; |
382 | - $der .= "\0".$key; |
|
382 | + $der .= "\0" . $key; |
|
383 | 383 | |
384 | 384 | $pem = "-----BEGIN PUBLIC KEY-----\r\n"; |
385 | 385 | $pem .= chunk_split(base64_encode($der), 64); |
@@ -394,12 +394,12 @@ discard block |
||
394 | 394 | */ |
395 | 395 | private function createChallenge() |
396 | 396 | { |
397 | - $challenge = openssl_random_pseudo_bytes(32, $crypto_strong ); |
|
398 | - if( $crypto_strong !== true ) { |
|
397 | + $challenge = openssl_random_pseudo_bytes(32, $crypto_strong); |
|
398 | + if ($crypto_strong !== true) { |
|
399 | 399 | throw new Error('Unable to obtain a good source of randomness', ERR_BAD_RANDOM); |
400 | 400 | } |
401 | 401 | |
402 | - $challenge = $this->base64u_encode( $challenge ); |
|
402 | + $challenge = $this->base64u_encode($challenge); |
|
403 | 403 | |
404 | 404 | return $challenge; |
405 | 405 | } |
@@ -412,7 +412,7 @@ discard block |
||
412 | 412 | */ |
413 | 413 | private function fixSignatureUnusedBits($cert) |
414 | 414 | { |
415 | - if(in_array(hash('sha256', $cert), $this->FIXCERTS)) { |
|
415 | + if (in_array(hash('sha256', $cert), $this->FIXCERTS)) { |
|
416 | 416 | $cert[strlen($cert) - 257] = "\0"; |
417 | 417 | } |
418 | 418 | return $cert; |
@@ -1,7 +1,7 @@ discard block |
||
1 | 1 | #!/usr/bin/php |
2 | 2 | <?php |
3 | 3 | |
4 | - /* Copyright (c) 2015 Yubico AB |
|
4 | + /* Copyright (c) 2015 Yubico AB |
|
5 | 5 | * All rights reserved. |
6 | 6 | * |
7 | 7 | * Redistribution and use in source and binary forms, with or without |
@@ -44,38 +44,38 @@ discard block |
||
44 | 44 | $regs; |
45 | 45 | |
46 | 46 | if(array_key_exists('r', $options)) { |
47 | - $mode = "register"; |
|
47 | + $mode = "register"; |
|
48 | 48 | } elseif(array_key_exists('a', $options)) { |
49 | - if(!array_key_exists('R', $options)) { |
|
49 | + if(!array_key_exists('R', $options)) { |
|
50 | 50 | print "a registration must be supplied with -R"; |
51 | 51 | exit(1); |
52 | - } |
|
53 | - $regs = json_decode('[' . $options['R'] . ']'); |
|
54 | - $mode = "authenticate"; |
|
52 | + } |
|
53 | + $regs = json_decode('[' . $options['R'] . ']'); |
|
54 | + $mode = "authenticate"; |
|
55 | 55 | } else { |
56 | - print "-r or -a must be used\n"; |
|
57 | - exit(1); |
|
56 | + print "-r or -a must be used\n"; |
|
57 | + exit(1); |
|
58 | 58 | } |
59 | 59 | if(!array_key_exists('o', $options)) { |
60 | - print "origin must be supplied with -o\n"; |
|
61 | - exit(1); |
|
60 | + print "origin must be supplied with -o\n"; |
|
61 | + exit(1); |
|
62 | 62 | } |
63 | 63 | |
64 | 64 | $u2f = new u2flib_server\U2F($options['o']); |
65 | 65 | |
66 | 66 | if($mode === "register") { |
67 | - $challenge = $u2f->getRegisterData(); |
|
67 | + $challenge = $u2f->getRegisterData(); |
|
68 | 68 | } elseif($mode === "authenticate") { |
69 | - $challenge = $u2f->getAuthenticateData($regs); |
|
69 | + $challenge = $u2f->getAuthenticateData($regs); |
|
70 | 70 | } |
71 | 71 | |
72 | 72 | print json_encode($challenge[0]) . "\n"; |
73 | 73 | $response = fgets(STDIN); |
74 | 74 | |
75 | 75 | if($mode === "register") { |
76 | - $result = $u2f->doRegister($challenge[0], json_decode($response)); |
|
76 | + $result = $u2f->doRegister($challenge[0], json_decode($response)); |
|
77 | 77 | } elseif($mode === "authenticate") { |
78 | - $result = $u2f->doAuthenticate($challenge, $regs, json_decode($response)); |
|
78 | + $result = $u2f->doAuthenticate($challenge, $regs, json_decode($response)); |
|
79 | 79 | } |
80 | 80 | |
81 | 81 | print json_encode($result) . "\n"; |
@@ -43,10 +43,10 @@ discard block |
||
43 | 43 | $result; |
44 | 44 | $regs; |
45 | 45 | |
46 | -if(array_key_exists('r', $options)) { |
|
46 | +if (array_key_exists('r', $options)) { |
|
47 | 47 | $mode = "register"; |
48 | -} elseif(array_key_exists('a', $options)) { |
|
49 | - if(!array_key_exists('R', $options)) { |
|
48 | +} elseif (array_key_exists('a', $options)) { |
|
49 | + if (!array_key_exists('R', $options)) { |
|
50 | 50 | print "a registration must be supplied with -R"; |
51 | 51 | exit(1); |
52 | 52 | } |
@@ -56,25 +56,25 @@ discard block |
||
56 | 56 | print "-r or -a must be used\n"; |
57 | 57 | exit(1); |
58 | 58 | } |
59 | -if(!array_key_exists('o', $options)) { |
|
59 | +if (!array_key_exists('o', $options)) { |
|
60 | 60 | print "origin must be supplied with -o\n"; |
61 | 61 | exit(1); |
62 | 62 | } |
63 | 63 | |
64 | 64 | $u2f = new u2flib_server\U2F($options['o']); |
65 | 65 | |
66 | -if($mode === "register") { |
|
66 | +if ($mode === "register") { |
|
67 | 67 | $challenge = $u2f->getRegisterData(); |
68 | -} elseif($mode === "authenticate") { |
|
68 | +} elseif ($mode === "authenticate") { |
|
69 | 69 | $challenge = $u2f->getAuthenticateData($regs); |
70 | 70 | } |
71 | 71 | |
72 | 72 | print json_encode($challenge[0]) . "\n"; |
73 | 73 | $response = fgets(STDIN); |
74 | 74 | |
75 | -if($mode === "register") { |
|
75 | +if ($mode === "register") { |
|
76 | 76 | $result = $u2f->doRegister($challenge[0], json_decode($response)); |
77 | -} elseif($mode === "authenticate") { |
|
77 | +} elseif ($mode === "authenticate") { |
|
78 | 78 | $result = $u2f->doAuthenticate($challenge, $regs, json_decode($response)); |
79 | 79 | } |
80 | 80 |
@@ -77,9 +77,9 @@ discard block |
||
77 | 77 | } |
78 | 78 | return $ret; |
79 | 79 | } |
80 | - if($_SERVER['REQUEST_METHOD'] === 'POST') { |
|
81 | - if(isset($_POST['startRegister'])) { |
|
82 | - $regs = json_decode($_POST['registrations']) ? : array(); |
|
80 | + if ($_SERVER['REQUEST_METHOD'] === 'POST') { |
|
81 | + if (isset($_POST['startRegister'])) { |
|
82 | + $regs = json_decode($_POST['registrations']) ?: array(); |
|
83 | 83 | list($data, $reqs) = $u2f->getRegisterData($regs); |
84 | 84 | echo "var request = " . json_encode($data) . ";\n"; |
85 | 85 | echo "var signs = " . json_encode($reqs) . ";\n"; |
@@ -101,7 +101,7 @@ discard block |
||
101 | 101 | }); |
102 | 102 | }, 1000); |
103 | 103 | <?php |
104 | - } else if($_POST['doRegister']) { |
|
104 | + } else if ($_POST['doRegister']) { |
|
105 | 105 | try { |
106 | 106 | $data = $u2f->doRegister(json_decode($_POST['request']), json_decode($_POST['doRegister'])); |
107 | 107 | echo "var registration = '" . json_encode($data) . "';\n"; |
@@ -109,10 +109,10 @@ discard block |
||
109 | 109 | addRegistration(registration); |
110 | 110 | alert("registration successful!"); |
111 | 111 | <?php |
112 | - } catch(u2flib_server\Error $e) { |
|
112 | + } catch (u2flib_server\Error $e) { |
|
113 | 113 | echo "alert('error:" . $e->getMessage() . "');\n"; |
114 | 114 | } |
115 | - } else if(isset($_POST['startAuthenticate'])) { |
|
115 | + } else if (isset($_POST['startAuthenticate'])) { |
|
116 | 116 | $regs = json_decode($_POST['registrations']); |
117 | 117 | $data = $u2f->getAuthenticateData($regs); |
118 | 118 | echo "var registrations = " . $_POST['registrations'] . ";\n"; |
@@ -133,7 +133,7 @@ discard block |
||
133 | 133 | }); |
134 | 134 | }, 1000); |
135 | 135 | <?php |
136 | - } else if($_POST['doAuthenticate']) { |
|
136 | + } else if ($_POST['doAuthenticate']) { |
|
137 | 137 | $reqs = json_decode($_POST['request']); |
138 | 138 | $regs = json_decode($_POST['registrations']); |
139 | 139 | try { |
@@ -141,7 +141,7 @@ discard block |
||
141 | 141 | echo "var registration = '" . json_encode($data) . "';\n"; |
142 | 142 | echo "addRegistration(registration);\n"; |
143 | 143 | echo "alert('Authentication successful, counter:" . $data->counter . "');\n"; |
144 | - } catch(u2flib_server\Error $e) { |
|
144 | + } catch (u2flib_server\Error $e) { |
|
145 | 145 | echo "alert('error:" . $e->getMessage() . "');\n"; |
146 | 146 | } |
147 | 147 | } |
@@ -1,32 +1,32 @@ discard block |
||
1 | 1 | <?php |
2 | 2 | /** |
3 | - * Copyright (c) 2014 Yubico AB |
|
4 | - * All rights reserved. |
|
5 | - * |
|
6 | - * Redistribution and use in source and binary forms, with or without |
|
7 | - * modification, are permitted provided that the following conditions are |
|
8 | - * met: |
|
9 | - * |
|
10 | - * * Redistributions of source code must retain the above copyright |
|
11 | - * notice, this list of conditions and the following disclaimer. |
|
12 | - * |
|
13 | - * * Redistributions in binary form must reproduce the above |
|
14 | - * copyright notice, this list of conditions and the following |
|
15 | - * disclaimer in the documentation and/or other materials provided |
|
16 | - * with the distribution. |
|
17 | - * |
|
18 | - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
19 | - * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
20 | - * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
21 | - * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
22 | - * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
23 | - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
24 | - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
25 | - * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
26 | - * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
27 | - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
28 | - * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 | - */ |
|
3 | + * Copyright (c) 2014 Yubico AB |
|
4 | + * All rights reserved. |
|
5 | + * |
|
6 | + * Redistribution and use in source and binary forms, with or without |
|
7 | + * modification, are permitted provided that the following conditions are |
|
8 | + * met: |
|
9 | + * |
|
10 | + * * Redistributions of source code must retain the above copyright |
|
11 | + * notice, this list of conditions and the following disclaimer. |
|
12 | + * |
|
13 | + * * Redistributions in binary form must reproduce the above |
|
14 | + * copyright notice, this list of conditions and the following |
|
15 | + * disclaimer in the documentation and/or other materials provided |
|
16 | + * with the distribution. |
|
17 | + * |
|
18 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
|
19 | + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
|
20 | + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
|
21 | + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
|
22 | + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
|
23 | + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
|
24 | + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
|
25 | + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
|
26 | + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
|
27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|
28 | + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|
29 | + */ |
|
30 | 30 | |
31 | 31 | /** |
32 | 32 | * This is a simple example using PDO and a sqlite database for storing |
@@ -94,15 +94,15 @@ discard block |
||
94 | 94 | <?php |
95 | 95 | |
96 | 96 | if($_SERVER['REQUEST_METHOD'] === 'POST') { |
97 | - if(!$_POST['username']) { |
|
97 | + if(!$_POST['username']) { |
|
98 | 98 | echo "alert('no username provided!');"; |
99 | - } else if(!isset($_POST['action']) && !isset($_POST['register2']) && !isset($_POST['authenticate2'])) { |
|
99 | + } else if(!isset($_POST['action']) && !isset($_POST['register2']) && !isset($_POST['authenticate2'])) { |
|
100 | 100 | echo "alert('no action provided!');"; |
101 | - } else { |
|
101 | + } else { |
|
102 | 102 | $user = createAndGetUser($_POST['username']); |
103 | 103 | |
104 | 104 | if(isset($_POST['action'])) { |
105 | - switch($_POST['action']): |
|
105 | + switch($_POST['action']): |
|
106 | 106 | case 'register': |
107 | 107 | try { |
108 | 108 | $data = $u2f->getRegisterData(getRegs($user->id)); |
@@ -130,11 +130,11 @@ discard block |
||
130 | 130 | }); |
131 | 131 | }, 1000); |
132 | 132 | <?php |
133 | - } catch( Exception $e ) { |
|
133 | + } catch( Exception $e ) { |
|
134 | 134 | echo "alert('error: " . $e->getMessage() . "');"; |
135 | - } |
|
135 | + } |
|
136 | 136 | |
137 | - break; |
|
137 | + break; |
|
138 | 138 | |
139 | 139 | case 'authenticate': |
140 | 140 | try { |
@@ -157,34 +157,34 @@ discard block |
||
157 | 157 | }); |
158 | 158 | }, 1000); |
159 | 159 | <?php |
160 | - } catch( Exception $e ) { |
|
160 | + } catch( Exception $e ) { |
|
161 | 161 | echo "alert('error: " . $e->getMessage() . "');"; |
162 | - } |
|
162 | + } |
|
163 | 163 | |
164 | - break; |
|
164 | + break; |
|
165 | 165 | |
166 | - endswitch; |
|
166 | + endswitch; |
|
167 | 167 | } else if($_POST['register2']) { |
168 | - try { |
|
168 | + try { |
|
169 | 169 | $reg = $u2f->doRegister(json_decode($_SESSION['regReq']), json_decode($_POST['register2'])); |
170 | 170 | addReg($user->id, $reg); |
171 | - } catch( Exception $e ) { |
|
171 | + } catch( Exception $e ) { |
|
172 | 172 | echo "alert('error: " . $e->getMessage() . "');"; |
173 | - } finally { |
|
173 | + } finally { |
|
174 | 174 | $_SESSION['regReq'] = null; |
175 | - } |
|
175 | + } |
|
176 | 176 | } else if($_POST['authenticate2']) { |
177 | - try { |
|
177 | + try { |
|
178 | 178 | $reg = $u2f->doAuthenticate(json_decode($_SESSION['authReq']), getRegs($user->id), json_decode($_POST['authenticate2'])); |
179 | 179 | updateReg($reg); |
180 | 180 | echo "alert('success: " . $reg->counter . "');"; |
181 | - } catch( Exception $e ) { |
|
181 | + } catch( Exception $e ) { |
|
182 | 182 | echo "alert('error: " . $e->getMessage() . "');"; |
183 | - } finally { |
|
183 | + } finally { |
|
184 | 184 | $_SESSION['authReq'] = null; |
185 | - } |
|
185 | + } |
|
186 | + } |
|
186 | 187 | } |
187 | - } |
|
188 | 188 | } |
189 | 189 | ?> |
190 | 190 | </script> |
@@ -54,7 +54,7 @@ discard block |
||
54 | 54 | $sel = $pdo->prepare("select * from users where name = ?"); |
55 | 55 | $sel->execute(array($name)); |
56 | 56 | $user = $sel->fetch(); |
57 | - if(!$user) { |
|
57 | + if (!$user) { |
|
58 | 58 | $ins = $pdo->prepare("insert into users (name) values(?)"); |
59 | 59 | $ins->execute(array($name)); |
60 | 60 | $sel->execute(array($name)); |
@@ -93,21 +93,21 @@ discard block |
||
93 | 93 | <script> |
94 | 94 | <?php |
95 | 95 | |
96 | - if($_SERVER['REQUEST_METHOD'] === 'POST') { |
|
97 | - if(!$_POST['username']) { |
|
96 | + if ($_SERVER['REQUEST_METHOD'] === 'POST') { |
|
97 | + if (!$_POST['username']) { |
|
98 | 98 | echo "alert('no username provided!');"; |
99 | - } else if(!isset($_POST['action']) && !isset($_POST['register2']) && !isset($_POST['authenticate2'])) { |
|
99 | + } else if (!isset($_POST['action']) && !isset($_POST['register2']) && !isset($_POST['authenticate2'])) { |
|
100 | 100 | echo "alert('no action provided!');"; |
101 | 101 | } else { |
102 | 102 | $user = createAndGetUser($_POST['username']); |
103 | 103 | |
104 | - if(isset($_POST['action'])) { |
|
105 | - switch($_POST['action']): |
|
104 | + if (isset($_POST['action'])) { |
|
105 | + switch ($_POST['action']): |
|
106 | 106 | case 'register': |
107 | 107 | try { |
108 | 108 | $data = $u2f->getRegisterData(getRegs($user->id)); |
109 | 109 | |
110 | - list($req,$sigs) = $data; |
|
110 | + list($req, $sigs) = $data; |
|
111 | 111 | $_SESSION['regReq'] = json_encode($req); |
112 | 112 | echo "var req = " . json_encode($req) . ";"; |
113 | 113 | echo "var sigs = " . json_encode($sigs) . ";"; |
@@ -130,7 +130,7 @@ discard block |
||
130 | 130 | }); |
131 | 131 | }, 1000); |
132 | 132 | <?php |
133 | - } catch( Exception $e ) { |
|
133 | + } catch (Exception $e) { |
|
134 | 134 | echo "alert('error: " . $e->getMessage() . "');"; |
135 | 135 | } |
136 | 136 | |
@@ -157,28 +157,28 @@ discard block |
||
157 | 157 | }); |
158 | 158 | }, 1000); |
159 | 159 | <?php |
160 | - } catch( Exception $e ) { |
|
160 | + } catch (Exception $e) { |
|
161 | 161 | echo "alert('error: " . $e->getMessage() . "');"; |
162 | 162 | } |
163 | 163 | |
164 | 164 | break; |
165 | 165 | |
166 | 166 | endswitch; |
167 | - } else if($_POST['register2']) { |
|
167 | + } else if ($_POST['register2']) { |
|
168 | 168 | try { |
169 | 169 | $reg = $u2f->doRegister(json_decode($_SESSION['regReq']), json_decode($_POST['register2'])); |
170 | 170 | addReg($user->id, $reg); |
171 | - } catch( Exception $e ) { |
|
171 | + } catch (Exception $e) { |
|
172 | 172 | echo "alert('error: " . $e->getMessage() . "');"; |
173 | 173 | } finally { |
174 | 174 | $_SESSION['regReq'] = null; |
175 | 175 | } |
176 | - } else if($_POST['authenticate2']) { |
|
176 | + } else if ($_POST['authenticate2']) { |
|
177 | 177 | try { |
178 | 178 | $reg = $u2f->doAuthenticate(json_decode($_SESSION['authReq']), getRegs($user->id), json_decode($_POST['authenticate2'])); |
179 | 179 | updateReg($reg); |
180 | 180 | echo "alert('success: " . $reg->counter . "');"; |
181 | - } catch( Exception $e ) { |
|
181 | + } catch (Exception $e) { |
|
182 | 182 | echo "alert('error: " . $e->getMessage() . "');"; |
183 | 183 | } finally { |
184 | 184 | $_SESSION['authReq'] = null; |