src/LiteDesk/CoreBundle/Security/AppServiceAADAuthenticator.php line 66

Open in your IDE?
  1. <?php
  2. namespace LiteDesk\CoreBundle\Security;
  3. use LiteDesk\CoreBundle\Util\AADAuthUtils;
  4. use Symfony\Component\DependencyInjection\ContainerInterface;
  5. use Symfony\Component\HttpFoundation\RedirectResponse;
  6. use Symfony\Component\HttpFoundation\Request;
  7. use Symfony\Component\HttpFoundation\Response;
  8. use Symfony\Component\Security\Core\Authentication\SimpleAuthenticatorInterface;
  9. use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
  10. use Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken;
  11. use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface;
  12. use Symfony\Component\Security\Core\Exception\AuthenticationException;
  13. use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
  14. use Symfony\Component\Security\Core\User\AdvancedUserInterface;
  15. use Symfony\Component\Security\Core\User\UserInterface;
  16. use Symfony\Component\Security\Core\User\UserProviderInterface;
  17. use Symfony\Component\Security\Guard\AbstractGuardAuthenticator;
  18. use Symfony\Component\Security\Guard\GuardAuthenticatorInterface;
  19. use Symfony\Component\Security\Http\Authentication\SimpleFormAuthenticatorInterface;
  20. use JMS\DiExtraBundle\Annotation\Service;
  21. use JMS\DiExtraBundle\Annotation\InjectParams;
  22. use JMS\DiExtraBundle\Annotation\Inject;
  23. use Symfony\Component\Security\Http\EntryPoint\AuthenticationEntryPointInterface;
  24. /**
  25.  * @Service("security.app_service_aad_authenticator")
  26.  */
  27. class AppServiceAADAuthenticator extends AbstractGuardAuthenticator
  28. {
  29.     private $encoder;
  30.     private $container;
  31.     /**
  32.      * @InjectParams({
  33.      *     "encoder" = @Inject("security.password_encoder"),
  34.      *     "container" = @Inject("service_container"),
  35.      * })
  36.      */
  37.     public function __construct(UserPasswordEncoderInterface $encoderContainerInterface $container)
  38.     {
  39.         $this->encoder $encoder;
  40.         $this->container $container;
  41.     }
  42.     /**
  43.      * @return ContainerInterface
  44.      */
  45.     protected function getContainer()
  46.     {
  47.         return $this->container;
  48.     }
  49.     public function start(Request $requestAuthenticationException $authException null)
  50.     {
  51.         if (AADAuthUtils::mustRedirectToAuthentication($request)) {
  52.             throw new AuthenticationException('NO_AUTHENTICATION_FOUND');
  53.         }
  54.     }
  55.     public function getCredentials(Request $request)
  56.     {
  57.         if (AADAuthUtils::mustRedirectToAuthentication($request)) {
  58.             throw new AuthenticationException('NO_AUTHENTICATION_FOUND');
  59.         }
  60.         $idToken $request->server->get('HTTP_X_MS_TOKEN_AAD_ID_TOKEN'null);
  61.         if (!$idToken) {
  62.             return null;
  63.         }
  64.         $tokenParts explode('.'$idToken);
  65.         $token json_decode(base64_decode($tokenParts[1]), true);
  66.         $U_NUMBER_TOKEN_FIELD 'governorTKID';
  67.         $EMAIL_TOKEN_FIELD 'email';
  68.         if (isset($token[$U_NUMBER_TOKEN_FIELD]) && $token[$U_NUMBER_TOKEN_FIELD]) {
  69.             return $token[$U_NUMBER_TOKEN_FIELD];
  70.         }
  71.         if (isset($token[$EMAIL_TOKEN_FIELD]) && $token[$EMAIL_TOKEN_FIELD]) {
  72.             return $token[$EMAIL_TOKEN_FIELD];
  73.         }
  74.         return null;
  75.     }
  76.     protected function isTestUserPossible()
  77.     {
  78.         return $_ENV['SYMFONY_ENV'] === 'dev';
  79.     }
  80.     protected function getTestUser($credentialsUserProviderInterface $userProvider)
  81.     {
  82.         if (!$this->isTestUserPossible()) {
  83.             return null;
  84.         }
  85.         $testUserMap = [
  86.             'marc-severin-manfred.burgstaller.sp@dlh.de' => 999999,
  87.             'manuela.remus-woelffling@dlh.de' => 212884,
  88.             'cihan.simsek@dlh.de' => 733143,
  89.             'robert.barbieri@dlh.de' => 201230,
  90.         ];
  91.         $email strtolower($credentials);
  92.         if (isset($testUserMap[$email]) && $testPersonnalNumber $testUserMap[$email]) {
  93.             return $userProvider->loadUserByUsername($testPersonnalNumber);
  94.         }
  95.         return null;
  96.     }
  97.     public function getUser($credentialsUserProviderInterface $userProvider)
  98.     {
  99.         $testUser $this->getTestUser($credentials$userProvider);
  100.         if ($testUser) {
  101.             return $testUser;
  102.         }
  103.         if (!preg_match('/([0-9]{6,6})/'$credentials$matches)) {
  104.             throw new AuthenticationException('Wrong username');
  105.         }
  106.         $personalNumber $matches[1];
  107.         //We map HR Colleagues to Max Mustermann in dev Environment.
  108.         if (($personalNumber == 266845 || $personalNumber == 541546 || $personalNumber == 760449 )
  109.             && $_ENV['SYMFONY_ENV'] === 'dev') {
  110.             $personalNumber 999999;
  111.         }
  112.         return $userProvider->loadUserByUsername($personalNumber);
  113.     }
  114.     public function checkCredentials($credentialsUserInterface $user)
  115.     {
  116.         return true;
  117.     }
  118.     public function onAuthenticationFailure(Request $requestAuthenticationException $exception)
  119.     {
  120.         return AADAuthUtils::getLoginRedirectResponse($request);
  121.     }
  122.     public function onAuthenticationSuccess(Request $requestTokenInterface $token$providerKey)
  123.     {
  124.         return null;
  125.     }
  126.     public function supportsRememberMe()
  127.     {
  128.         return false;
  129.     }
  130. }