TwoFactorAuth.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. /**
  3. * @package Grav\Plugin\Login
  4. *
  5. * @copyright Copyright (C) 2014 - 2021 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 Grav\Common\HTTP\Client;
  11. use Grav\Common\Utils;
  12. use RobThree\Auth\TwoFactorAuth as Auth;
  13. use RobThree\Auth\TwoFactorAuthException;
  14. use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
  15. use Symfony\Contracts\HttpClient\Exception\RedirectionExceptionInterface;
  16. use Symfony\Contracts\HttpClient\Exception\ServerExceptionInterface;
  17. use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
  18. /**
  19. * Class TwoFactorAuth
  20. * @package Grav\Plugin\Login\RememberMe
  21. */
  22. class TwoFactorAuth
  23. {
  24. protected $twoFa;
  25. /**
  26. * TwoFactorAuth constructor.
  27. * @throws TwoFactorAuthException
  28. */
  29. public function __construct()
  30. {
  31. $this->twoFa = new Auth('Grav', 6, 30, 'sha1', new BaconQrProvider);
  32. }
  33. /**
  34. * @return Auth
  35. */
  36. public function get2FA()
  37. {
  38. return $this->twoFa;
  39. }
  40. /**
  41. * @param int $bits
  42. * @return string
  43. * @throws TwoFactorAuthException
  44. */
  45. public function createSecret($bits = 160)
  46. {
  47. return $this->twoFa->createSecret($bits);
  48. }
  49. /**
  50. * @param string $secret
  51. * @param string $code
  52. * @return bool
  53. */
  54. public function verifyCode($secret, $code)
  55. {
  56. if (!$secret || !$code) {
  57. return false;
  58. }
  59. $secret = str_replace(' ', '', $secret);
  60. return $this->twoFa->verifyCode($secret, $code);
  61. }
  62. /**
  63. * @param string $username
  64. * @param string $secret
  65. * @return string
  66. * @throws TwoFactorAuthException
  67. */
  68. public function getQrImageData($username, $secret)
  69. {
  70. $label = $username . ':' . Grav::instance()['config']->get('site.title');
  71. $secret = str_replace(' ', '', $secret);
  72. return $this->twoFa->getQRCodeImageAsDataUri($label, $secret);
  73. }
  74. /**
  75. * @param string $yubikey_id
  76. * @param string $otp
  77. * @return bool
  78. */
  79. public function verifyYubikeyOTP(string $yubikey_id, string $otp): bool
  80. {
  81. // Quick sanity check
  82. if (!$yubikey_id || !$otp || !Utils::startsWith($otp, $yubikey_id)) {
  83. return false;
  84. }
  85. $api_url = "https://api.yubico.com/wsapi/2.0/verify?id=1&otp=%s&nonce=%s";
  86. $client = Client::getClient();
  87. $url = sprintf($api_url, $otp, Utils::getNonce('yubikey'));
  88. try {
  89. $response = $client->request('GET', $url);
  90. if ($response->getStatusCode() === 200) {
  91. $content = $response->getContent();
  92. if (Utils::contains($content, 'status=OK')) {
  93. return true;
  94. }
  95. }
  96. } catch (TransportExceptionInterface|ClientExceptionInterface|RedirectionExceptionInterface|ServerExceptionInterface $e) {
  97. return false;
  98. }
  99. return false;
  100. }
  101. }