1.增加谷歌验证器

This commit is contained in:
2026-06-24 16:25:31 +08:00
parent 5187330c38
commit 3e88a5dd91
21 changed files with 822 additions and 46 deletions

View File

@@ -24,6 +24,8 @@ class Auth extends \ba\Auth
public const LOGIN_RESPONSE_CODE = 303;
public const NEED_LOGIN = 'need login';
public const LOGGED_IN = 'logged in';
public const NEED_TOTP = 'need_totp';
public const NEED_BIND_TOTP = 'need_bind_totp';
public const TOKEN_TYPE = 'admin';
protected bool $loginEd = false;
@@ -90,6 +92,21 @@ class Auth extends \ba\Auth
}
public function login(string $username, string $password, bool $keep = false): bool
{
if (!$this->verifyCredentials($username, $password)) {
return false;
}
if (config('buildadmin.admin_sso')) {
Token::clear(self::TOKEN_TYPE, $this->model->id);
Token::clear(self::TOKEN_TYPE . '-refresh', $this->model->id);
}
if ($keep) {
$this->setRefreshToken($this->refreshTokenKeepTime);
}
return $this->loginSuccessful();
}
public function verifyCredentials(string $username, string $password): bool
{
$this->model = Admin::where('username', $username)->find();
if (!$this->model) {
@@ -124,18 +141,44 @@ class Auth extends \ba\Auth
return false;
}
return true;
}
public function hasTotpBound(): bool
{
if (!$this->model) {
return false;
}
return \app\common\library\AdminTotp::isBound($this->model->getData('totp_secret'));
}
public function loadAdminById(int $adminId): bool
{
$this->model = Admin::where('id', $adminId)->find();
if (!$this->model) {
$this->setError('Account not exist');
return false;
}
if ($this->model->status === 'disable') {
$this->setError('Account disabled');
return false;
}
return true;
}
public function finalizeLogin(bool $keep = false): bool
{
if (!$this->model) {
return false;
}
if (config('buildadmin.admin_sso')) {
Token::clear(self::TOKEN_TYPE, $this->model->id);
Token::clear(self::TOKEN_TYPE . '-refresh', $this->model->id);
}
if ($keep) {
$this->setRefreshToken($this->refreshTokenKeepTime);
}
if (!$this->loginSuccessful()) {
return false;
}
return true;
return $this->loginSuccessful();
}
public function setRefreshToken(int $keepTime = 0): void