authenticator.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * authenticator excample
  4. *
  5. * @created 09.07.2023
  6. * @author smiley <smiley@chillerlan.net>
  7. * @copyright 2023 smiley
  8. * @license MIT
  9. */
  10. use chillerlan\Authenticator\{Authenticator, AuthenticatorOptionsTrait};
  11. use chillerlan\Authenticator\Authenticators\AuthenticatorInterface;
  12. use chillerlan\Settings\SettingsContainerAbstract;
  13. use chillerlan\QRCode\{QRCode, QROptionsTrait};
  14. use chillerlan\QRCode\Data\QRMatrix;
  15. require_once __DIR__.'/../vendor/autoload.php';
  16. // create a new options container on the fly that hosts both, authenticator and qrcode
  17. $options = new class extends SettingsContainerAbstract{
  18. use AuthenticatorOptionsTrait, QROptionsTrait;
  19. };
  20. /*
  21. * AuthenticatorOptionsTrait
  22. *
  23. * @see https://github.com/chillerlan/php-authenticator
  24. */
  25. $options->mode = AuthenticatorInterface::TOTP;
  26. $options->digits = 8;
  27. $options->algorithm = AuthenticatorInterface::ALGO_SHA512;
  28. /*
  29. * QROptionsTrait
  30. */
  31. $options->version = 7;
  32. $options->addQuietzone = false;
  33. $options->outputBase64 = false;
  34. $options->svgAddXmlHeader = false;
  35. $options->cssClass = 'my-qrcode';
  36. $options->drawLightModules = false;
  37. $options->markupDark = '';
  38. $options->markupLight = '';
  39. $options->drawCircularModules = true;
  40. $options->circleRadius = 0.4;
  41. $options->connectPaths = true;
  42. $options->keepAsSquare = [
  43. QRMatrix::M_FINDER_DARK,
  44. QRMatrix::M_FINDER_DOT,
  45. QRMatrix::M_ALIGNMENT_DARK,
  46. ];
  47. $options->svgDefs = '
  48. <linearGradient id="gradient" x1="1" y2="1">
  49. <stop id="stop1" offset="0" />
  50. <stop id="stop2" offset="0.5"/>
  51. <stop id="stop3" offset="1"/>
  52. </linearGradient>';
  53. // invoke the worker instances
  54. $authenticator = new Authenticator($options);
  55. $qrcode = new QRCode($options);
  56. // create a secret and URI, generate the QR Code
  57. $secret = $authenticator->createSecret(24);
  58. $uri = $authenticator->getUri('your authenticator', 'this website');
  59. $svg = $qrcode->render($uri);
  60. // dump the output
  61. header('Content-type: text/html');
  62. ?>
  63. <!DOCTYPE html>
  64. <html lang="en">
  65. <head>
  66. <meta charset="UTF-8"/>
  67. <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  68. <title>QRCode Authenticator Example</title>
  69. <style>
  70. #authenticator-qrcode{
  71. width: 500px;
  72. margin: 2em auto;
  73. }
  74. #secret{
  75. box-sizing: border-box;
  76. display: inline-block;
  77. width: 100%;
  78. font-size: 20px;
  79. }
  80. /* styles for the embedded SVG QR code */
  81. .my-qrcode.qr-svg{
  82. margin-bottom: 1em;
  83. background: #eee; /* example for https://github.com/chillerlan/php-qrcode/discussions/199 */
  84. }
  85. .my-qrcode.qr-data-dark{
  86. fill: url(#gradient); /* the gradient defined in the SVG defs */
  87. }
  88. .my-qrcode > defs > #gradient > #stop1{
  89. stop-color: #D70071;
  90. }
  91. .my-qrcode > defs > #gradient > #stop2{
  92. stop-color: #9C4E97;
  93. }
  94. .my-qrcode > defs > #gradient > #stop3{
  95. stop-color: #0035A9;
  96. }
  97. </style>
  98. </head>
  99. <body>
  100. <div id="authenticator-qrcode">
  101. <!-- embed the SVG directly -->
  102. <?php echo $svg ?>
  103. <!-- the input that holds the authenticator secret -->
  104. <input value="<?php echo $secret; ?>" id="secret" type="text" readonly="readonly" onclick="this.select();" />
  105. <label for="secret">secret</label>
  106. </div>
  107. </body>
  108. </html>
  109. <?php
  110. exit;