How to hook into user authentication?
I'm assuming this is Drupal 8 so my answer is for Drupal 8.

You need to do two things:


Create a service provider in your custom module that extends ServiceProviderBase.  The name of this class must be your module name, in camel case, plus "ServiceProvider", i.e. MyUserAuthenticationModuleServiceProvider.  Then, implement the alter function which sets the class for the user.auth service, like this:



    public function alter(ContainerBuilder $container)
    {
        $definition = $container->getDefinition('user.auth');
        $definition- >setClass('Drupal\my_module\AuthenticationClass');
    }



The class you specify in the call to setClass above has to extend \Drupal\user\UserAuth and implement the following function, which returns true/false based on successful authentication


public function authenticate($username, $password)