Helper.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /*
  3. * This file is part of the Sonata project.
  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. protected $server;
  17. protected $authenticator;
  18. /**
  19. * @param $server
  20. * @param \Google\Authenticator\GoogleAuthenticator $authenticator
  21. */
  22. public function __construct($server, BaseGoogleAuthenticator $authenticator)
  23. {
  24. $this->server = $server;
  25. $this->authenticator = $authenticator;
  26. }
  27. /**
  28. * @param \Sonata\UserBundle\Model\UserInterface $user
  29. * @param $code
  30. * @return bool
  31. */
  32. public function checkCode(UserInterface $user, $code)
  33. {
  34. return $this->authenticator->checkCode($user->getTwoStepVerificationCode(), $code);
  35. }
  36. /**
  37. * @param \Sonata\UserBundle\Model\UserInterface $user
  38. * @return string
  39. */
  40. public function getUrl(UserInterface $user)
  41. {
  42. return $this->authenticator->getUrl($user->getUsername(), $this->server, $user->getTwoStepVerificationCode());
  43. }
  44. /**
  45. * @return string
  46. */
  47. public function generateSecret()
  48. {
  49. return $this->authenticator->generateSecret();
  50. }
  51. /**
  52. * @param \Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken $token
  53. * @return string
  54. */
  55. public function getSessionKey(UsernamePasswordToken $token)
  56. {
  57. return sprintf('sonata_user_google_authenticator_%s_%s', $token->getProviderKey(), $token->getUsername());
  58. }
  59. }