0

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/'
    App\Controller\:
        resource: '../src/Controller/'
        tags: ['controller.service_arguments']
    # App\Service\CodeGeneratorService:
    #     arguments: ['@markdown.parser', '@doctrine_cache.providers.my_markdown_cache']
    auth_mailer:
        # class: Scheb\TwoFactorBundle\Mailer\SymfonyAuthCodeMailer
        class: App\Mailer\MyAuthCodeMailer
    code_generator_service:
        class: App\Service\CodeGeneratorService
        # public: true
        arguments:
            # $mailer: "%scheb_two_factor.security.email.symfony_auth_code_mailer%"
            # $mailer: "%scheb_two_factor.security.email.swift_auth_code_mailer%"
            # $mailer: App\Mailer\MyAuthCodeMailer
            # $mailer: Scheb\TwoFactorBundle\Mailer\SymfonyAuthCodeMailer
            # $mailer: Scheb\TwoFactorBundle\Mailer\AuthCodeMailerInterface
            # $mailer: App\service\CodeGeneratorService
            # $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:
        - Symfony\Component\Security\Guard\Token\PostAuthenticationGuardToken
    email:
        enabled: true
        sender_email: quezako35@gmail.com
        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

App\Service\CodeGeneratorService.php:

<?php
declare(strict_types=1);

namespace App\Service;

use Scheb\TwoFactorBundle\Model\PersisterInterface;
use Scheb\TwoFactorBundle\Mailer\AuthCodeMailerInterface;
use Scheb\TwoFactorBundle\Model\Email\TwoFactorInterface;
use Scheb\TwoFactorBundle\Security\TwoFactor\Provider\Email\Generator\CodeGeneratorInterface;

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 "App\Service\CodeGeneratorService::__construct()" references interface "Scheb\TwoFactorBundle\Mailer\AuthCodeMailerInterface" but no such service exists. You should maybe alias this interface to one of these existing services: "App\Mailer\MyAuthCodeMailer", "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 App\Service\CodeGeneratorService::__construct() must implement interface Scheb\TwoFactorBundle\Mailer\AuthCodeMailerInterface, string given, called in D:\Dev\meet_the_team\var\cache\dev\Container0FXY0Rx\srcApp_KernelDevDebugContainer.php on line 621

Quezako
  • 35
  • 1
  • 10

1 Answers1

0

The author, Christian Scheb, gave me the answer.

You have to configure a service and reference that service using the @ annotation to tell Symfony to use that service instance. https://github.com/scheb/2fa/issues/22

services:
    # ...
    auth_mailer:
        class: App\Mailer\MyAuthCodeMailer

    code_generator_service:
        class: App\Service\CodeGeneratorService
        arguments:
            $mailer: "@auth_mailer"
            $digits: "%scheb_two_factor.email.digits%"
Quezako
  • 35
  • 1
  • 10