Skip to content

Commit 1faaf71

Browse files
committed
Merge branch 'clean-up-demo'
2 parents 148e409 + a627b88 commit 1faaf71

File tree

2 files changed

+35
-69
lines changed

2 files changed

+35
-69
lines changed

demo/demo.php

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,30 +6,46 @@
66
<body>
77
<ol>
88
<?php
9-
require_once 'loader.php';
10-
Loader::register('../lib','RobThree\\Auth');
9+
// in practice you would require the composer loader if it was not already part of your framework or project
10+
spl_autoload_register(function ($className) {
11+
include_once str_replace(array('RobThree\\Auth', '\\'), array(__DIR__.'/../lib', '/'), $className) . '.php';
12+
});
1113

12-
use \RobThree\Auth\TwoFactorAuth;
13-
14-
$tfa = new TwoFactorAuth('MyApp');
15-
16-
echo '<li>First create a secret and associate it with a user';
17-
$secret = $tfa->createSecret(160); // Though the default is an 80 bits secret (for backwards compatibility reasons) we recommend creating 160+ bits secrets (see RFC 4226 - Algorithm Requirements)
18-
echo '<li>Next create a QR code and let the user scan it:<br><img src="' . $tfa->getQRCodeImageAsDataUri('My label', $secret) . '"><br>...or display the secret to the user for manual entry: ' . chunk_split($secret, 4, ' ');
19-
$code = $tfa->getCode($secret);
20-
echo '<li>Next, have the user verify the code; at this time the code displayed by a 2FA-app would be: <span style="color:#00c">' . $code . '</span> (but that changes periodically)';
21-
echo '<li>When the code checks out, 2FA can be / is enabled; store (encrypted?) secret with user and have the user verify a code each time a new session is started.';
22-
echo '<li>When aforementioned code (' . $code . ') was entered, the result would be: ' . (($tfa->verifyCode($secret, $code) === true) ? '<span style="color:#0c0">OK</span>' : '<span style="color:#c00">FAIL</span>');
14+
// substitute your company or app name here
15+
$tfa = new RobThree\Auth\TwoFactorAuth('RobThree TwoFactorAuth');
16+
?>
17+
<li>First create a secret and associate it with a user</li>
18+
<?php
19+
$secret = $tfa->createSecret();
20+
?>
21+
<li>
22+
Next create a QR code and let the user scan it:<br>
23+
<img src="<?php echo $tfa->getQRCodeImageAsDataUri('Demo', $secret); ?>"><br>
24+
...or display the secret to the user for manual entry:
25+
<?php echo chunk_split($secret, 4, ' '); ?>
26+
</li>
27+
<?php
28+
$code = $tfa->getCode($secret);
2329
?>
30+
<li>Next, have the user verify the code; at this time the code displayed by a 2FA-app would be: <span style="color:#00c"><?php echo $code; ?></span> (but that changes periodically)</li>
31+
<li>When the code checks out, 2FA can be / is enabled; store (encrypted?) secret with user and have the user verify a code each time a new session is started.</li>
32+
<li>
33+
When aforementioned code (<?php echo $code; ?>) was entered, the result would be:
34+
<?php if ($tfa->verifyCode($secret, $code) === true) { ?>
35+
<span style="color:#0c0">OK</span>
36+
<?php } else { ?>
37+
<span style="color:#c00">FAIL</span>
38+
<?php } ?>
39+
</li>
2440
</ol>
2541
<p>Note: Make sure your server-time is <a href="http://en.wikipedia.org/wiki/Network_Time_Protocol">NTP-synced</a>! Depending on the $discrepancy allowed your time cannot drift too much from the users' time!</p>
2642
<?php
27-
try {
28-
$tfa->ensureCorrectTime();
29-
echo 'Your hosts time seems to be correct / within margin';
30-
} catch (RobThree\Auth\TwoFactorAuthException $ex) {
31-
echo '<b>Warning:</b> Your hosts time seems to be off: ' . $ex->getMessage();
32-
}
43+
try {
44+
$tfa->ensureCorrectTime();
45+
echo 'Your hosts time seems to be correct / within margin';
46+
} catch (RobThree\Auth\TwoFactorAuthException $ex) {
47+
echo '<b>Warning:</b> Your hosts time seems to be off: ' . $ex->getMessage();
48+
}
3349
?>
3450
</body>
3551
</html>

demo/loader.php

Lines changed: 0 additions & 50 deletions
This file was deleted.

0 commit comments

Comments
 (0)