authenticator.php 3.2 KB

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