LoginSignup
0
0

More than 1 year has passed since last update.

Laravelの認証機能に使用するハッシュ変更 bcrypt→PHPass(Wordpress)

Last updated at Posted at 2021-06-15

概要

Laravelではlaravel/ui vue --auth等で作った認証ページに使用される会員登録とログイン時のパスワードのハッシュ化には、
bcryptがデフォルトで設定されているので今回はWordPressに使用されているPHPassに変更する。
注意しなければならないのが、今回はbcryptとPHPassを共存させるわけではなく全てPHPassにハッシュ方式を統一させます。

背景として、既存システムのWordPressのwp_usersを新規システムのLaravel側のusersテーブルに移行する必要がありました。
なので、bcryptは完全にやめてWordpressのハッシュ方式に変更しました。

bcrypt→PHPass(WordPress)にハッシュ変更

①必要パッケージインストール

WordPressに使用されているパスワードのチェック、平文のハッシュ化を行うためのパッケージをインストールします。

composer.json
"require": {
  "mikemclin/laravel-wp-password": "~2.0.1"
}
composer update

参考
https://github.com/mikemclin/laravel-wp-password

②Service作成

makeとcheckは会員登録とログインで使用される関数です。
ここをインストールしたパッケージを使用してWordPress仕様に変更します。

App\Services\PHPass\PHPassHashService.php

<?php

namespace App\Services\PHPass;

use MikeMcLin\WpPassword\Facades\WpPassword;

class PHPassHashService implements \Illuminate\Contracts\Hashing\Hasher
{

    /**
     * Hash the given value.
     *
     * @param  string  $value
     * @return array   $options
     * @return string
     */
    public function make($value, array $options = array())
    {
        return WpPassword::make($value);
    }

    /**
     * Check the given plain value against a hash.
     *
     * @param  string  $value
     * @param  string  $hashedValue
     * @param  array   $options
     * @return bool
     */
    public function check($value, $hashedValue, array $options = array())
    {
        return WpPassword::check($value, $hashedValue);
    }

    /**
     * Check if the given hash has been hashed using the given options.
     *
     * @param  string  $hashedValue
     * @param  array   $options
     * @return bool
     */
    public function needsRehash($hashedValue, array $options = array())
    {
        return false;
    }

    /**
     * Get information about the given hashed value.
     *
     * @param string $hashedValue
     * @return array
     */
    public function info($hashedValue)
    {
        // TODO: Implement info() method.
    }
}


③Provider作成

サービスを使用するためのプロバイダーを作成

App\Providers\PHPassHashServiceProvider.php
<?php

namespace App\Providers;

use App\Services\PHPass\PHPassHashService;
use Illuminate\Support\ServiceProvider;

class PHPassHashServiceProvider extends ServiceProvider
{
  /**
   * Register services.
   *
   * @return void
   */
  public function register()
  {
    $this->app->bind('hash', PHPassHashService::class);
  }

  /**
   * Bootstrap services.
   *
   * @return void
   */
  public function boot()
  {
    //
  }

  public function provides()
  {
    return array('hash');
  }
}

④プロバイダー変更

config\app.php
'providers' => [
    //Illuminate\Hashing\HashServiceProvider::class, ←コメントアウト
    App\Providers\PHPassHashServiceProvider::class, 追加
]

これでlaravel/ui等で作った認証でbcryptではなくWordPressのハッシュ方式に変更が完了となります。
最後に再読み込みとキャッシュクリアして終了です。

composer dump-autoload
php artisan config:cache

参考
https://solomaker.club/how-to-change-default-laravel-hash/

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0