sdespont
sdespont

Reputation: 14025

Silex2 : overwrite `checkAuthentication` method of the `DaoAuthenticationProvider` class

Using Silex 2, I am stuck for hours to find a way to overwrite checkAuthentication method of the DaoAuthenticationProvider class?

About the context: I defined an authentication listener and provider using a custom UserToken.

$app['security.authentication_listener.factory.sds'] = $app->protect(function ($name, $options) use ($app) {
    // define the authentication provider object
    $app['security.authentication_provider.'.$name.'.sds'] = function () use ($app) {
        return new CustomAuthenticationProvider($app['user.provider'], $app['security.encoder_factory']);
    };

    // define the authentication listener object
    $app['security.authentication_listener.'.$name.'.sds'] = function () use ($app) {
        return new CustomAuthenticationListener($app['security.token_storage'], $app['security.authentication_manager']);
    };

    return array(
        // the authentication provider id
        'security.authentication_provider.'.$name.'.sds',
        // the authentication listener id
        'security.authentication_listener.'.$name.'.sds',
        // the entry point id
        null,
        // the position of the listener in the stack
        'pre_auth'
    );
});

But I need to customize the checkAuthentication of DaoAuthenticationProvider called automatically when the custom authentication provider reture a successfully authenticated token.

protected function checkAuthentication(UserInterface $user, UsernamePasswordToken $token)
{
    $currentUser = $token->getUser();
    if ($currentUser instanceof UserInterface) {
        if ($currentUser->getPassword() !== $user->getPassword()) {
            throw new BadCredentialsException('The credentials were changed from another session.');
        }
    } else {
        if ('' === ($presentedPassword = $token->getCredentials())) {
            throw new BadCredentialsException('The presented password cannot be empty.');
        }

        if (!$this->encoderFactory->getEncoder($user)->isPasswordValid($user->getPassword(), $presentedPassword, $user->getSalt())) {
            throw new BadCredentialsException('The presented password is invalid.');
        }
    }
}

Solution

Define like this in app.php :

$app['security.authentication_provider.dao._proto'] = $app->protect(function ($name) use($app) {
    return new \Trilogis\Classes\CustomUserAuthenticationProvider(
        $app['security.user_provider.' . $name],
        $app['security.user_checker'],
        $name,
        $app['security.encoder_factory']
    );
});

Upvotes: 1

Views: 263

Answers (1)

Max P.
Max P.

Reputation: 5679

You can make custom authentication provider, you can extend it from DaoAuthenticationProvider. And redefine authentication provider definition in application:

...

$app['security.authentication_provider.sds.dao'] = function() {
    return new MyAuthenticationProvider(
        $app['security.user_provider.sds'],
        $app['security.user_checker'],
        'sds',
        $app['security.encoder_factory'],
        $app['security.hide_user_not_found']
    );
};

$app['security.authentication_listener.sds.form'] = function() {
    return new CustomAuthenticationListener($app['security.token_storage'], $app['security.authentication_manager']);
};

...

$app->run();

Upvotes: 2

Related Questions