1
votes

I did follow the cookbook to implement a custom authentication provider, but authenticate() will not execute from the WsseProvider.

I checked the supports() function and it works as it should.

PHP Storm gives the following error with the authenticate function:

Declaration must be compatible with AuthenticationManagerInterface->authenticate(token : \Symfony\Component\Security\Core\Authentication\TokenInterface)

But I have allready imported TokenInterface as explained in the cookbook:

use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;

So PHP Storm asks me to change to:

use Symfony\Component\Security\Core\Authentication\TokenInterface;

But by using this supports() doesnt execute.

Anyone know why I get this error, and/or why authenticate doesnt execute? I followed the steps exactly as it is explained in the cookbook.

Here is the code:

<?php

namespace AppBundle\Security\Authentication\Provider;

use Symfony\Component\Security\Core\Authentication\Provider\AuthenticationProviderInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\AuthenticationException;
use Symfony\Component\Security\Core\Exception\NonceExpiredException;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface; //Using this authenticate doesnt work
//use Symfony\Component\Security\Core\Authentication\TokenInterface; //Using this supports doesnt work
use AppBundle\Security\Authentication\Token\WsseUserToken;
use Symfony\Component\Security\Core\Util\StringUtils;

class WsseProvider implements  AuthenticationProviderInterface
{
    private $userProvider;
    private $cacheDir;

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

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

    public function authenticate(TokenInterface $token)
    {
        die('authenticate is executed'); //This doesnt fire

        $user = $this->userProvider->loadUserByUsername($token->getUsername());

        if ($user && $this->validateDigest($token->digest, $token->nonce, $token->created, $user->getPassword())) {
            $authenticatedToken = new WsseUserToken($user->getRoles());
            $authenticatedToken->setUser($user);

            return $authenticatedToken;
        }

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

    /**
     * This function is specific to Wsse authentication and is only used to help this example
     *
     * For more information specific to the logic here, see
     * https://github.com/symfony/symfony-docs/pull/3134#issuecomment-27699129
     */
    protected function validateDigest($digest, $nonce, $created, $secret)
    {
        // Check created time is not in the future
        if (strtotime($created) > time()) {
            return false;
        }

        // Expire timestamp after 5 minutes
        if (time() - strtotime($created) > 300) {
            return false;
        }

        // Validate that the nonce is *not* used in the last 5 minutes
        // if it has, this could be a replay attack
        if (file_exists($this->cacheDir.'/'.$nonce) && file_get_contents($this->cacheDir.'/'.$nonce) + 300 > time()) {
            throw new NonceExpiredException('Previously used nonce detected');
        }
        // If cache directory does not exist we create it
        if (!is_dir($this->cacheDir)) {
            mkdir($this->cacheDir, 0777, true);
        }
        file_put_contents($this->cacheDir.'/'.$nonce, time());

        // Validate Secret
        $expected = base64_encode(sha1(base64_decode($nonce).$created.$secret, true));

        return StringUtils::equals($expected, $digest);
    }

}
Which version of Symfony are you using? Authentication/TokenInterface makes no sense at all. Can't see where phpstorm is finding it.Cerad
if youre on 2.8 (which ideally you should be), then guard is great and makes life much easier.DevDonkey
Thx @DevDonkey exactly what I was looking for!Steffan