authenticator.php 3.3 KB

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