tavy315 / sylius-two-factor-plugin
Two-factor authentication for Sylius administrators and shop users.
Package info
github.com/tavy315/SyliusTwoFactorPlugin
Type:sylius-plugin
pkg:composer/tavy315/sylius-two-factor-plugin
Requires
- php: >=8.2
- ext-sodium: *
- bacon/bacon-qr-code: ^3.0
- doctrine/doctrine-migrations-bundle: ^3.0.1
- doctrine/orm: ^2.15
- spomky-labs/otphp: ^11.0
- sylius/sylius: ^1.12
- symfony/mailer: ^5.4
- symfony/rate-limiter: ^6.4
- symfony/security-core: ^5.4
Requires (Dev)
- phpunit/phpunit: ^10.5
Suggests
None
Provides
None
Conflicts
None
Replaces
None
README
Adds authenticator enrollment, single-use recovery codes, and optional email verification to Admin and Shop sign-in. This package targets Sylius 1.12 applications using Symfony 5.4 Security and Doctrine ORM annotations.
Installation
-
Require
tavy315/sylius-two-factor-pluginand registerTavy315\SyliusTwoFactorPlugin\Tavy315SyliusTwoFactorPlugininconfig/bundles.php. -
Implement
Tavy315\SyliusTwoFactorPlugin\Entity\User\TwoFactorUserInterfaceand useTwoFactorUserTraitfrom the same namespace in both of the application's Doctrine user entities. The entities must implement their respective Sylius Admin or Shop user interfaces. -
Enable DoctrineMigrationsBundle and run
php bin/console doctrine:migrations:migrateafter the Sylius Core migrations. The bundled migration targets the standard Admin and Shop user tables on MySQL or MariaDB. If your app already containsDoctrineMigrations\Version20260921120000, remove that local file when upgrading to the bundled version and keep its migration history. For custom user tables, adapt the migration to your schema before running it. -
Import the routes in
config/routes/two_factor.yaml.tavy315_sylius_two_factor: resource: '@Tavy315SyliusTwoFactorPlugin/Resources/config/routing.yaml'
-
Protect the imported paths with the corresponding Admin and Shop firewalls and roles. Place these rules before broader rules in
security.access_control. The Admin regex must match the configured Admin path.- { path: '%sylius.security.admin_regex%/two-factor(?:/.*)?$', roles: ROLE_ADMINISTRATION_ACCESS } - { path: '^/two-factor(?:/.*)?$', roles: ROLE_USER }
Configuration
TWO_FACTOR_TOTP_ENABLED=1enables authenticator enrollment and verification.TWO_FACTOR_ENCRYPTION_KEYis a base64-encoded, 32-byte secret. Keep it stable across deployments and available to every application instance.TWO_FACTOR_EMAIL_ENABLED=1enables verification by email. Configure Symfony Mailer and setTWO_FACTOR_EMAIL_SENDERto a valid sender. Prevent unverified email changes in the host application before enabling this method.- Override the Symfony parameter
tavy315_sylius_two_factor.issuerto customize the authenticator label. It defaults toSylius. - Override templates under
templates/bundles/Tavy315SyliusTwoFactorPlugin/to apply storefront branding.
Both verification methods are disabled by default.
Tests
Run composer install and then vendor/bin/phpunit from the plugin root.
When developing the plugin alongside a store that already installs it through a Composer path repository, you can run the plugin suite with the store's installed dependencies. From the store root, run:
SYLIUS_TWO_FACTOR_TEST_AUTOLOAD="$PWD/vendor/autoload.php" php vendor/bin/phpunit -c ../sylius-two-factor-plugin/phpunit.xml.dist