public function authenticateToken(TokenInterface $token, UserProviderInterface $userProvider, $providerKey)
    {
        $apiKey = $token->getCredentials();

        $user = $this
            ->om
            ->getRepository($this->modelName)
            ->findOneBy(array($this->metadata->getPropertyName(ClassMetadata::API_KEY_PROPERTY) => (string) $apiKey));

        if (!$user) {
            $this->dispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::FIREWALL_FAILURE, new OnFirewallFailureEvent());

            throw new AuthenticationException(
                sprintf('API key %s does not exist!', $apiKey)
            );
        }

        $token = new PreAuthenticatedToken(
            $user,
            $apiKey,
            $providerKey,
            $user->getRoles() ?: array()
        );

        $firewallEvent = new OnFirewallAuthenticationEvent($user);
        $firewallEvent->setToken($token);

        $this->dispatcher->dispatch(Ma27ApiKeyAuthenticationEvents::FIREWALL_LOGIN, $firewallEvent);

        return $token;
    }