Helper.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\UserBundle\GoogleAuthenticator;
  11. use Google\Authenticator\GoogleAuthenticator as BaseGoogleAuthenticator;
  12. use Sonata\UserBundle\Model\UserInterface;
  13. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  14. class Helper
  15. {
  16. /**
  17. * @var string
  18. */
  19. protected $server;
  20. /**
  21. * @var BaseGoogleAuthenticator
  22. */
  23. protected $authenticator;
  24. /**
  25. * @param string $server
  26. * @param BaseGoogleAuthenticator $authenticator
  27. */
  28. public function __construct($server, BaseGoogleAuthenticator $authenticator)
  29. {
  30. $this->server = $server;
  31. $this->authenticator = $authenticator;
  32. }
  33. /**
  34. * @param UserInterface $user
  35. * @param $code
  36. *
  37. * @return bool
  38. */
  39. public function checkCode(UserInterface $user, $code)
  40. {
  41. return $this->authenticator->checkCode($user->getTwoStepVerificationCode(), $code);
  42. }
  43. /**
  44. * @param UserInterface $user
  45. *
  46. * @return string
  47. */
  48. public function getUrl(UserInterface $user)
  49. {
  50. return $this->authenticator->getUrl($user->getUsername(), $this->server, $user->getTwoStepVerificationCode());
  51. }
  52. /**
  53. * @return string
  54. */
  55. public function generateSecret()
  56. {
  57. return $this->authenticator->generateSecret();
  58. }
  59. /**
  60. * @param UsernamePasswordToken $token
  61. *
  62. * @return string
  63. */
  64. public function getSessionKey(UsernamePasswordToken $token)
  65. {
  66. return sprintf('sonata_user_google_authenticator_%s_%s', $token->getProviderKey(), $token->getUsername());
  67. }
  68. }