r/u2f-two-factor-bundle
Composer 安装命令:
composer require r/u2f-two-factor-bundle
包简介
Use U2F-Keys as 2FA for Symfony2, using scheb/two-factor-bundle
README 文档
README
This Symfony2 bundle provides u2f authentication for your website using scheb/two-factor-bundle.
Installation
Step 1: Download using Composer
php composer.phar require r/u2f-two-factor-bundle
Step 2: Enable the bundles (skip when using Symfony Flex)
Add this to you app/AppKernel.php:
<?php // ... public function registerBundles() { $bundles = array( // ... new Scheb\TwoFactorBundle\SchebTwoFactorBundle(), new R\U2FTwoFactorBundle\RU2FTwoFactorBundle(), // ... ); // ... } // ...
Step 3: Configure
These options are available but not required:
r_u2f_two_factor: formTemplate: RU2FTwoFactorBundle:Authentication:form.html.twig registerTemplate: RU2FTwoFactorBundle:Registration:register.html.twig authCodeParameter: _auth_code
For the Authentication to work you User has to implement R\U2FTwoFactorBundle\Model\U2F\TwoFactorInterface
<?php // ... use Doctrine\Common\Collections\Collection; use R\U2FTwoFactorBundle\Model\U2F\TwoFactorInterface as U2FTwoFactorInterface; use R\U2FTwoFactorBundle\Model\U2F\TwoFactorKeyInterface; use Club\BaseBundle\Entity\U2FKey; // ... class User implements U2FTwoFactorInterface { // ... /** * @ORM\OneToMany(targetEntity=U2FKey::class, mappedBy="user") * @var Collection<TwoFactorKeyInterface> **/ protected $u2fKeys; public function isU2FAuthEnabled(): bool { // If the User has Keys associated, use U2F // You may use a different logic here return count($this->u2fKeys) > 0; } /** @return Collection<TwoFactorKeyInterface> **/ public function getU2FKeys(): Collection { return $this->u2fKeys; } public function addU2FKey(TwoFactorKeyInterface $key): void { $this->u2fKeys->add($key); } public function removeU2FKey(TwoFactorKeyInterface $key): void { $this->u2fKeys->remove($key); } public function __construct() { // ... $this->u2fKeys = new ArrayCollection(); // ... } }
For the Registration you also need an entity that implements
R\U2FTwoFactorBundle\Model\U2F\TwoFactorKeyInterface.
Here is an example using doctrine.
<?php // ... use R\U2FTwoFactorBundle\Model\U2F\TwoFactorKeyInterface; use u2flib_server\Registration; /** * @ORM\Entity * @ORM\Table(name="u2f_keys", * uniqueConstraints={@ORM\UniqueConstraint(name="user_unique",columns={"user_id", * "keyHandle"})}) */ class U2FKey implements TwoFactorKeyInterface { /** * @ORM\Id * @ORM\Column(type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ protected $id; /** * @ORM\Column(type="string") * @var string **/ protected $keyHandle; /** * @ORM\Column(type="string") * @var string **/ protected $publicKey; /** * @ORM\Column(type="text") * @var string **/ protected $certificate; /** * @ORM\Column(type="string") * @var int **/ protected $counter; /** * @ORM\ManyToOne(targetEntity="AcmeBundle\Entity\User", inversedBy="u2fKeys") * @var User **/ protected $user; /** * @ORM\Column(type="string") * @var string **/ protected $name; // ... public function fromRegistrationData(Registration $data): void { $this->keyHandle = $data->keyHandle; $this->publicKey = $data->publicKey; $this->certificate = $data->certificate; $this->counter = $data->counter; } /** @inheritDoc */ public function getKeyHandle() { return $this->keyHandle; } /** @inheritDoc */ public function setKeyHandle($keyHandle) { $this->keyHandle = $keyHandle; } /** @inheritDoc */ public function getPublicKey() { return $this->publicKey; } /** @inheritDoc */ public function setPublicKey($publicKey) { $this->publicKey = $publicKey; } /** @inheritDoc */ public function getCertificate() { return $this->certificate; } /** @inheritDoc */ public function setCertificate($certificate) { $this->certificate = $certificate; } /** @inheritDoc */ public function getCounter() { return $this->counter; } /** @inheritDoc */ public function setCounter($counter) { $this->counter = $counter; } /** @inheritDoc */ public function getName() { return $this->name; } /** @inheritDoc */ public function setName($name) { $this->name = $name; } }
Then you need to create an event subscriber to get and store the data of the registered key.
<?php use AcmeBundle\Entity\U2FKey; use R\U2FTwoFactorBundle\Event\RegisterEvent; use Symfony\Component\HttpFoundation\RedirectResponse; use Symfony\Component\EventDispatcher\EventSubscriberInterface; use Symfony\Component\Routing\Generator\UrlGeneratorInterface; class U2FRegistrationSubscriber implements EventSubscriberInterface { /** @var UrlGeneratorInterface */ private $router; public function __construct(UrlGeneratorInterface $router) { $this->router = $router; } // .. /** @return string[] **/ public static function getSubscribedEvents(): array { return array( 'r_u2f_two_factor.register' => 'onRegister', ); } public function onRegister(RegisterEvent $event): void { $user = $event->getUser($event); $registration = $event->getRegistration(); $newKey = new U2FKey(); $newKey->fromRegistrationData($registration); $newKey->setUser($user); $newKey->setName($event->getKeyName()); // persist the new key // generate new response, here we redirect the user to the fos user // profile $response = new RedirectResponse($this->router->generate('fos_user_profile_show')); $event->setResponse($response); } }
Also add routing definitions to your app/config/routing.yml
r_u2f: resource: "@RU2FTwoFactorBundle/Resources/config/routing.yml" prefix: /
The Keys can be registered visiting /u2f_register. It needs to be served as
https!
Step 4: Include Javascript
First you need to add the dependency u2f-api to your package.json.
If you're using Webpack Encore, include this line in your webpack.config.js and you're done:
.addEntry('ru2ftwofactor', './web/bundles/ru2ftwofactor/js/auth.js')
If you're not using Webpack Encore, you need to bundle web/bundles/ru2ftwofactor/js/auth.js yourself, overwrite formTemplate and registerTemplate and add your JavaScript reference there.
License
This bundle is available under the MIT license.
r/u2f-two-factor-bundle 适用场景与选型建议
r/u2f-two-factor-bundle 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 12.78k 次下载、GitHub Stars 达 11, 最近一次更新时间为 2015 年 11 月 06 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「Symfony2」 「Authentication」 「two-factor」 「yubikey」 「two-step」 「fido」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 r/u2f-two-factor-bundle 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 r/u2f-two-factor-bundle 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 r/u2f-two-factor-bundle 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Filament Two Factor Authentication: Google 2FA + Passkey Authentication
Automatically logs-in users if they are already authenticated by a remote source. (e.g. environment variable REMOTE_USER)
GraphQL authentication for your headless Craft CMS applications.
Novactive eZ 2FA Bundle is an Ibexa bundle that provides two-factor authentication for your Ibexa project
Two-factor Authentication for WordPress
Laravel middleware to restrict a site or specific routes using HTTP basic authentication
统计信息
- 总下载量: 12.78k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 12
- 点击次数: 36
- 依赖项目数: 0
- 推荐数: 1
其他信息
- 授权协议: MIT
- 更新时间: 2015-11-06