实现自定义双因素提供程序

编辑本页

实现自定义双因素提供程序

开始

一个很好的起点是谷歌Authenticator、TOTP和电子邮件身份验证实现,它们在代码库中可用。请看以下文件:

您将获得如何实现自定义双因素方法的基本思想。

的TwoFactorProviderInterface

您必须创建一个服务,该服务实现Scheb\ TwoFactorBundle\安全\ TwoFactor\提供者\ TwoFactorProviderInterface接口。它需要这些方法:

beginAuthentication

1
公共函数beginAuthentication(AuthenticationContextInterface上下文保龄球

该方法在成功登录后调用。它接收一个AuthenticationContextInterface对象作为实参(参见classScheb\ TwoFactorBundle\安全\ TwoFactor\ AuthenticationContext),其中包含请求对象、认证令牌、用户实体和其他信息。

该方法必须决定是否应该向该提供者请求用户进行双因素身份验证。在这种情况下返回真正的,否则

1
公共函数prepareAuthentication(对象用户无效

你应该用这种方法为你的双因素提供者做准备工作。如电子邮件提供者正在生成代码并将其发送给用户。

validateAuthenticationCode

1
公共函数validateAuthenticationCode(对象用户、字符串authenticationCode保龄球

此方法负责验证用户输入的身份验证代码。返回真正的如果代码是正确的当它是错误的。

getFormRenderer

1
公共函数getFormRenderer()TwoFactorFormRendererInterface

此方法必须提供用于呈现身份验证表单的服务。这样的服务必须实现Scheb\ TwoFactorBundle\安全\ TwoFactor\提供者\ TwoFactorFormRendererInterface接口:

1
公共函数renderForm(请求请求数组,templateVars响应

如何渲染形式完全取决于你自己。唯一重要的是返回a响应,也可以是aRedirectResponse重定向到外部服务。使用Twig呈现表单的默认实现为Scheb\ TwoFactorBundle\安全\ TwoFactor\提供者\ DefaultTwoFactorFormRenderer

注册提供者

现在必须将双因素提供者类注册为服务。

一个名为scheb_two_factor.provider将使您的提供程序对包可用。tag属性别名必须设置,并且必须是身份验证提供程序的应用程序范围的唯一标识符。

请注意

别名谷歌你觉得而且电子邮件由包中包含的身份验证方法保留。

  • YAML
  • XML
1 2 3 4 5 6 7
#配置/ services.yaml服务:#……acme.custom_two_factor_provider:类:Acme \ Demo \ MyTwoFactorProvider标签:-名称:scheb_two_factor.provider,别名:acme_two_factor_provider
此工作,包括代码示例,是根据创作共用BY-SA 3.0许可证。