受信任的设备
它的作用
您可以让用户可以选择将设备标记为“受信任”,这意味着在该设备上通过一次两因素验证后,将跳过两因素验证过程。
您必须在配置中启用此功能
1 2 3 4 5 6 7 8 9 10 11
# config/packages/scheb_2fa.yaml
scheb_two_factor:
trusted_device:
enabled: false # If the trusted device feature should be enabled
lifetime: 5184000 # Lifetime of the trusted device token
extend_lifetime: false # Automatically extend lifetime of the trusted cookie on re-login
cookie_name: trusted_device # Name of the trusted device cookie
cookie_secure: false # Set the 'Secure' (HTTPS Only) flag on the trusted device cookie
cookie_same_site: "lax" # The same-site option of the cookie, can be "lax" or "strict"
cookie_domain: ".example.com" # Domain to use when setting the cookie, fallback to the request domain if not set
cookie_path: "/" # Path to use when setting the cookie
受信任设备 Cookie 是版本化的,这使您(或用户)可以一次性使所有受信任设备 Cookie 失效,例如在发生安全漏洞时。要使用此功能,您必须在用户实体中实现 Scheb\TwoFactorBundle\Model\TrustedDeviceInterface
。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
<?php
namespace Acme\Demo\Entity;
use Doctrine\ORM\Mapping as ORM;
use Scheb\TwoFactorBundle\Model\TrustedDeviceInterface;
class User implements TrustedDeviceInterface
{
/**
* @ORM\Column(type="integer")
*/
private int $trustedVersion;
// [...]
public function getTrustedTokenVersion(): int
{
return $this->trustedVersion;
}
}
如果未实现,则捆绑包默认使用版本 0
。
将设备标记为“受信任”
要将设备标记为“受信任”,在两因素验证过程的最后一步,您必须传递一个参数 _trusted
,其值为类似 true
的值。参数名称可以在防火墙配置中更改
1 2 3 4 5 6
security:
firewalls:
your_firewall_name:
# ...
two_factor:
trusted_parameter_name: _trusted # Name of the parameter for the trusted device option
请查看 默认身份验证表单模板,了解它是如何实现的。
清除受信任的 Cookie
要清除特定用户的所有受信任 Cookie(例如,在发生安全问题时),请增加用户实体上 getTrustedTokenVersion
返回的版本。
如果您需要在设备上为特定用户和防火墙组合以编程方式清除受信任 Cookie,则可以使用 scheb_two_factor.trusted_token_storage
服务上的 clearTrustedToken
方法。
自定义受信任设备管理器
如果您不喜欢这种实现方式,您也可以拥有自己的受信任设备管理器。创建一个实现 Scheb
的服务,并在配置中注册它
1 2 3 4
# config/packages/scheb_2fa.yaml
scheb_two_factor:
trusted_device:
manager: acme.custom_trusted_device_manager # Use a custom trusted device manager
受信任设备的条件
有一种方法可以检查设备/用户是否满足某些条件,然后再将设备标记为“受信任”。例如,您可能希望仅在内部网络中允许受信任设备。在这种情况下,请实现您自己的受信任设备管理器实例(如上所述),并使用您需要的决策逻辑来实现 canSetTrustedDevice
方法。
1 2 3 4
public function canSetTrustedDevice($user, Request $request, string $firewallName): bool
{
return true; // Always allow trusted device feature
}