Skip to content
This repository was archived by the owner on Jan 16, 2023. It is now read-only.

Commit be789ff

Browse files
committed
Completed Basic Non AJAX authentication
1 parent f9e231f commit be789ff

File tree

7 files changed

+66
-15
lines changed

7 files changed

+66
-15
lines changed

PDO Without AJAX/authenticate.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,12 @@
3030
// Store the request for later
3131
$_SESSION['authenticationRequest'] = $authenticationRequest;
3232

33+
// Store the user attempting to authenticate
34+
$_SESSION['authenticatingUser'] = $user;
35+
3336
// now pass the data to the U2F authentication view.
3437
$templates = new League\Plates\Engine(__DIR__.'/views');
35-
echo $templates->render('u2f-authenticate', ['authenticationRequest' => $authenticationRequest]);
38+
echo $templates->render('u2f-authentication', ['authenticationRequest' => json_encode($authenticationRequest)]);
3639

3740
}
3841

3 KB
Binary file not shown.

PDO Without AJAX/functions.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,24 @@ function getU2FRegistrations(stdClass $user)
5959
function storeU2FRegistration(stdClass $user, Registration $registration)
6060
{
6161
$pdo = getDBConnection();
62-
$ins = $pdo->prepare("
62+
$statement = $pdo->prepare("
6363
INSERT INTO registrations
6464
(user_id, keyHandle, publicKey, certificate, counter)
6565
VALUES (?, ?, ?, ?, ?)
6666
");
67-
$ins->execute([
67+
$statement->execute([
6868
$user->id,
6969
$registration->getKeyHandle(),
7070
$registration->getPublicKey(),
7171
$registration->getCertificate(),
7272
$registration->getCounter()
7373
]);
7474

75+
}
76+
77+
function updateU2FRegistration(stdClass $registration)
78+
{
79+
$pdo = getDBConnection();
80+
$statement = $pdo->prepare("UPDATE registrations SET counter = ? WHERE id = ?");
81+
$statement->execute([$registration->counter, $registration->id]);
7582
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
require("../vendor/autoload.php");
3+
require("functions.php");
4+
5+
use Samyoul\U2F\U2FServer\U2FServer as U2F;
6+
session_start();
7+
8+
$user = $_SESSION['authenticatingUser'];
9+
10+
// Get any U2F registrations associated with the user
11+
$registrations = getU2FRegistrations($user);
12+
13+
try {
14+
15+
// Validate the registration response against the registration request.
16+
// The output are the credentials you need to store for U2F authentication.
17+
$validatedAuthentication = U2F::authenticate(
18+
$_SESSION['authenticationRequest'],
19+
$registrations,
20+
json_decode($_POST['authentication_response'])
21+
);
22+
23+
// Store of the validated U2F registration data against the authenticated user.
24+
updateU2FRegistration($validatedAuthentication);
25+
26+
// Set authenticated user
27+
$_SESSION['authenticatedUser'] = $user;
28+
29+
// Then let your user know what happened
30+
$_SESSION['message'] = "You have successfully authenticated with a U2F key.";
31+
32+
} catch( Exception $e ) {
33+
$_SESSION['error'] = "We had an error: ". $e->getMessage();
34+
redirect("index.php");
35+
}
36+
37+
unset($_SESSION['authenticatingUser']);
38+
unset($_SESSION['authenticationRequest']);
39+
redirect("dashboard.php");

PDO Without AJAX/views/dashboard.php

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<p>Hello <?=$this->e($user->name)?> welcome to your super secure dashboard.</p>
66
<hr/>
77
<a class="btn btn-danger" href="logout.php">Log Out</a>
8+
<a class="btn btn-danger" href="database/reset.php">Reset Database</a>
89
</div>
910

1011
<h3>U2F Registrations</h3>

PDO Without AJAX/views/u2f-authentication.php

+8-7
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,21 @@
44
<h2>Please enter your FIDO U2F device into your computer's USB port. Then confirm authentication on the device.</h2>
55

66
<div style="display:none;">
7-
<form id="u2f_submission" method="post" action="u2f-authentication.php">
7+
<form id="u2f_submission" method="post" action="u2f-authentication-validation.php">
88
<input id="u2f_authentication_response" name="authentication_response" value="" />
99
</form>
1010
</div>
1111

12-
<script type="javascript" src="https://raw.githubusercontent.com/google/u2f-ref-code/master/u2f-gae-demo/war/js/u2f-api.js"></script>
13-
<script>
14-
window.u2f || document.write('%3Cscript src="../js/u2f-api.js"%3E%3C/script%3E');
15-
</script>
12+
<script src="../js/u2f-api.js"></script>
1613
<script>
1714
setTimeout(function() {
1815

16+
var authentication_request = <?=$authenticationRequest?>;
17+
18+
console.log("Authentication Request: ", authentication_request);
19+
1920
// Magic JavaScript talking to your HID
20-
u2f.sign(<?=$this->e($authenticationRequest)?>, function(data) {
21+
u2f.sign(authentication_request, function(data) {
2122

2223
// Handle returning error data
2324
if(data.errorCode && errorCode != 0) {
@@ -35,7 +36,7 @@
3536
var response = document.getElementById('u2f_authentication_response');
3637

3738
// Fill and submit form.
38-
response.value = JSON.stringify(authentication_response);
39+
response.value = authentication_response;
3940
form.submit();
4041
});
4142
}, 1000);

composer.lock

+5-5
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)