<commit_before><?php

namespace App\Helpers\ExternalLogin;

use App\Exceptions\BadRequestException;
use App\Exceptions\WrongCredentialsException;
use App\Model\Entity\User;
use App\Model\Repository\ExternalLogins;


/**
 * Mapper of service identification to object instance
 */
class ExternalServiceAuthenticator {

  /**
   * External authentication services
   * @var IExternalLoginService[]
   */
  private $services;

  /**
   * @var ExternalLogins
   */
  private $externalLogins;

  /**
   * Constructor with instantiation of all login services
   * @param ExternalLogins $externalLogins
   * @param array $services
   * @internal param CAS $cas Charles University autentication service
   */
  public function __construct(ExternalLogins $externalLogins, ...$services) {
    $this->externalLogins = $externalLogins;
    $this->services = $services;
  }

  /**
   * Get external service depending on the ID
   * @param string $serviceId Identifier of wanted service
   * @param string $type Type of authentication process
   * @return IExternalLoginService Instance of login service with given ID
   * @throws BadRequestException when such service is not known
   */
  public function findService(string $serviceId, string $type): IExternalLoginService {
    $found = NULL;
    foreach ($this->services as $service) {
      if ($service->getServiceId() === $serviceId && $service->getType() === $type) {
        $found = $service;
        break;
      }
    }

    if ($found === NULL) {
      throw new BadRequestException("Authentication service '$serviceId' is not supported.");
    }

    return $found;
  }

  /**
   * Authenticate a user against given external authentication service
   * @param IExternalLoginService $service
   * @param array $credentials
   * @return User
   */
  public function authenticate(IExternalLoginService $service, ...$credentials) {
    $userData = $service->getUser(...$credentials);
    return $this->findUser($service, $userData);
  }

  /**
   * Try to find a user account based on the data collected from
   * an external login service.
   * @param IExternalLoginService $service
   * @param UserData|null $userData
   * @return User
   * @throws WrongCredentialsException
   */
  private function findUser(IExternalLoginService $service, ?UserData $userData): User {
      if ($userData === NULL) {
          throw new WrongCredentialsException("Authentication failed.");
      }

      $user = $this->externalLogins->getUser($service->getServiceId(), $userData->getId());
      if ($user === NULL) {
          throw new WrongCredentialsException("Cannot authenticate this user through {$service->getServiceId()}");
      }

      return $user;
  }
}
<commit_msg>adds default value<commit_after><?php

namespace App\Helpers\ExternalLogin;

use App\Exceptions\BadRequestException;
use App\Exceptions\WrongCredentialsException;
use App\Model\Entity\User;
use App\Model\Repository\ExternalLogins;


/**
 * Mapper of service identification to object instance
 */
class ExternalServiceAuthenticator {

  /**
   * External authentication services
   * @var IExternalLoginService[]
   */
  private $services;

  /**
   * @var ExternalLogins
   */
  private $externalLogins;

  /**
   * Constructor with instantiation of all login services
   * @param ExternalLogins $externalLogins
   * @param array $services
   * @internal param CAS $cas Charles University autentication service
   */
  public function __construct(ExternalLogins $externalLogins, ...$services) {
    $this->externalLogins = $externalLogins;
    $this->services = $services;
  }

  /**
   * Get external service depending on the ID
   * @param string $serviceId Identifier of wanted service
   * @param string|null $type Type of authentication process
   * @return IExternalLoginService Instance of login service with given ID
   * @throws BadRequestException when such service is not known
   */
  public function findService(string $serviceId, ?string $type = "default"): IExternalLoginService {
    $found = NULL;
    foreach ($this->services as $service) {
      if ($service->getServiceId() === $serviceId && $service->getType() === $type) {
        $found = $service;
        break;
      }
    }

    if ($found === NULL) {
      throw new BadRequestException("Authentication service '$serviceId' is not supported.");
    }

    return $found;
  }

  /**
   * Authenticate a user against given external authentication service
   * @param IExternalLoginService $service
   * @param array $credentials
   * @return User
   */
  public function authenticate(IExternalLoginService $service, ...$credentials) {
    $userData = $service->getUser(...$credentials);
    return $this->findUser($service, $userData);
  }

  /**
   * Try to find a user account based on the data collected from
   * an external login service.
   * @param IExternalLoginService $service
   * @param UserData|null $userData
   * @return User
   * @throws WrongCredentialsException
   */
  private function findUser(IExternalLoginService $service, ?UserData $userData): User {
      if ($userData === NULL) {
          throw new WrongCredentialsException("Authentication failed.");
      }

      $user = $this->externalLogins->getUser($service->getServiceId(), $userData->getId());
      if ($user === NULL) {
          throw new WrongCredentialsException("Cannot authenticate this user through {$service->getServiceId()}");
      }

      return $user;
  }
}

{'fraction_non_alphanumeric': 0.11143748852579402, 'fraction_numerical': 0.0, 'mean_word_length': 3.5513784461152884, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7147973', 'n_tokens_mistral': 1488, 'n_tokens_neox': 1466, 'n_words': 433}