Skip to content

Commit

Permalink
fix #3882 [要望]baserCMS4系で使用していたときのパスワードが引き継げない問題を解決 (#3883)
Browse files Browse the repository at this point in the history
Co-authored-by: kato <[email protected]>
Co-authored-by: ryuring <[email protected]>
  • Loading branch information
3 people authored Oct 24, 2024
1 parent 117c888 commit 8ec87ea
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 1 deletion.
3 changes: 3 additions & 0 deletions config/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ export USE_CORE_ADMIN_API="false"
export SHOW_TEST_METHOD="false"
## プロキシサーバーを利用するかどうか(SSL判定に利用)
export TRUST_PROXY="false"
## 4系のパスワード暗号化を使用する場合は下記のコメントアウトを外し4系で利用していたセキュリティーソルトを設定する
# export HASH_TYPE="sha1"
# export SECURITY_SALT=""

# Uncomment these to define cache configuration via environment variables.
#export CACHE_DURATION="+2 minutes"
Expand Down
5 changes: 5 additions & 0 deletions plugins/baser-core/config/setting.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,11 @@
*/
'twoFactorAuthenticationCodeAllowTime' => 10,

/**
* 4系のパスワードでログインする際に、新しいハッシュアルゴリズムでハッシュ化するかどうか
*/
'needsPasswordRehash' => true,

/**
* エディタ
*/
Expand Down
21 changes: 20 additions & 1 deletion plugins/baser-core/src/BaserCorePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
use Cake\Database\Exception\MissingConnectionException;
use Cake\Event\EventManager;
use Cake\Http\Middleware\CsrfProtectionMiddleware;
use Cake\Http\Middleware\HttpsEnforcerMiddleware;
use Cake\Http\MiddlewareQueue;
use Cake\Http\ServerRequestFactory;
use Cake\I18n\I18n;
Expand Down Expand Up @@ -436,6 +435,25 @@ public function setupSessionAuth(AuthenticationService $service, array $authSett
],
'loginUrl' => Router::url($authSetting['loginAction']),
]);

$passwordHasher = null;
if(!empty($authSetting['passwordHasher'])) {
$passwordHasher = $authSetting['passwordHasher'];
} elseif(env('HASH_TYPE') === 'sha1') {
// .env に HASH_TYPE で sha1が設定されている場合 4系のハッシュアルゴリズムを使用
$passwordHasher = [
'className' => 'Authentication.Fallback',
'hashers' => [
'Authentication.Default',
[
'className' => 'Authentication.Legacy',
'hashType' => 'sha1',
'salt' => true
]
]
];
}

$service->loadIdentifier('Authentication.Password', [
'fields' => [
'username' => $authSetting['username'],
Expand All @@ -446,6 +464,7 @@ public function setupSessionAuth(AuthenticationService $service, array $authSett
'userModel' => $authSetting['userModel'],
'finder' => $authSetting['finder']?? 'available'
],
'passwordHasher' => $passwordHasher
]);
return $service;
}
Expand Down
19 changes: 19 additions & 0 deletions plugins/baser-core/src/Controller/Admin/UsersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,25 @@ public function login(UsersAdminServiceInterface $service)
$this->response = $service->setCookieAutoLoginKey($this->response, $user->id);
}
$this->BcMessage->setInfo(__d('baser_core', 'ようこそ、{0}さん。', $user->getDisplayName()));

// baserCMS4系のパスワードでログインした場合、新しいハッシュアルゴリズムでパスワードをハッシュし直す
if (Configure::read('BcApp.needsPasswordRehash') &&
$this->request->getAttribute('authentication')
->identifiers()
->get('Password')
->needsPasswordRehash()
) {
try {
$password = $this->getRequest()->getData('password');
$service->update($user, [
'password_1' => $password,
'password_2' => $password
]);
} catch (PersistenceFailedException) {
// バリデーションでパスワードの更新に失敗した場合はスルーする
}
}

return $this->redirect($target);
} else {
$this->BcMessage->setError(__d('baser_core', 'Eメール、または、パスワードが間違っています。'));
Expand Down

0 comments on commit 8ec87ea

Please sign in to comment.