简体   繁体   中英

Laravel Passport custom hasher to create token

I have used a SHA1 hash for a password like this: https://arjunphp.com/laravel-5-sha1-encryption-instead-of-bcrypt/

Now I am using passport API to create a token, but it is not allowing me to create a token as the hasher is changed now.

Symfony\\Component\\Debug\\Exception\\FatalThrowableError: Argument 1 passed to Laravel\\Passport\\Bridge\\UserRepository::__construct() must be an instance of Illuminate\\Hashing\\HashManager, instance of App\\Libraries\\ShaHash\\SHAHasher given in file C:\\xampp1\\htdocs\\coursekartv2\\vendor\\laravel\\passport\\src\\Bridge\\UserRepository.php on line 26

How can I override UserRepository to use SHAHasher instead of HashManager ? Or any other help to overcome this issue.

we came across the same problem as the one described here, I am working on a Laravel API that needs to handle Passport and also has it own custom Hasher (SHA1). like the one in here

Our fix for this was not only make our class ShaHasher extends HashManager like so:

class ShaHasher extends HashManager implements Hasher { ..... }

you also need to make sure that your provider for this Hasher get and instance of the $app container in the constructor like so:

<?php


namespace App\Providers;


use Illuminate\Hashing\HashServiceProvider;
use Psytech\ShaHasher;

class ShaHashServiceProvider extends HashServiceProvider {

    public function register()
    {
       $this->app->singleton('hash', function () {
           return new ShaHasher($this->app);
       });
    }

}

Hope this helps someone!

Found the solution:

I was using my custom hasher (SHAHasher) instead of Passport hashManager, now extend HashManager of passport instead of complete new hasher (SHAHasher). So now even i am sending SHAHasher (custom) it is accepting as my SHAHasher extends hashManager.

Extend hashManager inside custom hasher library.

You can also create a custom hasher and extend the existing HashManager with your custom hashing using the extend method on the HashManager

In the AuthServiceProvider:

$this->app->get('hash')->extend('custom_hasher', function(){
    return new CustomHasher();
});

In the hash.php config file you can then change the hash driver:

'driver' => 'custom_hasher',

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM