demo.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435
  1. <!doctype html>
  2. <html>
  3. <head>
  4. <title>Demo</title>
  5. </head>
  6. <body>
  7. <ol>
  8. <?php
  9. require_once 'loader.php';
  10. Loader::register('../lib','RobThree\\Auth');
  11. use \RobThree\Auth\TwoFactorAuth;
  12. $tfa = new TwoFactorAuth('MyApp');
  13. echo '<li>First create a secret and associate it with a user';
  14. $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)
  15. 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, ' ');
  16. $code = $tfa->getCode($secret);
  17. 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)';
  18. 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.';
  19. 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>');
  20. ?>
  21. </ol>
  22. <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>
  23. <?php
  24. try {
  25. $tfa->ensureCorrectTime();
  26. echo 'Your hosts time seems to be correct / within margin';
  27. } catch (RobThree\Auth\TwoFactorAuthException $ex) {
  28. echo '<b>Warning:</b> Your hosts time seems to be off: ' . $ex->getMessage();
  29. }
  30. ?>
  31. </body>
  32. </html>