authenticator.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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->svgUseFillAttributes = false;
  38. $options->drawCircularModules = true;
  39. $options->circleRadius = 0.4;
  40. $options->connectPaths = true;
  41. $options->keepAsSquare = [
  42. QRMatrix::M_FINDER_DARK,
  43. QRMatrix::M_FINDER_DOT,
  44. QRMatrix::M_ALIGNMENT_DARK,
  45. ];
  46. $options->svgDefs = '
  47. <linearGradient id="gradient" x1="1" y2="1">
  48. <stop id="stop1" offset="0" />
  49. <stop id="stop2" offset="0.5"/>
  50. <stop id="stop3" offset="1"/>
  51. </linearGradient>';
  52. // invoke the worker instances
  53. $authenticator = new Authenticator($options);
  54. $qrcode = new QRCode($options);
  55. // create a secret and URI, generate the QR Code
  56. $secret = $authenticator->createSecret(24);
  57. $uri = $authenticator->getUri('your authenticator', 'this website');
  58. $svg = $qrcode->render($uri);
  59. // dump the output
  60. header('Content-type: text/html');
  61. ?>
  62. <!DOCTYPE html>
  63. <html lang="en">
  64. <head>
  65. <meta charset="UTF-8"/>
  66. <meta name="viewport" content="width=device-width, initial-scale=1.0"/>
  67. <title>QRCode Authenticator Example</title>
  68. <style>
  69. #authenticator-qrcode{
  70. width: 500px;
  71. margin: 2em auto;
  72. }
  73. #secret{
  74. box-sizing: border-box;
  75. display: inline-block;
  76. width: 100%;
  77. font-size: 20px;
  78. }
  79. /* styles for the embedded SVG QR code */
  80. .my-qrcode.qr-svg{
  81. margin-bottom: 1em;
  82. background: #eee; /* example for https://github.com/chillerlan/php-qrcode/discussions/199 */
  83. }
  84. .my-qrcode.qr-data-dark{
  85. fill: url(#gradient); /* the gradient defined in the SVG defs */
  86. }
  87. .my-qrcode > defs > #gradient > #stop1{
  88. stop-color: #D70071;
  89. }
  90. .my-qrcode > defs > #gradient > #stop2{
  91. stop-color: #9C4E97;
  92. }
  93. .my-qrcode > defs > #gradient > #stop3{
  94. stop-color: #0035A9;
  95. }
  96. </style>
  97. </head>
  98. <body>
  99. <div id="authenticator-qrcode">
  100. <!-- embed the SVG directly -->
  101. <?php echo $svg ?>
  102. <!-- the input that holds the authenticator secret -->
  103. <input value="<?php echo $secret; ?>" id="secret" type="text" readonly="readonly" onclick="this.select();" />
  104. <label for="secret">secret</label>
  105. </div>
  106. </body>
  107. </html>
  108. <?php
  109. exit;