Skip to content

Commit 1d54f18

Browse files
committed
Added Authenticaton example code
1 parent 577820d commit 1d54f18

File tree

1 file changed

+121
-4
lines changed

1 file changed

+121
-4
lines changed

README.md

Lines changed: 121 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ TODO the descriptions
106106

107107
## Example Code
108108

109-
For a full working example of this repository please see [the dedicated example repository](https://github.com/Samyoul/U2F-php-server-examples)
109+
For a full working code example for this repository please see [the dedicated example repository](https://github.com/Samyoul/U2F-php-server-examples)
110110

111111
You can also install it with the following:
112112

@@ -255,19 +255,136 @@ try {
255255
}
256256
257257
//Fictitious view.
258-
echo View::make('template/location/u2f-registration-result.html', ['userMessage' => $userMessage]);
258+
echo View::make('template/location/u2f-registration-result.html', compact('userMessage'));
259259
```
260260
261261
### Authentication Code
262262
263+
#### Authentication Step 1:
263264
**Starting the authentication process:**
264265
265-
We assume that user has successfully authenticated and has previously registered.
266+
We assume that user has successfully authenticated and has previously registered to use FIDO U2F.
266267
267268
```php
268269
<?php
269-
// All the amazing authentication code
270+
271+
require('vendor/autoload.php');
272+
use Samyoul\U2F;
273+
274+
session_start();
275+
276+
// Fictitious function representing getting the authenticated user object
277+
$user = getAuthenticatedUser();
278+
279+
// Fictitious function, get U2F registrations associated with the user
280+
$registrations = $user->U2FRegistrations();
281+
282+
// This can be anything, but usually easier if you choose your applications domain and top level domain.
283+
$appId = "yourdomain.tld";
284+
285+
// Call the U2F makeAuthentication method, passing in the user's registration(s) and the app ID
286+
$authenticationRequest = U2F::makeAuthentication($registrations, $appId);
287+
288+
// Store the request for later
289+
$_SESSION['authenticationRequest'] = $authenticationRequest;
290+
291+
// now pass the data to a fictitious view.
292+
echo View::make('template/location/u2f-authentication.html', compact("authenticationRequest"));
293+
```
294+
295+
#### Registration Step 2:
296+
**Client-side, Talking To The USB**
297+
298+
Non-AJAX client-side registration of U2F key token. AJAX can of course be used in your application, but it is easier to demonstrate a linear process without AJAX and callbacks.
299+
300+
301+
```html
302+
<html>
303+
<head>
304+
<title>U2F Key Authentication</title>
305+
</head>
306+
<body>
307+
<h1>U2F Authentication</h1>
308+
<h2>Please enter your FIDO U2F device into your computer's USB port. Then confirm authentication on the device.</h2>
309+
310+
<div style="display:none;">
311+
<form id="u2f_submission" method="post" action="auth/u2f-authentication/confirm">
312+
<input id="u2f_authentication_response" name="authentication_response" value="" />
313+
</form>
314+
</div>
315+
316+
<script type="javascript" src="https://raw.githubusercontent.com/google/u2f-ref-code/master/u2f-gae-demo/war/js/u2f-api.js"></script>
317+
<script>
318+
setTimeout(function() {
319+
320+
// Magic JavaScript talking to your HID
321+
u2f.sign(<?php echo $authenticationRequest; ?>, function(data) {
322+
323+
// Handle returning error data
324+
if(data.errorCode && errorCode != 0) {
325+
alert("Authentication failed with error: " + data.errorCode);
326+
// Or handle the error however you'd like.
327+
328+
return;
329+
}
330+
331+
// On success process the data from USB device to send to the server
332+
var authentication_response = JSON.stringify(data);
333+
334+
// Get the form items so we can send data back to the server
335+
var form = document.getElementById('u2f_submission');
336+
var response = document.getElementById('u2f_authentication_response');
337+
338+
// Fill and submit form.
339+
response.value = JSON.stringify(authentication_response);
340+
form.submit();
341+
});
342+
}, 1000);
343+
</script>
344+
</body>
345+
</html>
346+
```
347+
348+
#### Authentication Step 3:
349+
**Validation**
270350

351+
This is the last stage of authentication. Validate the authentication response data against the original request data.
352+
353+
```php
354+
<?php
355+
356+
require('vendor/autoload.php');
357+
use Samyoul\U2F;
358+
359+
session_start();
360+
361+
// Fictitious function representing getting the authenticated user object
362+
$user = authenticatedUser();
363+
364+
// Fictitious function, get U2F registrations associated with the user
365+
$registrations = $user->U2FRegistrations();
366+
367+
try {
368+
369+
// Validate the authentication response against the registration request.
370+
// The output are the credentials you need to store for U2F authentication.
371+
$validatedAuthentication = U2F::authenticate(
372+
$_SESSION['authenticationRequest'],
373+
$registrations,
374+
json_decode($_POST['u2f_authentication_response'])
375+
);
376+
377+
// Fictitious function representing the updating of the U2F token count integer.
378+
$user->updateU2FRegistrationCount($validatedAuthentication);
379+
380+
// Then let your user know what happened
381+
$userMessage = "Success";
382+
} catch( Exception $e ) {
383+
$userMessage = "We had an error: ". $e->getMessage();
384+
}
385+
386+
//Fictitious view.
387+
echo View::make('template/location/u2f-authentication-result.html', compact('userMessage'));
271388
```
272389

273390
## Frameworks

0 commit comments

Comments
 (0)