在Symfony 3

时间:2016-04-06 06:07:18

标签: php authentication symfony

问题

我希望用户通过作为第一个请求的GET参数提供的访问令牌进行身份验证。

我从来没有在Symfony中实现过这样的东西,所以我按照How to Create a custom Authentication Provider中列出的步骤进行了操作,但它“不起作用”。 <{1}}的{​​{1}}方法未被触发。

我尝试了什么

因为它主要是很多配置,我甚至不知道如何调试它。这就是我到目前为止所得出的结论:只有authenticate被构造,没有别的。

代码

这些是系统的相关部分:

security.yml

AuthenticationProviderInterface

services.yml

AccessTokenProvider

AccessTokenFactory

security:

    # Snip default (empty) in_memory provider

    firewalls:
        # Snip dev and main (symfony default)

        accesstoken_secured:
            pattern:       ^/admin/
            accesstoken:   true

AccessTokenProvider

services:
    accesstoken.security.authentication.provider:
        class: AppBundle\Security\Authentication\Provider\AccessTokenProvider
        arguments:
            - '' # User Provider
            - '%kernel.cache_dir%/security/nonces'
        public: false

    accesstoken.security.authentication.listener:
        class: AppBundle\Security\Firewall\AccessTokenListener
        arguments: ['@security.token_storage', '@security.authentication.manager']
        public: false

AccessTokenListener

class AccessTokenFactory implements SecurityFactoryInterface
{
    public function create(ContainerBuilder $container, $id, $config, $userProvider, $defaultEntryPoint)
    {
        $providerId = 'security.authentication.provider.accesstoken.'.$id;
        $container
            ->setDefinition($providerId, new DefinitionDecorator('accesstoken.security.authentication.provider'))
            ->replaceArgument(0, new Reference($userProvider))
        ;

        $listenerId = 'security.authentication.listener.accesstoken.'.$id;
        $container->setDefinition($listenerId, new DefinitionDecorator('accesstoken.security.authentication.listener'));

        return array($providerId, $listenerId, $defaultEntryPoint);
    }

    public function getPosition()
    {
        return 'pre_auth';
    }

    public function getKey()
    {
        return 'accesstoken';
    }

    public function addConfiguration(NodeDefinition $node)
    {
    }
}

的accessToken

class AccessTokenProvider implements AuthenticationProviderInterface
{
    private $userProvider;

    public function __construct(UserProviderInterface $userProvider)
    {
        $this->userProvider = $userProvider;
    }

    public function authenticate(TokenInterface $token)
    {
        $user = $this->userProvider->loadUserByAccessToken($token->getAttribute('token'));

        if ($this->isTokenValid($token)) {
            $authenticatedToken = new AccessToken(['role_user']);
            $authenticatedToken->setUser($user);

            return $authenticatedToken;
        }

        throw new AuthenticationException('The WSSE authentication failed.');
    }

    protected function isTokenValid(AccessToken $token)
    {
        //TODO: Implement
        return (bool)$token->token;
    }

    public function supports(TokenInterface $token)
    {
        return $token instanceof AccessToken;
    }
}

1 个答案:

答案 0 :(得分:0)

我最终尝试使用How to Create a Custom Authentication System with Guard上的教程以另一种方式实现它。

这使用了Symfony的新Guard系统。它实际上很容易设置!

相关问题