TwoFactorAuth.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. /**
  3. * @package Grav\Plugin\Login
  4. *
  5. * @copyright Copyright (C) 2014 - 2018 RocketTheme, LLC. All rights reserved.
  6. * @license MIT License; see LICENSE file for details.
  7. */
  8. namespace Grav\Plugin\Login\TwoFactorAuth;
  9. use Grav\Common\Grav;
  10. use RobThree\Auth\TwoFactorAuth as Auth;
  11. use RobThree\Auth\TwoFactorAuthException;
  12. /**
  13. * Class TwoFactorAuth
  14. * @package Grav\Plugin\Login\RememberMe
  15. */
  16. class TwoFactorAuth
  17. {
  18. protected $twoFa;
  19. /**
  20. * TwoFactorAuth constructor.
  21. * @throws TwoFactorAuthException
  22. */
  23. public function __construct()
  24. {
  25. $this->twoFa = new Auth('Grav', 6, 30, 'sha1', new BaconQrProvider);
  26. }
  27. /**
  28. * @return Auth
  29. */
  30. public function get2FA()
  31. {
  32. return $this->twoFa;
  33. }
  34. /**
  35. * @param int $bits
  36. * @return string
  37. * @throws TwoFactorAuthException
  38. */
  39. public function createSecret($bits = 160)
  40. {
  41. return $this->twoFa->createSecret($bits);
  42. }
  43. /**
  44. * @param string $secret
  45. * @param string $code
  46. * @return bool
  47. */
  48. public function verifyCode($secret, $code)
  49. {
  50. $secret = str_replace(' ', '', $secret);
  51. return $this->twoFa->verifyCode($secret, $code);
  52. }
  53. /**
  54. * @param string $username
  55. * @param string $secret
  56. * @return string
  57. * @throws TwoFactorAuthException
  58. */
  59. public function getQrImageData($username, $secret)
  60. {
  61. $label = $username . ':' . Grav::instance()['config']->get('site.title');
  62. $secret = str_replace(' ', '', $secret);
  63. return $this->twoFa->getQRCodeImageAsDataUri($label, $secret);
  64. }
  65. }