Google 身份验证器
Google 身份验证器 是 TOTP 算法 的流行实现,用于生成身份验证代码。与 TOTP 双因素提供程序 相比,此实现具有固定的配置,这对于与 Google 身份验证器应用兼容是必要的
- 它生成 6 位代码
- 代码每 30 秒更改一次
- 它使用 sha1 哈希算法
如果您需要不同的设置,请使用 TOTP 双因素提供程序。 请注意,自定义 TOTP 配置可能与 Google 身份验证器应用不兼容。
身份验证如何工作
用户必须先将其帐户链接到 Google 身份验证器应用。 这通过生成共享密钥代码来完成,该代码存储在用户实体中。 用户可以通过手动键入代码或扫描自动传输信息的二维码将代码添加到 Google 身份验证器应用。
成功身份验证后,bundle 会检查用户实体中是否存储了密钥。 如果是这种情况,它将请求身份验证代码。 用户必须输入当前在 Google 身份验证器应用中显示的代码才能获得访问权限。
有关更多信息,请参阅 Google 身份验证器网站。
基本配置
要启用此身份验证方法,请将其添加到您的配置中
1 2 3 4
# config/packages/scheb_2fa.yaml
scheb_two_factor:
google:
enabled: true
您的用户实体必须实现 Scheb
。 要为用户激活 Google 身份验证器,请生成密钥并将其与用户实体一起持久化。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
<?php
namespace Acme\Demo\Entity;
use Doctrine\ORM\Mapping as ORM;
use Scheb\TwoFactorBundle\Model\Google\TwoFactorInterface;
use Symfony\Component\Security\Core\User\UserInterface;
class User implements UserInterface, TwoFactorInterface
{
/**
* @ORM\Column(type="string", nullable=true)
*/
private ?string $googleAuthenticatorSecret;
// [...]
public function isGoogleAuthenticatorEnabled(): bool
{
return null !== $this->googleAuthenticatorSecret;
}
public function getGoogleAuthenticatorUsername(): string
{
return $this->username;
}
public function getGoogleAuthenticatorSecret(): ?string
{
return $this->googleAuthenticatorSecret;
}
public function setGoogleAuthenticatorSecret(?string $googleAuthenticatorSecret): void
{
$this->googleAuthenticatorSecret = $googleAuthenticatorSecret;
}
}
配置参考
1 2 3 4 5 6 7 8 9
# config/packages/scheb_2fa.yaml
scheb_two_factor:
google:
enabled: true # If Google Authenticator should be enabled, default false
server_name: Server Name # Server name used in QR code
issuer: Issuer Name # Issuer name used in QR code
digits: 6 # Number of digits in authentication code
leeway: 0 # Acceptable time drift in seconds, must be less or equal than 30 seconds
template: security/2fa_form.html.twig # Template used to render the authentication form
自定义身份验证表单模板
bundle 使用 Resources/views/Authentication/form.html.twig
来渲染身份验证表单。 如果您想使用不同的模板,您可以简单地在配置中注册它
1 2 3 4
# config/packages/scheb_2fa.yaml
scheb_two_factor:
google:
template: security/2fa_form.html.twig
自定义表单渲染
在某些情况下,仅更改模板是不够的。 例如,您在多个防火墙上使用双因素身份验证,并且您需要 为每个防火墙以不同方式渲染表单。 在这种情况下,您可以实现表单渲染器以完全自定义渲染逻辑。
创建一个实现 Scheb
的类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<?php
namespace Acme\Demo\FormRenderer;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\TwoFactorFormRendererInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class MyFormRenderer implements TwoFactorFormRendererInterface
{
// [...]
public function renderForm(Request $request, array $templateVars): Response
{
// Customize form rendering
}
}
然后将其注册为服务并更新您的配置
1 2 3 4
# config/packages/scheb_2fa.yaml
scheb_two_factor:
google:
form_renderer: acme.custom_form_renderer_service
生成密钥
服务 scheb_two_factor.security.google_authenticator
提供了一种为 Google 身份验证器生成新密钥的方法。 Scheb
的自动装配也是可能的。
1
$secret = $container->get("scheb_two_factor.security.google_authenticator")->generateSecret();