Symfony 4.4 + scheb/2fa : Custom code generator and mailer

I’m trying to follow this documentation, but the yaml config throws some errors.
I’m don’t know Symfony enough to understand what I am doing wrong.
https://github.com/scheb/2fa/blob/5.x/doc/providers/email.md

services.yaml:

https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
    uploads_path: '/uploads/files/'
services:
    # default configuration for services in *this* file
    _defaults:
        autowire: true      # Automatically injects dependencies in your services.
        autoconfigure: true # Automatically registers your services as commands, event subscribers, etc.
    App:
        resource: '../src/'
        exclude:
            - '../src/DependencyInjection/'
            - '../src/Entity/'
            - '../src/Kernel.php'
            - '../src/Tests/'
    AppController:
        resource: '../src/Controller/'
        tags: ['controller.service_arguments']
    # AppServiceCodeGeneratorService:
    #     arguments: ['@markdown.parser', '@doctrine_cache.providers.my_markdown_cache']
    auth_mailer:
        # class: SchebTwoFactorBundleMailerSymfonyAuthCodeMailer
        class: AppMailerMyAuthCodeMailer
    code_generator_service:
        class: AppServiceCodeGeneratorService
        # public: true
        arguments:
            # $mailer: "%scheb_two_factor.security.email.symfony_auth_code_mailer%"
            # $mailer: "%scheb_two_factor.security.email.swift_auth_code_mailer%"
            # $mailer: AppMailerMyAuthCodeMailer
            # $mailer: SchebTwoFactorBundleMailerSymfonyAuthCodeMailer
            # $mailer: SchebTwoFactorBundleMailerAuthCodeMailerInterface
            # $mailer: AppserviceCodeGeneratorService
            # $mailer: "%scheb_two_factor.email.mailer%"
            $digits: "%scheb_two_factor.email.digits%"

scheb_2fa.yaml:

# See the configuration reference at https://github.com/scheb/2fa/blob/master/doc/configuration.md
scheb_two_factor:
    security_tokens:
        - SymfonyComponentSecurityGuardTokenPostAuthenticationGuardToken
    email:
        enabled: true
        sender_email: [email protected]
        sender_name: Quezako  # Optional
        digits: 12
        code_generator: code_generator_service  # Use alternative service to generate authentication code
        mailer: auth_mailer  # Use alternative service to send the authentication code

AppServiceCodeGeneratorService.php:

<?php
declare(strict_types=1);

namespace AppService;

use SchebTwoFactorBundleModelPersisterInterface;
use SchebTwoFactorBundleMailerAuthCodeMailerInterface;
use SchebTwoFactorBundleModelEmailTwoFactorInterface;
use SchebTwoFactorBundleSecurityTwoFactorProviderEmailGeneratorCodeGeneratorInterface;

class CodeGeneratorService implements CodeGeneratorInterface
{
    /**
     * @var PersisterInterface
     */
    private $persister;

    /**
     * @var AuthCodeMailerInterface
     */
    private $mailer;

    /**
     * @var int
     */
    private $digits;

    public function __construct(PersisterInterface $persister, AuthCodeMailerInterface $mailer, int $digits)
    {
        $this->persister = $persister;
        $this->mailer = $mailer;
        $this->digits = $digits;
    }

    public function generateAndSend(TwoFactorInterface $user): void
    {
        $min = 10 ** ($this->digits - 1);
        $max = 10 ** $this->digits - 1;
        $code = $this->generateCode($min, $max);
        $user->setEmailAuthCode((string) $code);
        $this->persister->persist($user);
        $this->mailer->sendAuthCode($user);
    }

    public function reSend(TwoFactorInterface $user): void
    {
        $this->mailer->sendAuthCode($user);
    }

    protected function generateCode(int $min, int $max): string
    {
        return substr(md5(rand()), $min, $max); 
    }
}

The various error messages I get:

Cannot autowire service "code_generator_service": argument "$mailer" of method "AppServiceCodeGeneratorService::__construct()" references interface "SchebTwoFactorBundleMailerAuthCodeMailerInterface" but no such service exists. You should maybe alias this interface to one of these existing services: "AppMailerMyAuthCodeMailer", "auth_mailer", "scheb_two_factor.security.email.swift_auth_code_mailer", "scheb_two_factor.security.email.symfony_auth_code_mailer".

You have requested a non-existent parameter "scheb_two_factor.email.mailer". Did you mean one of these: "scheb_two_factor.email.sender_name", "scheb_two_factor.email.template", "scheb_two_factor.email.digits"?

Argument 2 passed to AppServiceCodeGeneratorService::__construct() must implement interface SchebTwoFactorBundleMailerAuthCodeMailerInterface, string given, called in D:Devmeet_the_teamvarcachedevContainer0FXY0RxsrcApp_KernelDevDebugContainer.php on line 621

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *