開発環境
Laravel 5.8.26
困っている事
Laravelのデフォルトのハッシュ化のアルゴリズムはBcryptですが、今回はパスワードがSHA256で保存されている既存のテーブルを使用したい為、こちらの記事を参考にSHA256のHasherとProviderを追加したのですが、api認証を行うと500エラーが返ってきており認証をすることが出来ません。
追加したHasherとProviderは以下の通りです。何処を修正すれば認証ができるのか、ご助言を頂けないでしょうか。
laravel
1<?php 2 3namespace App\Libraries; 4 5use App\Customer; 6use RuntimeException; 7use Illuminate\Contracts\Hashing\Hasher as HasherContract; 8use Illuminate\Hashing\AbstractHasher; 9use Illuminate\Http\Request; 10 11class SHA256Hasher implements HasherContract 12{ 13 /** 14 * Hash the given value. 15 * 16 * @param string $value 17 * @param array $options 18 * @return string 19 * 20 * @throws \RuntimeException 21 */ 22 public function make($value, array $options = array(), Request $request) 23 { 24 $request->validate([ 25 'email' => 'required|string|email' 26 ]); 27 $email = request(['email']); 28 $salt = Customer::where('email', $email)->select('salt')->get(); 29 return hash_hmac('SHA256', $value.':'.'秘密鍵', $salt); 30 } 31 32 /** 33 * Check the given plain value against a hash. 34 * 35 * @param string $value 36 * @param string $hashedValue 37 * @param array $options 38 * @return bool 39 * 40 * @throws \RuntimeException 41 */ 42 public function check($value, $hashedValue, array $options = array()) 43 { 44 return $this->make($value) === $hashedValue; 45 } 46 47 /** 48 * Check if the given hash has been hashed using the given options. 49 * 50 * @param string $hashedValue 51 * @param array $options 52 * @return bool 53 */ 54 public function needsRehash($hashedValue, array $options = array()) 55 { 56 return false; 57 } 58}
laravel
1<?php 2 3namespace App\Providers; 4 5use Illuminate\Support\ServiceProvider; 6use App\Libraries\SHA256Hasher; 7 8class SHA256HashServiceProvider extends ServiceProvider 9{ 10 /** 11 * Indicates if loading of the provider is deferred. 12 * 13 * @var bool 14 */ 15 protected $defer = true; 16 17 /** 18 * Register the service provider. 19 * 20 * @return void 21 */ 22 public function register() 23 { 24 $this->app->singleton('hash', function() { return new SHA256Hasher; }); 25 } 26 27 /** 28 * Get the services provided by the provider. 29 * 30 * @return array 31 */ 32 public function provides() 33 { 34 return array('hash'); 35 } 36}
追記
Laravelのlogに出力されていたエラーはこちらになります。
log
1local.ERROR: Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\Customer given, called in /var/www/html/siro/vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php on line 377 {"exception":"[object] (Symfony\Component\Debug\Exception\FatalThrowableError(code: 0): Argument 1 passed to Illuminate\Auth\EloquentUserProvider::validateCredentials() must be an instance of Illuminate\Contracts\Auth\Authenticatable, instance of App\Customer given, called in /var/www/html/siro/vendor/laravel/framework/src/Illuminate/Auth/SessionGuard.php on line 377 at /var/www/html/siro/vendor/laravel/framework/src/Illuminate/Auth/EloquentUserProvider.php:141)
追記2
500エラーは新規モデルがうまく適用されていなかったのが原因だったので、中の構文をapp/User.phpに移動させたところ消えたのですが、今度は401エラーが表示され認証することが出来ません。これはどう対処すればいいでしょうか。