Skip to content

Commit

Permalink
Merge pull request #34 from citrus-framework/fix_token_regen
Browse files Browse the repository at this point in the history
認証処理の修正
  • Loading branch information
take64 authored Jul 16, 2024
2 parents 6cc6d59 + 5b14aa8 commit 424da43
Showing 1 changed file with 34 additions and 2 deletions.
36 changes: 34 additions & 2 deletions src/Authentication/Database.php
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,40 @@ public function isAuthenticated(AuthItem|null $item = null): bool
is_null($item),
'認証情報がない'
);

// 対象テーブル
$table_name = Authentication::$AUTHORIZE_TABLE_NAME;

// トークンが無く、ID・パスワードがある場合はデータ取得
if (is_null($item->token) && !is_null($item->user_id) && !is_null($item->password))
{
// 対象ユーザーがいるか?
$condition = new AuthItem();
$condition->user_id = $item->user_id;
/** @var AuthItem $result */
$result = (new Builder($this->connection))->select($table_name, $condition)->execute(AuthItem::class)->one();

// いなければ認証失敗
AuthenticationException::exceptionIf(
is_null($result),
sprintf(
'存在しないユーザーのログイン試行です(%s : %s)',
$item->user_id,
$item->password,
),
);
// パスワード照合
AuthenticationException::exceptionElse(
password_verify($item->password, $result->password),
sprintf(
'パスワード照合に失敗しました(%s : %s)',
$item->user_id,
$item->password,
),
);
$item = $result;
}

// ユーザーIDとトークン、認証期間があるか
AuthenticationException::exceptionIf(
is_null($item->user_id) or is_null($item->token) or is_null($item->expired_at),
Expand All @@ -154,8 +188,6 @@ public function isAuthenticated(AuthItem|null $item = null): bool
),
);

// 対象テーブル
$table_name = Authentication::$AUTHORIZE_TABLE_NAME;
$condition = new AuthItem();
$condition->user_id = $item->user_id;
$result = (new Builder($this->connection))->select($table_name, $condition)->execute(AuthItem::class)->one();
Expand Down

0 comments on commit 424da43

Please sign in to comment.