RunAsAuthenticationProvider.php 1.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /*
  3. * Copyright 2010 Johannes M. Schmitt <schmittjoh@gmail.com>
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. namespace JMS\SecurityExtraBundle\Security\Authentication\Provider;
  18. use Symfony\Component\Security\Core\Exception\BadCredentialsException;
  19. use JMS\SecurityExtraBundle\Security\Authentication\Token\RunAsUserToken;
  20. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  21. use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
  22. /**
  23. * Class which authenticates RunAsTokens.
  24. *
  25. * @author Johannes M. Schmitt <schmittjoh@gmail.com>
  26. */
  27. class RunAsAuthenticationProvider implements AuthenticationProviderInterface
  28. {
  29. private $key;
  30. public function __construct($key)
  31. {
  32. $this->key = $key;
  33. }
  34. public function authenticate(TokenInterface $token)
  35. {
  36. if (!$this->supports($token)) {
  37. return null;
  38. }
  39. if ($token->getKey() === $this->key) {
  40. return $token;
  41. } else {
  42. throw new BadCredentialsException('The keys do not match.');
  43. }
  44. }
  45. public function supports(TokenInterface $token)
  46. {
  47. return $token instanceof RunAsUserToken;
  48. }
  49. }