authenticator.php 3.3 KB

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