实现自定义双因素提供程序
编辑本页实现自定义双因素提供程序
开始
一个很好的起点是谷歌Authenticator、TOTP和电子邮件身份验证实现,它们在代码库中可用。请看以下文件:
- src / google-authenticator /安全/ /谷歌/ GoogleAuthenticatorTwoFactorProvider.php TwoFactor /提供者
- src /你觉得/安全/ /你觉得/ TotpAuthenticatorTwoFactorProvider.php TwoFactor /提供者
- src /电子邮件/安全/ TwoFactor /供应商/电子邮件/ EmailTwoFactorProvider.php
您将获得如何实现自定义双因素方法的基本思想。
的TwoFactorProviderInterface
您必须创建一个服务,该服务实现Scheb
接口。它需要这些方法:
beginAuthentication
1
公共函数beginAuthentication(AuthenticationContextInterface$上下文):保龄球;
该方法在成功登录后调用。它接收一个AuthenticationContextInterface
对象作为实参(参见classScheb
),其中包含请求对象、认证令牌、用户实体和其他信息。
该方法必须决定是否应该向该提供者请求用户进行双因素身份验证。在这种情况下返回真正的
,否则假
.
1
公共函数prepareAuthentication(对象$用户):无效;
你应该用这种方法为你的双因素提供者做准备工作。如电子邮件提供者正在生成代码并将其发送给用户。
validateAuthenticationCode
1
公共函数validateAuthenticationCode(对象$用户、字符串$authenticationCode):保龄球;
此方法负责验证用户输入的身份验证代码。返回真正的
如果代码是正确的假
当它是错误的。
getFormRenderer
1
公共函数getFormRenderer():TwoFactorFormRendererInterface;
此方法必须提供用于呈现身份验证表单的服务。这样的服务必须实现Scheb
接口:
1
公共函数renderForm(请求$请求数组,$templateVars):响应;
如何渲染形式完全取决于你自己。唯一重要的是返回a响应
,也可以是aRedirectResponse
重定向到外部服务。使用Twig呈现表单的默认实现为Scheb
.
注册提供者
现在必须将双因素提供者类注册为服务。
一个名为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}